Skip to content

Commit

Permalink
Patch ModuleRCS to not allow throttling to get around control locker …
Browse files Browse the repository at this point in the history
…(and prevent any actuation when non-axial or from MJ)
  • Loading branch information
NathanKell committed Oct 6, 2023
1 parent 6e81718 commit 816d865
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Source/RP0/Avionics/ControlLocker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class ControlLocker : MonoBehaviour
private static PropertyInfo _mjDeactivateControl = null;
private object _masterMechJeb = null;

public static ControlLocker Instance { get; private set; }

private void Awake()
{
if (!_isFirstLoad) return;
Expand All @@ -52,6 +54,11 @@ private void Awake()
RP0Debug.Log($"{ModTag} MechJeb methods found", true);
else
RP0Debug.Log($"{ModTag} MJ assembly or methods NOT found", true);

if (Instance != null)
Destroy(Instance);

Instance = this;
}

private void Start()
Expand Down Expand Up @@ -175,6 +182,9 @@ public void OnDestroy()
GameEvents.onVesselSwitching.Remove(OnVesselSwitchingHandler);
GameEvents.onVesselGoOnRails.Remove(OnRailsHandler);
GameEvents.onVesselGoOffRails.Remove(OffRailsHandler);

if (Instance == this)
Instance = null;
}

private void DisableAutopilot()
Expand Down
70 changes: 70 additions & 0 deletions Source/RP0/Harmony/ModuleRCS.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using HarmonyLib;

namespace RP0.Harmony
{
[HarmonyPatch(typeof(ModuleRCS))]
internal class PatchModuleRCS
{
private static bool _resetState = false;
private static readonly FlightCtrlState _state = new FlightCtrlState();

[HarmonyPrefix]
[HarmonyPatch("Update")]
internal static void Prefix_Update(ModuleRCS __instance)
{
if (!HighLogic.LoadedSceneIsFlight || __instance.vessel == null || __instance.vessel != FlightGlobals.ActiveVessel)
return;

if (ControlLocker.Instance == null)
return;

var level = ControlLocker.Instance.ShouldLock();
if (level == ControlLockerUtils.LockLevel.Unlocked)
return;

_resetState = true;
_state.X = __instance.vessel.ctrlState.X;
_state.Y = __instance.vessel.ctrlState.Y;
_state.Z = __instance.vessel.ctrlState.Z;
_state.pitch = __instance.vessel.ctrlState.pitch;
_state.yaw = __instance.vessel.ctrlState.yaw;
_state.roll = __instance.vessel.ctrlState.roll;
_state.mainThrottle = __instance.vessel.ctrlState.mainThrottle;

__instance.vessel.ctrlState.X = __instance.vessel.ctrlState.Y = __instance.vessel.ctrlState.pitch = __instance.vessel.ctrlState.yaw = __instance.vessel.ctrlState.roll = 0f;

if (level == ControlLockerUtils.LockLevel.Axial)
{
if (__instance.vessel.ctrlState.Z < 0f)
__instance.vessel.ctrlState.Z = -1f;
else if (__instance.vessel.ctrlState.Z > 0f)
__instance.vessel.ctrlState.Z = 1f;

if (__instance.vessel.ctrlState.mainThrottle > 0f)
__instance.vessel.ctrlState.mainThrottle = 1f;
}
else
{
__instance.vessel.ctrlState.Z = 0f;
__instance.vessel.ctrlState.mainThrottle = 0f;
}
}

[HarmonyPostfix]
[HarmonyPatch("Update")]
internal static void Postfix_Update(ModuleRCS __instance)
{
if (!_resetState)
return;

_resetState = false;
__instance.vessel.ctrlState.X = _state.X;
__instance.vessel.ctrlState.Y = _state.Y;
__instance.vessel.ctrlState.Z = _state.Z;
__instance.vessel.ctrlState.pitch = _state.pitch;
__instance.vessel.ctrlState.yaw = _state.yaw;
__instance.vessel.ctrlState.roll = _state.roll;
__instance.vessel.ctrlState.mainThrottle = _state.mainThrottle;
}
}
}
1 change: 1 addition & 0 deletions Source/RP0/RP0.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<ItemGroup>
<Compile Include="Avionics\ControlLockerUtils.cs" />
<Compile Include="Avionics\EditorBinder.cs" />
<Compile Include="Harmony\ModuleRCS.cs" />
<Compile Include="Harmony\KSPWheel.cs" />
<Compile Include="UI\ProceduralAvionicsWindow.cs" />
<Compile Include="CareerLog\CareerEvent.cs" />
Expand Down

0 comments on commit 816d865

Please sign in to comment.