Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit 64fbab3

Browse files
committed
fix: Invoke Times patching incorrectly
1 parent ab3df91 commit 64fbab3

1 file changed

Lines changed: 26 additions & 14 deletions

File tree

src/RustServerMetrics/HarmonyPatches/Delayed/InvokeHandlerBase_DoTick_Patch.cs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,21 @@ internal static class InvokeHandlerBase_DoTick_Patch
1616
static System.Diagnostics.Stopwatch _stopwatch = new System.Diagnostics.Stopwatch();
1717
static bool _failedExecution = false;
1818

19-
readonly static CodeInstruction[] _sequenceToFind = new CodeInstruction[]
19+
readonly static CodeInstruction[] _replacementSequenceToFind = new CodeInstruction[]
2020
{
2121
new CodeInstruction(OpCodes.Ldloc_S),
2222
new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(InvokeAction), nameof(InvokeAction.action))),
23-
new CodeInstruction(OpCodes.Callvirt, AccessTools.Method(typeof(Action), nameof(Action.Invoke)))
23+
new CodeInstruction(OpCodes.Callvirt, AccessTools.Method(typeof(Action), nameof(Action.Invoke))),
24+
new CodeInstruction(OpCodes.Br_S)
2425
};
2526

27+
readonly static CodeInstruction[] _jmpSequenceToFind = new CodeInstruction[]
28+
{
29+
new CodeInstruction(OpCodes.Ldloc_S),
30+
new CodeInstruction(OpCodes.Ldc_I4_1),
31+
new CodeInstruction(OpCodes.Add)
32+
};
33+
2634
[HarmonyPrepare]
2735
public static bool Prepare()
2836
{
@@ -44,18 +52,25 @@ public static IEnumerable<MethodBase> TargetMethods(Harmony harmonyInstance)
4452
[HarmonyTranspiler]
4553
public static IEnumerable<CodeInstruction> Transpile(IEnumerable<CodeInstruction> originalInstructions, MethodBase methodBase)
4654
{
47-
_sequenceToFind[0].operand = methodBase.GetMethodBody().LocalVariables.FirstOrDefault(x => x.LocalType == typeof(InvokeAction));
48-
49-
var methodToCallInfo = typeof(InvokeHandlerBase_DoTick_Patch)
50-
.GetMethod(nameof(InvokeHandlerBase_DoTick_Patch.InvokeWrapper), BindingFlags.Static | BindingFlags.NonPublic);
55+
LocalVariableInfo variableInfo = methodBase.GetMethodBody().LocalVariables.FirstOrDefault(x => x.LocalType == typeof(InvokeAction));
56+
_replacementSequenceToFind[0].operand = variableInfo;
57+
_jmpSequenceToFind[0].operand = variableInfo;
5158

5259
var instructionsList = new List<CodeInstruction>(originalInstructions);
53-
var idx = GetSequenceStartIndex(instructionsList, _sequenceToFind);
5460

55-
if (idx < 0) throw new Exception($"Failed to find injection index for {nameof(InvokeHandlerBase_DoTick_Patch)}");
61+
var jmpIdx = GetSequenceStartIndex(instructionsList, _jmpSequenceToFind);
62+
if (jmpIdx < 0) throw new Exception($"Failed to find jmp injection index for {nameof(InvokeHandlerBase_DoTick_Patch)}");
63+
64+
_replacementSequenceToFind[3].operand = instructionsList[jmpIdx];
5665

57-
instructionsList.RemoveRange(idx + 1, _sequenceToFind.Length - 1);
58-
instructionsList.InsertRange(idx + 1, new CodeInstruction[]
66+
var methodToCallInfo = typeof(InvokeHandlerBase_DoTick_Patch)
67+
.GetMethod(nameof(InvokeWrapper), BindingFlags.Static | BindingFlags.NonPublic);
68+
69+
var replacementIdx = GetSequenceStartIndex(instructionsList, _replacementSequenceToFind);
70+
if (replacementIdx < 0) throw new Exception($"Failed to find replacement injection index for {nameof(InvokeHandlerBase_DoTick_Patch)}");
71+
72+
instructionsList.RemoveRange(replacementIdx + 1, _replacementSequenceToFind.Length - 1);
73+
instructionsList.InsertRange(replacementIdx + 1, new CodeInstruction[]
5974
{
6075
new CodeInstruction(OpCodes.Call, methodToCallInfo)
6176
});
@@ -79,10 +94,7 @@ static void InvokeWrapper(InvokeAction invokeAction)
7994
finally
8095
{
8196
_stopwatch.Stop();
82-
if (MetricsLogger.Instance != null)
83-
{
84-
MetricsLogger.Instance.ServerInvokes.LogTime(invokeAction.action.Method, _stopwatch.Elapsed.TotalMilliseconds);
85-
}
97+
MetricsLogger.Instance?.ServerInvokes.LogTime(invokeAction.action.Method, _stopwatch.Elapsed.TotalMilliseconds);
8698
}
8799
}
88100

0 commit comments

Comments
 (0)