From 6af779e74e3abe13e0a7dc929a01cbe2de57a855 Mon Sep 17 00:00:00 2001 From: HughesMDflyer4 Date: Sat, 18 Mar 2023 19:27:25 -0700 Subject: [PATCH] Rewrite to use a HarmonyPatch --- TimeWarpHider/swinfo.json | 2 +- TimeWarpHiderProject/TimeWarpHider.csproj | 18 +++++++-------- TimeWarpHiderProject/TimeWarpHiderPlugin.cs | 25 +++++---------------- 3 files changed, 16 insertions(+), 29 deletions(-) diff --git a/TimeWarpHider/swinfo.json b/TimeWarpHider/swinfo.json index 74dca01..c502e49 100644 --- a/TimeWarpHider/swinfo.json +++ b/TimeWarpHider/swinfo.json @@ -4,7 +4,7 @@ "name": "Time Warp Hider", "description": "A temporary fix to hide the Time Warp UI when the rest of the UI is hidden.", "source": "https://github.com/HughesMDflyer4/TimeWarpHider", - "version": "1.0.0", + "version": "1.1.0", "dependencies": [ { "id": "SpaceWarp", diff --git a/TimeWarpHiderProject/TimeWarpHider.csproj b/TimeWarpHiderProject/TimeWarpHider.csproj index e1c546a..7a54386 100644 --- a/TimeWarpHiderProject/TimeWarpHider.csproj +++ b/TimeWarpHiderProject/TimeWarpHider.csproj @@ -8,7 +8,7 @@ TimeWarpHider Time Warp Hider A temporary fix to hide the Time Warp UI when the rest of the UI is hidden. - 1.0.0 + 1.1.0 https://api.nuget.org/v3/index.json; https://nuget.bepinex.dev/v3/index.json @@ -24,15 +24,15 @@ - - - - - - - + + + + + + + - + diff --git a/TimeWarpHiderProject/TimeWarpHiderPlugin.cs b/TimeWarpHiderProject/TimeWarpHiderPlugin.cs index bac2cd3..04e8afc 100644 --- a/TimeWarpHiderProject/TimeWarpHiderPlugin.cs +++ b/TimeWarpHiderProject/TimeWarpHiderPlugin.cs @@ -1,8 +1,7 @@ using BepInEx; using HarmonyLib; using KSP.Game; -using KSP.Messages; -using RTG; +using KSP.UI.Flight; using SpaceWarp; using SpaceWarp.API.Mods; using UnityEngine; @@ -19,29 +18,17 @@ public class TimeWarpHiderPlugin : BaseSpaceWarpPlugin public static TimeWarpHiderPlugin Instance { get; set; } - private GameObject timeWarpGameObject; - private GameObject gameViewGameObject; - public override void OnInitialized() { base.OnInitialized(); Instance = this; - Harmony.CreateAndPatchAll(typeof(TimeWarpHiderPlugin).Assembly); - - GameManager.Instance.Game.Messages.Subscribe(OnLoaded); - GameManager.Instance.Game.Messages.Subscribe(OnLoaded); - } - - private void OnLoaded(MessageCenterMessage message) - { - GameManager.Instance.Game.UI.FlightHud.onFlightHudCanvasActiveChanged += new Action(OnFlightHudToggled); - timeWarpGameObject = GameManager.Instance.Game.UI.GetRootCanvas().gameObject.GetAllChildren().Where(g => g.name == "group_instruments(Clone)").FirstOrDefault(); - gameViewGameObject = GameManager.Instance.Game.UI.GetRootCanvas().gameObject.GetAllChildren().Where(g => g.name == "group_gameview(Clone)").FirstOrDefault(); + Harmony.CreateAndPatchAll(typeof(TimeWarpHiderPlugin)); } - private void OnFlightHudToggled(bool isEnabled) + [HarmonyPatch(typeof(UIFlightHud), nameof(UIFlightHud.OnFlightHudCanvasActiveChanged))] + [HarmonyPostfix] + public static void OnFlightHudCanvasActiveChanged(bool isVisible) { - timeWarpGameObject.SetActive(isEnabled); - gameViewGameObject.SetActive(isEnabled); + GameManager.Instance.Game.UI.FlightHud.gameObject.SetActive(isVisible); } } \ No newline at end of file