Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement DeclareFieldsTransformer #25

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ fun clinit(block: (MethodBuilder.() -> Unit)? = null): MethodNode = method(
null,
).apply { if (block != null) modify(block) }

@NodeDSL
fun init(block: (MethodBuilder.() -> Unit)? = null): MethodNode = method(
0,
"<init>",
"()V", null,
null,
).apply { if (block != null) modify(block) }

@NodeDSL
fun method(
access: Modifiers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,10 @@ fun InsnListBuilder.SIPUSH(value: Int) {
}

/**
* Push a constant number to the stack
* -> I/J/F/D
* Push any constant object to the stack
*/
@BuilderDSL
fun InsnListBuilder.LDC(value: Number) = +LdcInsnNode(value)

/**
* Push a constant string to the stack
* -> Ljava/lang/String;
*/
@BuilderDSL
fun InsnListBuilder.LDC(string: String) = +LdcInsnNode(string)

/**
* Push constant class to the stack
* -> Ljava/lang/Class;
*/
@BuilderDSL
fun InsnListBuilder.LDC(type: Type) = +LdcInsnNode(type)
fun InsnListBuilder.LDC(value: Any) = +LdcInsnNode(value)

@BuilderDSL
fun InsnListBuilder.LDC_TYPE(typeDesc: String, isArray: Boolean = false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ object Transformers : MutableList<Transformer> by mutableListOf(
ClonedClassTransformer order 100,
TrashClassTransformer order 101,
HWIDAuthenticatorTransformer order 102,
DeclareFieldsTransformer order 103,
ReflectionSupportTransformer order 199,
//ControlflowTransformer order 200,
StringEncryptTransformer order 300,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package net.spartanb312.grunt.process.transformers.misc

import net.spartanb312.genesis.kotlin.clinit
import net.spartanb312.genesis.kotlin.extensions.INT
import net.spartanb312.genesis.kotlin.extensions.LONG
import net.spartanb312.genesis.kotlin.extensions.insn.BIPUSH
import net.spartanb312.genesis.kotlin.extensions.insn.LDC
import net.spartanb312.genesis.kotlin.extensions.insn.PUTFIELD
import net.spartanb312.genesis.kotlin.extensions.insn.PUTSTATIC
import net.spartanb312.genesis.kotlin.extensions.insn.SIPUSH
import net.spartanb312.genesis.kotlin.init
import net.spartanb312.genesis.kotlin.instructions
import net.spartanb312.grunt.config.setting
import net.spartanb312.grunt.process.Transformer
import net.spartanb312.grunt.process.resource.ResourceCache
import net.spartanb312.grunt.utils.count
import net.spartanb312.grunt.utils.extensions.isAnnotation
import net.spartanb312.grunt.utils.logging.Logger
import net.spartanb312.grunt.utils.notInList
import org.objectweb.asm.Opcodes.RETURN
import org.objectweb.asm.tree.ClassNode
import org.objectweb.asm.tree.FieldNode
import org.objectweb.asm.tree.InsnList
import org.objectweb.asm.tree.InsnNode
import java.lang.reflect.Modifier

/**
* Fields will be — instead of the field having a constant value —
* initialized in the '<clinit>' or '<init>' of the respective class.
* This allows other transformers to transform these final fields.
*
* @author jonesdevelopment
*/
object DeclareFieldsTransformer : Transformer("HideDeclaredFields", Category.Miscellaneous) {

private val exclusion by setting("Exclusion", listOf())

override fun ResourceCache.transform() {
Logger.info(" - Hiding declared fields...")
val count = count {
nonExcluded.asSequence()
.filter { !it.isAnnotation && it.name.notInList(exclusion) }
.forEach { classNode ->
var clinit = classNode.methods.firstOrNull { it.name.equals("<clinit>") }
var init = classNode.methods.firstOrNull { it.name.equals("<init>") }
for (field in classNode.fields) {
if (field.value != null) {
if (Modifier.isStatic(field.access)) {
if (clinit == null) {
clinit = clinit()
clinit.instructions.add(InsnNode(RETURN))
classNode.methods.add(clinit)
}
clinit.instructions.insert(box(classNode, field))
} else {
if (init == null) {
init = init()
init.instructions.add(InsnNode(RETURN))
classNode.methods.add(init)
}
init.instructions.insert(box(classNode, field))
}
field.value = null
add()
}
}
}
}.get()
Logger.info(" Hid $count declared fields")
}

fun box(owner: ClassNode, field: FieldNode): InsnList {
return instructions {
when (field.desc) {
"I" -> INT(field.value as Int)
"J" -> LONG(field.value as Long)
"B" -> BIPUSH(field.value as Int)
"S" -> SIPUSH(field.value as Int)
else -> LDC(field.value)
}
if (Modifier.isStatic(field.access)) {
PUTSTATIC(owner.name, field.name, field.desc)
} else {
PUTFIELD(owner.name, field.name, field.desc)
}
}
}
}
4 changes: 4 additions & 0 deletions sample/alien.json
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@
"Exceptions": true,
"Exclusion": []
},
"HideDeclaredFields": {
"Enabled": true,
"Exclusion": []
},
"Crasher": {
"Enabled": false,
"Random": false,
Expand Down