Skip to content

Commit 48315e0

Browse files
authored
Improve ModuleAutoCutDrogue performance (#11)
* Improve performance - Improved performance of ModuleAutoCutDrogue by harmony patching events to fire when the chutes deploy instead of polling every frame. * Update ModuleAutoCutDrogue.cs - Moved if statement for checking if drogue cutting has already happened this frame, would prevent setting triggered to true previously. * Rearange if statement - Actually check if auto cutting drogues is enabled & rearange if statement to check the simple booleans first. * Update workflow via CKAN - Updated workflow files to set `use-ckan` to true to install the harmony dependency. * Update ModuleAutoCutDrogue.cs - Reversed if in repack chute event so simple boolean gets evaluated first, might save a tiny amount of time.
1 parent 63ad0b4 commit 48315e0

File tree

5 files changed

+71
-18
lines changed

5 files changed

+71
-18
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ jobs:
1515
build:
1616
uses: KSPModdingLibs/KSPBuildTools/.github/workflows/build.yml@main
1717
with:
18-
solution-file-path: 'Source/KSPCommunityPartModules.sln'
18+
solution-file-path: 'Source/KSPCommunityPartModules.sln'
19+
use-ckan: true

.github/workflows/create-release.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ jobs:
1212
uses: KSPModdingLibs/KSPBuildTools/.github/workflows/create-release.yml@main
1313
with:
1414
solution-file-path: 'Source/KSPCommunityPartModules.sln'
15-
version-string: ${{ inputs.version-string }}
15+
version-string: ${{ inputs.version-string }}
16+
use-ckan: true

GameData/KSPCommunityPartModules/KSPCommunityPartModules.ckan

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ description: >-
99

1010
This mod is meant as community project, so feel free to propose additional module ideas by opening an issue, or contribute with a pull request on GitHub.
1111
ksp_version_min: '1.12.3'
12+
depends:
13+
- name: Harmony2
1214
supports:
1315
- name: BoringCrewServices
1416
- name: BluedogDB

Source/KSPCommunityPartModules.csproj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<WarningLevel>4</WarningLevel>
3232
</PropertyGroup>
3333
<ItemGroup>
34-
<PackageReference Include="KSPBuildTools" Version="0.0.2-alpha.5" />
34+
<PackageReference Include="KSPBuildTools" Version="0.0.2-alpha.7" />
3535
</ItemGroup>
3636
<ItemGroup>
3737
<Compile Include="Modules\ModuleAutoCutDrogue.cs" />
@@ -41,6 +41,11 @@
4141
<ItemGroup>
4242
<Content Include="HOW_TO_BUILD.txt" />
4343
</ItemGroup>
44-
<ItemGroup />
44+
<ItemGroup>
45+
<Reference Include="$(KSPRoot)/GameData/000_Harmony/0Harmony.dll">
46+
<Private>False</Private>
47+
<CKANIdentifier>Harmony2</CKANIdentifier>
48+
</Reference>
49+
</ItemGroup>
4550
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
4651
</Project>

Source/Modules/ModuleAutoCutDrogue.cs

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
Originally By: Jsolson
55
Originally For: Bluedog Design Bureau
66
*/
7+
using HarmonyLib;
8+
using System;
79
using System.Linq;
810
using UnityEngine;
911

@@ -22,6 +24,10 @@ class ModuleAutoCutDrogue : PartModule
2224
public bool triggered = false;
2325

2426
private ModuleParachute chute = null;
27+
28+
// Technically this creates an edge case where if two loaded vessels deploy a main parachute at the same time only one of the vessels will cut its drogue chutes,
29+
// but I feel like its unlikely enough to not warrent the effort required to fix it.
30+
private static int lastFrame;
2531
private bool IsChuteDeployed(ModuleParachute pParachute) => pParachute.deploymentState == ModuleParachute.deploymentStates.DEPLOYED
2632
|| pParachute.deploymentState == ModuleParachute.deploymentStates.SEMIDEPLOYED;
2733
public override void OnStart(StartState state)
@@ -32,33 +38,71 @@ public override void OnStart(StartState state)
3238

3339
Fields[nameof(autoCutDrogue)].guiActive = !isDrogueChute;
3440
Fields[nameof(autoCutDrogue)].guiActiveEditor = !isDrogueChute;
41+
42+
if (!isDrogueChute) ModuleParachuteEvents.OnDeployed += OnParachuteDeployed;
43+
if (!isDrogueChute) ModuleParachuteEvents.OnRepacked += OnParachuteRepacked;
3544
}
3645

37-
public void FixedUpdate()
46+
public void OnDestroy()
3847
{
39-
if (isDrogueChute || chute == null)
40-
{
41-
this.isEnabled = false;
42-
this.enabled = false;
43-
return;
44-
}
48+
ModuleParachuteEvents.OnDeployed -= OnParachuteDeployed;
49+
ModuleParachuteEvents.OnRepacked -= OnParachuteRepacked;
50+
}
4551

46-
if (IsChuteDeployed(chute))
52+
private void OnParachuteDeployed(ModuleParachute pChute)
53+
{
54+
if (autoCutDrogue && !triggered && pChute == chute)
4755
{
48-
if (!triggered)
56+
if (lastFrame != Time.frameCount)
4957
{
5058
var drogues = vessel.FindPartModulesImplementing<ModuleAutoCutDrogue>().Where(d => d.isDrogueChute && d.chute != null);
5159
foreach (ModuleAutoCutDrogue d in drogues)
5260
{
5361
if (IsChuteDeployed(d.chute)) d.chute.CutParachute();
5462
}
55-
triggered = true;
63+
lastFrame = Time.frameCount;
5664
}
65+
triggered = true;
5766
}
58-
else if (chute.deploymentState == ModuleParachute.deploymentStates.STOWED)
59-
{
60-
triggered = false;
61-
}
67+
}
68+
69+
private void OnParachuteRepacked(ModuleParachute pChute)
70+
{
71+
if (triggered && pChute == chute) triggered = false;
72+
}
73+
}
74+
75+
[HarmonyPatch(typeof(ModuleParachute))]
76+
internal class ModuleParachuteEvents
77+
{
78+
public static void ModuleManagerPostLoad()
79+
{
80+
Harmony harmony = new Harmony("KSPCommunityPartModules");
81+
harmony.PatchAll();
82+
}
83+
84+
public static event Action<ModuleParachute> OnDeployed;
85+
public static event Action<ModuleParachute> OnRepacked;
86+
87+
[HarmonyPostfix]
88+
[HarmonyPatch("OnParachuteSemiDeployed")]
89+
static void RaiseEventOnSemiDeployed(ModuleParachute __instance)
90+
{
91+
OnDeployed?.Invoke(__instance);
92+
}
93+
94+
[HarmonyPostfix]
95+
[HarmonyPatch("OnParachuteFullyDeployed")]
96+
static void RaiseEventOnFullyDeployed(ModuleParachute __instance)
97+
{
98+
OnDeployed?.Invoke(__instance);
99+
}
100+
101+
[HarmonyPostfix]
102+
[HarmonyPatch("Repack")]
103+
static void RaiseEventOnRepack(ModuleParachute __instance)
104+
{
105+
OnRepacked?.Invoke(__instance);
62106
}
63107
}
64108
}

0 commit comments

Comments
 (0)