Skip to content

Commit

Permalink
Possible conditional patching
Browse files Browse the repository at this point in the history
  • Loading branch information
cheese3660 committed Dec 21, 2023
1 parent c941261 commit 3b7ea7d
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 4 deletions.
10 changes: 10 additions & 0 deletions SpaceWarp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpaceWarpPatcher", "src/Spa
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpaceWarpTest", "src/SpaceWarpTest/SpaceWarpTest.csproj", "{8DB42693-9177-40B9-AC6A-B6D7A4823FAD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpaceWarp.ResourceFix", "src\SpaceWarp.ResourceFix\SpaceWarp.ResourceFix.csproj", "{0C3A5074-3F79-4B81-8390-F98B3D876F0D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -98,6 +100,14 @@ Global
{8DB42693-9177-40B9-AC6A-B6D7A4823FAD}.Deploy|Any CPU.Build.0 = Deploy|Any CPU
{8DB42693-9177-40B9-AC6A-B6D7A4823FAD}.DeployAndRun|Any CPU.ActiveCfg = DeployAndRun|Any CPU
{8DB42693-9177-40B9-AC6A-B6D7A4823FAD}.DeployAndRun|Any CPU.Build.0 = DeployAndRun|Any CPU
{0C3A5074-3F79-4B81-8390-F98B3D876F0D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0C3A5074-3F79-4B81-8390-F98B3D876F0D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0C3A5074-3F79-4B81-8390-F98B3D876F0D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0C3A5074-3F79-4B81-8390-F98B3D876F0D}.Release|Any CPU.Build.0 = Release|Any CPU
{0C3A5074-3F79-4B81-8390-F98B3D876F0D}.Deploy|Any CPU.ActiveCfg = Deploy|Any CPU
{0C3A5074-3F79-4B81-8390-F98B3D876F0D}.Deploy|Any CPU.Build.0 = Deploy|Any CPU
{0C3A5074-3F79-4B81-8390-F98B3D876F0D}.DeployAndRun|Any CPU.ActiveCfg = DeployAndRun|Any CPU
{0C3A5074-3F79-4B81-8390-F98B3D876F0D}.DeployAndRun|Any CPU.Build.0 = DeployAndRun|Any CPU
EndGlobalSection
EndGlobal

1 change: 0 additions & 1 deletion src/SpaceWarp.Core/Modules/ModuleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ internal static void LoadAllModules()
AllSpaceWarpModules.Add(mod);
}

Harmony.CreateAndPatchAll(assembly);
}
catch (Exception e)
{
Expand Down
4 changes: 3 additions & 1 deletion src/SpaceWarp.Game/Modules/Game.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using JetBrains.Annotations;
using HarmonyLib;
using JetBrains.Annotations;
using KSP.Game;
using KSP.Messages;
using SpaceWarp.API.Game.Messages;
Expand All @@ -11,6 +12,7 @@ public class Game : SpaceWarpModule
public override string Name => "SpaceWarp.Game";

public override void LoadModule()

{
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using JetBrains.Annotations;
using HarmonyLib;
using JetBrains.Annotations;
using KSP.Sim.impl;
using SpaceWarp.API.Logging;

namespace SpaceWarp.API.Parts;
namespace SpaceWarp.ResourceFix.API;

[PublicAPI]
public static class PartComponentModuleOverride
Expand All @@ -27,6 +28,10 @@ public static void RegisterModuleForBackgroundResourceProcessing<T>() where T :

RegisteredPartComponentOverrides.Add(typeof(T));
_LOGGER.LogInfo($"Registered '{moduleName}' for background resources processing.");
if (RegisteredPartComponentOverrides.Count > 0)
{
Harmony.CreateAndPatchAll(typeof(Modules.ResourceFix).Assembly,"ResourceFix");
}
}

/// <summary>
Expand All @@ -39,5 +44,9 @@ public static void UnRegisterModuleForBackgroundResourceProcessing<T>() where T

RegisteredPartComponentOverrides.Remove(typeof(T));
_LOGGER.LogInfo($"Unregistered '{typeof(T).Name}' from background resources processing.");
if (RegisteredPartComponentOverrides.Count == 0)
{
Harmony.UnpatchID("ResourceFix");
}
}
}
27 changes: 27 additions & 0 deletions src/SpaceWarp.ResourceFix/Modules/ResourceFix.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using SpaceWarp.Modules;

namespace SpaceWarp.ResourceFix.Modules;

public class ResourceFix : SpaceWarpModule
{
public override string Name { get; }
public override void LoadModule()
{
throw new NotImplementedException();
}

public override void PreInitializeModule()
{
throw new NotImplementedException();
}

public override void InitializeModule()
{
throw new NotImplementedException();
}

public override void PostInitializeModule()
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using HarmonyLib;
using KSP.Sim.impl;
using SpaceWarp.API.Parts;
using SpaceWarp.ResourceFix.API;

namespace SpaceWarp.Patching;

Expand All @@ -13,6 +14,7 @@ private static bool PerformBackgroundCalculationsForRegisteredModules(double uni
double deltaUniversalTime,
PartOwnerComponent __instance)
{

var isModulePresent = false;
var hasPartModuleMethod = __instance.GetType().GetMethod("HasPartModule");

Expand Down
16 changes: 16 additions & 0 deletions src/SpaceWarp.ResourceFix/SpaceWarp.ResourceFix.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\SpaceWarp.Core\SpaceWarp.Core.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="KerbalSpaceProgram2.GameLibs" Version="0.1.5" />
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions src/SpaceWarp.UI/Modules/UI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using BepInEx.Bootstrap;
using HarmonyLib;
using JetBrains.Annotations;
using KSP.Assets;
using KSP.Game;
Expand Down Expand Up @@ -51,6 +52,7 @@ public class UI : SpaceWarpModule

public override void LoadModule()
{
Harmony.CreateAndPatchAll(GetType().Assembly);
AppbarBackend.AppBarInFlightSubscriber.AddListener(Appbar.LoadAllButtons);
AppbarBackend.AppBarOABSubscriber.AddListener(Appbar.LoadOABButtons);
AppbarBackend.AppBarKSCSubscriber.AddListener(Appbar.LoadKSCButtons);
Expand Down

0 comments on commit 3b7ea7d

Please sign in to comment.