diff --git a/Source/B9PSPatchProject/B9PSMFTHandler.cs b/Source/B9PSPatchProject/B9PSMFTHandler.cs new file mode 100644 index 00000000..f8ce3528 --- /dev/null +++ b/Source/B9PSPatchProject/B9PSMFTHandler.cs @@ -0,0 +1,40 @@ + +using B9PartSwitch.PartSwitch.PartModifiers; + +namespace RealFuels.B9PSPatch +{ + 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/B9PSPatchProject/Harmony/B9PS.cs b/Source/B9PSPatchProject/Harmony/B9PS.cs new file mode 100644 index 00000000..e678a0ef --- /dev/null +++ b/Source/B9PSPatchProject/Harmony/B9PS.cs @@ -0,0 +1,32 @@ +using HarmonyLib; +using System.Collections.Generic; +using B9PartSwitch.PartSwitch.PartModifiers; +using B9PartSwitch; + +namespace RealFuels.B9PSPatch +{ + [HarmonyPatch(typeof(ModuleModifierInfo))] + internal class PatchModuleModifierInfo + { + [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/B9PSPatchProject/HarmonyPatcher.cs b/Source/B9PSPatchProject/HarmonyPatcher.cs new file mode 100644 index 00000000..00030cc9 --- /dev/null +++ b/Source/B9PSPatchProject/HarmonyPatcher.cs @@ -0,0 +1,14 @@ +using UnityEngine; + +namespace RealFuels.B9PSPatch +{ + [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/B9PSPatchProject/RealFuelsB9PSPatch.csproj b/Source/B9PSPatchProject/RealFuelsB9PSPatch.csproj new file mode 100644 index 00000000..baf10108 --- /dev/null +++ b/Source/B9PSPatchProject/RealFuelsB9PSPatch.csproj @@ -0,0 +1,66 @@ + + + + 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/B9PSPatchProject/assembly/AssemblyInfoB9PSPatch.cs b/Source/B9PSPatchProject/assembly/AssemblyInfoB9PSPatch.cs new file mode 100644 index 00000000..6893e36b --- /dev/null +++ b/Source/B9PSPatchProject/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.sln b/Source/RealFuels.sln index 47fa9d36..02a42697 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", "B9PSPatchProject/RealFuelsB9PSPatch.csproj", "{0041813D-DCD1-4AC7-8327-85765BF924A3}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution