Skip to content

Commit

Permalink
Ignore var indices when finding return insn ordinal
Browse files Browse the repository at this point in the history
  • Loading branch information
Su5eD committed Jan 25, 2024
1 parent 469839c commit 8c733ec
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,22 @@ public String findReplacement(List<String> cleanCallOrder, List<String> dirtyCal
}

public boolean test(InstructionMatcher other) {
return test(other, 0);
}

public boolean test(InstructionMatcher other, int flags) {
if (this.before.size() == other.before.size() && this.after.size() == other.after.size()) {
for (int i = 0; i < this.before.size(); i++) {
AbstractInsnNode insn = this.before.get(i);
AbstractInsnNode otherInsn = other.before.get(i);
if (!InsnComparator.instructionsEqual(insn, otherInsn)) {
if (!InsnComparator.instructionsEqual(insn, otherInsn, flags)) {
return false;
}
}
for (int i = 0; i < this.after.size(); i++) {
AbstractInsnNode insn = this.after.get(i);
AbstractInsnNode otherInsn = other.after.get(i);
if (!InsnComparator.instructionsEqual(insn, otherInsn)) {
if (!InsnComparator.instructionsEqual(insn, otherInsn, flags)) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import dev.su5ed.sinytra.adapter.patch.selector.AnnotationHandle;
import dev.su5ed.sinytra.adapter.patch.selector.AnnotationValueHandle;
import dev.su5ed.sinytra.adapter.patch.util.GeneratedVariables;
import dev.su5ed.sinytra.adapter.patch.util.InsnComparator;
import dev.su5ed.sinytra.adapter.patch.util.LocalVariableLookup;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
Expand Down Expand Up @@ -144,7 +145,7 @@ public OptionalInt getUpdatedOrdinal(PatchContext context, MethodNode methodNode
.map(i -> new InstructionMatcher(i, findReturnPrecedingInsns(i), List.of()))
.toList();
List<InstructionMatcher> matches = dirtyMatchers.stream()
.filter(original::test)
.filter(m -> original.test(m, InsnComparator.IGNORE_VAR_INDEX))
.toList();
if (matches.size() == 1) {
return OptionalInt.of(dirtyMatchers.indexOf(matches.get(0)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@

// Source: https://git.sleeping.town/Nil/NilLoader/src/commit/d66d783a5f7ac72a3688594335b3285fcb975b07/src/main/java/nilloader/api/lib/mini/PatchContext.java
public class InsnComparator {
public static final int IGNORE_VAR_INDEX = 0x001;

public static boolean instructionsEqual(AbstractInsnNode a, AbstractInsnNode b) {
return instructionsEqual(a, b, 0);
}

public static boolean instructionsEqual(AbstractInsnNode a, AbstractInsnNode b, int flags) {
if (a == b) return true;
if (a == null || b == null) return false;
if (a.getClass() != b.getClass()) return false;
Expand Down Expand Up @@ -105,7 +110,7 @@ public static boolean instructionsEqual(AbstractInsnNode a, AbstractInsnNode b)
} else if (a instanceof VarInsnNode) {
VarInsnNode va = (VarInsnNode) a;
VarInsnNode vb = (VarInsnNode) b;
return va.var == vb.var;
return (flags & IGNORE_VAR_INDEX) != 0 || va.var == vb.var;
}
throw new IllegalArgumentException("Unknown insn type " + a.getClass().getName());
}
Expand Down

0 comments on commit 8c733ec

Please sign in to comment.