Skip to content

Commit 7d1dc05

Browse files
committed
fix: fix arg remap bug in InsertParamAndRemapIndices for Ldarg_1/Ldarg_2
1 parent da22828 commit 7d1dc05

1 file changed

Lines changed: 8 additions & 16 deletions

File tree

src/OTAPI.UnifiedServerProcess/Core/Patching/PatchingCommon.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -194,38 +194,30 @@ public static void InsertParamAndRemapIndices(MethodBody body, int index, Parame
194194
}
195195
}
196196

197+
int insertedArgSlot = body.Method.IsStatic ? index : index + 1;
198+
int shiftedLdarg3ParamIndex = body.Method.IsStatic ? 4 : 3;
199+
197200
foreach (Instruction instruction in body.Instructions) {
198201
switch (instruction.OpCode.Code) {
199202
case Code.Ldarg_0:
200-
if (index == 0 && body.Method.IsStatic) {
203+
if (insertedArgSlot <= 0) {
201204
instruction.OpCode = OpCodes.Ldarg_1;
202205
}
203206
break;
204207
case Code.Ldarg_1:
205-
if (index <= 1) {
208+
if (insertedArgSlot <= 1) {
206209
instruction.OpCode = OpCodes.Ldarg_2;
207210
}
208211
break;
209212
case Code.Ldarg_2:
210-
if (index <= 2) {
213+
if (insertedArgSlot <= 2) {
211214
instruction.OpCode = OpCodes.Ldarg_3;
212215
}
213216
break;
214217
case Code.Ldarg_3:
215-
if (index <= 3) {
218+
if (insertedArgSlot <= 3) {
216219
instruction.OpCode = OpCodes.Ldarg_S;
217-
int newindex;
218-
if (body.Method.IsStatic) {
219-
// static -> static (add inserted-param at first):
220-
// [[arg0, arg1, arg2, <arg3>]] -> [[param, arg0, arg1, arg2, <arg3>]]
221-
newindex = 4;
222-
}
223-
else {
224-
// instance -> instance (add inserted-param after 'this'):
225-
// [this, [arg0, arg1, <arg2>]] -> [this, [param, arg0, arg1, <arg2>]]
226-
newindex = 3;
227-
}
228-
instruction.Operand = body.Method.Parameters[newindex];
220+
instruction.Operand = body.Method.Parameters[shiftedLdarg3ParamIndex];
229221
}
230222
break;
231223
case Code.Ldarg_S:

0 commit comments

Comments
 (0)