From 7d1dc05c6553f496dceec5b0fe95a39c29ed06db Mon Sep 17 00:00:00 2001 From: CCat <63068823+CedaryCat@users.noreply.github.com> Date: Tue, 10 Feb 2026 01:05:33 +0800 Subject: [PATCH] fix: fix arg remap bug in InsertParamAndRemapIndices for Ldarg_1/Ldarg_2 --- .../Core/Patching/PatchingCommon.cs | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/OTAPI.UnifiedServerProcess/Core/Patching/PatchingCommon.cs b/src/OTAPI.UnifiedServerProcess/Core/Patching/PatchingCommon.cs index 02b982d..026da3d 100644 --- a/src/OTAPI.UnifiedServerProcess/Core/Patching/PatchingCommon.cs +++ b/src/OTAPI.UnifiedServerProcess/Core/Patching/PatchingCommon.cs @@ -194,38 +194,30 @@ public static void InsertParamAndRemapIndices(MethodBody body, int index, Parame } } + int insertedArgSlot = body.Method.IsStatic ? index : index + 1; + int shiftedLdarg3ParamIndex = body.Method.IsStatic ? 4 : 3; + foreach (Instruction instruction in body.Instructions) { switch (instruction.OpCode.Code) { case Code.Ldarg_0: - if (index == 0 && body.Method.IsStatic) { + if (insertedArgSlot <= 0) { instruction.OpCode = OpCodes.Ldarg_1; } break; case Code.Ldarg_1: - if (index <= 1) { + if (insertedArgSlot <= 1) { instruction.OpCode = OpCodes.Ldarg_2; } break; case Code.Ldarg_2: - if (index <= 2) { + if (insertedArgSlot <= 2) { instruction.OpCode = OpCodes.Ldarg_3; } break; case Code.Ldarg_3: - if (index <= 3) { + if (insertedArgSlot <= 3) { instruction.OpCode = OpCodes.Ldarg_S; - int newindex; - if (body.Method.IsStatic) { - // static -> static (add inserted-param at first): - // [[arg0, arg1, arg2, ]] -> [[param, arg0, arg1, arg2, ]] - newindex = 4; - } - else { - // instance -> instance (add inserted-param after 'this'): - // [this, [arg0, arg1, ]] -> [this, [param, arg0, arg1, ]] - newindex = 3; - } - instruction.Operand = body.Method.Parameters[newindex]; + instruction.Operand = body.Method.Parameters[shiftedLdarg3ParamIndex]; } break; case Code.Ldarg_S: