Skip to content

Commit

Permalink
New KSP bugfix : [**ModuleAnimateGenericCrewModSpawnIVA**](#169) [KSP…
Browse files Browse the repository at this point in the history
… 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
gotmachine committed Dec 6, 2023
1 parent 246ea78 commit 0640f7e
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 3 deletions.
2 changes: 1 addition & 1 deletion GameData/KSPCommunityFixes/KSPCommunityFixes.version
Expand Up @@ -2,7 +2,7 @@
"NAME": "KSPCommunityFixes",
"URL": "https://raw.githubusercontent.com/KSPModdingLibs/KSPCommunityFixes/master/GameData/KSPCommunityFixes/KSPCommunityFixes.version",
"DOWNLOAD": "https://github.com/KSPModdingLibs/KSPCommunityFixes/releases",
"VERSION": {"MAJOR": 1, "MINOR": 31, "PATCH": 1, "BUILD": 0},
"VERSION": {"MAJOR": 1, "MINOR": 32, "PATCH": 0, "BUILD": 0},
"KSP_VERSION": {"MAJOR": 1, "MINOR": 12, "PATCH": 5},
"KSP_VERSION_MIN": {"MAJOR": 1, "MINOR": 8, "PATCH": 0},
"KSP_VERSION_MAX": {"MAJOR": 1, "MINOR": 12, "PATCH": 5}
Expand Down
4 changes: 4 additions & 0 deletions GameData/KSPCommunityFixes/Settings.cfg
Expand Up @@ -183,6 +183,10 @@ KSP_COMMUNITY_FIXES
// Notably, docking port Kraken drives will no longer work.
DockingPortConserveMomentum = true

// Fix IVA & crew portrait not spawning/despawning when ModuleAnimateGeneric is used to
// change the part crew capacity. Notably affect the stock inflatable airlock.
ModuleAnimateGenericCrewModSpawnIVA = true

// ##########################
// Obsolete bugfixes
// ##########################
Expand Down
57 changes: 57 additions & 0 deletions KSPCommunityFixes/BugFixes/ModuleAnimateGenericCrewModSpawnIVA.cs
@@ -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();
}
}
}
1 change: 1 addition & 0 deletions KSPCommunityFixes/KSPCommunityFixes.csproj
Expand Up @@ -106,6 +106,7 @@
<Compile Include="BugFixes\LadderToggleableLight.cs" />
<Compile Include="BugFixes\MapSOCorrectWrapping.cs" />
<Compile Include="BugFixes\FixGetUnivseralTime.cs" />
<Compile Include="BugFixes\ModuleAnimateGenericCrewModSpawnIVA.cs" />
<Compile Include="BugFixes\ReRootPreserveSurfaceAttach.cs" />
<Compile Include="BugFixes\ThumbnailSpotlight.cs" />
<Compile Include="BugFixes\UpgradeBugs.cs" />
Expand Down
4 changes: 2 additions & 2 deletions KSPCommunityFixes/Properties/AssemblyInfo.cs
Expand Up @@ -30,7 +30,7 @@
// Revision
//
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.31.1.0")]
[assembly: AssemblyFileVersion("1.32.0.0")]

[assembly: KSPAssembly("KSPCommunityFixes", 1, 31, 1)]
[assembly: KSPAssembly("KSPCommunityFixes", 1, 32, 0)]
[assembly: KSPAssemblyDependency("MultipleModulePartAPI", 1, 0, 0)]
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -78,6 +78,7 @@ User options are available from the "ESC" in-game settings menu :<br/><img src="
- [**ThumbnailSpotlight**](https://github.com/KSPModdingLibs/KSPCommunityFixes/pull/149) [KSP 1.12.0 - 1.12.5], fix rogue spotlight staying in the scene when a part thumbnail fails to be generated.
- [**FixGetUnivseralTime**](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/155) [KSP 1.8.0 - 1.12.5]<br/>Fix Planetarium.GetUniversalTime returning bad values in the editor.
- [**DockingPortConserveMomentum**](https://github.com/KSPModdingLibs/KSPCommunityFixes/pull/160) [KSP 1.12.3 - 1.12.5]<br/>Make docking ports conserve momentum by averaging the acquire force between the two ports. Notably, docking port Kraken drives will no longer work.
- [**ModuleAnimateGenericCrewModSpawnIVA**](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/169) [KSP 1.8.0 - 1.12.5]<br/>Fix IVA & crew portrait not spawning/despawning when ModuleAnimateGeneric is used to change the part crew capacity. Notably affect the stock inflatable airlock.

#### Quality of Life tweaks

Expand Down Expand Up @@ -175,6 +176,10 @@ If doing so in the `Debug` configuration and if your KSP install is modified to

### Changelog

##### 1.32.0
- **ConfigNodePerf** : fixed [issue #167](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/167), incorrect parsing of config files using `cr` only (old MacOs style) line endings.
- New KSP bugfix : [**ModuleAnimateGenericCrewModSpawnIVA**](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/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.

##### 1.31.1
- **DragCubeGeneration** : Actually enable patch by default, I somehow failed to push that change in the last release (Thanks @dok_377 for reporting)
- **ReflectionTypeLoadExceptionHandler** : Fixed the exception handler itself throwing an exception in a corner case situation where a dynamic assembly is loaded, causing a call to `Assembly.Location` to throw (Thanks @Lisias for reporting).
Expand Down

0 comments on commit 0640f7e

Please sign in to comment.