diff --git a/build.gradle.kts b/build.gradle.kts index cea3a18..7ac8594 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -11,6 +11,7 @@ buildscript { plugins { kotlin("jvm") version "1.5.20-M1" + kotlin("plugin.serialization") version "1.5.20-M1" id("com.github.johnrengelman.shadow") version "7.0.0" apply false id("architectury-plugin") version "[3.0.100, 3.3[" id("dev.architectury.loom") version "0.7.2.110" apply false @@ -31,6 +32,7 @@ val javaVer: String = "8" val sourceJavaVer: String = "16" val kotlinVer: String by rootProject val kotlinBaseVer = kotlinVer.substring(0..2) +val serializationVer: String by rootProject val mcBase: String = rootProject.architectury.minecraft.substring(0..3) val kotlinX: String = "org.jetbrains.kotlinx" @@ -38,6 +40,7 @@ subprojects { group = rootProject.group.toString() lateinit var mappingsDep: Dependency apply(plugin = "dev.architectury.loom") + apply(plugin = "org.jetbrains.kotlin.plugin.serialization") extensions.configure("loom") { mappingsDep = officialMojangMappings() silentMojangMappingsLicense() @@ -49,6 +52,8 @@ subprojects { repositories { maven(url = "https://jitpack.io"); mavenCentral() } dependencies { implementation("$kotlinX:kotlinx-coroutines-core:$kotlinBaseVer.0-RC") + implementation("$kotlinX:kotlinx-serialization-core:$serializationVer") + implementation("$kotlinX:kotlinx-serialization-json:$serializationVer") listOf( "$kotlinX:kotlinx-datetime:0.2.1", "com.typesafe:config:1.4.1", @@ -102,6 +107,11 @@ subprojects { adaptclassstrings() "$group.**".also { dontnote(it); dontwarn(it) } + // kotlinx-serialization related configuration: + keep(mapOf("includedescriptorclasses" to true), "class $group.snowygui.**$\$serializer { * ; }") + keepclassmembers("class $group.snowygui.** { *** Companion; }") + keepclasseswithmembers("class $group.snowygui.** { kotlinx.serialization.KSerializer serializer(...); }") + // Required for discord-rpc keepclassmembers("class $group.drpc.** { public * ; }") diff --git a/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt b/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt index 3b282c5..50bb00e 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt @@ -1,7 +1,5 @@ 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.gui.feature.ButtonImpl @@ -16,7 +14,6 @@ abstract class Snowy { protected fun Class<*>.isValidForButtonCollection(): Boolean = (!((Modifier.isAbstract(javaClass.modifiers)) || javaClass.isAnnotationPresent(Ignored::class.java))) 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 6511c9d..cd87a1f 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt @@ -12,22 +12,26 @@ 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 import java.util.function.Supplier object Configuration { val ModifiableValues = mutableMapOf>( "enabledFeatures" to Supplier { ConfigValueFactory.fromMap(enabledFeatures) }, - "riskyCheats" to Supplier { ConfigValueFactory.fromAnyRef(riskyCheatsEnabled) } + "riskyCheats" to Supplier { ConfigValueFactory.fromAnyRef(riskyCheatsEnabled) }, + "macros" to Supplier { ConfigValueFactory.fromIterable((ClickGui.components.find { it is ClickBox && it.isCategory(Category.MACROS) } as ClickBox).buttons.map { + Json.encodeToString(it as Macro) + }) } ); inline fun setConfigValue(s: String, crossinline value: () -> Any) = ModifiableValues.put(s, Supplier { ConfigValueFactory.fromAnyRef(value.invoke()) }) private fun Config.withFullModifiableValues() = ModifiableValues.entries.fold(this) { previous, entry -> previous.withValue(entry.key, entry.value.get()) } private val configDirectory: String = (Minecraft.getInstance().gameDirectory.absolutePath + File.separator + "config").also { File(it).mkdir() } private val file: File = File(configDirectory + File.separator + "snowy.conf") - private val macroFile: File = File(configDirectory + File.separator + "snowy-macros.json") private suspend fun writeConfig(c: Config) = coroutineScope { launch { file.writeText( @@ -64,6 +68,7 @@ object Configuration { """ |Snowy { | displayInitMessage = false + | macros = [] | riskyCheats = false |} """.trimMargin() @@ -80,9 +85,8 @@ object Configuration { } } val foundMacros: Set = run { - if (!macroFile.exists()) macroFile.createNewFile() - macroFile.readLines(Charset.forName("UTF-8")).filterNot { it.isBlank() }.map { - Snowy.Gson.fromJson(it, Macro::class.java).copy() + obtained.getStringList("macros").map { + Json.decodeFromString(it) }.toSet() } init { @@ -98,12 +102,6 @@ object Configuration { } ) } - with(macroFile) { - writeText("") - (ClickGui.components.find { it is ClickBox && it.isCategory(Category.MACROS) } as ClickBox).buttons.map { Snowy.Gson.toJson(it as Macro) }.forEach { s -> - appendText("$s\n") - } - } writeConfig(obtained.withFullModifiableValues()).join() } } 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 9dabcad..6c1fc80 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,14 @@ package fr.username404.snowygui.gui.feature -import com.google.gson.annotations.Expose -import net.minecraft.client.Minecraft import fr.username404.snowygui.gui.feature.ButtonInfo.Companion.Type +import kotlinx.serialization.Serializable +import net.minecraft.client.Minecraft -@Ignored +@Serializable @ButtonInfo(Category.MACROS, kind = Type.CLICK) data class Macro( - @Expose @JvmField var command: String, - @Expose override var title: String = command + @JvmField var command: String, + override var title: String = command ): ButtonImpl() { override fun execAction() = Minecraft.getInstance().player!!.chat("/$command") } \ No newline at end of file