From ef052c347485d1e7f37295d5922ed9d56ec10e6c Mon Sep 17 00:00:00 2001 From: Lexi Date: Wed, 31 Jan 2024 10:20:43 -0500 Subject: [PATCH] Make the necessary patches to make waypoints actually work? --- .../API/Game/Waypoints/Waypoint.cs | 19 ++++++++++++++----- src/SpaceWarp.Game/Modules/Game.cs | 18 +++++++++++++++++- src/SpaceWarp.Game/Patches/MapPatches.cs | 18 ++++++++++++++++++ src/SpaceWarp.Game/SpaceWarp.Game.csproj | 1 + 4 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 src/SpaceWarp.Game/Patches/MapPatches.cs diff --git a/src/SpaceWarp.Game/API/Game/Waypoints/Waypoint.cs b/src/SpaceWarp.Game/API/Game/Waypoints/Waypoint.cs index 419de13..502143d 100644 --- a/src/SpaceWarp.Game/API/Game/Waypoints/Waypoint.cs +++ b/src/SpaceWarp.Game/API/Game/Waypoints/Waypoint.cs @@ -85,6 +85,11 @@ public WaypointState State private bool _isDestroyed; + /// + /// Whether or not this waypoint has been destroyed + /// + public bool IsDestroyed => _isDestroyed || _waypointObject is { _isDestroyed: true }; + private static long _nextID; /// @@ -147,13 +152,17 @@ public Waypoint(double latitude, double longitude, double? altitudeFromRadius = /// Thrown if the waypoint was already destroyed public void Destroy() { - if (_isDestroyed) + if (IsDestroyed) { throw new Exception("Waypoint was already destroyed"); } - _waypointObject.Destroy(); - _waypointObject = null; + if (State == WaypointState.Visible) + { + _waypointObject.Destroy(); + _waypointObject = null; + } + _isDestroyed = true; } @@ -168,7 +177,7 @@ public void Destroy() public void Move(double latitude, double longitude, double? altitudeFromRadius = null, [CanBeNull] string bodyName = null) { - if (_isDestroyed) + if (IsDestroyed) { throw new Exception("Waypoint was already destroyed"); } @@ -197,7 +206,7 @@ public void Move(double latitude, double longitude, double? altitudeFromRadius = /// Thrown if the waypoint is destroyed public void Rename([CanBeNull] string name = null) { - if (_isDestroyed) + if (IsDestroyed) { throw new Exception("Waypoint was already destroyed"); } diff --git a/src/SpaceWarp.Game/Modules/Game.cs b/src/SpaceWarp.Game/Modules/Game.cs index c9db243..b6a3944 100644 --- a/src/SpaceWarp.Game/Modules/Game.cs +++ b/src/SpaceWarp.Game/Modules/Game.cs @@ -1,7 +1,14 @@ -using JetBrains.Annotations; +using System.Reflection; +using HarmonyLib; +using JetBrains.Annotations; using KSP.Game; using KSP.Messages; +using SpaceWarp.API.Assets; using SpaceWarp.API.Game.Messages; +using SpaceWarp.API.Game.Waypoints; +using UnityEngine; +using ILogger = SpaceWarp.API.Logging.ILogger; +using Object = UnityEngine.Object; namespace SpaceWarp.Modules; @@ -11,9 +18,18 @@ namespace SpaceWarp.Modules; [UsedImplicitly] public class Game : SpaceWarpModule { + + internal static ILogger Logger; /// public override string Name => "SpaceWarp.Game"; + /// + public override void PreInitializeModule() + { + Logger = ModuleLogger; + Harmony.CreateAndPatchAll(typeof(Patches.MapPatches)); + } + /// public override void InitializeModule() { diff --git a/src/SpaceWarp.Game/Patches/MapPatches.cs b/src/SpaceWarp.Game/Patches/MapPatches.cs new file mode 100644 index 0000000..0d0c3e5 --- /dev/null +++ b/src/SpaceWarp.Game/Patches/MapPatches.cs @@ -0,0 +1,18 @@ +using HarmonyLib; +using KSP.Map; +using KSP.Sim; +using KSP.Sim.impl; +using UnityEngine; + +namespace SpaceWarp.Patches; + +[HarmonyPatch] +internal static class MapPatches +{ + [HarmonyPatch(typeof(Map3DView), nameof(Map3DView.ProcessSingleMapItem))] + [HarmonyPrefix] + // ReSharper disable once InconsistentNaming + public static bool SkipBadItems(Map3DView __instance, MapItem item) => + item.MapItemType != MapItemType.Waypoint || + ((ISimulationModelMap)__instance.Game.UniverseModel).FromGlobalId(item.SimGUID) != null; +} \ No newline at end of file diff --git a/src/SpaceWarp.Game/SpaceWarp.Game.csproj b/src/SpaceWarp.Game/SpaceWarp.Game.csproj index 6de7c73..0959ba0 100644 --- a/src/SpaceWarp.Game/SpaceWarp.Game.csproj +++ b/src/SpaceWarp.Game/SpaceWarp.Game.csproj @@ -13,5 +13,6 @@ +