-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add some Kinematic prevention patches
- Loading branch information
Showing
5 changed files
with
88 additions
and
7 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
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
56 changes: 56 additions & 0 deletions
56
NitroxPatcher/Patches/Dynamic/FreezeRigidbodyWhenFar_FixedUpdate_Patch.cs
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,56 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Reflection.Emit; | ||
using HarmonyLib; | ||
using NitroxClient.GameLogic; | ||
using NitroxClient.MonoBehaviours; | ||
using NitroxModel.Helper; | ||
using UnityEngine; | ||
|
||
namespace NitroxPatcher.Patches.Dynamic | ||
{ | ||
public sealed partial class FreezeRigidbodyWhenFar_FixedUpdate_Patch : NitroxPatch, IDynamicPatch | ||
{ | ||
public static readonly MethodInfo TARGET_METHOD = Reflect.Method((FreezeRigidbodyWhenFar t) => t.FixedUpdate()); | ||
|
||
public static IEnumerable<CodeInstruction> Transpiler(MethodBase original, IEnumerable<CodeInstruction> instructions, ILGenerator generator) | ||
{ | ||
List<CodeInstruction> instructionList = instructions.ToList(); | ||
for (int i = 0; i < instructionList.Count; i++) | ||
{ | ||
CodeInstruction instruction = instructionList[i]; | ||
|
||
if (instruction.opcode == OpCodes.Call && instruction.operand.Equals(Reflect.Method((Component c) => c.GetComponent<Rigidbody>()))) | ||
{ | ||
yield return instruction; | ||
yield return instructionList[i+1]; | ||
object jmpLabel = null; | ||
|
||
for (int j = i; j < instructionList.Count; j++) // search for branch instruction | ||
{ | ||
if (instructionList[j].opcode == OpCodes.Ble_Un) | ||
{ | ||
jmpLabel = instructionList[j].operand; | ||
break; | ||
} | ||
} | ||
yield return new CodeInstruction(OpCodes.Ldarg_0); | ||
yield return new CodeInstruction(OpCodes.Call, Reflect.Property((Component c) => c.gameObject).GetGetMethod()); | ||
yield return new CodeInstruction(OpCodes.Call, Reflect.Method(() => IsMoving(default))); | ||
yield return new CodeInstruction(OpCodes.Brtrue, jmpLabel); | ||
i = i + 1; | ||
continue; | ||
} | ||
|
||
yield return instruction; | ||
} | ||
} | ||
|
||
public static bool IsMoving(GameObject go) | ||
{ | ||
return go.TryGetComponent(out MovementController mc) && mc.IsMoving; | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
NitroxPatcher/Patches/Dynamic/Vehicle_ShouldSetKinematic_Patch.cs
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,20 @@ | ||
using System.Reflection; | ||
using NitroxClient.MonoBehaviours; | ||
using NitroxModel.Helper; | ||
|
||
namespace NitroxPatcher.Patches.Dynamic; | ||
|
||
public sealed partial class Vehicle_ShouldSetKinematic_Patch : NitroxPatch, IDynamicPatch | ||
{ | ||
public static readonly MethodInfo TARGET_METHOD = Reflect.Method((Vehicle t) => t.ShouldSetKinematic()); | ||
|
||
public static bool Prefix(Vehicle __instance, ref bool __result) | ||
{ | ||
if (__instance.TryGetComponent(out MovementController movementController)) | ||
{ | ||
__result = !movementController.IsMoving; | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
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