Skip to content

Commit b3354d8

Browse files
committed
Fixed ConstObf & Added Annotations
1 parent 3fbe77a commit b3354d8

File tree

11 files changed

+107
-10
lines changed

11 files changed

+107
-10
lines changed

grunt-main/src/main/kotlin/net/spartanb312/grunt/Grunt.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import net.spartanb312.grunt.config.Configs
44
import net.spartanb312.grunt.gui.GuiFrame
55
import net.spartanb312.grunt.process.Transformers
66
import net.spartanb312.grunt.process.resource.ResourceCache
7+
import net.spartanb312.grunt.process.transformers.PostProcessTransformer
8+
import net.spartanb312.grunt.process.transformers.PostProcessTransformer.finalize
79
import net.spartanb312.grunt.utils.logging.Logger
810
import java.awt.GraphicsEnvironment
911
import kotlin.system.measureTimeMillis
@@ -80,6 +82,7 @@ fun runProcess() {
8082
val obfTime = measureTimeMillis {
8183
Logger.info("Processing...")
8284
Transformers.sortedBy { it.order }.forEach { if (it.enabled) with(it) { transform() } }
85+
with(PostProcessTransformer) { finalize() }
8386
}
8487
Logger.info("Took $obfTime ms to process!")
8588
Logger.info("Dumping to ${Configs.Settings.output}")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package net.spartanb312.grunt.annotation
2+
3+
val DONT_SCRAMBLE = "Lnet/spartanb312/grunt/annotation/DontScramble;"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package net.spartanb312.grunt.annotation
2+
3+
annotation class DontScramble

grunt-main/src/main/kotlin/net/spartanb312/grunt/process/transformers/PostProcessTransformer.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ package net.spartanb312.grunt.process.transformers
33
import com.google.gson.Gson
44
import com.google.gson.JsonArray
55
import com.google.gson.JsonObject
6+
import net.spartanb312.grunt.annotation.DONT_SCRAMBLE
67
import net.spartanb312.grunt.config.setting
78
import net.spartanb312.grunt.process.Transformer
89
import net.spartanb312.grunt.process.resource.ResourceCache
10+
import net.spartanb312.grunt.utils.builder.insnList
911
import net.spartanb312.grunt.utils.dot
12+
import net.spartanb312.grunt.utils.extensions.removeAnnotation
1013
import net.spartanb312.grunt.utils.logging.Logger
1114
import net.spartanb312.grunt.utils.splash
1215
import java.nio.charset.StandardCharsets
@@ -32,6 +35,20 @@ object PostProcessTransformer : Transformer("PostProcess", Category.Miscellaneou
3235
if (fabricMain) processFabricMain()
3336
}
3437

38+
fun ResourceCache.finalize() {
39+
// Remove annotation
40+
val annotationRemoval = arrayOf(DONT_SCRAMBLE)
41+
nonExcluded.forEach { clazz ->
42+
clazz.methods.forEach { method ->
43+
annotationRemoval.forEach { method.removeAnnotation(it) }
44+
}
45+
clazz.fields.forEach { field ->
46+
annotationRemoval.forEach { field.removeAnnotation(it) }
47+
}
48+
annotationRemoval.forEach { clazz.removeAnnotation(it) }
49+
}
50+
}
51+
3552
private fun ResourceCache.processManifest() {
3653
val manifestFile = resources["META-INF/MANIFEST.MF"] ?: return
3754
Logger.info(" Processing MANIFEST.MF...")

grunt-main/src/main/kotlin/net/spartanb312/grunt/process/transformers/encrypt/ConstPoolEncryptTransformer.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package net.spartanb312.grunt.process.transformers.encrypt
22

3+
import net.spartanb312.grunt.annotation.DONT_SCRAMBLE
34
import net.spartanb312.grunt.config.setting
45
import net.spartanb312.grunt.process.Transformer
56
import net.spartanb312.grunt.process.resource.ResourceCache
67
import net.spartanb312.grunt.process.transformers.encrypt.StringEncryptTransformer.createDecryptMethod
78
import net.spartanb312.grunt.process.transformers.encrypt.StringEncryptTransformer.encrypt
89
import net.spartanb312.grunt.process.transformers.redirect.FieldScrambleTransformer
910
import net.spartanb312.grunt.utils.builder.*
11+
import net.spartanb312.grunt.utils.extensions.appendAnnotation
1012
import net.spartanb312.grunt.utils.extensions.isAbstract
1113
import net.spartanb312.grunt.utils.extensions.isNative
1214
import net.spartanb312.grunt.utils.getRandomString
@@ -48,7 +50,7 @@ object ConstPoolEncryptTransformer : Transformer("ConstPollEncrypt", Category.En
4850
"java/lang/Object",
4951
null
5052
)
51-
if (dontScramble) FieldScrambleTransformer.blackList.add(this)
53+
if (dontScramble) appendAnnotation(DONT_SCRAMBLE)
5254
}
5355
] = mutableListOf()
5456
}
@@ -100,7 +102,7 @@ object ConstPoolEncryptTransformer : Transformer("ConstPollEncrypt", Category.En
100102
if (refList.isNotEmpty()) {
101103
addTrashClass(clazz)
102104
val clinit = method(
103-
Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC,
105+
Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC,
104106
"<clinit>",
105107
"()V",
106108
null,

grunt-main/src/main/kotlin/net/spartanb312/grunt/process/transformers/encrypt/NumberEncryptTransformer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ object NumberEncryptTransformer : Transformer("NumberEncrypt", Category.Encrypti
7070
add()
7171
}
7272
} else if (it.opcode in Opcodes.LCONST_0..Opcodes.LCONST_1) {
73-
methodNode.instructions.insertBefore(it, xor(it.opcode - 0x9))
73+
methodNode.instructions.insertBefore(it, xor((it.opcode - 0x9).toLong()))
7474
methodNode.instructions.remove(it)
7575
add()
7676
} else if (it is LdcInsnNode && it.cst is Long) {

grunt-main/src/main/kotlin/net/spartanb312/grunt/process/transformers/redirect/FieldScrambleTransformer.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package net.spartanb312.grunt.process.transformers.redirect
22

3+
import net.spartanb312.grunt.annotation.DONT_SCRAMBLE
34
import net.spartanb312.grunt.config.setting
45
import net.spartanb312.grunt.process.Transformer
56
import net.spartanb312.grunt.process.resource.ResourceCache
67
import net.spartanb312.grunt.process.transformers.misc.NativeCandidateTransformer
78
import net.spartanb312.grunt.utils.*
89
import net.spartanb312.grunt.utils.builder.*
10+
import net.spartanb312.grunt.utils.extensions.hasAnnotation
11+
import net.spartanb312.grunt.utils.extensions.hasAnnotations
912
import net.spartanb312.grunt.utils.extensions.isInitializer
1013
import net.spartanb312.grunt.utils.extensions.isPublic
1114
import net.spartanb312.grunt.utils.logging.Logger
@@ -36,23 +39,20 @@ object FieldScrambleTransformer : Transformer("FieldScramble", Category.Redirect
3639
private val downCalls by setting("NativeDownCalls", true)
3740
private val upCalls by setting("NativeUpCalls", false)
3841

39-
val blackList = mutableListOf<ClassNode>()
40-
4142
override fun ResourceCache.transform() {
4243
Logger.info(" - Redirecting field calls...")
4344
val newClasses = mutableMapOf<ClassNode, ClassNode>() // Owner Companion
4445
var count = 0
4546
repeat(intensity) {
4647
count += process(newClasses)
4748
}
48-
blackList.clear()
4949
Logger.info(" Redirected $count field calls")
5050
}
5151

5252
private fun ResourceCache.process(newClasses: MutableMap<ClassNode, ClassNode>): Int {
5353
val count = count {
5454
nonExcluded.asSequence()
55-
.filter { it.name.notInList(excludedClasses) && !blackList.contains(it) }
55+
.filter { it.name.notInList(excludedClasses) }
5656
.forEach { classNode ->
5757
classNode.methods.toList().asSequence()
5858
.filter { !it.isInitializer }
@@ -63,7 +63,9 @@ object FieldScrambleTransformer : Transformer("FieldScramble", Category.Redirect
6363
val callingField = callingOwner?.fields?.find { field ->
6464
field.name == it.name && field.desc == it.desc
6565
}
66-
if (callingField != null) {
66+
val skipOwner = callingOwner?.hasAnnotation(DONT_SCRAMBLE) == true
67+
val skipField = callingField?.hasAnnotation(DONT_SCRAMBLE) == true
68+
if (callingField != null && !skipField && !skipOwner) {
6769
val shouldOuter =
6870
generateOuterClass && callingOwner.isPublic && callingField.isPublic
6971
val genMethod = when {

grunt-main/src/main/kotlin/net/spartanb312/grunt/process/transformers/redirect/MethodScrambleTransformer.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package net.spartanb312.grunt.process.transformers.redirect
22

3+
import net.spartanb312.grunt.annotation.DONT_SCRAMBLE
34
import net.spartanb312.grunt.config.setting
45
import net.spartanb312.grunt.process.Transformer
56
import net.spartanb312.grunt.process.resource.ResourceCache
67
import net.spartanb312.grunt.process.transformers.misc.NativeCandidateTransformer
78
import net.spartanb312.grunt.utils.*
89
import net.spartanb312.grunt.utils.builder.*
910
import net.spartanb312.grunt.utils.extensions.getCallingMethodNodeAndOwner
11+
import net.spartanb312.grunt.utils.extensions.hasAnnotation
1012
import net.spartanb312.grunt.utils.extensions.isPrivate
1113
import net.spartanb312.grunt.utils.logging.Logger
1214
import org.objectweb.asm.Opcodes
@@ -44,7 +46,9 @@ object MethodScrambleTransformer : Transformer("MethodScramble", Category.Redire
4446
if (pair != null) {
4547
val callingOwner = pair.first
4648
val callingMethod = pair.second
47-
if (nonExcluded.contains(callingOwner)) {
49+
val skipOwner = callingOwner.hasAnnotation(DONT_SCRAMBLE)
50+
val skipMethod = callingMethod.hasAnnotation(DONT_SCRAMBLE)
51+
if (nonExcluded.contains(callingOwner) && !skipOwner && !skipMethod) {
4852
var shouldOuter = generateOuterClass
4953
// Set accesses
5054
if (shouldOuter) {

grunt-main/src/main/kotlin/net/spartanb312/grunt/utils/extensions/Class.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package net.spartanb312.grunt.utils.extensions
44

55
import org.objectweb.asm.Opcodes
6+
import org.objectweb.asm.tree.AnnotationNode
67
import org.objectweb.asm.tree.ClassNode
78
import org.objectweb.asm.tree.MethodNode
89
import java.lang.reflect.Modifier
@@ -51,3 +52,23 @@ val ClassNode.hasAnnotations: Boolean
5152
get() {
5253
return (visibleAnnotations != null && visibleAnnotations.isNotEmpty()) || (invisibleAnnotations != null && invisibleAnnotations.isNotEmpty())
5354
}
55+
56+
fun ClassNode.appendAnnotation(annotation: String): ClassNode {
57+
visitAnnotation(annotation, false)
58+
return this
59+
}
60+
61+
fun ClassNode.removeAnnotation(annotation: String) {
62+
invisibleAnnotations?.toList()?.forEach {
63+
if (it.desc == annotation) invisibleAnnotations.remove(it)
64+
}
65+
visibleAnnotations?.toList()?.forEach {
66+
if (it.desc == annotation) visibleAnnotations.remove(it)
67+
}
68+
}
69+
70+
fun ClassNode.hasAnnotation(desc: String): Boolean = findAnnotation(desc) != null
71+
72+
fun ClassNode.findAnnotation(desc: String): AnnotationNode? {
73+
return visibleAnnotations?.find { it.desc == desc } ?: invisibleAnnotations?.find { it.desc == desc }
74+
}

grunt-main/src/main/kotlin/net/spartanb312/grunt/utils/extensions/Field.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
package net.spartanb312.grunt.utils.extensions
44

55
import org.objectweb.asm.Opcodes
6+
import org.objectweb.asm.tree.AnnotationNode
7+
import org.objectweb.asm.tree.ClassNode
68
import org.objectweb.asm.tree.FieldNode
79
import java.lang.reflect.Modifier
810

@@ -40,3 +42,23 @@ inline val FieldNode.hasAnnotations: Boolean
4042
return visibleAnnotations != null && visibleAnnotations.isNotEmpty()
4143
|| invisibleAnnotations != null && invisibleAnnotations.isNotEmpty()
4244
}
45+
46+
fun FieldNode.appendAnnotation(annotation: String): FieldNode {
47+
visitAnnotation(annotation, false)
48+
return this
49+
}
50+
51+
fun FieldNode.removeAnnotation(annotation: String) {
52+
invisibleAnnotations?.toList()?.forEach {
53+
if (it.desc == annotation) invisibleAnnotations.remove(it)
54+
}
55+
visibleAnnotations?.toList()?.forEach {
56+
if (it.desc == annotation) visibleAnnotations.remove(it)
57+
}
58+
}
59+
60+
fun FieldNode.hasAnnotation(desc: String): Boolean = findAnnotation(desc) != null
61+
62+
fun FieldNode.findAnnotation(desc: String): AnnotationNode? {
63+
return visibleAnnotations?.find { it.desc == desc } ?: invisibleAnnotations?.find { it.desc == desc }
64+
}

0 commit comments

Comments
 (0)