Replace forEach calls with for loops

These are artifacts of the Java port.
bzip2
Graham 5 years ago
parent 4314aededd
commit 64abe46971
  1. 12
      asm/src/main/java/dev/openrs2/asm/MethodNodeUtils.kt
  2. 4
      asm/src/main/java/dev/openrs2/asm/remap/ClassForNameRemapper.kt
  3. 4
      bundler/src/main/java/dev/openrs2/bundler/Bundler.kt
  4. 6
      bundler/src/main/java/dev/openrs2/bundler/transform/BufferSizeTransformer.kt
  5. 14
      bundler/src/main/java/dev/openrs2/bundler/transform/HostCheckTransformer.kt
  6. 6
      bundler/src/main/java/dev/openrs2/bundler/transform/LoadLibraryTransformer.kt
  7. 16
      bundler/src/main/java/dev/openrs2/bundler/transform/MacResizeTransformer.kt
  8. 4
      deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.kt
  9. 8
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IncrementTransformer.kt
  10. 4
      deob/src/main/java/dev/openrs2/deob/SignedClassUtils.kt
  11. 6
      deob/src/main/java/dev/openrs2/deob/transform/BitShiftTransformer.kt
  12. 24
      deob/src/main/java/dev/openrs2/deob/transform/CounterTransformer.kt
  13. 8
      deob/src/main/java/dev/openrs2/deob/transform/DummyArgTransformer.kt
  14. 2
      deob/src/main/java/dev/openrs2/deob/transform/ExceptionTracingTransformer.kt
  15. 14
      deob/src/main/java/dev/openrs2/deob/transform/OpaquePredicateTransformer.kt

@ -73,17 +73,23 @@ fun MethodNode.removeArgument(argIndex: Int) {
if (localVariables != null) { if (localVariables != null) {
localVariables.removeIf { it.index == localIndex } 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) { if (visibleLocalVariableAnnotations != null) {
visibleLocalVariableAnnotations.removeIf { it.index.contains(localIndex) } 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) { if (invisibleLocalVariableAnnotations != null) {
invisibleLocalVariableAnnotations.removeIf { it.index.contains(localIndex) } 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) { for (insn in instructions) {

@ -23,8 +23,8 @@ object ClassForNameRemapper {
} }
fun remap(remapper: Remapper, method: MethodNode) { fun remap(remapper: Remapper, method: MethodNode) {
INVOKE_MATCHER.match(method).filter(ClassForNameRemapper::isClassForName).forEach { for (match in INVOKE_MATCHER.match(method).filter(ClassForNameRemapper::isClassForName)) {
val ldc = it[0] as LdcInsnNode val ldc = match[0] as LdcInsnNode
val name = remapper.map(ldc.cst as String) val name = remapper.map(ldc.cst as String)
if (name != null) { if (name != null) {
ldc.cst = name ldc.cst = name

@ -114,7 +114,9 @@ class Bundler @Inject constructor(publicKeyTransformer: PublicKeyTransformer) {
*glNatives.flatten().toTypedArray(), *glNatives.flatten().toTypedArray(),
*miscNatives.toTypedArray() *miscNatives.toTypedArray()
) )
resources.forEach { it.write(output) } for (resource in resources) {
resource.write(output)
}
// write unsigned client and loaders // write unsigned client and loaders
client.writeJar(output.resolve("runescape.jar"), unsignedManifest) client.writeJar(output.resolve("runescape.jar"), unsignedManifest)

@ -41,10 +41,10 @@ class BufferSizeTransformer : Transformer() {
return false return false
} }
NEW_BUFFER_MATCHER.match(method).forEach { for (match in NEW_BUFFER_MATCHER.match(method)) {
val putstatic = it[4] as FieldInsnNode val putstatic = match[4] as FieldInsnNode
if (MemberRef(putstatic) == buffer!!) { if (MemberRef(putstatic) == buffer!!) {
method.instructions[it[2]] = createIntConstant(65536) method.instructions[match[2]] = createIntConstant(65536)
buffersResized++ buffersResized++
} }
} }

@ -24,24 +24,24 @@ class HostCheckTransformer : Transformer() {
return false return false
} }
GET_HOST_MATCHER.match(method).forEach { for (match in GET_HOST_MATCHER.match(method)) {
val insn1 = it[0] as MethodInsnNode val insn1 = match[0] as MethodInsnNode
if (insn1.owner != clazz.name || insn1.name != "getDocumentBase" || insn1.desc != "()Ljava/net/URL;") { 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;") { 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 ( if (
insn3.owner != "java/lang/String" || insn3.owner != "java/lang/String" ||
insn3.name != "toLowerCase" || insn3.name != "toLowerCase" ||
insn3.desc != "()Ljava/lang/String;" insn3.desc != "()Ljava/lang/String;"
) { ) {
return@forEach continue
} }
method.instructions.clear() method.instructions.clear()

@ -24,10 +24,10 @@ class LoadLibraryTransformer : Transformer() {
jnilib.cst = "libjaggl.dylib" jnilib.cst = "libjaggl.dylib"
jnilibs++ jnilibs++
AMD64_CHECK_MATCHER.match(method).forEach { for (match in AMD64_CHECK_MATCHER.match(method)) {
val ldc = it[1] as LdcInsnNode val ldc = match[1] as LdcInsnNode
if (ldc.cst == "amd64") { if (ldc.cst == "amd64") {
it.forEach(method.instructions::remove) match.forEach(method.instructions::remove)
amd64Checks++ amd64Checks++
} }
} }

@ -21,35 +21,35 @@ class MacResizeTransformer : Transformer() {
} }
override fun transformCode(classPath: ClassPath, library: Library, clazz: ClassNode, method: MethodNode): Boolean { override fun transformCode(classPath: ClassPath, library: Library, clazz: ClassNode, method: MethodNode): Boolean {
DETECT_MAC_MATCHER.match(method).forEach { for (match in DETECT_MAC_MATCHER.match(method)) {
val getstatic = it[0] as FieldInsnNode val getstatic = match[0] as FieldInsnNode
if ( if (
getstatic.owner == "loader" || getstatic.owner == "loader" ||
getstatic.owner == clazz.name || getstatic.owner == clazz.name ||
getstatic.desc != "Ljava/lang/String;" getstatic.desc != "Ljava/lang/String;"
) { ) {
return@forEach continue
} }
val ldc = it[1] as LdcInsnNode val ldc = match[1] as LdcInsnNode
if (ldc.cst != "mac") { if (ldc.cst != "mac") {
return@forEach continue
} }
val invokevirtual = it[2] as MethodInsnNode val invokevirtual = match[2] as MethodInsnNode
if ( if (
invokevirtual.owner != "java/lang/String" || invokevirtual.owner != "java/lang/String" ||
invokevirtual.name != "startsWith" || invokevirtual.name != "startsWith" ||
invokevirtual.desc != "(Ljava/lang/String;)Z" invokevirtual.desc != "(Ljava/lang/String;)Z"
) { ) {
return@forEach continue
} }
method.instructions.remove(getstatic) method.instructions.remove(getstatic)
method.instructions.remove(ldc) method.instructions.remove(ldc)
method.instructions.remove(invokevirtual) method.instructions.remove(invokevirtual)
val branch = it[3] as JumpInsnNode val branch = match[3] as JumpInsnNode
if (branch.opcode == Opcodes.IFEQ) { if (branch.opcode == Opcodes.IFEQ) {
branch.opcode = Opcodes.GOTO branch.opcode = Opcodes.GOTO
} else { } else {

@ -67,8 +67,8 @@ class AstDeobfuscator(private val modules: List<Path>) {
require(result.isSuccessful) { result } require(result.isSuccessful) { result }
} }
root.compilationUnits.forEach { unit -> for (unit in root.compilationUnits) {
TRANSFORMERS.forEach { transformer -> for (transformer in TRANSFORMERS) {
transformer.transform(unit) transformer.transform(unit)
} }
} }

@ -19,12 +19,12 @@ class IncrementTransformer : Transformer() {
} }
unit.walk(Node.TreeTraversal.POSTORDER) { stmt: ForStmt -> unit.walk(Node.TreeTraversal.POSTORDER) { stmt: ForStmt ->
stmt.update.forEach { for (expr in stmt.update) {
if (!it.isUnaryExpr) { if (!expr.isUnaryExpr) {
return@forEach continue
} }
val unaryExpr = it.asUnaryExpr() val unaryExpr = expr.asUnaryExpr()
unaryExpr.operator = prefixToPostfix(unaryExpr.operator) unaryExpr.operator = prefixToPostfix(unaryExpr.operator)
} }
} }

@ -58,8 +58,8 @@ object SignedClassUtils {
private fun findSignedClasses(method: MethodNode): Set<String> { private fun findSignedClasses(method: MethodNode): Set<String> {
val classes = mutableSetOf<String>() val classes = mutableSetOf<String>()
LOAD_SIGNED_CLASS_MATCHER.match(method).forEach { for (match in LOAD_SIGNED_CLASS_MATCHER.match(method)) {
val ldc = it[0] as LdcInsnNode val ldc = match[0] as LdcInsnNode
val cst = ldc.cst val cst = ldc.cst
if (cst is String && cst != "unpack") { if (cst is String && cst != "unpack") {
classes.add(cst) classes.add(cst)

@ -24,11 +24,11 @@ class BitShiftTransformer : Transformer() {
clazz: ClassNode, clazz: ClassNode,
method: MethodNode method: MethodNode
): Boolean { ): Boolean {
CONST_SHIFT_MATCHER.match(method).forEach { for (match in CONST_SHIFT_MATCHER.match(method)) {
val push = it[0] val push = match[0]
val bits = push.intConstant!! 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 mask = if (LONG_SHIFTS.contains(opcode)) 63 else 31
val simplifiedBits = bits and mask val simplifiedBits = bits and mask

@ -46,14 +46,14 @@ class CounterTransformer : Transformer() {
} }
} }
RESET_PATTERN.match(method).forEach { for (match in RESET_PATTERN.match(method)) {
val putstatic = MemberRef(it[1] as FieldInsnNode) val putstatic = MemberRef(match[1] as FieldInsnNode)
resets.merge(putstatic, 1, Integer::sum) resets.merge(putstatic, 1, Integer::sum)
} }
INCREMENT_PATTERN.match(method).forEach { for (match in INCREMENT_PATTERN.match(method)) {
val getstatic = MemberRef(it[0] as FieldInsnNode) val getstatic = MemberRef(match[0] as FieldInsnNode)
val putstatic = MemberRef(it[3] as FieldInsnNode) val putstatic = MemberRef(match[3] as FieldInsnNode)
if (getstatic == putstatic) { if (getstatic == putstatic) {
increments.merge(putstatic, 1, Integer::sum) increments.merge(putstatic, 1, Integer::sum)
} }
@ -92,18 +92,18 @@ class CounterTransformer : Transformer() {
clazz: ClassNode, clazz: ClassNode,
method: MethodNode method: MethodNode
): Boolean { ): Boolean {
RESET_PATTERN.match(method).forEach { for (match in RESET_PATTERN.match(method)) {
val putstatic = it[1] as FieldInsnNode val putstatic = match[1] as FieldInsnNode
if (counters.contains(MemberRef(putstatic))) { if (counters.contains(MemberRef(putstatic))) {
it.forEach(method.instructions::remove) match.forEach(method.instructions::remove)
} }
} }
INCREMENT_PATTERN.match(method).forEach { for (match in INCREMENT_PATTERN.match(method)) {
val getstatic = MemberRef(it[0] as FieldInsnNode) val getstatic = MemberRef(match[0] as FieldInsnNode)
val putstatic = MemberRef(it[3] as FieldInsnNode) val putstatic = MemberRef(match[3] as FieldInsnNode)
if (getstatic == putstatic && counters.contains(putstatic)) { if (getstatic == putstatic && counters.contains(putstatic)) {
it.forEach(method.instructions::remove) match.forEach(method.instructions::remove)
} }
} }

@ -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 var matchIndex = 0
val load = match[matchIndex++] as VarInsnNode val load = match[matchIndex++] as VarInsnNode
if (stores[load.`var`]) { if (stores[load.`var`]) {
return@forEach continue
} }
var callerSlots = Type.getArgumentsAndReturnSizes(method.desc) shr 2 var callerSlots = Type.getArgumentsAndReturnSizes(method.desc) shr 2
@ -182,7 +182,7 @@ class DummyArgTransformer : Transformer() {
callerSlots++ callerSlots++
} }
if (load.`var` >= callerSlots) { if (load.`var` >= callerSlots) {
return@forEach continue
} }
val conditionValue: Int? 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( conditionalCalls.put(
parentMethod, parentMethod,
ConditionalCall(load.`var`, conditionOpcode, conditionValue, callee, constArgs.asList()) ConditionalCall(load.`var`, conditionOpcode, conditionValue, callee, constArgs.asList())

@ -22,7 +22,7 @@ class ExceptionTracingTransformer : Transformer() {
clazz: ClassNode, clazz: ClassNode,
method: MethodNode method: MethodNode
): Boolean { ): Boolean {
CATCH_MATCHER.match(method).forEach { match -> for (match in CATCH_MATCHER.match(method)) {
val foundTryCatch = method.tryCatchBlocks.removeIf { tryCatch -> val foundTryCatch = method.tryCatchBlocks.removeIf { tryCatch ->
tryCatch.type == "java/lang/RuntimeException" && tryCatch.handler.nextReal === match[0] tryCatch.type == "java/lang/RuntimeException" && tryCatch.handler.nextReal === match[0]
} }

@ -39,7 +39,7 @@ class OpaquePredicateTransformer : Transformer() {
} }
private fun findFlowObstructors(library: Library, method: MethodNode) { 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 // add flow obstructor to set
val putstatic = match.last() as FieldInsnNode val putstatic = match.last() as FieldInsnNode
flowObstructors.add(MemberRef(putstatic)) flowObstructors.add(MemberRef(putstatic))
@ -81,23 +81,23 @@ class OpaquePredicateTransformer : Transformer() {
override fun transformCode(classPath: ClassPath, library: Library, clazz: ClassNode, method: MethodNode): Boolean { override fun transformCode(classPath: ClassPath, library: Library, clazz: ClassNode, method: MethodNode): Boolean {
// find and fix opaque predicates // find and fix opaque predicates
OPAQUE_PREDICATE_MATCHER.match(method).filter { isOpaquePredicate(method, it) }.forEach { for (match in OPAQUE_PREDICATE_MATCHER.match(method).filter { isOpaquePredicate(method, it) }) {
val branch = it[1] as JumpInsnNode val branch = match[1] as JumpInsnNode
if (branch.opcode == Opcodes.IFEQ) { if (branch.opcode == Opcodes.IFEQ) {
// branch is always taken // branch is always taken
method.instructions.remove(it[0]) method.instructions.remove(match[0])
branch.opcode = Opcodes.GOTO branch.opcode = Opcodes.GOTO
} else { // IFNE } else { // IFNE
// branch is never taken // branch is never taken
it.forEach(method.instructions::remove) match.forEach(method.instructions::remove)
} }
opaquePredicates++ opaquePredicates++
} }
// remove redundant stores // remove redundant stores
STORE_MATCHER.match(method).filter(this::isRedundantStore).forEach { for (match in STORE_MATCHER.match(method).filter(this::isRedundantStore)) {
it.forEach(method.instructions::remove) match.forEach(method.instructions::remove)
stores++ stores++
} }

Loading…
Cancel
Save