using System; using System.Collections.Generic; using BepInEx.Configuration; using HarmonyLib; namespace BanquetForCyka { [HarmonyPatch] public class ModuleBoosterManager { private static readonly MultipliedObjectManager FanManager = new MultipliedObjectManager(ConfigureFanThruster); private static readonly MultipliedObjectManager JetManager = new MultipliedObjectManager(ConfigureJetThruster); private static ConfigEntry playerOnly; private static ConfigEntry fanThrustMultiplier; private static ConfigEntry jetThrustMultiplier; public static void Setup(ConfigFile config) { float min = 0.01f; float max = 32f; playerOnly = config.Bind("Booster", "Player Only", false, new ConfigDescription("Player Only")); playerOnly.SettingChanged += (sender, args) => DoPatch(); fanThrustMultiplier = config.Bind("Booster", "Fan Thrust Multiplier", 1f, new ConfigDescription("Fan Thrust Multiplier", new AcceptableValueRange(min, max))); fanThrustMultiplier.SettingChanged += (sender, args) => DoPatch(); jetThrustMultiplier = config.Bind("Booster", "Jet Thrust Multiplier", 1f, new ConfigDescription("Jet Thrust Multiplier", new AcceptableValueRange(min, max))); jetThrustMultiplier.SettingChanged += (sender, args) => DoPatch(); } private static void ConfigureFanThruster(MultipliedObject obj) { obj.AddField(new FieldConfiguration("m_Force", fanThrustMultiplier, ShouldApply)); } private static void ConfigureJetThruster(MultipliedObject obj) { obj.AddField(new FieldConfiguration("m_Force", jetThrustMultiplier, ShouldApply)); } private static readonly Func ShouldApply = obj => { if (!playerOnly.Value) return true; return CykUtil.isObjectPlayerTank(obj); }; [HarmonyPrefix] [HarmonyPatch(typeof(ModuleBooster), "OnAttached")] public 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")] public 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); } private static void DoPatch() { FanManager.ApplyAll(); JetManager.ApplyAll(); } public static readonly Func Register = obj => { if (Main.debug.Value) Console.WriteLine("Registering ModuleBooster: {0}", obj); PostfixCreate(obj as ModuleBooster); return true; }; } }