|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Reflection; |
| 6 | +using UnityEngine; |
| 7 | + |
| 8 | +namespace RealFuels |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Handles integration with B9PartSwitch and TestFlight mods. |
| 12 | + /// Manages B9PS module linking and TestFlight interop values. |
| 13 | + /// </summary> |
| 14 | + public class EngineConfigIntegrations |
| 15 | + { |
| 16 | + private readonly ModuleEngineConfigsBase _module; |
| 17 | + |
| 18 | + // B9PartSwitch reflection fields |
| 19 | + protected static bool _b9psReflectionInitialized = false; |
| 20 | + protected static FieldInfo B9PS_moduleID; |
| 21 | + protected static MethodInfo B9PS_SwitchSubtype; |
| 22 | + protected static FieldInfo B9PS_switchInFlight; |
| 23 | + |
| 24 | + public Dictionary<string, PartModule> B9PSModules; |
| 25 | + protected Dictionary<string, string> RequestedB9PSVariants = new Dictionary<string, string>(); |
| 26 | + |
| 27 | + public EngineConfigIntegrations(ModuleEngineConfigsBase module) |
| 28 | + { |
| 29 | + _module = module; |
| 30 | + InitializeB9PSReflection(); |
| 31 | + } |
| 32 | + |
| 33 | + #region TestFlight Integration |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Updates TestFlight interop values with current configuration. |
| 37 | + /// </summary> |
| 38 | + public void UpdateTFInterops() |
| 39 | + { |
| 40 | + TestFlightWrapper.AddInteropValue(_module.part, _module.isMaster ? "engineConfig" : "vernierConfig", _module.configuration, "RealFuels"); |
| 41 | + } |
| 42 | + |
| 43 | + #endregion |
| 44 | + |
| 45 | + #region B9PartSwitch Integration |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Initializes reflection for B9PartSwitch if available. |
| 49 | + /// </summary> |
| 50 | + private void InitializeB9PSReflection() |
| 51 | + { |
| 52 | + if (_b9psReflectionInitialized || !Utilities.B9PSFound) return; |
| 53 | + B9PS_moduleID = Type.GetType("B9PartSwitch.CustomPartModule, B9PartSwitch")?.GetField("moduleID"); |
| 54 | + B9PS_SwitchSubtype = Type.GetType("B9PartSwitch.ModuleB9PartSwitch, B9PartSwitch")?.GetMethod("SwitchSubtype"); |
| 55 | + B9PS_switchInFlight = Type.GetType("B9PartSwitch.ModuleB9PartSwitch, B9PartSwitch")?.GetField("switchInFlight"); |
| 56 | + _b9psReflectionInitialized = true; |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Loads all B9PartSwitch modules that are linked to configs. |
| 61 | + /// </summary> |
| 62 | + public void LoadB9PSModules() |
| 63 | + { |
| 64 | + IEnumerable<string> b9psModuleIDs = _module.configs |
| 65 | + .Where(cfg => cfg.HasNode("LinkB9PSModule")) |
| 66 | + .SelectMany(cfg => cfg.GetNodes("LinkB9PSModule")) |
| 67 | + .Select(link => link?.GetValue("name")) |
| 68 | + .Where(moduleID => moduleID != null) |
| 69 | + .Distinct(); |
| 70 | + |
| 71 | + B9PSModules = new Dictionary<string, PartModule>(b9psModuleIDs.Count()); |
| 72 | + |
| 73 | + foreach (string moduleID in b9psModuleIDs) |
| 74 | + { |
| 75 | + var module = ModuleEngineConfigsBase.GetSpecifiedModules(_module.part, string.Empty, -1, "ModuleB9PartSwitch", false) |
| 76 | + .FirstOrDefault(m => (string)B9PS_moduleID?.GetValue(m) == moduleID); |
| 77 | + if (module == null) |
| 78 | + Debug.LogError($"*RFMEC* B9PartSwitch module with ID {moduleID} was not found for {_module.part}!"); |
| 79 | + else |
| 80 | + B9PSModules[moduleID] = module; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// Hide the GUI for all `ModuleB9PartSwitch`s managed by RF. |
| 86 | + /// This is somewhat of a hack-ish approach... |
| 87 | + /// </summary> |
| 88 | + public void HideB9PSVariantSelectors() |
| 89 | + { |
| 90 | + if (B9PSModules == null) return; |
| 91 | + foreach (var module in B9PSModules.Values) |
| 92 | + { |
| 93 | + module.Fields["currentSubtypeTitle"].guiActive = false; |
| 94 | + module.Fields["currentSubtypeTitle"].guiActiveEditor = false; |
| 95 | + module.Fields["currentSubtypeIndex"].guiActive = false; |
| 96 | + module.Fields["currentSubtypeIndex"].guiActiveEditor = false; |
| 97 | + module.Events["ShowSubtypesWindow"].guiActive = false; |
| 98 | + module.Events["ShowSubtypesWindow"].guiActiveEditor = false; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Coroutine to hide B9PS in-flight selector after a frame delay. |
| 104 | + /// </summary> |
| 105 | + private IEnumerator HideB9PSInFlightSelector_Coroutine(PartModule module) |
| 106 | + { |
| 107 | + yield return null; |
| 108 | + module.Events["ShowSubtypesWindow"].guiActive = false; |
| 109 | + } |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Requests B9PS variant changes for the given config. |
| 113 | + /// </summary> |
| 114 | + public void RequestB9PSVariantsForConfig(ConfigNode node) |
| 115 | + { |
| 116 | + if (B9PSModules == null || B9PSModules.Count == 0) return; |
| 117 | + RequestedB9PSVariants.Clear(); |
| 118 | + if (node.GetNodes("LinkB9PSModule") is ConfigNode[] links) |
| 119 | + { |
| 120 | + foreach (ConfigNode link in links) |
| 121 | + { |
| 122 | + string moduleID = null, subtype = null; |
| 123 | + if (!link.TryGetValue("name", ref moduleID)) |
| 124 | + Debug.LogError($"*RFMEC* Config `{_module.configurationDisplay}` of {_module.part} has a LinkB9PSModule specification without a name key!"); |
| 125 | + if (!link.TryGetValue("subtype", ref subtype)) |
| 126 | + Debug.LogError($"*RFMEC* Config `{_module.configurationDisplay}` of {_module.part} has a LinkB9PSModule specification without a subtype key!"); |
| 127 | + if (moduleID != null && subtype != null) |
| 128 | + RequestedB9PSVariants[moduleID] = subtype; |
| 129 | + } |
| 130 | + } |
| 131 | + _module.StartCoroutine(ApplyRequestedB9PSVariants_Coroutine()); |
| 132 | + } |
| 133 | + |
| 134 | + /// <summary> |
| 135 | + /// Coroutine that applies requested B9PS variant changes after a frame delay. |
| 136 | + /// </summary> |
| 137 | + protected IEnumerator ApplyRequestedB9PSVariants_Coroutine() |
| 138 | + { |
| 139 | + yield return new WaitForEndOfFrame(); |
| 140 | + |
| 141 | + if (RequestedB9PSVariants.Count == 0) yield break; |
| 142 | + |
| 143 | + foreach (var entry in B9PSModules) |
| 144 | + { |
| 145 | + string moduleID = entry.Key; |
| 146 | + PartModule module = entry.Value; |
| 147 | + |
| 148 | + if (HighLogic.LoadedSceneIsFlight |
| 149 | + && B9PS_switchInFlight != null |
| 150 | + && !(bool)B9PS_switchInFlight.GetValue(module)) continue; |
| 151 | + |
| 152 | + if (!RequestedB9PSVariants.TryGetValue(moduleID, out string subtypeName)) |
| 153 | + { |
| 154 | + Debug.LogError($"*RFMEC* Config {_module.configurationDisplay} of {_module.part} does not specify a subtype for linked B9PS module with ID {moduleID}; defaulting to `{_module.configuration}`."); |
| 155 | + subtypeName = _module.configuration; |
| 156 | + } |
| 157 | + |
| 158 | + B9PS_SwitchSubtype?.Invoke(module, new object[] { subtypeName }); |
| 159 | + if (HighLogic.LoadedSceneIsFlight) _module.StartCoroutine(HideB9PSInFlightSelector_Coroutine(module)); |
| 160 | + } |
| 161 | + |
| 162 | + RequestedB9PSVariants.Clear(); |
| 163 | + // Clear symmetry counterparts' queues since B9PS already handles symmetry. |
| 164 | + _module.DoForEachSymmetryCounterpart(mec => mec.Integrations.ClearRequestedB9PSVariants()); |
| 165 | + } |
| 166 | + |
| 167 | + /// <summary> |
| 168 | + /// Clears the requested B9PS variants queue. |
| 169 | + /// </summary> |
| 170 | + public void ClearRequestedB9PSVariants() |
| 171 | + { |
| 172 | + RequestedB9PSVariants.Clear(); |
| 173 | + } |
| 174 | + |
| 175 | + /// <summary> |
| 176 | + /// Updates B9PS variants based on current config. |
| 177 | + /// </summary> |
| 178 | + public void UpdateB9PSVariants() => RequestB9PSVariantsForConfig(_module.config); |
| 179 | + |
| 180 | + #endregion |
| 181 | + } |
| 182 | +} |
0 commit comments