Skip to content

Commit

Permalink
Make the necessary patches to make waypoints actually work?
Browse files Browse the repository at this point in the history
  • Loading branch information
cheese3660 committed Jan 31, 2024
1 parent 91ef880 commit ef052c3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
19 changes: 14 additions & 5 deletions src/SpaceWarp.Game/API/Game/Waypoints/Waypoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public WaypointState State

private bool _isDestroyed;

/// <summary>
/// Whether or not this waypoint has been destroyed
/// </summary>
public bool IsDestroyed => _isDestroyed || _waypointObject is { _isDestroyed: true };

private static long _nextID;

/// <summary>
Expand Down Expand Up @@ -147,13 +152,17 @@ public Waypoint(double latitude, double longitude, double? altitudeFromRadius =
/// <exception cref="Exception">Thrown if the waypoint was already destroyed</exception>
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;
}

Expand All @@ -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");
}
Expand Down Expand Up @@ -197,7 +206,7 @@ public void Move(double latitude, double longitude, double? altitudeFromRadius =
/// <exception cref="Exception">Thrown if the waypoint is destroyed</exception>
public void Rename([CanBeNull] string name = null)
{
if (_isDestroyed)
if (IsDestroyed)
{
throw new Exception("Waypoint was already destroyed");
}
Expand Down
18 changes: 17 additions & 1 deletion src/SpaceWarp.Game/Modules/Game.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -11,9 +18,18 @@ namespace SpaceWarp.Modules;
[UsedImplicitly]
public class Game : SpaceWarpModule
{

internal static ILogger Logger;
/// <inheritdoc />
public override string Name => "SpaceWarp.Game";

/// <inheritdoc />
public override void PreInitializeModule()
{
Logger = ModuleLogger;
Harmony.CreateAndPatchAll(typeof(Patches.MapPatches));
}

/// <inheritdoc />
public override void InitializeModule()
{
Expand Down
18 changes: 18 additions & 0 deletions src/SpaceWarp.Game/Patches/MapPatches.cs
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions src/SpaceWarp.Game/SpaceWarp.Game.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
</ItemGroup>
<ItemGroup Label="Project references">
<ProjectReference Include="$(SolutionDir)/src/SpaceWarp.Core/SpaceWarp.Core.csproj" Private="false"/>
<ProjectReference Include="..\SpaceWarp.UI\SpaceWarp.UI.csproj" />
</ItemGroup>
</Project>

0 comments on commit ef052c3

Please sign in to comment.