From ef5db18ea136dbebb0145c064ff7f00f2aa1be60 Mon Sep 17 00:00:00 2001 From: Graham Date: Thu, 30 Apr 2020 22:08:32 +0100 Subject: [PATCH] Replace runescape.com with domain from the config file Signed-off-by: Graham --- .../java/dev/openrs2/bundler/BundlerModule.kt | 2 + .../bundler/transform/DomainTransformer.kt | 51 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 bundler/src/main/java/dev/openrs2/bundler/transform/DomainTransformer.kt diff --git a/bundler/src/main/java/dev/openrs2/bundler/BundlerModule.kt b/bundler/src/main/java/dev/openrs2/bundler/BundlerModule.kt index 0bcfbc9b5c..8084333619 100644 --- a/bundler/src/main/java/dev/openrs2/bundler/BundlerModule.kt +++ b/bundler/src/main/java/dev/openrs2/bundler/BundlerModule.kt @@ -5,6 +5,7 @@ import com.google.inject.multibindings.Multibinder import dev.openrs2.asm.transform.Transformer import dev.openrs2.bundler.transform.BufferSizeTransformer import dev.openrs2.bundler.transform.CachePathTransformer +import dev.openrs2.bundler.transform.DomainTransformer import dev.openrs2.bundler.transform.HostCheckTransformer import dev.openrs2.bundler.transform.LoadLibraryTransformer import dev.openrs2.bundler.transform.MacResizeTransformer @@ -24,6 +25,7 @@ object BundlerModule : AbstractModule() { binder.addBinding().to(BufferSizeTransformer::class.java) binder.addBinding().to(CachePathTransformer::class.java) binder.addBinding().to(HostCheckTransformer::class.java) + binder.addBinding().to(DomainTransformer::class.java) binder.addBinding().to(LoadLibraryTransformer::class.java) binder.addBinding().to(MacResizeTransformer::class.java) binder.addBinding().to(PlatformDetectionTransformer::class.java) diff --git a/bundler/src/main/java/dev/openrs2/bundler/transform/DomainTransformer.kt b/bundler/src/main/java/dev/openrs2/bundler/transform/DomainTransformer.kt new file mode 100644 index 0000000000..a52a1728c2 --- /dev/null +++ b/bundler/src/main/java/dev/openrs2/bundler/transform/DomainTransformer.kt @@ -0,0 +1,51 @@ +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 DomainTransformer @Inject constructor( + private val config: Config +) : Transformer() { + private var domains = 0 + + override fun preTransform(classPath: ClassPath) { + domains = 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.com", config.domain) + if (insn.cst != cst) { + domains++ + } + } + + return false + } + + override fun postTransform(classPath: ClassPath) { + logger.info { "Replaced $domains domains" } + } + + companion object { + private val logger = InlineLogger() + } +}