Skip to content

Commit

Permalink
Rewrite to use a HarmonyPatch
Browse files Browse the repository at this point in the history
  • Loading branch information
HughesMDflyer4 committed Mar 19, 2023
1 parent a77b8d3 commit 6af779e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 29 deletions.
2 changes: 1 addition & 1 deletion TimeWarpHider/swinfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 9 additions & 9 deletions TimeWarpHiderProject/TimeWarpHider.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<AssemblyName>TimeWarpHider</AssemblyName>
<Product>Time Warp Hider</Product>
<Description>A temporary fix to hide the Time Warp UI when the rest of the UI is hidden.</Description>
<Version>1.0.0</Version>
<Version>1.1.0</Version>
<RestoreAdditionalProjectSources>
https://api.nuget.org/v3/index.json;
https://nuget.bepinex.dev/v3/index.json
Expand All @@ -24,15 +24,15 @@
</Reference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="BepInEx.Analyzers" Version="1.*" PrivateAssets="all"/>
<PackageReference Include="BepInEx.AssemblyPublicizer.MSBuild" Version="0.4.0" PrivateAssets="all"/>
<PackageReference Include="BepInEx.Core" Version="5.*"/>
<PackageReference Include="BepInEx.PluginInfoProps" Version="2.*"/>
<PackageReference Include="HarmonyX" Version="2.10.1"/>
<PackageReference Include="SpaceWarp" Version="0.4.0"/>
<PackageReference Include="UnityEngine.Modules" Version="2020.3.33" IncludeAssets="compile"/>
<PackageReference Include="BepInEx.Analyzers" Version="1.*" PrivateAssets="all" />
<PackageReference Include="BepInEx.AssemblyPublicizer.MSBuild" Version="0.4.0" PrivateAssets="all" />
<PackageReference Include="BepInEx.Core" Version="5.*" />
<PackageReference Include="BepInEx.PluginInfoProps" Version="2.*" />
<PackageReference Include="HarmonyX" Version="2.10.1" />
<PackageReference Include="SpaceWarp" Version="0.4.0" />
<PackageReference Include="UnityEngine.Modules" Version="2020.3.33" IncludeAssets="compile" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="echo d | xcopy /y /s &quot;$(ProjectDir)..\$(ModId)\&quot; &quot;$(ProjectDir)..\$(ConfigurationName)\BepInEx\plugins\$(ModId)\&quot;&#xA;echo f | xcopy /y &quot;$(TargetPath)&quot; &quot;$(ProjectDir)..\$(ConfigurationName)\BepInEx\plugins\$(ModId)\$(ModId).dll&quot;&#xA;if $(ConfigurationName) == Debug echo f | xcopy /y &quot;$(TargetDir)$(TargetName).pdb&quot; &quot;$(ProjectDir)..\$(ConfigurationName)\BepInEx\plugins\$(ModId)\$(ModId).pdb&quot;&#xA;xcopy /y &quot;$(ProjectDir)..\LICENSE&quot; &quot;$(ProjectDir)..\$(ConfigurationName)\BepInEx\plugins\$(ModId)\&quot;&#xA;echo f | xcopy /y &quot;$(ProjectDir)..\README.md&quot; &quot;$(ProjectDir)..\$(ConfigurationName)\BepInEx\plugins\$(ModId)\README.txt&quot;"/>
<Exec Command="echo d | xcopy /y /s &quot;$(ProjectDir)..\$(ModId)\&quot; &quot;$(ProjectDir)..\$(ConfigurationName)\BepInEx\plugins\$(ModId)\&quot;&#xA;echo f | xcopy /y &quot;$(TargetPath)&quot; &quot;$(ProjectDir)..\$(ConfigurationName)\BepInEx\plugins\$(ModId)\$(ModId).dll&quot;&#xA;if $(ConfigurationName) == Debug echo f | xcopy /y &quot;$(TargetDir)$(TargetName).pdb&quot; &quot;$(ProjectDir)..\$(ConfigurationName)\BepInEx\plugins\$(ModId)\$(ModId).pdb&quot;&#xA;xcopy /y &quot;$(ProjectDir)..\LICENSE&quot; &quot;$(ProjectDir)..\$(ConfigurationName)\BepInEx\plugins\$(ModId)\&quot;&#xA;echo f | xcopy /y &quot;$(ProjectDir)..\README.md&quot; &quot;$(ProjectDir)..\$(ConfigurationName)\BepInEx\plugins\$(ModId)\README.txt&quot;" />
</Target>
</Project>
25 changes: 6 additions & 19 deletions TimeWarpHiderProject/TimeWarpHiderPlugin.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<VesselCreatedMessage>(OnLoaded);
GameManager.Instance.Game.Messages.Subscribe<VesselChangedMessage>(OnLoaded);
}

private void OnLoaded(MessageCenterMessage message)
{
GameManager.Instance.Game.UI.FlightHud.onFlightHudCanvasActiveChanged += new Action<bool>(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);
}
}

0 comments on commit 6af779e

Please sign in to comment.