From 64abe46971e53ae8db287805bde06a5da917018c Mon Sep 17 00:00:00 2001 From: Graham Date: Tue, 4 Feb 2020 20:14:07 +0000 Subject: [PATCH] Replace forEach calls with for loops These are artifacts of the Java port. --- .../java/dev/openrs2/asm/MethodNodeUtils.kt | 12 +++++++--- .../openrs2/asm/remap/ClassForNameRemapper.kt | 4 ++-- .../main/java/dev/openrs2/bundler/Bundler.kt | 4 +++- .../transform/BufferSizeTransformer.kt | 6 ++--- .../bundler/transform/HostCheckTransformer.kt | 14 +++++------ .../transform/LoadLibraryTransformer.kt | 6 ++--- .../bundler/transform/MacResizeTransformer.kt | 16 ++++++------- .../dev/openrs2/deob/ast/AstDeobfuscator.kt | 4 ++-- .../ast/transform/IncrementTransformer.kt | 8 +++---- .../java/dev/openrs2/deob/SignedClassUtils.kt | 4 ++-- .../deob/transform/BitShiftTransformer.kt | 6 ++--- .../deob/transform/CounterTransformer.kt | 24 +++++++++---------- .../deob/transform/DummyArgTransformer.kt | 8 +++---- .../transform/ExceptionTracingTransformer.kt | 2 +- .../transform/OpaquePredicateTransformer.kt | 14 +++++------ 15 files changed, 70 insertions(+), 62 deletions(-) diff --git a/asm/src/main/java/dev/openrs2/asm/MethodNodeUtils.kt b/asm/src/main/java/dev/openrs2/asm/MethodNodeUtils.kt index e7edcda99b..ef5f10469f 100644 --- a/asm/src/main/java/dev/openrs2/asm/MethodNodeUtils.kt +++ b/asm/src/main/java/dev/openrs2/asm/MethodNodeUtils.kt @@ -73,17 +73,23 @@ fun MethodNode.removeArgument(argIndex: Int) { if (localVariables != null) { localVariables.removeIf { it.index == localIndex } - localVariables.forEach { it.index = remap(it.index, argType, localIndex) } + for (v in localVariables) { + v.index = remap(v.index, argType, localIndex) + } } if (visibleLocalVariableAnnotations != null) { visibleLocalVariableAnnotations.removeIf { it.index.contains(localIndex) } - visibleLocalVariableAnnotations.forEach { it.index = remapAll(it.index, argType, localIndex) } + for (annotation in visibleLocalVariableAnnotations) { + annotation.index = remapAll(annotation.index, argType, localIndex) + } } if (invisibleLocalVariableAnnotations != null) { invisibleLocalVariableAnnotations.removeIf { it.index.contains(localIndex) } - invisibleLocalVariableAnnotations.forEach { it.index = remapAll(it.index, argType, localIndex) } + for (annotation in invisibleLocalVariableAnnotations) { + annotation.index = remapAll(annotation.index, argType, localIndex) + } } for (insn in instructions) { diff --git a/asm/src/main/java/dev/openrs2/asm/remap/ClassForNameRemapper.kt b/asm/src/main/java/dev/openrs2/asm/remap/ClassForNameRemapper.kt index e928ca60b1..0121850150 100644 --- a/asm/src/main/java/dev/openrs2/asm/remap/ClassForNameRemapper.kt +++ b/asm/src/main/java/dev/openrs2/asm/remap/ClassForNameRemapper.kt @@ -23,8 +23,8 @@ object ClassForNameRemapper { } fun remap(remapper: Remapper, method: MethodNode) { - INVOKE_MATCHER.match(method).filter(ClassForNameRemapper::isClassForName).forEach { - val ldc = it[0] as LdcInsnNode + for (match in INVOKE_MATCHER.match(method).filter(ClassForNameRemapper::isClassForName)) { + val ldc = match[0] as LdcInsnNode val name = remapper.map(ldc.cst as String) if (name != null) { ldc.cst = name diff --git a/bundler/src/main/java/dev/openrs2/bundler/Bundler.kt b/bundler/src/main/java/dev/openrs2/bundler/Bundler.kt index 89805f0628..95954e7a84 100644 --- a/bundler/src/main/java/dev/openrs2/bundler/Bundler.kt +++ b/bundler/src/main/java/dev/openrs2/bundler/Bundler.kt @@ -114,7 +114,9 @@ class Bundler @Inject constructor(publicKeyTransformer: PublicKeyTransformer) { *glNatives.flatten().toTypedArray(), *miscNatives.toTypedArray() ) - resources.forEach { it.write(output) } + for (resource in resources) { + resource.write(output) + } // write unsigned client and loaders client.writeJar(output.resolve("runescape.jar"), unsignedManifest) diff --git a/bundler/src/main/java/dev/openrs2/bundler/transform/BufferSizeTransformer.kt b/bundler/src/main/java/dev/openrs2/bundler/transform/BufferSizeTransformer.kt index 2d7f862ee9..7b2874ff66 100644 --- a/bundler/src/main/java/dev/openrs2/bundler/transform/BufferSizeTransformer.kt +++ b/bundler/src/main/java/dev/openrs2/bundler/transform/BufferSizeTransformer.kt @@ -41,10 +41,10 @@ class BufferSizeTransformer : Transformer() { return false } - NEW_BUFFER_MATCHER.match(method).forEach { - val putstatic = it[4] as FieldInsnNode + for (match in NEW_BUFFER_MATCHER.match(method)) { + val putstatic = match[4] as FieldInsnNode if (MemberRef(putstatic) == buffer!!) { - method.instructions[it[2]] = createIntConstant(65536) + method.instructions[match[2]] = createIntConstant(65536) buffersResized++ } } diff --git a/bundler/src/main/java/dev/openrs2/bundler/transform/HostCheckTransformer.kt b/bundler/src/main/java/dev/openrs2/bundler/transform/HostCheckTransformer.kt index e1ed8a24ef..3f222595ac 100644 --- a/bundler/src/main/java/dev/openrs2/bundler/transform/HostCheckTransformer.kt +++ b/bundler/src/main/java/dev/openrs2/bundler/transform/HostCheckTransformer.kt @@ -24,24 +24,24 @@ class HostCheckTransformer : Transformer() { return false } - GET_HOST_MATCHER.match(method).forEach { - val insn1 = it[0] as MethodInsnNode + for (match in GET_HOST_MATCHER.match(method)) { + val insn1 = match[0] as MethodInsnNode if (insn1.owner != clazz.name || insn1.name != "getDocumentBase" || insn1.desc != "()Ljava/net/URL;") { - return@forEach + continue } - val insn2 = it[1] as MethodInsnNode + val insn2 = match[1] as MethodInsnNode if (insn2.owner != "java/net/URL" || insn2.name != "getHost" || insn2.desc != "()Ljava/lang/String;") { - return@forEach + continue } - val insn3 = it[2] as MethodInsnNode + val insn3 = match[2] as MethodInsnNode if ( insn3.owner != "java/lang/String" || insn3.name != "toLowerCase" || insn3.desc != "()Ljava/lang/String;" ) { - return@forEach + continue } method.instructions.clear() diff --git a/bundler/src/main/java/dev/openrs2/bundler/transform/LoadLibraryTransformer.kt b/bundler/src/main/java/dev/openrs2/bundler/transform/LoadLibraryTransformer.kt index 1667dfe086..2dc1fa153b 100644 --- a/bundler/src/main/java/dev/openrs2/bundler/transform/LoadLibraryTransformer.kt +++ b/bundler/src/main/java/dev/openrs2/bundler/transform/LoadLibraryTransformer.kt @@ -24,10 +24,10 @@ class LoadLibraryTransformer : Transformer() { jnilib.cst = "libjaggl.dylib" jnilibs++ - AMD64_CHECK_MATCHER.match(method).forEach { - val ldc = it[1] as LdcInsnNode + for (match in AMD64_CHECK_MATCHER.match(method)) { + val ldc = match[1] as LdcInsnNode if (ldc.cst == "amd64") { - it.forEach(method.instructions::remove) + match.forEach(method.instructions::remove) amd64Checks++ } } diff --git a/bundler/src/main/java/dev/openrs2/bundler/transform/MacResizeTransformer.kt b/bundler/src/main/java/dev/openrs2/bundler/transform/MacResizeTransformer.kt index 246797e7fb..5aa3186bb4 100644 --- a/bundler/src/main/java/dev/openrs2/bundler/transform/MacResizeTransformer.kt +++ b/bundler/src/main/java/dev/openrs2/bundler/transform/MacResizeTransformer.kt @@ -21,35 +21,35 @@ class MacResizeTransformer : Transformer() { } override fun transformCode(classPath: ClassPath, library: Library, clazz: ClassNode, method: MethodNode): Boolean { - DETECT_MAC_MATCHER.match(method).forEach { - val getstatic = it[0] as FieldInsnNode + for (match in DETECT_MAC_MATCHER.match(method)) { + val getstatic = match[0] as FieldInsnNode if ( getstatic.owner == "loader" || getstatic.owner == clazz.name || getstatic.desc != "Ljava/lang/String;" ) { - return@forEach + continue } - val ldc = it[1] as LdcInsnNode + val ldc = match[1] as LdcInsnNode if (ldc.cst != "mac") { - return@forEach + continue } - val invokevirtual = it[2] as MethodInsnNode + val invokevirtual = match[2] as MethodInsnNode if ( invokevirtual.owner != "java/lang/String" || invokevirtual.name != "startsWith" || invokevirtual.desc != "(Ljava/lang/String;)Z" ) { - return@forEach + continue } method.instructions.remove(getstatic) method.instructions.remove(ldc) method.instructions.remove(invokevirtual) - val branch = it[3] as JumpInsnNode + val branch = match[3] as JumpInsnNode if (branch.opcode == Opcodes.IFEQ) { branch.opcode = Opcodes.GOTO } else { diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.kt index 09b6c50c28..1a2f31a1d5 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.kt @@ -67,8 +67,8 @@ class AstDeobfuscator(private val modules: List) { require(result.isSuccessful) { result } } - root.compilationUnits.forEach { unit -> - TRANSFORMERS.forEach { transformer -> + for (unit in root.compilationUnits) { + for (transformer in TRANSFORMERS) { transformer.transform(unit) } } diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IncrementTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IncrementTransformer.kt index 4037de9d41..c25e7459f2 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IncrementTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IncrementTransformer.kt @@ -19,12 +19,12 @@ class IncrementTransformer : Transformer() { } unit.walk(Node.TreeTraversal.POSTORDER) { stmt: ForStmt -> - stmt.update.forEach { - if (!it.isUnaryExpr) { - return@forEach + for (expr in stmt.update) { + if (!expr.isUnaryExpr) { + continue } - val unaryExpr = it.asUnaryExpr() + val unaryExpr = expr.asUnaryExpr() unaryExpr.operator = prefixToPostfix(unaryExpr.operator) } } diff --git a/deob/src/main/java/dev/openrs2/deob/SignedClassUtils.kt b/deob/src/main/java/dev/openrs2/deob/SignedClassUtils.kt index 71fa0459d1..6f692d1c3c 100644 --- a/deob/src/main/java/dev/openrs2/deob/SignedClassUtils.kt +++ b/deob/src/main/java/dev/openrs2/deob/SignedClassUtils.kt @@ -58,8 +58,8 @@ object SignedClassUtils { private fun findSignedClasses(method: MethodNode): Set { val classes = mutableSetOf() - LOAD_SIGNED_CLASS_MATCHER.match(method).forEach { - val ldc = it[0] as LdcInsnNode + for (match in LOAD_SIGNED_CLASS_MATCHER.match(method)) { + val ldc = match[0] as LdcInsnNode val cst = ldc.cst if (cst is String && cst != "unpack") { classes.add(cst) diff --git a/deob/src/main/java/dev/openrs2/deob/transform/BitShiftTransformer.kt b/deob/src/main/java/dev/openrs2/deob/transform/BitShiftTransformer.kt index 4cc3456312..adf0e9e756 100644 --- a/deob/src/main/java/dev/openrs2/deob/transform/BitShiftTransformer.kt +++ b/deob/src/main/java/dev/openrs2/deob/transform/BitShiftTransformer.kt @@ -24,11 +24,11 @@ class BitShiftTransformer : Transformer() { clazz: ClassNode, method: MethodNode ): Boolean { - CONST_SHIFT_MATCHER.match(method).forEach { - val push = it[0] + for (match in CONST_SHIFT_MATCHER.match(method)) { + val push = match[0] val bits = push.intConstant!! - val opcode = it[1].opcode + val opcode = match[1].opcode val mask = if (LONG_SHIFTS.contains(opcode)) 63 else 31 val simplifiedBits = bits and mask diff --git a/deob/src/main/java/dev/openrs2/deob/transform/CounterTransformer.kt b/deob/src/main/java/dev/openrs2/deob/transform/CounterTransformer.kt index 8d792eb9e3..a90f8908c3 100644 --- a/deob/src/main/java/dev/openrs2/deob/transform/CounterTransformer.kt +++ b/deob/src/main/java/dev/openrs2/deob/transform/CounterTransformer.kt @@ -46,14 +46,14 @@ class CounterTransformer : Transformer() { } } - RESET_PATTERN.match(method).forEach { - val putstatic = MemberRef(it[1] as FieldInsnNode) + for (match in RESET_PATTERN.match(method)) { + val putstatic = MemberRef(match[1] as FieldInsnNode) resets.merge(putstatic, 1, Integer::sum) } - INCREMENT_PATTERN.match(method).forEach { - val getstatic = MemberRef(it[0] as FieldInsnNode) - val putstatic = MemberRef(it[3] as FieldInsnNode) + for (match in INCREMENT_PATTERN.match(method)) { + val getstatic = MemberRef(match[0] as FieldInsnNode) + val putstatic = MemberRef(match[3] as FieldInsnNode) if (getstatic == putstatic) { increments.merge(putstatic, 1, Integer::sum) } @@ -92,18 +92,18 @@ class CounterTransformer : Transformer() { clazz: ClassNode, method: MethodNode ): Boolean { - RESET_PATTERN.match(method).forEach { - val putstatic = it[1] as FieldInsnNode + for (match in RESET_PATTERN.match(method)) { + val putstatic = match[1] as FieldInsnNode if (counters.contains(MemberRef(putstatic))) { - it.forEach(method.instructions::remove) + match.forEach(method.instructions::remove) } } - INCREMENT_PATTERN.match(method).forEach { - val getstatic = MemberRef(it[0] as FieldInsnNode) - val putstatic = MemberRef(it[3] as FieldInsnNode) + for (match in INCREMENT_PATTERN.match(method)) { + val getstatic = MemberRef(match[0] as FieldInsnNode) + val putstatic = MemberRef(match[3] as FieldInsnNode) if (getstatic == putstatic && counters.contains(putstatic)) { - it.forEach(method.instructions::remove) + match.forEach(method.instructions::remove) } } diff --git a/deob/src/main/java/dev/openrs2/deob/transform/DummyArgTransformer.kt b/deob/src/main/java/dev/openrs2/deob/transform/DummyArgTransformer.kt index 06319d7538..054ffd8bdd 100644 --- a/deob/src/main/java/dev/openrs2/deob/transform/DummyArgTransformer.kt +++ b/deob/src/main/java/dev/openrs2/deob/transform/DummyArgTransformer.kt @@ -169,12 +169,12 @@ class DummyArgTransformer : Transformer() { } } - CONDITIONAL_CALL_MATCHER.match(method).forEach { match -> + for (match in CONDITIONAL_CALL_MATCHER.match(method)) { var matchIndex = 0 val load = match[matchIndex++] as VarInsnNode if (stores[load.`var`]) { - return@forEach + continue } var callerSlots = Type.getArgumentsAndReturnSizes(method.desc) shr 2 @@ -182,7 +182,7 @@ class DummyArgTransformer : Transformer() { callerSlots++ } if (load.`var` >= callerSlots) { - return@forEach + continue } val conditionValue: Int? @@ -210,7 +210,7 @@ class DummyArgTransformer : Transformer() { } } - val callee = inheritedMethodSets[MemberRef(invoke)] ?: return@forEach + val callee = inheritedMethodSets[MemberRef(invoke)] ?: continue conditionalCalls.put( parentMethod, ConditionalCall(load.`var`, conditionOpcode, conditionValue, callee, constArgs.asList()) diff --git a/deob/src/main/java/dev/openrs2/deob/transform/ExceptionTracingTransformer.kt b/deob/src/main/java/dev/openrs2/deob/transform/ExceptionTracingTransformer.kt index 556b9635ca..3efe61bc49 100644 --- a/deob/src/main/java/dev/openrs2/deob/transform/ExceptionTracingTransformer.kt +++ b/deob/src/main/java/dev/openrs2/deob/transform/ExceptionTracingTransformer.kt @@ -22,7 +22,7 @@ class ExceptionTracingTransformer : Transformer() { clazz: ClassNode, method: MethodNode ): Boolean { - CATCH_MATCHER.match(method).forEach { match -> + for (match in CATCH_MATCHER.match(method)) { val foundTryCatch = method.tryCatchBlocks.removeIf { tryCatch -> tryCatch.type == "java/lang/RuntimeException" && tryCatch.handler.nextReal === match[0] } diff --git a/deob/src/main/java/dev/openrs2/deob/transform/OpaquePredicateTransformer.kt b/deob/src/main/java/dev/openrs2/deob/transform/OpaquePredicateTransformer.kt index aebad54b44..04b0c4e11b 100644 --- a/deob/src/main/java/dev/openrs2/deob/transform/OpaquePredicateTransformer.kt +++ b/deob/src/main/java/dev/openrs2/deob/transform/OpaquePredicateTransformer.kt @@ -39,7 +39,7 @@ class OpaquePredicateTransformer : Transformer() { } private fun findFlowObstructors(library: Library, method: MethodNode) { - FLOW_OBSTRUCTOR_INITIALIZER_MATCHER.match(method).forEach { match -> + for (match in FLOW_OBSTRUCTOR_INITIALIZER_MATCHER.match(method)) { // add flow obstructor to set val putstatic = match.last() as FieldInsnNode flowObstructors.add(MemberRef(putstatic)) @@ -81,23 +81,23 @@ class OpaquePredicateTransformer : Transformer() { override fun transformCode(classPath: ClassPath, library: Library, clazz: ClassNode, method: MethodNode): Boolean { // find and fix opaque predicates - OPAQUE_PREDICATE_MATCHER.match(method).filter { isOpaquePredicate(method, it) }.forEach { - val branch = it[1] as JumpInsnNode + for (match in OPAQUE_PREDICATE_MATCHER.match(method).filter { isOpaquePredicate(method, it) }) { + val branch = match[1] as JumpInsnNode if (branch.opcode == Opcodes.IFEQ) { // branch is always taken - method.instructions.remove(it[0]) + method.instructions.remove(match[0]) branch.opcode = Opcodes.GOTO } else { // IFNE // branch is never taken - it.forEach(method.instructions::remove) + match.forEach(method.instructions::remove) } opaquePredicates++ } // remove redundant stores - STORE_MATCHER.match(method).filter(this::isRedundantStore).forEach { - it.forEach(method.instructions::remove) + for (match in STORE_MATCHER.match(method).filter(this::isRedundantStore)) { + match.forEach(method.instructions::remove) stores++ }