Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- New stock configs fix, **[LandingGearLights](#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.
- Loading branch information
1 parent
d34b6b7
commit 5d3f197
Showing
8 changed files
with
230 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
27 changes: 27 additions & 0 deletions
27
GameData/KSPCommunityFixes/MMPatches/StockTweaks/LadderToggleableLight.cfg
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,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 | ||
} |
23 changes: 23 additions & 0 deletions
23
GameData/KSPCommunityFixes/MMPatches/StockTweaks/LandingGearLights.cfg
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,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 | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
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