Skip to content

Commit a448a98

Browse files
committed
Re-use new variable indexes
1 parent 89c98ab commit a448a98

File tree

1 file changed

+74
-24
lines changed

1 file changed

+74
-24
lines changed

src/main/java/com/zero/retrowrapper/injector/M1ColorTweakInjector.java

Lines changed: 74 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ public byte[] transform(final String name, final String transformedName, final b
215215
methodNode.instructions.remove(toPatch);
216216
}
217217

218+
int index1 = -1;
219+
int index2 = -1;
220+
int index3 = -1;
221+
int index4 = -1;
222+
218223
for (final MethodInsnNode toPatch : foundFogFloatBufCalls) {
219224
System.out.println("Patching call to " + toPatch.owner + "." + toPatch.name + toPatch.desc + " at class " + name);
220225
// RGBA to BRGA
@@ -234,20 +239,28 @@ public byte[] transform(final String name, final String transformedName, final b
234239
final InsnNode dup_3 = new InsnNode(Opcodes.DUP);
235240
final MethodInsnNode get_4 = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/nio/FloatBuffer", "get", "()F");
236241
final InsnNode swap_4 = new InsnNode(Opcodes.SWAP);
242+
243+
if (index1 == -1) {
244+
index1 = methodNode.maxLocals;
245+
methodNode.maxLocals++;
246+
}
247+
237248
// Store reference to buffer
238-
final int indexR = methodNode.maxLocals;
239-
methodNode.maxLocals++;
240-
final VarInsnNode storeBuffer = new VarInsnNode(Opcodes.ASTORE, indexR);
249+
final VarInsnNode storeBuffer = new VarInsnNode(Opcodes.ASTORE, index1);
241250
// RGBA -> ARGB because values get consumed in reverse order when putting float values in buffer
242251
final InsnNode swap = new InsnNode(Opcodes.SWAP);
243-
final int indexB = methodNode.maxLocals;
244-
methodNode.maxLocals++;
245-
final VarInsnNode storeBlue = new VarInsnNode(Opcodes.FSTORE, indexB);
252+
253+
if (index2 == -1) {
254+
index2 = methodNode.maxLocals;
255+
methodNode.maxLocals++;
256+
}
257+
258+
final VarInsnNode storeBlue = new VarInsnNode(Opcodes.FSTORE, index2);
246259
final InsnNode dup_x2 = new InsnNode(Opcodes.DUP_X2);
247260
final InsnNode pop = new InsnNode(Opcodes.POP);
248-
final VarInsnNode loadBlue = new VarInsnNode(Opcodes.FLOAD, indexB);
261+
final VarInsnNode loadBlue = new VarInsnNode(Opcodes.FLOAD, index2);
249262
// Load reference to buffer
250-
final VarInsnNode loadBuffer = new VarInsnNode(Opcodes.ALOAD, indexR);
263+
final VarInsnNode loadBuffer = new VarInsnNode(Opcodes.ALOAD, index1);
251264
final InsnNode dup = new InsnNode(Opcodes.DUP);
252265
// Clear buffer
253266
final MethodInsnNode clear = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/nio/FloatBuffer", "clear", "()Ljava/nio/Buffer;");
@@ -264,7 +277,7 @@ public byte[] transform(final String name, final String transformedName, final b
264277
// Flip buffer
265278
final MethodInsnNode flip = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/nio/FloatBuffer", "flip", "()Ljava/nio/Buffer;");
266279
final InsnNode pop_return_2 = new InsnNode(Opcodes.POP);
267-
final VarInsnNode loadBuffer2 = new VarInsnNode(Opcodes.ALOAD, indexR);
280+
final VarInsnNode loadBuffer2 = new VarInsnNode(Opcodes.ALOAD, index1);
268281

269282
if (RetroTweaker.m1PatchMode != M1PatchMode.ForceEnable) {
270283
methodNode.instructions.insertBefore(toPatch, getFullscreen);
@@ -343,7 +356,17 @@ public byte[] transform(final String name, final String transformedName, final b
343356
if (reorderLoadIns != null) {
344357
reorder3LoadIns(methodNode, reorderLoadIns, toPatch, _p1, _p2, _p3);
345358
} else {
346-
swap3Type2(methodNode, toPatch);
359+
if (index1 == -1) {
360+
index1 = methodNode.maxLocals;
361+
methodNode.maxLocals++;
362+
}
363+
364+
if (index2 == -1) {
365+
index2 = methodNode.maxLocals;
366+
methodNode.maxLocals++;
367+
}
368+
369+
swap3Type2(methodNode, toPatch, index1);
347370
}
348371
}
349372
}
@@ -366,8 +389,13 @@ public byte[] transform(final String name, final String transformedName, final b
366389
} else if (isP1Load) {
367390
swap3Type1(methodNode, _p1);
368391
} else {
392+
if (index1 == -1) {
393+
index1 = methodNode.maxLocals;
394+
methodNode.maxLocals++;
395+
}
396+
369397
final Type storeType = Type.getArgumentTypes(toPatch.desc)[0];
370-
swap4Type1(methodNode, toPatch, storeType);
398+
swap4Type1(methodNode, toPatch, storeType, index1);
371399
}
372400
}
373401
}
@@ -390,9 +418,39 @@ public byte[] transform(final String name, final String transformedName, final b
390418
if (isP1Load && (reorderLoadIns != null)) {
391419
reorder3LoadIns(methodNode, reorderLoadIns, _p1, _p2, _p3, _p4);
392420
} else if (isP1Load) {
393-
swap3Type2(methodNode, _p1);
421+
if (index1 == -1) {
422+
index1 = methodNode.maxLocals;
423+
methodNode.maxLocals++;
424+
}
425+
426+
if (index2 == -1) {
427+
index2 = methodNode.maxLocals;
428+
methodNode.maxLocals++;
429+
}
430+
431+
swap3Type2(methodNode, _p1, index1);
394432
} else {
395-
swap4Type2(methodNode, toPatch);
433+
if (index1 == -1) {
434+
index1 = methodNode.maxLocals;
435+
methodNode.maxLocals++;
436+
}
437+
438+
if (index2 == -1) {
439+
index2 = methodNode.maxLocals;
440+
methodNode.maxLocals++;
441+
}
442+
443+
if (index3 == -1) {
444+
index3 = methodNode.maxLocals;
445+
methodNode.maxLocals++;
446+
}
447+
448+
if (index4 == -1) {
449+
index4 = methodNode.maxLocals;
450+
methodNode.maxLocals++;
451+
}
452+
453+
swap4Type2(methodNode, toPatch, index1, index3);
396454
}
397455
}
398456
}
@@ -484,12 +542,10 @@ private static void swap3Type1(MethodNode methodNode, AbstractInsnNode toPatch)
484542
}
485543
}
486544

487-
private static void swap3Type2(MethodNode methodNode, AbstractInsnNode toPatch) {
545+
private static void swap3Type2(MethodNode methodNode, AbstractInsnNode toPatch, int index) {
488546
final LabelNode target = new LabelNode();
489547
final FieldInsnNode getFullscreen = new FieldInsnNode(Opcodes.GETSTATIC, "com/zero/retrowrapper/injector/M1ColorTweakInjector", "isMinecraftFullscreen", "Z");
490548
final JumpInsnNode skipIfFullscreen = new JumpInsnNode(Opcodes.IFNE, target);
491-
final int index = methodNode.maxLocals;
492-
methodNode.maxLocals += 2;
493549
// RGB
494550
final InsnNode dup2_x2 = new InsnNode(Opcodes.DUP2_X2);
495551
// RBGB
@@ -527,9 +583,7 @@ private static void swap3Type2(MethodNode methodNode, AbstractInsnNode toPatch)
527583
}
528584
}
529585

530-
private static void swap4Type1(MethodNode methodNode, AbstractInsnNode toPatch, Type storeType) {
531-
final int index = methodNode.maxLocals;
532-
methodNode.maxLocals++;
586+
private static void swap4Type1(MethodNode methodNode, AbstractInsnNode toPatch, Type storeType, int index) {
533587
final LabelNode target = new LabelNode();
534588
final FieldInsnNode getFullscreen = new FieldInsnNode(Opcodes.GETSTATIC, "com/zero/retrowrapper/injector/M1ColorTweakInjector", "isMinecraftFullscreen", "Z");
535589
final JumpInsnNode skipIfFullscreen = new JumpInsnNode(Opcodes.IFNE, target);
@@ -555,11 +609,7 @@ private static void swap4Type1(MethodNode methodNode, AbstractInsnNode toPatch,
555609
}
556610
}
557611

558-
private static void swap4Type2(MethodNode methodNode, AbstractInsnNode toPatch) {
559-
final int index = methodNode.maxLocals;
560-
methodNode.maxLocals += 2;
561-
final int index2 = methodNode.maxLocals;
562-
methodNode.maxLocals += 2;
612+
private static void swap4Type2(MethodNode methodNode, AbstractInsnNode toPatch, int index, int index2) {
563613
final LabelNode target = new LabelNode();
564614
final FieldInsnNode getFullscreen = new FieldInsnNode(Opcodes.GETSTATIC, "com/zero/retrowrapper/injector/M1ColorTweakInjector", "isMinecraftFullscreen", "Z");
565615
final JumpInsnNode skipIfFullscreen = new JumpInsnNode(Opcodes.IFNE, target);

0 commit comments

Comments
 (0)