From c488a155c4f753b3c8654033c9465019428fdc8b Mon Sep 17 00:00:00 2001 From: Graham Date: Thu, 30 Apr 2020 22:09:11 +0100 Subject: [PATCH] Replace operator and game names with values from the config file Signed-off-by: Graham --- .../java/dev/openrs2/bundler/BundlerModule.kt | 2 + .../bundler/transform/NameTransformer.kt | 52 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 bundler/src/main/java/dev/openrs2/bundler/transform/NameTransformer.kt diff --git a/bundler/src/main/java/dev/openrs2/bundler/BundlerModule.kt b/bundler/src/main/java/dev/openrs2/bundler/BundlerModule.kt index 80843336..c5e7704d 100644 --- a/bundler/src/main/java/dev/openrs2/bundler/BundlerModule.kt +++ b/bundler/src/main/java/dev/openrs2/bundler/BundlerModule.kt @@ -9,6 +9,7 @@ import dev.openrs2.bundler.transform.DomainTransformer import dev.openrs2.bundler.transform.HostCheckTransformer import dev.openrs2.bundler.transform.LoadLibraryTransformer import dev.openrs2.bundler.transform.MacResizeTransformer +import dev.openrs2.bundler.transform.NameTransformer import dev.openrs2.bundler.transform.PlatformDetectionTransformer import dev.openrs2.bundler.transform.PublicKeyTransformer import dev.openrs2.bundler.transform.RightClickTransformer @@ -28,6 +29,7 @@ object BundlerModule : AbstractModule() { binder.addBinding().to(DomainTransformer::class.java) binder.addBinding().to(LoadLibraryTransformer::class.java) binder.addBinding().to(MacResizeTransformer::class.java) + binder.addBinding().to(NameTransformer::class.java) binder.addBinding().to(PlatformDetectionTransformer::class.java) binder.addBinding().to(PublicKeyTransformer::class.java) binder.addBinding().to(RightClickTransformer::class.java) diff --git a/bundler/src/main/java/dev/openrs2/bundler/transform/NameTransformer.kt b/bundler/src/main/java/dev/openrs2/bundler/transform/NameTransformer.kt new file mode 100644 index 00000000..fb173082 --- /dev/null +++ b/bundler/src/main/java/dev/openrs2/bundler/transform/NameTransformer.kt @@ -0,0 +1,52 @@ +package dev.openrs2.bundler.transform + +import com.github.michaelbull.logging.InlineLogger +import dev.openrs2.asm.classpath.ClassPath +import dev.openrs2.asm.classpath.Library +import dev.openrs2.asm.transform.Transformer +import dev.openrs2.conf.Config +import org.objectweb.asm.tree.ClassNode +import org.objectweb.asm.tree.LdcInsnNode +import org.objectweb.asm.tree.MethodNode +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +class NameTransformer @Inject constructor( + private val config: Config +) : Transformer() { + private var names = 0 + + override fun preTransform(classPath: ClassPath) { + names = 0 + } + + override fun transformCode(classPath: ClassPath, library: Library, clazz: ClassNode, method: MethodNode): Boolean { + for (insn in method.instructions) { + if (insn !is LdcInsnNode) { + continue + } + + val cst = insn.cst + if (cst !is String) { + continue + } + + insn.cst = cst.replace("RuneScape", config.game) + .replace("Jagex", config.operator) + if (insn.cst != cst) { + names++ + } + } + + return false + } + + override fun postTransform(classPath: ClassPath) { + logger.info { "Replaced $names operator and game names" } + } + + companion object { + private val logger = InlineLogger() + } +}