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) {
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) {

@ -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

@ -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)

@ -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++
}
}

@ -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()

@ -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++
}
}

@ -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 {

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

@ -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)
}
}

@ -58,8 +58,8 @@ object SignedClassUtils {
private fun findSignedClasses(method: MethodNode): Set<String> {
val classes = mutableSetOf<String>()
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)

@ -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

@ -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)
}
}

@ -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())

@ -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]
}

@ -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++
}

Loading…
Cancel
Save