From 5aa92fd6001ee6c281c9cf56bf562cce96c445e3 Mon Sep 17 00:00:00 2001 From: Graham Date: Sat, 19 Sep 2020 22:13:55 +0100 Subject: [PATCH] Add JSON module Signed-off-by: Graham --- asm/build.gradle.kts | 2 +- .../main/java/dev/openrs2/asm/AsmModule.kt | 3 --- .../java/dev/openrs2/conf/ConfigProvider.kt | 3 ++- .../deob/processor/NameMapProcessor.kt | 4 ++- deob-util/build.gradle.kts | 1 + .../openrs2/deob/util/map/NameMapProvider.kt | 3 ++- deob/build.gradle.kts | 1 + .../java/dev/openrs2/deob/ProfileProvider.kt | 3 ++- json/build.gradle.kts | 27 +++++++++++++++++++ json/src/main/java/dev/openrs2/json/Json.kt | 8 ++++++ .../main/java/dev/openrs2/json/JsonModule.kt | 18 +++++++++++++ .../dev/openrs2/json/ObjectMapperProvider.kt | 19 +++++++++++++ settings.gradle.kts | 1 + yaml/src/main/java/dev/openrs2/yaml/Yaml.kt | 8 ++++++ .../main/java/dev/openrs2/yaml/YamlModule.kt | 1 + 15 files changed, 94 insertions(+), 8 deletions(-) create mode 100644 json/build.gradle.kts create mode 100644 json/src/main/java/dev/openrs2/json/Json.kt create mode 100644 json/src/main/java/dev/openrs2/json/JsonModule.kt create mode 100644 json/src/main/java/dev/openrs2/json/ObjectMapperProvider.kt create mode 100644 yaml/src/main/java/dev/openrs2/yaml/Yaml.kt diff --git a/asm/build.gradle.kts b/asm/build.gradle.kts index 32e6c060..3c78001b 100644 --- a/asm/build.gradle.kts +++ b/asm/build.gradle.kts @@ -5,7 +5,7 @@ plugins { dependencies { api(project(":util")) - api(project(":yaml")) + api("com.fasterxml.jackson.core:jackson-databind:${Versions.jackson}") api("com.google.inject:guice:${Versions.guice}") api("org.ow2.asm:asm:${Versions.asm}") api("org.ow2.asm:asm-commons:${Versions.asm}") diff --git a/asm/src/main/java/dev/openrs2/asm/AsmModule.kt b/asm/src/main/java/dev/openrs2/asm/AsmModule.kt index dc16b3a8..41704f38 100644 --- a/asm/src/main/java/dev/openrs2/asm/AsmModule.kt +++ b/asm/src/main/java/dev/openrs2/asm/AsmModule.kt @@ -3,12 +3,9 @@ package dev.openrs2.asm import com.fasterxml.jackson.databind.Module import com.google.inject.AbstractModule import com.google.inject.multibindings.Multibinder -import dev.openrs2.yaml.YamlModule public object AsmModule : AbstractModule() { override fun configure() { - install(YamlModule) - Multibinder.newSetBinder(binder(), Module::class.java) .addBinding().to(AsmJacksonModule::class.java) } diff --git a/conf/src/main/java/dev/openrs2/conf/ConfigProvider.kt b/conf/src/main/java/dev/openrs2/conf/ConfigProvider.kt index d248ea1b..13cc6e9b 100644 --- a/conf/src/main/java/dev/openrs2/conf/ConfigProvider.kt +++ b/conf/src/main/java/dev/openrs2/conf/ConfigProvider.kt @@ -1,12 +1,13 @@ package dev.openrs2.conf import com.fasterxml.jackson.databind.ObjectMapper +import dev.openrs2.yaml.Yaml import java.nio.file.Files import java.nio.file.Paths import javax.inject.Inject import javax.inject.Provider -public class ConfigProvider @Inject constructor(private val mapper: ObjectMapper) : Provider { +public class ConfigProvider @Inject constructor(@Yaml private val mapper: ObjectMapper) : Provider { override fun get(): Config { if (Files.notExists(CONFIG_PATH)) { Files.copy(EXAMPLE_CONFIG_PATH, CONFIG_PATH) diff --git a/deob-processor/src/main/java/dev/openrs2/deob/processor/NameMapProcessor.kt b/deob-processor/src/main/java/dev/openrs2/deob/processor/NameMapProcessor.kt index 64fddbcf..a2dd6018 100644 --- a/deob-processor/src/main/java/dev/openrs2/deob/processor/NameMapProcessor.kt +++ b/deob-processor/src/main/java/dev/openrs2/deob/processor/NameMapProcessor.kt @@ -2,6 +2,7 @@ package dev.openrs2.deob.processor import com.fasterxml.jackson.databind.ObjectMapper import com.google.inject.Guice +import com.google.inject.Key import com.sun.source.util.Trees import dev.openrs2.asm.MemberRef import dev.openrs2.asm.toInternalClassName @@ -12,6 +13,7 @@ import dev.openrs2.deob.util.map.Field import dev.openrs2.deob.util.map.Method import dev.openrs2.deob.util.map.NameMap import dev.openrs2.util.io.useAtomicBufferedWriter +import dev.openrs2.yaml.Yaml import java.nio.file.Files import java.nio.file.Path import java.nio.file.Paths @@ -45,7 +47,7 @@ public class NameMapProcessor : AbstractProcessor() { init { val injector = Guice.createInjector(DeobfuscatorProcessorModule) - mapper = injector.getInstance(ObjectMapper::class.java) + mapper = injector.getInstance(Key.get(ObjectMapper::class.java, Yaml::class.java)) } override fun init(env: ProcessingEnvironment) { diff --git a/deob-util/build.gradle.kts b/deob-util/build.gradle.kts index 759f82a9..49c1e275 100644 --- a/deob-util/build.gradle.kts +++ b/deob-util/build.gradle.kts @@ -5,6 +5,7 @@ plugins { dependencies { api(project(":asm")) + api(project(":yaml")) } publishing { diff --git a/deob-util/src/main/java/dev/openrs2/deob/util/map/NameMapProvider.kt b/deob-util/src/main/java/dev/openrs2/deob/util/map/NameMapProvider.kt index c1937049..c0f992d4 100644 --- a/deob-util/src/main/java/dev/openrs2/deob/util/map/NameMapProvider.kt +++ b/deob-util/src/main/java/dev/openrs2/deob/util/map/NameMapProvider.kt @@ -1,13 +1,14 @@ package dev.openrs2.deob.util.map import com.fasterxml.jackson.databind.ObjectMapper +import dev.openrs2.yaml.Yaml import java.nio.file.Files import java.nio.file.Path import java.nio.file.Paths import javax.inject.Inject import javax.inject.Provider -public class NameMapProvider @Inject constructor(private val mapper: ObjectMapper) : Provider { +public class NameMapProvider @Inject constructor(@Yaml private val mapper: ObjectMapper) : Provider { override fun get(): NameMap { val combinedMap = NameMap() diff --git a/deob/build.gradle.kts b/deob/build.gradle.kts index b8aa2e28..006e5f10 100644 --- a/deob/build.gradle.kts +++ b/deob/build.gradle.kts @@ -14,6 +14,7 @@ dependencies { implementation(project(":bundler")) implementation(project(":deob-annotations")) implementation(project(":deob-util")) + implementation(project(":yaml")) implementation("com.google.guava:guava:${Versions.guava}") implementation("it.unimi.dsi:fastutil:${Versions.fastutil}") implementation("org.jgrapht:jgrapht-core:${Versions.jgrapht}") diff --git a/deob/src/main/java/dev/openrs2/deob/ProfileProvider.kt b/deob/src/main/java/dev/openrs2/deob/ProfileProvider.kt index 4d0dd3c3..9f6efeeb 100644 --- a/deob/src/main/java/dev/openrs2/deob/ProfileProvider.kt +++ b/deob/src/main/java/dev/openrs2/deob/ProfileProvider.kt @@ -1,12 +1,13 @@ package dev.openrs2.deob import com.fasterxml.jackson.databind.ObjectMapper +import dev.openrs2.yaml.Yaml import java.nio.file.Files import java.nio.file.Paths import javax.inject.Inject import javax.inject.Provider -public class ProfileProvider @Inject constructor(private val mapper: ObjectMapper) : Provider { +public class ProfileProvider @Inject constructor(@Yaml private val mapper: ObjectMapper) : Provider { override fun get(): Profile { return Files.newBufferedReader(PATH).use { reader -> mapper.readValue(reader, Profile::class.java) diff --git a/json/build.gradle.kts b/json/build.gradle.kts new file mode 100644 index 00000000..647241ad --- /dev/null +++ b/json/build.gradle.kts @@ -0,0 +1,27 @@ +plugins { + `maven-publish` + kotlin("jvm") +} + +dependencies { + api("com.fasterxml.jackson.core:jackson-databind:${Versions.jackson}") + api("com.google.inject:guice:${Versions.guice}") + + implementation("com.fasterxml.jackson.module:jackson-module-kotlin:${Versions.jackson}") +} + +publishing { + publications.create("maven") { + from(components["java"]) + + pom { + packaging = "jar" + name.set("OpenRS2 JSON") + description.set( + """ + Guava module for creating a JSON ObjectMapper. + """.trimIndent() + ) + } + } +} diff --git a/json/src/main/java/dev/openrs2/json/Json.kt b/json/src/main/java/dev/openrs2/json/Json.kt new file mode 100644 index 00000000..4626c544 --- /dev/null +++ b/json/src/main/java/dev/openrs2/json/Json.kt @@ -0,0 +1,8 @@ +package dev.openrs2.json + +import javax.inject.Qualifier + +@Qualifier +@Retention(AnnotationRetention.RUNTIME) +@Target(AnnotationTarget.FIELD, AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.FUNCTION) +public annotation class Json diff --git a/json/src/main/java/dev/openrs2/json/JsonModule.kt b/json/src/main/java/dev/openrs2/json/JsonModule.kt new file mode 100644 index 00000000..5ee0f708 --- /dev/null +++ b/json/src/main/java/dev/openrs2/json/JsonModule.kt @@ -0,0 +1,18 @@ +package dev.openrs2.json + +import com.fasterxml.jackson.databind.Module +import com.fasterxml.jackson.databind.ObjectMapper +import com.google.inject.AbstractModule +import com.google.inject.Scopes +import com.google.inject.multibindings.Multibinder + +public object JsonModule : AbstractModule() { + override fun configure() { + Multibinder.newSetBinder(binder(), Module::class.java) + + bind(ObjectMapper::class.java) + .annotatedWith(Json::class.java) + .toProvider(ObjectMapperProvider::class.java) + .`in`(Scopes.SINGLETON) + } +} diff --git a/json/src/main/java/dev/openrs2/json/ObjectMapperProvider.kt b/json/src/main/java/dev/openrs2/json/ObjectMapperProvider.kt new file mode 100644 index 00000000..f58a0e1e --- /dev/null +++ b/json/src/main/java/dev/openrs2/json/ObjectMapperProvider.kt @@ -0,0 +1,19 @@ +package dev.openrs2.json + +import com.fasterxml.jackson.databind.Module +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.databind.PropertyNamingStrategy +import com.fasterxml.jackson.module.kotlin.registerKotlinModule +import javax.inject.Inject +import javax.inject.Provider + +public class ObjectMapperProvider @Inject constructor( + private val modules: Set<@JvmSuppressWildcards Module> +) : Provider { + override fun get(): ObjectMapper { + return ObjectMapper() + .registerKotlinModule() + .registerModules(modules) + .setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE) + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts index fce18f47..802100e4 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -19,6 +19,7 @@ include( "deob-processor", "deob-util", "game", + "json", "nonfree", "nonfree:client", "nonfree:gl", diff --git a/yaml/src/main/java/dev/openrs2/yaml/Yaml.kt b/yaml/src/main/java/dev/openrs2/yaml/Yaml.kt new file mode 100644 index 00000000..844cee48 --- /dev/null +++ b/yaml/src/main/java/dev/openrs2/yaml/Yaml.kt @@ -0,0 +1,8 @@ +package dev.openrs2.yaml + +import javax.inject.Qualifier + +@Qualifier +@Retention(AnnotationRetention.RUNTIME) +@Target(AnnotationTarget.FIELD, AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.FUNCTION) +public annotation class Yaml diff --git a/yaml/src/main/java/dev/openrs2/yaml/YamlModule.kt b/yaml/src/main/java/dev/openrs2/yaml/YamlModule.kt index 956ae73a..36a62fb9 100644 --- a/yaml/src/main/java/dev/openrs2/yaml/YamlModule.kt +++ b/yaml/src/main/java/dev/openrs2/yaml/YamlModule.kt @@ -11,6 +11,7 @@ public object YamlModule : AbstractModule() { Multibinder.newSetBinder(binder(), Module::class.java) bind(ObjectMapper::class.java) + .annotatedWith(Yaml::class.java) .toProvider(ObjectMapperProvider::class.java) .`in`(Scopes.SINGLETON) }