diff --git a/Projects/TerraTech/TerraTech/ModuleBoosterManager.cs b/Projects/TerraTech/TerraTech/ModuleBoosterManager.cs new file mode 100644 index 0000000..c72110f --- /dev/null +++ b/Projects/TerraTech/TerraTech/ModuleBoosterManager.cs @@ -0,0 +1,66 @@ +using System; +using BepInEx.Configuration; +using HarmonyLib; + +namespace TerraTech { + [HarmonyPatch] + public class ModuleBoosterManager { + private static readonly MultipliedObjectManager fanManager = + new MultipliedObjectManager(ConfigureFanThruster); + private static readonly MultipliedObjectManager jetManager = + new MultipliedObjectManager(ConfigureJetThruster); + + public static ConfigEntry boosterFanThrustMultiplier; + public static ConfigEntry boosterJetThrustMultiplier; + + public static void Setup(ConfigFile config) { + float min = 0.01f; + float max = 32f; + + boosterFanThrustMultiplier = config.Bind( + "Booster", "Booster Fan Thrust Multiplier", 1f, + new ConfigDescription("Booster Fan Thrust Multiplier", new AcceptableValueRange(min, max))); + boosterFanThrustMultiplier.SettingChanged += (sender, args) => DoPatch(); + + boosterJetThrustMultiplier = config.Bind( + "Booster", "Booster Jet Thrust Multiplier", 1f, + new ConfigDescription("Booster Jet Thrust Multiplier", new AcceptableValueRange(min, max))); + boosterJetThrustMultiplier.SettingChanged += (sender, args) => DoPatch(); + } + + private static void ConfigureFanThruster(MultipliedObject obj) { + obj.AddField(new FieldConfiguration("m_Force", boosterFanThrustMultiplier)); + } + + private static void ConfigureJetThruster(MultipliedObject obj) { + obj.AddField(new FieldConfiguration("m_Force", boosterJetThrustMultiplier)); + } + + [HarmonyPrefix] + [HarmonyPatch(typeof(ModuleBooster), "OnAttached")] + static void PostfixCreate(ModuleBooster __instance) { + var trav = Traverse.Create(__instance); + var fans = trav.Field("fans").GetValue(); + var jets = trav.Field("jets").GetValue(); + + foreach (var fan in fans) fanManager.OnObjectAttached(fan); + foreach (var jet in jets) jetManager.OnObjectAttached(jet); + } + + [HarmonyPrefix] + [HarmonyPatch(typeof(ModuleBooster), "OnDetaching")] + static void PostfixDestroy(ModuleBooster __instance) { + var trav = Traverse.Create(__instance); + var fans = trav.Field("fans").GetValue(); + var jets = trav.Field("jets").GetValue(); + + foreach (var fan in fans) fanManager.OnObjectDetached(fan); + foreach (var jet in jets) jetManager.OnObjectDetached(jet); + } + + public static void DoPatch() { + fanManager.ApplyAll(); + jetManager.ApplyAll(); + } + } +}