Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Source/HarmonyPatches/ModuleParachutePatch.cs
Original file line number Diff line number Diff line change
@@ -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<ModuleParachute> OnDeployed;
public static event Action<ModuleParachute> 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);
}
}
}
2 changes: 2 additions & 0 deletions Source/KSPCommunityPartModules.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
<PackageReference Include="KSPBuildTools" Version="0.0.2-alpha.7" />
</ItemGroup>
<ItemGroup>
<Compile Include="HarmonyPatches\ModuleParachutePatch.cs" />
<Compile Include="Modules\ModuleAutoCutDrogue.cs" />
<Compile Include="Modules\ModuleCenterFollowTransform.cs" />
<Compile Include="Modules\ModuleCoPFollowTransform.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
37 changes: 1 addition & 36 deletions Source/Modules/ModuleAutoCutDrogue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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<ModuleParachute> OnDeployed;
public static event Action<ModuleParachute> 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);
}
}
}
87 changes: 87 additions & 0 deletions Source/Modules/ModuleCenterFollowTransform.cs
Original file line number Diff line number Diff line change
@@ -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;
Copy link
Collaborator Author

@SofieBrink SofieBrink Nov 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm debating if this should only be done here or in fixed-update too.
Doing it here is probably better for performance, while doing it in fixed-update would mean if something other than B9PS manipulates the KSPFields without calling OnLoad the module would reset the now no longer updating center too.

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;
}
}
}
}
1 change: 1 addition & 0 deletions Source/Modules/ModuleCoPFollowTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down