Use kotlinx-serialization-json instead of gson and move the macros to the main configuration file

This commit is contained in:
Username404-59 2021-06-01 12:37:19 +02:00 committed by Username404
parent c74456c000
commit d8bbab0ff7
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
4 changed files with 25 additions and 20 deletions

View File

@ -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<net.fabricmc.loom.LoomGradleExtension>("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 * ; }")

View File

@ -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<Class<out ButtonImpl>>

View File

@ -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<String, Supplier<ConfigValue>>(
"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<Macro> = 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<Macro>(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()
}
}

View File

@ -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")
}