From cef58d2b633133bd7390fe79455fab5a13bc4461 Mon Sep 17 00:00:00 2001 From: Stonesmile <62522391+StonesmileGit@users.noreply.github.com> Date: Sun, 24 Mar 2024 20:41:19 +0100 Subject: [PATCH 1/6] Add Harmony patch to handle MFTs in B9PS --- Source/Harmony/B9PS.cs | 80 ++++++++++++++++++++++++++++++++ Source/Harmony/HarmonyPatcher.cs | 14 ++++++ Source/RealFuels.csproj | 9 ++++ 3 files changed, 103 insertions(+) create mode 100644 Source/Harmony/B9PS.cs create mode 100644 Source/Harmony/HarmonyPatcher.cs diff --git a/Source/Harmony/B9PS.cs b/Source/Harmony/B9PS.cs new file mode 100644 index 00000000..e502faf4 --- /dev/null +++ b/Source/Harmony/B9PS.cs @@ -0,0 +1,80 @@ +using HarmonyLib; +using UnityEngine; +using System.Linq; +using System.Collections.Generic; +using B9PartSwitch.PartSwitch.PartModifiers; +using B9PartSwitch; + +namespace RealFuels.Harmony +{ + [HarmonyPatch(typeof(ModuleModifierInfo))] + internal class PatchModuleModifierInfo + { + internal static bool Prepare() + { + bool foundB9PS = AssemblyLoader.loadedAssemblies.FirstOrDefault(a => a.name.Equals("B9PartSwitch", System.StringComparison.OrdinalIgnoreCase)) != null; + return foundB9PS; + } + + + [HarmonyPostfix] + [HarmonyPatch("CreatePartModifiers")] + internal static IEnumerable Postfix_handleMFTConfig(IEnumerable result, Part part, ModuleModifierInfo __instance, BaseEventDetails moduleDataChangedEventDetails) + { + foreach (var partModifier in result) + { + if (partModifier is ModuleDataHandlerBasic) + { + ModuleMatcher moduleMatcher = new ModuleMatcher(__instance.identifierNode); + PartModule module = moduleMatcher.FindModule(part); + ConfigNode originalNode = moduleMatcher.FindPrefabNode(module); + if (module.moduleName == "ModuleFuelTanks") + { + yield return new ModuleFuelTanksHandler(module, originalNode, __instance.dataNode, moduleDataChangedEventDetails); + continue; + } + } + yield return partModifier; + } + + } + } +} + +namespace B9PartSwitch.PartSwitch.PartModifiers +{ + public class ModuleFuelTanksHandler : PartModifierBase + { + public const string PART_ASPECT_LOCK = "ModuleFuelTanks"; + + private readonly PartModule module; + private readonly ConfigNode originalNode; + private readonly ConfigNode dataNode; + private readonly BaseEventDetails moduleDataChangedEventDetails; + public ModuleFuelTanksHandler(PartModule module, ConfigNode originalNode, ConfigNode dataNode, BaseEventDetails moduleDataChangedEventDetails) + { + this.module = module; + this.originalNode = originalNode; + this.dataNode = dataNode; + this.moduleDataChangedEventDetails = moduleDataChangedEventDetails; + } + + public object PartAspectLock => PART_ASPECT_LOCK; + public override string Description => "a part's ModuleFuelTanks"; + public override void DeactivateOnStartEditor() => Deactivate(); + public override void ActivateOnStartEditor() => Activate(); + public override void DeactivateOnSwitchEditor() => Deactivate(); + public override void ActivateOnSwitchEditor() => Activate(); + + private void Activate() => ApplyNode(dataNode); + private void Deactivate() => ApplyNode(originalNode); + + private void ApplyNode(ConfigNode sourceNode) + { + var evtDetails = new BaseEventDetails(BaseEventDetails.Sender.USER); + evtDetails.Set("MFTNode", sourceNode); + module.Events.Send("LoadMFTModuleFromConfigNode", evtDetails); + module.Events.Send("ModuleDataChanged", moduleDataChangedEventDetails); + } + } +} diff --git a/Source/Harmony/HarmonyPatcher.cs b/Source/Harmony/HarmonyPatcher.cs new file mode 100644 index 00000000..21e8172d --- /dev/null +++ b/Source/Harmony/HarmonyPatcher.cs @@ -0,0 +1,14 @@ +using UnityEngine; + +namespace RealFuels.Harmony +{ + [KSPAddon(KSPAddon.Startup.Instantly, true)] + public class HarmonyPatcher : MonoBehaviour + { + internal void Start() + { + var harmony = new HarmonyLib.Harmony("RealFuels.Harmony.HarmonyPatcher"); + harmony.PatchAll(); + } + } +} \ No newline at end of file diff --git a/Source/RealFuels.csproj b/Source/RealFuels.csproj index 0733d6c9..008361b3 100644 --- a/Source/RealFuels.csproj +++ b/Source/RealFuels.csproj @@ -38,9 +38,16 @@ x64 + + False + False + False + + False + ..\..\..\..\..\..\Games\R112\GameData\ROUtils\Plugins\ROUtils.dll False @@ -102,6 +109,8 @@ + + From 5e0932b08160d79d87b61e8e4714480bdc06fb9a Mon Sep 17 00:00:00 2001 From: Stonesmile <62522391+StonesmileGit@users.noreply.github.com> Date: Sun, 24 Mar 2024 21:49:49 +0100 Subject: [PATCH 2/6] Change some odd names --- Source/Harmony/B9PS.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Harmony/B9PS.cs b/Source/Harmony/B9PS.cs index e502faf4..796dbb37 100644 --- a/Source/Harmony/B9PS.cs +++ b/Source/Harmony/B9PS.cs @@ -19,7 +19,7 @@ internal static bool Prepare() [HarmonyPostfix] [HarmonyPatch("CreatePartModifiers")] - internal static IEnumerable Postfix_handleMFTConfig(IEnumerable result, Part part, ModuleModifierInfo __instance, BaseEventDetails moduleDataChangedEventDetails) + internal static IEnumerable Postfix_CreatePartModifiers(IEnumerable result, Part part, ModuleModifierInfo __instance, BaseEventDetails moduleDataChangedEventDetails) { foreach (var partModifier in result) { @@ -41,7 +41,7 @@ internal static IEnumerable Postfix_handleMFTConfig(IEnumerable Date: Tue, 26 Mar 2024 17:30:57 +0100 Subject: [PATCH 3/6] Move harmony patch to separate project --- Source/B9PSPatch/B9PS.cs | 41 +++++++++++ .../B9PS.cs => B9PSPatch/B9PSMFTHandler.cs} | 42 +---------- .../{Harmony => B9PSPatch}/HarmonyPatcher.cs | 0 Source/B9PSPatch/RealFuelsB9PSPatch.csproj | 70 +++++++++++++++++++ .../assembly/AssemblyInfoB9PSPatch.cs | 36 ++++++++++ Source/RealFuels.csproj | 9 --- Source/RealFuels.sln | 1 + 7 files changed, 149 insertions(+), 50 deletions(-) create mode 100644 Source/B9PSPatch/B9PS.cs rename Source/{Harmony/B9PS.cs => B9PSPatch/B9PSMFTHandler.cs} (52%) rename Source/{Harmony => B9PSPatch}/HarmonyPatcher.cs (100%) create mode 100644 Source/B9PSPatch/RealFuelsB9PSPatch.csproj create mode 100644 Source/B9PSPatch/assembly/AssemblyInfoB9PSPatch.cs diff --git a/Source/B9PSPatch/B9PS.cs b/Source/B9PSPatch/B9PS.cs new file mode 100644 index 00000000..5b695b82 --- /dev/null +++ b/Source/B9PSPatch/B9PS.cs @@ -0,0 +1,41 @@ +using HarmonyLib; +using System.Linq; +using System.Collections.Generic; +using B9PartSwitch.PartSwitch.PartModifiers; +using B9PartSwitch; + +namespace RealFuels.Harmony +{ + [HarmonyPatch(typeof(ModuleModifierInfo))] + internal class PatchModuleModifierInfo + { + internal static bool Prepare() + { + bool foundB9PS = AssemblyLoader.loadedAssemblies.FirstOrDefault(a => a.name.Equals("B9PartSwitch", System.StringComparison.OrdinalIgnoreCase)) != null; + return foundB9PS; + } + + + [HarmonyPostfix] + [HarmonyPatch("CreatePartModifiers")] + internal static IEnumerable Postfix_CreatePartModifiers(IEnumerable result, Part part, ModuleModifierInfo __instance, BaseEventDetails moduleDataChangedEventDetails) + { + foreach (var partModifier in result) + { + if (partModifier is ModuleDataHandlerBasic) + { + ModuleMatcher moduleMatcher = new ModuleMatcher(__instance.identifierNode); + PartModule module = moduleMatcher.FindModule(part); + ConfigNode originalNode = moduleMatcher.FindPrefabNode(module); + if (module.moduleName == "ModuleFuelTanks") + { + yield return new ModuleFuelTanksHandler(module, originalNode, __instance.dataNode, moduleDataChangedEventDetails); + continue; + } + } + yield return partModifier; + } + + } + } +} diff --git a/Source/Harmony/B9PS.cs b/Source/B9PSPatch/B9PSMFTHandler.cs similarity index 52% rename from Source/Harmony/B9PS.cs rename to Source/B9PSPatch/B9PSMFTHandler.cs index 796dbb37..71cf69b9 100644 --- a/Source/Harmony/B9PS.cs +++ b/Source/B9PSPatch/B9PSMFTHandler.cs @@ -1,45 +1,5 @@ -using HarmonyLib; -using UnityEngine; -using System.Linq; -using System.Collections.Generic; -using B9PartSwitch.PartSwitch.PartModifiers; -using B9PartSwitch; - -namespace RealFuels.Harmony -{ - [HarmonyPatch(typeof(ModuleModifierInfo))] - internal class PatchModuleModifierInfo - { - internal static bool Prepare() - { - bool foundB9PS = AssemblyLoader.loadedAssemblies.FirstOrDefault(a => a.name.Equals("B9PartSwitch", System.StringComparison.OrdinalIgnoreCase)) != null; - return foundB9PS; - } - - [HarmonyPostfix] - [HarmonyPatch("CreatePartModifiers")] - internal static IEnumerable Postfix_CreatePartModifiers(IEnumerable result, Part part, ModuleModifierInfo __instance, BaseEventDetails moduleDataChangedEventDetails) - { - foreach (var partModifier in result) - { - if (partModifier is ModuleDataHandlerBasic) - { - ModuleMatcher moduleMatcher = new ModuleMatcher(__instance.identifierNode); - PartModule module = moduleMatcher.FindModule(part); - ConfigNode originalNode = moduleMatcher.FindPrefabNode(module); - if (module.moduleName == "ModuleFuelTanks") - { - yield return new ModuleFuelTanksHandler(module, originalNode, __instance.dataNode, moduleDataChangedEventDetails); - continue; - } - } - yield return partModifier; - } - - } - } -} +using B9PartSwitch.PartSwitch.PartModifiers; namespace RealFuels.Harmony { diff --git a/Source/Harmony/HarmonyPatcher.cs b/Source/B9PSPatch/HarmonyPatcher.cs similarity index 100% rename from Source/Harmony/HarmonyPatcher.cs rename to Source/B9PSPatch/HarmonyPatcher.cs diff --git a/Source/B9PSPatch/RealFuelsB9PSPatch.csproj b/Source/B9PSPatch/RealFuelsB9PSPatch.csproj new file mode 100644 index 00000000..48eb47a3 --- /dev/null +++ b/Source/B9PSPatch/RealFuelsB9PSPatch.csproj @@ -0,0 +1,70 @@ + + + + Debug + AnyCPU + {0041813D-DCD1-4AC7-8327-85765BF924A3} + Library + RealFuelsB9PSPatch + RealFuelsB9PSPatch + v4.7.2 + + 10.8.0 + ..\..\Build\RealFuels\obj + + + portable + False + ..\..\Build\RealFuels\obj\ + ..\..\RealFuels\Plugins\ + DEBUG;ENABLE_PROFILER + prompt + 4 + False + true + false + x64 + + + none + True + ..\..\Build\RealFuels\obj\ + ..\..\RealFuels\Plugins\ + prompt + 4 + False + false + false + x64 + + + + False + False + + + False + + + False + + + False + + + False + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Source/B9PSPatch/assembly/AssemblyInfoB9PSPatch.cs b/Source/B9PSPatch/assembly/AssemblyInfoB9PSPatch.cs new file mode 100644 index 00000000..6893e36b --- /dev/null +++ b/Source/B9PSPatch/assembly/AssemblyInfoB9PSPatch.cs @@ -0,0 +1,36 @@ +#define CIBUILD_disabled +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle("RealFuelsB9PSPatch")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("KSP-RO Group")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion("1.0.0.0")] +// [assembly: AssemblyInformationalVersionAttribute("@FULL_VERSION@")] +#if CIBUILD +[assembly: AssemblyFileVersion("@MAJOR@.@MINOR@.@PATCH@.@BUILD@")] +[assembly: KSPAssembly("RealFuelsB9PSPatch", @MAJOR@, @MINOR@, @PATCH@)] +#else +[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: KSPAssembly("RealFuelsB9PSPatch", 1, 0, 0)] +#endif + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] +[assembly: KSPAssemblyDependency("B9PartSwitch", 2, 20)] \ No newline at end of file diff --git a/Source/RealFuels.csproj b/Source/RealFuels.csproj index 008361b3..0733d6c9 100644 --- a/Source/RealFuels.csproj +++ b/Source/RealFuels.csproj @@ -38,16 +38,9 @@ x64 - - False - False - False - - False - ..\..\..\..\..\..\Games\R112\GameData\ROUtils\Plugins\ROUtils.dll False @@ -109,8 +102,6 @@ - - diff --git a/Source/RealFuels.sln b/Source/RealFuels.sln index 47fa9d36..7b202f53 100644 --- a/Source/RealFuels.sln +++ b/Source/RealFuels.sln @@ -4,6 +4,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 VisualStudioVersion = 16.0.31229.75 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RealFuels", "RealFuels.csproj", "{0041813D-DCD1-4AC7-8327-85765BF924A3}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBD}") = "RealFuelsB9PSPatch", "B9PSPatch/RealFuelsB9PSPatch.csproj", "{0041813D-DCD1-4AC7-8327-85765BF924A3}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution From 9b7d52e46564e86f930482b61ba43634fd71a147 Mon Sep 17 00:00:00 2001 From: stonesmileGit Date: Sat, 30 Mar 2024 18:54:32 +0100 Subject: [PATCH 4/6] Remove check for assembly. We know it is present --- Source/B9PSPatch/B9PS.cs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/Source/B9PSPatch/B9PS.cs b/Source/B9PSPatch/B9PS.cs index 5b695b82..a1fa3bf3 100644 --- a/Source/B9PSPatch/B9PS.cs +++ b/Source/B9PSPatch/B9PS.cs @@ -1,5 +1,4 @@ using HarmonyLib; -using System.Linq; using System.Collections.Generic; using B9PartSwitch.PartSwitch.PartModifiers; using B9PartSwitch; @@ -9,13 +8,6 @@ namespace RealFuels.Harmony [HarmonyPatch(typeof(ModuleModifierInfo))] internal class PatchModuleModifierInfo { - internal static bool Prepare() - { - bool foundB9PS = AssemblyLoader.loadedAssemblies.FirstOrDefault(a => a.name.Equals("B9PartSwitch", System.StringComparison.OrdinalIgnoreCase)) != null; - return foundB9PS; - } - - [HarmonyPostfix] [HarmonyPatch("CreatePartModifiers")] internal static IEnumerable Postfix_CreatePartModifiers(IEnumerable result, Part part, ModuleModifierInfo __instance, BaseEventDetails moduleDataChangedEventDetails) @@ -35,7 +27,6 @@ internal static IEnumerable Postfix_CreatePartModifiers(IEnumerab } yield return partModifier; } - } } } From 2bced2670356a29eb933529686f6abf22807a7d9 Mon Sep 17 00:00:00 2001 From: stonesmileGit Date: Sun, 31 Mar 2024 22:04:32 +0200 Subject: [PATCH 5/6] Update file structure for B9PS patch project --- Source/{B9PSPatch => B9PSPatchProject}/B9PSMFTHandler.cs | 2 +- Source/{B9PSPatch => B9PSPatchProject/Harmony}/B9PS.cs | 2 +- Source/{B9PSPatch => B9PSPatchProject}/HarmonyPatcher.cs | 2 +- .../{B9PSPatch => B9PSPatchProject}/RealFuelsB9PSPatch.csproj | 4 ---- .../assembly/AssemblyInfoB9PSPatch.cs | 0 5 files changed, 3 insertions(+), 7 deletions(-) rename Source/{B9PSPatch => B9PSPatchProject}/B9PSMFTHandler.cs (98%) rename Source/{B9PSPatch => B9PSPatchProject/Harmony}/B9PS.cs (97%) rename Source/{B9PSPatch => B9PSPatchProject}/HarmonyPatcher.cs (90%) rename Source/{B9PSPatch => B9PSPatchProject}/RealFuelsB9PSPatch.csproj (93%) rename Source/{B9PSPatch => B9PSPatchProject}/assembly/AssemblyInfoB9PSPatch.cs (100%) diff --git a/Source/B9PSPatch/B9PSMFTHandler.cs b/Source/B9PSPatchProject/B9PSMFTHandler.cs similarity index 98% rename from Source/B9PSPatch/B9PSMFTHandler.cs rename to Source/B9PSPatchProject/B9PSMFTHandler.cs index 71cf69b9..f8ce3528 100644 --- a/Source/B9PSPatch/B9PSMFTHandler.cs +++ b/Source/B9PSPatchProject/B9PSMFTHandler.cs @@ -1,7 +1,7 @@ using B9PartSwitch.PartSwitch.PartModifiers; -namespace RealFuels.Harmony +namespace RealFuels.B9PSPatch { public class ModuleFuelTanksHandler : PartModifierBase { diff --git a/Source/B9PSPatch/B9PS.cs b/Source/B9PSPatchProject/Harmony/B9PS.cs similarity index 97% rename from Source/B9PSPatch/B9PS.cs rename to Source/B9PSPatchProject/Harmony/B9PS.cs index a1fa3bf3..e678a0ef 100644 --- a/Source/B9PSPatch/B9PS.cs +++ b/Source/B9PSPatchProject/Harmony/B9PS.cs @@ -3,7 +3,7 @@ using B9PartSwitch.PartSwitch.PartModifiers; using B9PartSwitch; -namespace RealFuels.Harmony +namespace RealFuels.B9PSPatch { [HarmonyPatch(typeof(ModuleModifierInfo))] internal class PatchModuleModifierInfo diff --git a/Source/B9PSPatch/HarmonyPatcher.cs b/Source/B9PSPatchProject/HarmonyPatcher.cs similarity index 90% rename from Source/B9PSPatch/HarmonyPatcher.cs rename to Source/B9PSPatchProject/HarmonyPatcher.cs index 21e8172d..00030cc9 100644 --- a/Source/B9PSPatch/HarmonyPatcher.cs +++ b/Source/B9PSPatchProject/HarmonyPatcher.cs @@ -1,6 +1,6 @@ using UnityEngine; -namespace RealFuels.Harmony +namespace RealFuels.B9PSPatch { [KSPAddon(KSPAddon.Startup.Instantly, true)] public class HarmonyPatcher : MonoBehaviour diff --git a/Source/B9PSPatch/RealFuelsB9PSPatch.csproj b/Source/B9PSPatchProject/RealFuelsB9PSPatch.csproj similarity index 93% rename from Source/B9PSPatch/RealFuelsB9PSPatch.csproj rename to Source/B9PSPatchProject/RealFuelsB9PSPatch.csproj index 48eb47a3..71dc9eeb 100644 --- a/Source/B9PSPatch/RealFuelsB9PSPatch.csproj +++ b/Source/B9PSPatchProject/RealFuelsB9PSPatch.csproj @@ -61,10 +61,6 @@ - - - - \ No newline at end of file diff --git a/Source/B9PSPatch/assembly/AssemblyInfoB9PSPatch.cs b/Source/B9PSPatchProject/assembly/AssemblyInfoB9PSPatch.cs similarity index 100% rename from Source/B9PSPatch/assembly/AssemblyInfoB9PSPatch.cs rename to Source/B9PSPatchProject/assembly/AssemblyInfoB9PSPatch.cs From d03441234db9eedbcb2cce6e9ba76fd6909b35c8 Mon Sep 17 00:00:00 2001 From: stonesmileGit Date: Sun, 31 Mar 2024 22:26:08 +0200 Subject: [PATCH 6/6] Fix paths in solution and project files --- Source/B9PSPatchProject/RealFuelsB9PSPatch.csproj | 2 +- Source/RealFuels.sln | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/B9PSPatchProject/RealFuelsB9PSPatch.csproj b/Source/B9PSPatchProject/RealFuelsB9PSPatch.csproj index 71dc9eeb..baf10108 100644 --- a/Source/B9PSPatchProject/RealFuelsB9PSPatch.csproj +++ b/Source/B9PSPatchProject/RealFuelsB9PSPatch.csproj @@ -60,7 +60,7 @@ - + \ No newline at end of file diff --git a/Source/RealFuels.sln b/Source/RealFuels.sln index 7b202f53..02a42697 100644 --- a/Source/RealFuels.sln +++ b/Source/RealFuels.sln @@ -4,7 +4,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 VisualStudioVersion = 16.0.31229.75 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RealFuels", "RealFuels.csproj", "{0041813D-DCD1-4AC7-8327-85765BF924A3}" -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBD}") = "RealFuelsB9PSPatch", "B9PSPatch/RealFuelsB9PSPatch.csproj", "{0041813D-DCD1-4AC7-8327-85765BF924A3}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBD}") = "RealFuelsB9PSPatch", "B9PSPatchProject/RealFuelsB9PSPatch.csproj", "{0041813D-DCD1-4AC7-8327-85765BF924A3}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution