Skip to content

Commit

Permalink
Add Harmony patch to handle MFTs in B9PS
Browse files Browse the repository at this point in the history
  • Loading branch information
StonesmileGit committed Mar 24, 2024
1 parent ed00517 commit cef58d2
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
80 changes: 80 additions & 0 deletions Source/Harmony/B9PS.cs
Original file line number Diff line number Diff line change
@@ -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<IPartModifier> Postfix_handleMFTConfig(IEnumerable<IPartModifier> 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<ConfigNode>("MFTNode", sourceNode);
module.Events.Send("LoadMFTModuleFromConfigNode", evtDetails);
module.Events.Send("ModuleDataChanged", moduleDataChangedEventDetails);
}
}
}
14 changes: 14 additions & 0 deletions Source/Harmony/HarmonyPatcher.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
9 changes: 9 additions & 0 deletions Source/RealFuels.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,16 @@
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<Reference Include="0Harmony, Version=2.2.1.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<Private>False</Private>
</Reference>
<Reference Include="Assembly-CSharp">
<Private>False</Private>
</Reference>
<Reference Include="B9PartSwitch">
<Private>False</Private>
</Reference>
<Reference Include="ROUtils">
<HintPath>..\..\..\..\..\..\Games\R112\GameData\ROUtils\Plugins\ROUtils.dll</HintPath>
<Private>False</Private>
Expand Down Expand Up @@ -102,6 +109,8 @@
<Compile Include="EntryCosts\UpgradeManager.cs" />
<Compile Include="EntryCosts\EntryCostDatabase.cs" />
<Compile Include="EntryCosts\PartEntryCostHolder.cs" />
<Compile Include="Harmony\HarmonyPatcher.cs" />
<Compile Include="Harmony\B9PS.cs" />
<Compile Include="Pumps\RefuelingPump.cs" />
<Compile Include="Tanks\EditorCrossfeedSetMaintainer.cs" />
<Compile Include="Tanks\FuelInfo.cs" />
Expand Down

0 comments on commit cef58d2

Please sign in to comment.