From e9d1cada2dd0f4ca10ebdcd0f3da151ef4f934b5 Mon Sep 17 00:00:00 2001 From: CiroZDP Date: Tue, 2 Jun 2026 20:16:32 +0200 Subject: [PATCH] refactor(GLConstants): unwrap if conditions --- .../mcp/tools/injector/GLConstants.java | 195 +++++++++--------- 1 file changed, 94 insertions(+), 101 deletions(-) diff --git a/src/main/java/org/mcphackers/mcp/tools/injector/GLConstants.java b/src/main/java/org/mcphackers/mcp/tools/injector/GLConstants.java index 70627a85..8cbfbd04 100644 --- a/src/main/java/org/mcphackers/mcp/tools/injector/GLConstants.java +++ b/src/main/java/org/mcphackers/mcp/tools/injector/GLConstants.java @@ -104,41 +104,39 @@ public static FieldInsnNode getKeyboardInsn(int constant) { } public static InsnList getGLInsn(MethodInsnNode invoke, int constant) { - String pkg = invoke.owner.substring(17); //invoke.owner.replace("org/lwjgl/opengl/", ""); + String pkg = invoke.owner.substring(17); // invoke.owner.replace("org/lwjgl/opengl/", ""); for (Pair>, Map>> group : CONSTANTS) { - Map> methodKeys = group.getLeft(); - List methodList = methodKeys.get(pkg); - if (methodList != null && methodList.contains(invoke.name)) { - Map> methodValues = group.getRight(); - for (Entry> entry : methodValues.entrySet()) { - String key = entry.getKey(); - Map value = entry.getValue(); - String constantValue = value.get(constant); - if (constantValue == null) continue; + List methodList = group.getLeft().get(pkg); + if (methodList == null || !methodList.contains(invoke.name)) + continue; - InsnList instructions = new InsnList(); - int i = -1; - do { - char operator = i == -1 ? 0 : constantValue.charAt(i); - int index1 = i + 1; - i = indexOf(OPERATORS, index1, constantValue); - int index2 = i == -1 ? constantValue.length() : i; - String name = constantValue.substring(index1, index2).trim(); - instructions.add(new FieldInsnNode(Opcodes.GETSTATIC, "org/lwjgl/opengl/" + key, name, "I")); - switch (operator) { - case '|': - instructions.add(new InsnNode(Opcodes.IOR)); - break; - case '&': - instructions.add(new InsnNode(Opcodes.IAND)); - break; - case '^': - instructions.add(new InsnNode(Opcodes.IXOR)); - break; - } - } while (i != -1); - return instructions; - } + for (Entry> entry : group.getRight().entrySet()) { + final String constantValue = entry.getValue().get(constant); + if (constantValue == null) + continue; + + final InsnList instructions = new InsnList(); + int i = -1; + do { + char operator = i == -1 ? 0 : constantValue.charAt(i); + int index1 = i + 1; + i = indexOf(OPERATORS, index1, constantValue); + int index2 = i == -1 ? constantValue.length() : i; + String name = constantValue.substring(index1, index2).trim(); + instructions.add(new FieldInsnNode(Opcodes.GETSTATIC, "org/lwjgl/opengl/" + entry.getKey(), name, "I")); + switch (operator) { + case '|': + instructions.add(new InsnNode(Opcodes.IOR)); + break; + case '&': + instructions.add(new InsnNode(Opcodes.IAND)); + break; + case '^': + instructions.add(new InsnNode(Opcodes.IXOR)); + break; + } + } while (i != -1); + return instructions; } } return null; @@ -146,12 +144,9 @@ public static InsnList getGLInsn(MethodInsnNode invoke, int constant) { public static int indexOf(char[] ch, int fromIndex, String string) { final int max = string.length(); - if (fromIndex < 0) { - fromIndex = 0; - } else if (fromIndex >= max) { - return -1; - } - + fromIndex = Math.max(0, fromIndex); // fromIndex will be clamped to [0, +∞) + if (fromIndex >= max) return -1; + final char[] value = string.toCharArray(); for (int i = fromIndex; i < max; i++) { for (char c : ch) { @@ -216,80 +211,78 @@ private static List getPackages(List>, Map @Override protected void visitMethod(MethodNode node) { if (!INIT) return; - InsnList instructions = node.instructions; - List glCalls = new ArrayList<>(); - List> keyboardConstants = new ArrayList<>(); - for (AbstractInsnNode insn : instructions) { - if (insn instanceof MethodInsnNode) { - MethodInsnNode invoke = (MethodInsnNode) insn; - if (PACKAGES != null && PACKAGES.contains(invoke.owner)) { - glCalls.add(invoke); - } - if (invoke.owner.equals("org/lwjgl/input/Keyboard")) { - if (invoke.name.equals("isKeyDown") || invoke.name.equals("getKeyName")) { - AbstractInsnNode iconst = invoke.getPrevious(); - if (iconst == null) { - continue; - } - Integer value = intValue(iconst); - if (value != null) { - FieldInsnNode getField = getKeyboardInsn(value); - if (getField != null) { - keyboardConstants.add(Pair.of(iconst, getField)); - } - } - } else if (invoke.name.equals("getEventKey")) { - AbstractInsnNode iconst = invoke.getNext(); - if (iconst == null) { - continue; - } - AbstractInsnNode insn2 = iconst.getNext(); - // INVOKE, ICONST, (ANY INSTRUCTION), IADD, ICMP - // or - // INVOKE, ICONST, ICMP - boolean hasCompare = false; - int count = 0; - while (insn2 != null && count < 3 && !hasCompare) { - if (count == 1 && insn2.getOpcode() != Opcodes.IADD) - break; - if (isICmp(insn2.getOpcode())) - hasCompare = true; - count++; - insn2 = insn2.getNext(); - } - if (hasCompare) { - Integer value = intValue(iconst); - if (value != null) { - FieldInsnNode getField = getKeyboardInsn(value); - if (getField != null) { - keyboardConstants.add(Pair.of(iconst, getField)); - } - } - } - } + + final List glCalls = new ArrayList<>(); + final List> keyboardConstants = new ArrayList<>(); + for (AbstractInsnNode insn : node.instructions) { + if (!(insn instanceof MethodInsnNode)) continue; + + MethodInsnNode invoke = (MethodInsnNode) insn; + if (PACKAGES != null && PACKAGES.contains(invoke.owner)) + glCalls.add(invoke); + + if (!invoke.owner.equals("org/lwjgl/input/Keyboard")) continue; + + if (invoke.name.equals("isKeyDown") || invoke.name.equals("getKeyName")) { + AbstractInsnNode iconst = invoke.getPrevious(); + if (iconst == null) { + continue; } + Integer value = intValue(iconst); + if (value == null) continue; + FieldInsnNode getField = getKeyboardInsn(value); + if (getField != null) + keyboardConstants.add(Pair.of(iconst, getField)); + + continue; + } + + if (!invoke.name.equals("getEventKey")) continue; + AbstractInsnNode iconst = invoke.getNext(); + if (iconst == null) continue; + AbstractInsnNode insn2 = iconst.getNext(); + // INVOKE, ICONST, (ANY INSTRUCTION), IADD, ICMP + // or + // INVOKE, ICONST, ICMP + boolean hasCompare = false; + int count = 0; + while (insn2 != null && count < 3 && !hasCompare) { + if (count == 1 && insn2.getOpcode() != Opcodes.IADD) + break; + if (isICmp(insn2.getOpcode())) + hasCompare = true; + count++; + insn2 = insn2.getNext(); + } + + if (!hasCompare) continue; + Integer value = intValue(iconst); + if (value == null) continue; + + FieldInsnNode getField = getKeyboardInsn(value); + if (getField != null) { + keyboardConstants.add(Pair.of(iconst, getField)); } } for (Pair pair : keyboardConstants) { - instructions.set(pair.getLeft(), pair.getRight()); + node.instructions.set(pair.getLeft(), pair.getRight()); } for (MethodInsnNode invoke : glCalls) { IdentifyCall identifiedCall = new IdentifyCall(invoke); for (AbstractInsnNode[] insns : identifiedCall.getArguments()) { for (AbstractInsnNode insn : insns) { - if (insn == null) { - continue; - } + if (insn == null) continue; + Integer intValue = intValue(insn); - if (intValue != null) { - InsnList newinsns = getGLInsn(invoke, intValue); - if (newinsns != null) { - instructions.insert(insn, newinsns); - instructions.remove(insn); - } - } + if (intValue == null) continue; + + InsnList newinsns = getGLInsn(invoke, intValue); + if (newinsns == null) continue; + + node.instructions.insert(insn, newinsns); + node.instructions.remove(insn); } } }