diff --git a/Source/HarmonyPatches/ModuleParachutePatch.cs b/Source/HarmonyPatches/ModuleParachutePatch.cs new file mode 100644 index 0000000..eeb2171 --- /dev/null +++ b/Source/HarmonyPatches/ModuleParachutePatch.cs @@ -0,0 +1,39 @@ +using HarmonyLib; +using System; + +namespace KSPCommunityPartModules.HarmonyPatches +{ + [HarmonyPatch(typeof(ModuleParachute))] + public class ModuleParachuteEvents + { + public static void ModuleManagerPostLoad() + { + Harmony harmony = new Harmony("KSPCommunityPartModules.ModuleParachutePatch"); + harmony.PatchAll(); + } + + public static event Action OnDeployed; + public static event Action OnRepacked; + + [HarmonyPostfix] + [HarmonyPatch("OnParachuteSemiDeployed")] + static void RaiseEventOnSemiDeployed(ModuleParachute __instance) + { + OnDeployed?.Invoke(__instance); + } + + [HarmonyPostfix] + [HarmonyPatch("OnParachuteFullyDeployed")] + static void RaiseEventOnFullyDeployed(ModuleParachute __instance) + { + OnDeployed?.Invoke(__instance); + } + + [HarmonyPostfix] + [HarmonyPatch("Repack")] + static void RaiseEventOnRepack(ModuleParachute __instance) + { + OnRepacked?.Invoke(__instance); + } + } +} diff --git a/Source/KSPCommunityPartModules.csproj b/Source/KSPCommunityPartModules.csproj index 9417581..14ed6de 100644 --- a/Source/KSPCommunityPartModules.csproj +++ b/Source/KSPCommunityPartModules.csproj @@ -34,7 +34,9 @@ + + diff --git a/Source/Modules/ModuleAutoCutDrogue.cs b/Source/Modules/ModuleAutoCutDrogue.cs index 451dd8e..1bcf62b 100644 --- a/Source/Modules/ModuleAutoCutDrogue.cs +++ b/Source/Modules/ModuleAutoCutDrogue.cs @@ -4,10 +4,9 @@ Originally By: Jsolson Originally For: Bluedog Design Bureau */ -using HarmonyLib; -using System; using System.Linq; using UnityEngine; +using KSPCommunityPartModules.HarmonyPatches; namespace KSPCommunityPartModules.Modules { @@ -71,38 +70,4 @@ private void OnParachuteRepacked(ModuleParachute pChute) if (triggered && pChute == chute) triggered = false; } } - - [HarmonyPatch(typeof(ModuleParachute))] - internal class ModuleParachuteEvents - { - public static void ModuleManagerPostLoad() - { - Harmony harmony = new Harmony("KSPCommunityPartModules"); - harmony.PatchAll(); - } - - public static event Action OnDeployed; - public static event Action OnRepacked; - - [HarmonyPostfix] - [HarmonyPatch("OnParachuteSemiDeployed")] - static void RaiseEventOnSemiDeployed(ModuleParachute __instance) - { - OnDeployed?.Invoke(__instance); - } - - [HarmonyPostfix] - [HarmonyPatch("OnParachuteFullyDeployed")] - static void RaiseEventOnFullyDeployed(ModuleParachute __instance) - { - OnDeployed?.Invoke(__instance); - } - - [HarmonyPostfix] - [HarmonyPatch("Repack")] - static void RaiseEventOnRepack(ModuleParachute __instance) - { - OnRepacked?.Invoke(__instance); - } - } } diff --git a/Source/Modules/ModuleCenterFollowTransform.cs b/Source/Modules/ModuleCenterFollowTransform.cs new file mode 100644 index 0000000..12b1998 --- /dev/null +++ b/Source/Modules/ModuleCenterFollowTransform.cs @@ -0,0 +1,87 @@ +/* + Usecase: Making the Center of Pressure, Mass or Lift follow a transform on the same part. + Example: BoringCrewServices Starliner main parachutes, + StarshipExpansionProject Starship Flaps + Originally By: Sofie Brink & JonnyOThan + Originally For: KSPCommunityPartModules +*/ +using UnityEngine; + +namespace KSPCommunityPartModules.Modules +{ + public class ModuleCenterFollowTransform : PartModule + { + public const string MODULENAME = nameof(ModuleCenterFollowTransform); + + [KSPField] + public bool enableCoP = false; + private bool wasEnabledCoP; + + [KSPField] + public bool enableCoM = false; + private bool wasEnabledCoM; + + [KSPField] + public bool enableCoL = false; + private bool wasEnabledCoL; + + [KSPField] + public string transformName; + + [SerializeField] + private Transform followTransform; + + public override void OnLoad(ConfigNode node) + { + bool anyModeActive = enableCoP || enableCoM || enableCoL; + + if (followTransform == null || followTransform.name != transformName) + { + if (transformName != null) followTransform = part.FindModelTransform(transformName); + if (followTransform == null) Debug.LogError($"[{MODULENAME}] transformName '{transformName}' was empty or does not exist on part '{part.partInfo?.name}'"); + } + if (!anyModeActive) + { + Debug.LogWarning($"[{MODULENAME}] no center is following transformName '{transformName}' on part '{part.partInfo?.name}'"); + } + + if (followTransform == null && part.partInfo != null) + { + // this may be important if someone is swapping out versions of this module with B9PS + // Note this probably isn't correct for parts that also have modules that mess with this field (e.g. ModuleProceduralFairing) + if (enableCoP) part.CoPOffset = part.partInfo.partPrefab.CoPOffset; + if (enableCoM) part.CoMOffset = part.partInfo.partPrefab.CoMOffset; + if (enableCoL) part.CoLOffset = part.partInfo.partPrefab.CoLOffset; + } + // Set offets back to the values in the prefab if the mode is disabled after initial initialisation; e.g. B9PS + if (!enableCoP && wasEnabledCoP) part.CoPOffset = part.partInfo.partPrefab.CoPOffset; + if (!enableCoM && wasEnabledCoM) part.CoMOffset = part.partInfo.partPrefab.CoMOffset; + if (!enableCoL && wasEnabledCoL) part.CoLOffset = part.partInfo.partPrefab.CoLOffset; + wasEnabledCoP = enableCoP; + wasEnabledCoM = enableCoM; + wasEnabledCoL = enableCoL; + + // NOTE: isEnabled will be persisted to the save file, but we want to treat it purely as runtime state + isEnabled = followTransform != null && anyModeActive; + enabled = followTransform != null && anyModeActive && HighLogic.LoadedSceneIsFlight; + } + + public void FixedUpdate() + { + // Note that we shouldn't ever get here if the transform just didn't exist + // But it's certainly possible for *something* to delete it later + if (followTransform == null) + { + isEnabled = false; + enabled = false; + } + else + { + Vector3 offset = part.transform.InverseTransformPoint(followTransform.position); + if (enableCoP) part.CoPOffset = offset; + if (enableCoM) part.CoMOffset = offset; + if (enableCoL) part.CoLOffset = offset; + } + } + } +} diff --git a/Source/Modules/ModuleCoPFollowTransform.cs b/Source/Modules/ModuleCoPFollowTransform.cs index 8784762..4a71d87 100644 --- a/Source/Modules/ModuleCoPFollowTransform.cs +++ b/Source/Modules/ModuleCoPFollowTransform.cs @@ -20,6 +20,7 @@ public class ModuleCoPFollowTransform : PartModule public override void OnLoad(ConfigNode node) { + Debug.LogWarning($"[{MODULENAME}] This module is deprecated! please use ModuleCenterFollowTransform instead!"); if (followTransform == null || followTransform.name != transformName) { if (transformName != null) followTransform = part.FindModelTransform(transformName);