-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Harmony patch to handle MFTs in B9PS
- Loading branch information
1 parent
ed00517
commit cef58d2
Showing
3 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters