Skip to content

Commit

Permalink
add QoL change to GroundMotor.movingPlatform.movementTransfer
Browse files Browse the repository at this point in the history
  • Loading branch information
tornac1234 committed Aug 18, 2023
1 parent da6c280 commit 9578635
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 48 deletions.
55 changes: 7 additions & 48 deletions NitroxPatcher/Patches/Dynamic/SubRoot_OnPlayerEntered_Patch.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
Expand All @@ -7,57 +7,16 @@

namespace NitroxPatcher.Patches.Dynamic;

public sealed partial class SubRoot_OnPlayerEntered_Patch : NitroxPatch, IDynamicPatch
public sealed partial class SubRoot_OnPlayerExited_Patch : NitroxPatch, IDynamicPatch
{
private static readonly MethodInfo TARGET_METHOD = Reflect.Method((SubRoot t) => t.OnPlayerEntered(default(Player)));
private static readonly OpCode START_INJECTION_CODE = OpCodes.Ldarg_0;
private static readonly OpCode START_INJECTION_CODE_INVINCIBLE = OpCodes.Stfld;
private static readonly FieldInfo LIVEMIXIN_INVINCIBLE = Reflect.Field((LiveMixin t) => t.invincible);
private static readonly MethodInfo TARGET_METHOD = Reflect.Method((SubRoot t) => t.OnPlayerExited(default(Player)));

/* There is a bug, where Subroot.live is not loaded when starting in a cyclops. Therefore this codepiece needs to check that and jump accordingly if not present
*
* For this change
*
* this.live.invincible = false
*
* to
*
* if (this.live != null)
* {
* this.live.invincible = false
* }
*/
public static IEnumerable<CodeInstruction> Transpiler(MethodBase original, IEnumerable<CodeInstruction> instructions, ILGenerator generator)
public static void Prefix()
{
List<CodeInstruction> instructionList = instructions.ToList();

int injectionPoint = 0;
Label newJumpPoint = generator.DefineLabel();
for (int i = 3; i < instructionList.Count; i++)
{
if (instructionList[i].opcode == START_INJECTION_CODE_INVINCIBLE &&
Equals(instructionList[i].operand, LIVEMIXIN_INVINCIBLE))
{
if (instructionList[i - 3].opcode == START_INJECTION_CODE)
{
instructionList[i + 1].labels.Add(newJumpPoint);
injectionPoint = i - 3;
}
}
}
if (injectionPoint != 0)
PlayerMotor motor = Player.main.playerController.activeController;
if (motor is GroundMotor groundMotor)
{
MethodInfo op_inequality_method = typeof(UnityEngine.Object).GetMethod("op_Inequality");
List<CodeInstruction> injectedInstructions = new()
{
new CodeInstruction(OpCodes.Ldarg_0),
new CodeInstruction(OpCodes.Ldfld, Reflect.Field((SubRoot t) => t.live)),
new CodeInstruction(OpCodes.Ldnull),
new CodeInstruction(OpCodes.Call, op_inequality_method),
new CodeInstruction(OpCodes.Brfalse, newJumpPoint)
};
instructionList.InsertRange(injectionPoint, injectedInstructions);
groundMotor.movingPlatform.movementTransfer = GroundMotor.MovementTransferOnJump.PermaTransfer;
}
return instructionList;
}
}
72 changes: 72 additions & 0 deletions NitroxPatcher/Patches/Dynamic/SubRoot_OnPlayerExited_Patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using HarmonyLib;
using NitroxModel.Helper;

namespace NitroxPatcher.Patches.Dynamic;

public sealed partial class SubRoot_OnPlayerEntered_Patch : NitroxPatch, IDynamicPatch
{
private static readonly MethodInfo TARGET_METHOD = Reflect.Method((SubRoot t) => t.OnPlayerEntered(default(Player)));
private static readonly OpCode START_INJECTION_CODE = OpCodes.Ldarg_0;
private static readonly OpCode START_INJECTION_CODE_INVINCIBLE = OpCodes.Stfld;
private static readonly FieldInfo LIVEMIXIN_INVINCIBLE = Reflect.Field((LiveMixin t) => t.invincible);

/* There is a bug, where Subroot.live is not loaded when starting in a cyclops. Therefore this codepiece needs to check that and jump accordingly if not present
*
* For this change
*
* this.live.invincible = false
*
* to
*
* if (this.live != null)
* {
* this.live.invincible = false
* }
*/
public static IEnumerable<CodeInstruction> Transpiler(MethodBase original, IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
List<CodeInstruction> instructionList = instructions.ToList();

int injectionPoint = 0;
Label newJumpPoint = generator.DefineLabel();
for (int i = 3; i < instructionList.Count; i++)
{
if (instructionList[i].opcode == START_INJECTION_CODE_INVINCIBLE &&
Equals(instructionList[i].operand, LIVEMIXIN_INVINCIBLE))
{
if (instructionList[i - 3].opcode == START_INJECTION_CODE)
{
instructionList[i + 1].labels.Add(newJumpPoint);
injectionPoint = i - 3;
}
}
}
if (injectionPoint != 0)
{
MethodInfo op_inequality_method = typeof(UnityEngine.Object).GetMethod("op_Inequality");
List<CodeInstruction> injectedInstructions = new()
{
new CodeInstruction(OpCodes.Ldarg_0),
new CodeInstruction(OpCodes.Ldfld, Reflect.Field((SubRoot t) => t.live)),
new CodeInstruction(OpCodes.Ldnull),
new CodeInstruction(OpCodes.Call, op_inequality_method),
new CodeInstruction(OpCodes.Brfalse, newJumpPoint)
};
instructionList.InsertRange(injectionPoint, injectedInstructions);
}
return instructionList;
}

public static void Prefix()
{
PlayerMotor motor = Player.main.playerController.activeController;
if (motor is GroundMotor groundMotor)
{
groundMotor.movingPlatform.movementTransfer = GroundMotor.MovementTransferOnJump.PermaLocked;
}
}
}

0 comments on commit 9578635

Please sign in to comment.