From 22009a53f195acbd165b647c35390a4f050c4fb0 Mon Sep 17 00:00:00 2001 From: Username404 Date: Thu, 20 May 2021 13:22:37 +0200 Subject: [PATCH] Use gson instead of kotlinx-serialization --- build.gradle.kts | 14 ++------------ .../main/kotlin/fr/username404/snowygui/Snowy.kt | 3 +++ .../username404/snowygui/config/Configuration.kt | 9 +++------ .../fr/username404/snowygui/gui/feature/Macro.kt | 7 +++---- forge/build.gradle.kts | 3 +-- .../fr/username404/snowygui/forge/ForgeInit.kt | 2 +- 6 files changed, 13 insertions(+), 25 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index a5ae084..675c314 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -11,7 +11,6 @@ buildscript { plugins { kotlin("jvm") version "1.5.0" - kotlin("plugin.serialization") version "1.5.0" id("com.github.johnrengelman.shadow") version "6.1.0" apply false id("architectury-plugin") version "[3.0.100, 3.3[" id("dev.architectury.loom") version "0.7.2.110" apply false @@ -72,7 +71,7 @@ subprojects { exclude("**/*.kotlin_metadata") exclude("**/*.kotlin_builtins") exclude("META-INF/maven/**/*") - archiveClassifier.set("shadow") + archiveClassifier.set("shadow-${this@subprojects.name}") } val shadowJar = getByName("shadowJar") as ShadowJar val shrinkJar = register("shrinkJar", proguard.gradle.ProGuardTask::class) { group = this@subprojects.group as String @@ -94,12 +93,7 @@ subprojects { adaptclassstrings() "$group.**".also { dontnote(it); dontwarn(it) } - // kotlinx-serialization related configuration: - dontnote("kotlinx.serialization.AnnotationsKt") - keepclasseswithmembers("class kotlinx.serialization.json.** { kotlinx.serialization.KSerializer serializer(...); }") - keep(mapOf("includedescriptorclasses" to true), "class $group.snowygui.**$\$serializer { * ; }") - keepclassmembers("class $group.snowygui.** { *** Companion; }") - keepclasseswithmembers("class $group.snowygui.** { kotlinx.serialization.KSerializer serializer(...); }") + dontwarn("kotlinx.serialization.**") val homeDir = System.getProperty("java.home") as String doFirst { @@ -151,17 +145,13 @@ allprojects { **/ apply(plugin = "java") apply(plugin = "org.jetbrains.kotlin.jvm") - apply(plugin = "org.jetbrains.kotlin.plugin.serialization") apply(plugin = "architectury-plugin") java { sourceCompatibility = JavaVersion.VERSION_11 } - val serializationVer: String by rootProject dependencies { implementation(kotlin("stdlib-jdk8", kotlinVer)) implementation(kotlin("reflect", kotlinVer)) - implementation("$kotlinX:kotlinx-serialization-core:$serializationVer") - implementation("$kotlinX:kotlinx-serialization-json:$serializationVer") annotationProcessor("com.github.bsideup.jabel:jabel-javac-plugin:0.3.0") { listOf("byte-buddy", "byte-buddy-agent").forEach { exclude(module = it) diff --git a/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt b/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt index 479c3e1..c504471 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt @@ -1,5 +1,7 @@ package fr.username404.snowygui +import com.google.gson.Gson +import com.google.gson.GsonBuilder import fr.username404.snowygui.EventSnowy.Companion.useKey import fr.username404.snowygui.config.Configuration.obtained import fr.username404.snowygui.misc.AddKeyMaps @@ -10,6 +12,7 @@ import org.apache.logging.log4j.Logger abstract class Snowy { private val displayInitMessage: Boolean by obtained companion object { + val Gson: Gson = GsonBuilder().excludeFieldsWithoutExposeAnnotation().create() @JvmStatic protected val FeaturePackage: String = "fr.username404.snowygui.gui.feature" lateinit var annotatedButtons: Set> diff --git a/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt b/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt index 6247c51..dd24fd8 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt @@ -13,9 +13,6 @@ import io.github.config4k.extract import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking -import kotlinx.serialization.decodeFromString -import kotlinx.serialization.encodeToString -import kotlinx.serialization.json.Json import net.minecraft.client.Minecraft import java.io.File import java.nio.charset.Charset @@ -78,8 +75,8 @@ object Configuration { val macros: MutableSet = run { if (!macroFile.exists()) macroFile.createNewFile() macroFile.readLines(Charset.forName("UTF-8")).map { - Json.decodeFromString(it) - }.toMutableSet() + Snowy.Gson.fromJson(it, Macro::class.java) + }.map { it.copy() }.toMutableSet() } init { Runtime.getRuntime().addShutdownHook( @@ -94,7 +91,7 @@ object Configuration { } ) } - macros.map { Json.encodeToString(it) }.forEachIndexed { index, s -> + macros.map { Snowy.Gson.toJson(it) }.forEachIndexed { index, s -> with(macroFile) { "$s\n".let { if (index == 0) { diff --git a/common/src/main/kotlin/fr/username404/snowygui/gui/feature/Macro.kt b/common/src/main/kotlin/fr/username404/snowygui/gui/feature/Macro.kt index d27f51a..3a68974 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/gui/feature/Macro.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/gui/feature/Macro.kt @@ -1,14 +1,13 @@ package fr.username404.snowygui.gui.feature +import com.google.gson.annotations.Expose import net.minecraft.client.Minecraft -import kotlinx.serialization.* import fr.username404.snowygui.gui.feature.ButtonInfo.Companion.Type -@Serializable @ButtonInfo(Category.MACROS, kind = Type.CLICK) data class Macro( - private val command: String, - override val title: String = command + @Expose private val command: String, + @Expose override val title: String = command ): ButtonImpl() { override fun execAction() = Minecraft.getInstance().player!!.chat("/$command") } \ No newline at end of file diff --git a/forge/build.gradle.kts b/forge/build.gradle.kts index 2b67623..35e103a 100644 --- a/forge/build.gradle.kts +++ b/forge/build.gradle.kts @@ -9,8 +9,7 @@ dependencies { implementation("thedarkcolour:kotlinforforge:${rootProject.property("kotlinforforge")}.0") implementation(project(path = ":common")) { isTransitive = false } add("developmentForge", project(path = ":common")) { isTransitive = false } - shadowC("org.jetbrains.kotlinx:kotlinx-serialization-core:$serializationVer") { isTransitive = false } - shadowC("org.jetbrains.kotlinx:kotlinx-serialization-json:$serializationVer") { isTransitive = false } + shadowC("org.jetbrains.kotlinx:kotlinx-serialization-core:$serializationVer") { isTransitive = false } // Needed for kotlinx-datetime shadowC(project(path = ":common", configuration = "transformProductionForge")) { isTransitive = false } }; loom { useFabricMixin = true diff --git a/forge/src/main/kotlin/fr/username404/snowygui/forge/ForgeInit.kt b/forge/src/main/kotlin/fr/username404/snowygui/forge/ForgeInit.kt index bda75b8..1dec3c6 100644 --- a/forge/src/main/kotlin/fr/username404/snowygui/forge/ForgeInit.kt +++ b/forge/src/main/kotlin/fr/username404/snowygui/forge/ForgeInit.kt @@ -42,7 +42,7 @@ class ForgeInit: Snowy() { (data!!.javaClass.getDeclaredField("clazz").apply { isAccessible = true }.get(data) as Type).className.let { classname -> classname.startsWith(FeaturePackage).let { it && with(Class.forName(classname)) { - (!kotlin.isAbstract || !kotlin.isData) && declaredAnnotations.any { annotation -> + (!(kotlin.isAbstract || kotlin.isData)) && declaredAnnotations.any { annotation -> annotation is ButtonInfo } }