From e25918a8a07e655d33551b3f32a1a954ac036515 Mon Sep 17 00:00:00 2001 From: Graham Date: Sun, 3 May 2020 11:40:33 +0100 Subject: [PATCH] Add NameMapProvider It returns a single NameMap combining all the maps in share/deob-map. This will ultimately be fed into the RemapTransformer/TypedRemapper in the deobfuscator. Signed-off-by: Graham --- .../openrs2/deob/map/DeobfuscatorMapModule.kt | 5 +++ .../main/java/dev/openrs2/deob/map/NameMap.kt | 11 ++++++- .../dev/openrs2/deob/map/NameMapProvider.kt | 32 +++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 deob-map/src/main/java/dev/openrs2/deob/map/NameMapProvider.kt diff --git a/deob-map/src/main/java/dev/openrs2/deob/map/DeobfuscatorMapModule.kt b/deob-map/src/main/java/dev/openrs2/deob/map/DeobfuscatorMapModule.kt index 98fb9d8b09..1c69d9c01b 100644 --- a/deob-map/src/main/java/dev/openrs2/deob/map/DeobfuscatorMapModule.kt +++ b/deob-map/src/main/java/dev/openrs2/deob/map/DeobfuscatorMapModule.kt @@ -2,6 +2,7 @@ package dev.openrs2.deob.map import com.fasterxml.jackson.databind.Module import com.google.inject.AbstractModule +import com.google.inject.Scopes import com.google.inject.multibindings.Multibinder import dev.openrs2.yaml.YamlModule @@ -11,5 +12,9 @@ object DeobfuscatorMapModule : AbstractModule() { Multibinder.newSetBinder(binder(), Module::class.java) .addBinding().to(DeobfuscatorMapJacksonModule::class.java) + + bind(NameMap::class.java) + .toProvider(NameMapProvider::class.java) + .`in`(Scopes.SINGLETON) } } diff --git a/deob-map/src/main/java/dev/openrs2/deob/map/NameMap.kt b/deob-map/src/main/java/dev/openrs2/deob/map/NameMap.kt index 815942ad5a..cc6ed6a00b 100644 --- a/deob-map/src/main/java/dev/openrs2/deob/map/NameMap.kt +++ b/deob-map/src/main/java/dev/openrs2/deob/map/NameMap.kt @@ -2,9 +2,18 @@ package dev.openrs2.deob.map import dev.openrs2.asm.MemberRef import java.util.SortedMap +import java.util.TreeMap data class NameMap( val classes: SortedMap, val fields: SortedMap, val methods: SortedMap -) +) { + constructor() : this(TreeMap(), TreeMap(), TreeMap()) + + fun add(other: NameMap) { + classes.putAll(other.classes) + fields.putAll(other.fields) + methods.putAll(other.methods) + } +} diff --git a/deob-map/src/main/java/dev/openrs2/deob/map/NameMapProvider.kt b/deob-map/src/main/java/dev/openrs2/deob/map/NameMapProvider.kt new file mode 100644 index 0000000000..0933f28773 --- /dev/null +++ b/deob-map/src/main/java/dev/openrs2/deob/map/NameMapProvider.kt @@ -0,0 +1,32 @@ +package dev.openrs2.deob.map + +import com.fasterxml.jackson.databind.ObjectMapper +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.Paths +import javax.inject.Inject +import javax.inject.Provider + +class NameMapProvider @Inject constructor(private val mapper: ObjectMapper) : Provider { + override fun get(): NameMap { + val combinedMap = NameMap() + + for (file in Files.list(PATH).filter(::isYamlFile)) { + val map = Files.newBufferedReader(file).use { reader -> + mapper.readValue(reader, NameMap::class.java) + } + combinedMap.add(map) + } + + return combinedMap + } + + private fun isYamlFile(path: Path): Boolean { + return Files.isRegularFile(path) && path.fileName.toString().endsWith(YAML_SUFFIX) + } + + companion object { + private val PATH = Paths.get("share/deob-map") + private const val YAML_SUFFIX = ".yaml" + } +}