Skip to content

Commit

Permalink
- New stock configs fix, **[LandingGearLights](#122 : fix the lights …
Browse files Browse the repository at this point in the history
…on the "LY-10" and "LY-35" landing gears not automatically turning on/off when extending/retracting the landing gear.

- New KSP bugfix/QoL patch, **LadderToggleableLight** : fix for the stock "Kelus-LV Bay Mobility Enhancer" light being always active even when the ladder is retracted, and implements manual control of the light.
  • Loading branch information
gotmachine committed Mar 30, 2023
1 parent d34b6b7 commit 5d3f197
Show file tree
Hide file tree
Showing 8 changed files with 230 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": 25, "PATCH": 5, "BUILD": 0},
"VERSION": {"MAJOR": 1, "MINOR": 26, "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
@@ -0,0 +1,27 @@
// Fix for the stock "Kelus-LV Bay Mobility Enhancer" light being always active,
// even when the ladder is retracted. This require the LadderToggleableLight patch
// to be active to work.

@PART[telescopicLadderBay]:FIRST
{
LadderToggleableLight = #$@KSP_COMMUNITY_FIXES/LadderToggleableLight$
}

@PART[telescopicLadderBay]:HAS[#LadderToggleableLight[?rue]]:FIRST
{
MODULE
{
name = ModuleLight
lightName = Point light
}

MODULE
{
name = RetractableLadderLightController
}
}

@PART[telescopicLadderBay]:HAS[#LadderToggleableLight]:FIRST
{
!LadderToggleableLight = delete
}
@@ -0,0 +1,23 @@
// The ModuleWheelDeployment.slaveModules field should match the index
// of the ModuleLight in order for the light to controlled depending on
// the extended / retracted state of the wheel.
// There is a mistake in the stock configs with that field being set to the
// wrong module index, resulting in the lights being always on even when
// the landing gear is retracted. These MM patches correct that.
// Fix issue #122 : https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/122

@PART[GearSmall]:FIRST
{
@MODULE[ModuleWheelDeployment]
{
@slaveModules = 9
}
}

@PART[GearSmall]:FIRST
{
@MODULE[ModuleWheelDeployment]
{
@slaveModules = 9
}
}
4 changes: 4 additions & 0 deletions GameData/KSPCommunityFixes/Settings.cfg
Expand Up @@ -164,6 +164,10 @@ KSP_COMMUNITY_FIXES
// Implements a cache of autogenerated drag cubes and switch the part default drag cube
CorrectDragForFlags = true

// Fix for the stock "Kelus-LV Bay Mobility Enhancer" light being always active even when the
// ladder is retracted, and implements manual control of the light.
LadderToggleableLight = true

// ##########################
// Obsolete bugfixes
// ##########################
Expand Down
166 changes: 166 additions & 0 deletions KSPCommunityFixes/BugFixes/LadderToggleableLight.cs
@@ -0,0 +1,166 @@
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;

// This patch is mainly intended as a fix for the stock "Kelus-LV Bay Mobility Enhancer" light being always active,
// even when the ladder is retracted.
// But this also provide a generalized way of linking a RetractableLadder module to a ModuleLight module, see comments
// in the module for how to use it.

namespace KSPCommunityFixes.BugFixes
{
public class LadderToggleableLight : 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(RetractableLadder), nameof(RetractableLadder.OnStart)),
this));
}

static IEnumerable<CodeInstruction> RetractableLadder_OnStart_Transpiler(IEnumerable<CodeInstruction> instructions)
{
MethodInfo m_KerbalFSM_StartFSM = AccessTools.Method(typeof(KerbalFSM), nameof(KerbalFSM.StartFSM), new[] { typeof(string) });
MethodInfo m_PatchRetractableLadderStateMachine = AccessTools.Method(typeof(LadderToggleableLight), nameof(PatchRetractableLadderStateMachine));

List<CodeInstruction> code = new List<CodeInstruction>(instructions);

for (int i = 0; i < code.Count; i++)
{
if (code[i].opcode == OpCodes.Callvirt && ReferenceEquals(code[i].operand, m_KerbalFSM_StartFSM))
{
for (int j = i; j > i - 6; j--)
{
if (code[j].opcode == OpCodes.Ldarg_0)
{
code.Insert(j, new CodeInstruction(OpCodes.Ldarg_0));
code.Insert(j + 1, new CodeInstruction(OpCodes.Call, m_PatchRetractableLadderStateMachine));
break;
}
}
break;
}
}

return code;
}

static void PatchRetractableLadderStateMachine(RetractableLadder retractableLadder)
{
retractableLadder.st_retracted.OnEnter += delegate
{
RetractableLadderLightController controller = RetractableLadderLightController.GetController(retractableLadder);
if (controller.IsNullRef())
return;

controller.ToggleLight(false);
};

retractableLadder.st_retracted.OnLeave += delegate
{
RetractableLadderLightController controller = RetractableLadderLightController.GetController(retractableLadder);
if (controller.IsNullRef())
return;

controller.ToggleLight(true);
};
}
}

public class RetractableLadderLightController : PartModule
{
/// <summary>
/// If not defined, the controller will target the first found RetractableLadder on the part.
/// If defined, the controller will target the RetractableLadder with a matching "ladderAnimationRootName".
/// </summary>
[KSPField] public string ladderAnimationRootName;

/// <summary>
/// If not defined, the controller will target the first found ModuleLight on the part.
/// If defined, the controller will target the ModuleLight with a matching "lightName".
/// </summary>
[KSPField] public string lightName;

private bool refsLoaded;
private RetractableLadder retractableLadder;
private ModuleLight moduleLight;

public override void OnStart(StartState state)
{
LoadRefs();
}

private void LoadRefs()
{
refsLoaded = true;

for (int i = part.modules.Count; i-- > 0;)
{
PartModule pm = part.modules[i];
if (moduleLight.IsNullRef() && pm is ModuleLight ml)
{
if (!string.IsNullOrEmpty(lightName))
{
if (ml.lightName == lightName)
{
moduleLight = ml;
}
}
else
{
moduleLight = ml;
}
}
else if (retractableLadder.IsNullRef() && pm is RetractableLadder rl)
{
if (!string.IsNullOrEmpty(ladderAnimationRootName))
{
if (rl.ladderAnimationRootName == ladderAnimationRootName)
{
retractableLadder = rl;
}
}
else
{
retractableLadder = rl;
}
}
}
}

public void ToggleLight(bool lightOn)
{
if (moduleLight.IsNullRef())
return;

if (lightOn)
moduleLight.LightsOn();
else
moduleLight.LightsOff();

}

public static RetractableLadderLightController GetController(RetractableLadder ladder)
{
List<PartModule> pmList = ladder.part.modules.modules;
for (int i = pmList.Count; i-- > 0;)
{
if (pmList[i] is RetractableLadderLightController rllc)
{
if (!rllc.refsLoaded)
rllc.LoadRefs();

if (rllc.retractableLadder.RefEquals(ladder))
return rllc;
}
}

return null;
}
}
}
1 change: 1 addition & 0 deletions KSPCommunityFixes/KSPCommunityFixes.csproj
Expand Up @@ -100,6 +100,7 @@
<Compile Include="BugFixes\AsteroidInfiniteMining.cs" />
<Compile Include="BugFixes\ChutePhantomSymmetry.cs" />
<Compile Include="BugFixes\CorrectDragForFlags.cs" />
<Compile Include="BugFixes\LadderToggleableLight.cs" />
<Compile Include="BugFixes\MapSOCorrectWrapping.cs" />
<Compile Include="BugFixes\UpgradeBugs.cs" />
<Compile Include="BugFixes\PartTooltipUpgradesApplyToSubstituteParts.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.25.5.0")]
[assembly: AssemblyFileVersion("1.26.0.0")]

[assembly: KSPAssembly("KSPCommunityFixes", 1, 25, 5)]
[assembly: KSPAssembly("KSPCommunityFixes", 1, 26, 0)]
[assembly: KSPAssemblyDependency("MultipleModulePartAPI", 1, 0, 0)]
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -73,6 +73,7 @@ User options are available from the "ESC" in-game settings menu :<br/><img src="
- **[MapSOCorrectWrapping](https://github.com/KSPModdingLibs/KSPCommunityFixes/pull/83)** [KSP 1.10.0 - 1.12.5]<br/>Fixes issues with biomes crossing the poles (South pole biome at north pole and vice versa). Fixes "polar spikes" in the terrain for 8-bit heightmaps.
- **[ChutePhantomSymmetry](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/107)** [KSP 1.10.0 - 1.12.5]<br/>Fix spread angle still being applied after decoupling symmetry-placed parachutes.
- **[CorrectDragForFlags](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/126)** [KSP 1.12.3 - 1.12.5]<br/>Fix the "panel" variants of the flag parts using a single drag cube, causing excessive drag for the smaller options.
- **LadderToggleableLight** [KSP 1.8.0 - 1.12.5]<br/>Fix for the stock "Kelus-LV Bay Mobility Enhancer" light being always active even when the ladder is retracted, and implements manual control of the light.

#### Quality of Life tweaks

Expand Down Expand Up @@ -122,6 +123,7 @@ User options are available from the "ESC" in-game settings menu :<br/><img src="

#### Stock configs tweaks
- **[ManufacturerFixes](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/62)**<br/>Fix a bunch of stock parts not having manufacturers, add icons for the stock "Stratus Corporation" and "LightYear Tire Company" and two new agents, "FreeFall Parachutes" and "Clamp-O-Tron".
- **[LandingGearLights](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/122)**<br/>Fix the lights on the "LY-10" and "LY-35" landing gears not automatically turning on/off when extending/retracting the landing gear.

### License

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

### Changelog

##### 1.26.0
- New stock configs fix, **[LandingGearLights](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/122)** : fix the lights on the "LY-10" and "LY-35" landing gears not automatically turning on/off when extending/retracting the landing gear.
- New KSP bugfix/QoL patch, **LadderToggleableLight** : fix for the stock "Kelus-LV Bay Mobility Enhancer" light being always active even when the ladder is retracted, and implements manual control of the light.

##### 1.25.5
- Fixed [issue #134](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/133) : fix for [issue #133](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/133) was overly conservative, causing loading to stop on exceptions that don't cause a fatal error in stock.

Expand Down

0 comments on commit 5d3f197

Please sign in to comment.