Skip to content

Commit

Permalink
(multi-os-engine/multi-os-engine#162) Only write modified classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Noisyfox committed Jan 24, 2022
1 parent a647675 commit 91c8e83
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
16 changes: 16 additions & 0 deletions src/main/kotlin/org/moe/tools/classvalidator/ClassModifier.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.moe.tools.classvalidator

import org.objectweb.asm.ClassVisitor

/**
* A [ClassVisitor] that could modify a class
*/
abstract class ClassModifier(api: Int, classVisitor: ClassVisitor?) : ClassVisitor(api, classVisitor) {

var modified: Boolean = false
private set

fun markAsModified() {
modified = true
}
}
18 changes: 13 additions & 5 deletions src/main/kotlin/org/moe/tools/classvalidator/ClassValidator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package org.moe.tools.classvalidator
import org.moe.common.utils.classAndJarInputIterator
import org.moe.tools.classvalidator.natj.AddMissingAnnotations
import org.moe.tools.classvalidator.natj.AddMissingNatJRegister
import org.moe.tools.classvalidator.substrate.CollectReflectionConfig
import org.moe.tools.classvalidator.substrate.ReflectionConfig
import org.objectweb.asm.ClassReader
import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.ClassWriter
Expand All @@ -25,13 +23,23 @@ object ClassValidator {
inputFiles.classAndJarInputIterator { _, inputStream ->
val cr = ClassReader(inputStream)

val chain = mutableListOf<ClassVisitor>()
fun ClassVisitor.chain(nextBuilder: (ClassVisitor) -> ClassVisitor): ClassVisitor {
val next = nextBuilder(this)
chain.add(next)
return next
}

val byteCode = processClass(cr) { next ->
next
.let(::AddMissingAnnotations)
.let(::AddMissingNatJRegister)
.chain(::AddMissingAnnotations)
.chain(::AddMissingNatJRegister)
}

classSaver.save(byteCode)
// Only save modified class
if (chain.any { it is ClassModifier && it.modified }) {
classSaver.save(byteCode)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.moe.tools.classvalidator.natj

import org.moe.tools.classvalidator.ClassModifier
import org.moe.tools.classvalidator.natj.NatJRuntime.toClass
import org.objectweb.asm.AnnotationVisitor
import org.objectweb.asm.ClassVisitor
Expand All @@ -10,7 +11,7 @@ import java.lang.reflect.AnnotatedElement

class AddMissingAnnotations(
next: ClassVisitor?
) : ClassVisitor(Opcodes.ASM5, next) {
) : ClassModifier(Opcodes.ASM5, next) {
private var skip: Boolean = false
private var superName: String? = null
private var interfaces: Array<out String>? = null
Expand Down Expand Up @@ -129,6 +130,7 @@ class AddMissingAnnotations(
}
}
}
markAsModified()
}

private fun convertClassToType(value: Any): Any {
Expand All @@ -148,4 +150,4 @@ class AddMissingAnnotations(
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.moe.tools.classvalidator.natj

import org.moe.tools.classvalidator.ClassModifier
import org.objectweb.asm.AnnotationVisitor
import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.MethodVisitor
Expand All @@ -14,7 +15,7 @@ import java.lang.reflect.Modifier
*/
class AddMissingNatJRegister(
next: ClassVisitor?
) : ClassVisitor(Opcodes.ASM5, next) {
) : ClassModifier(Opcodes.ASM5, next) {

private var skip: Boolean = false
private var visit: Boolean = false
Expand Down Expand Up @@ -90,6 +91,7 @@ class AddMissingNatJRegister(
NatJRuntime.NATJ_OWNER, NatJRuntime.NATJ_REGISTER_NAME,
NatJRuntime.NATJ_REGISTER_DESC, false)
CLI.accept(mv)
markAsModified()
println("Injected NatJ.register() into $name")
}
NATJREG_LEAVE_ALONE -> {
Expand All @@ -108,6 +110,7 @@ class AddMissingNatJRegister(
mv.visitInsn(Opcodes.RETURN)
mv.visitMaxs(-1, -1)
mv.visitEnd()
markAsModified()
println("Injected NatJ.register() into $name")
}

Expand Down

0 comments on commit 91c8e83

Please sign in to comment.