-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New KSP bugfix : [**ModuleAnimateGenericCrewModSpawnIVA**](#169) [KSP…
… 1.8.0 - 1.12.5], fix IVA & crew portrait not spawning/despawning when ModuleAnimateGeneric is used to change the part crew capacity. Notably affect the stock inflatable airlock.
- Loading branch information
1 parent
246ea78
commit 0640f7e
Showing
6 changed files
with
70 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
KSPCommunityFixes/BugFixes/ModuleAnimateGenericCrewModSpawnIVA.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using HarmonyLib; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Reflection.Emit; | ||
|
||
// fix issue #169 : IVA doesn’t work properly for parts with ModuleAnimateGeneric that modify crew capacity | ||
namespace KSPCommunityFixes.BugFixes | ||
{ | ||
internal class ModuleAnimateGenericCrewModSpawnIVA : BasePatch | ||
{ | ||
protected override Version VersionMin => new Version(1, 8, 0); | ||
|
||
protected override void ApplyPatches(List<PatchInfo> patches) | ||
{ | ||
patches.Add(new PatchInfo( | ||
PatchMethodType.Transpiler, | ||
AccessTools.Method(typeof(ModuleAnimateGeneric), nameof(ModuleAnimateGeneric.CheckCrewState)), | ||
this)); | ||
} | ||
|
||
// Insert a call to our static OnCrewCapacityChanged() method in the "if (crewCapacity != base.part.CrewCapacity)" condition | ||
static IEnumerable<CodeInstruction> ModuleAnimateGeneric_CheckCrewState_Transpiler(IEnumerable<CodeInstruction> instructions) | ||
{ | ||
MethodInfo m_RefreshPartContextWindow = AccessTools.Method(typeof(MonoUtilities), nameof(MonoUtilities.RefreshPartContextWindow)); | ||
MethodInfo m_PartModule_GetPart = AccessTools.PropertyGetter(typeof(PartModule), nameof(PartModule.part)); | ||
FieldInfo f_Part_CrewCapacity = AccessTools.Field(typeof(Part), nameof(Part.CrewCapacity)); | ||
MethodInfo m_OnCrewCapacityChanged = AccessTools.Method(typeof(ModuleAnimateGenericCrewModSpawnIVA), nameof(OnCrewCapacityChanged)); | ||
|
||
foreach (CodeInstruction code in instructions) | ||
{ | ||
if (code.opcode == OpCodes.Call && ReferenceEquals(code.operand, m_RefreshPartContextWindow)) | ||
{ | ||
yield return code; | ||
yield return new CodeInstruction(OpCodes.Ldarg_0); | ||
yield return new CodeInstruction(OpCodes.Call, m_PartModule_GetPart); | ||
yield return new CodeInstruction(OpCodes.Ldarg_0); | ||
yield return new CodeInstruction(OpCodes.Call, m_PartModule_GetPart); | ||
yield return new CodeInstruction(OpCodes.Ldfld, f_Part_CrewCapacity); | ||
yield return new CodeInstruction(OpCodes.Call, m_OnCrewCapacityChanged); | ||
} | ||
else | ||
{ | ||
yield return code; | ||
} | ||
} | ||
} | ||
|
||
static void OnCrewCapacityChanged(Part part, int newCrewCapacity) | ||
{ | ||
if (newCrewCapacity > 0) | ||
part.SpawnIVA(); | ||
else | ||
part.DespawnIVA(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters