Use kotlinx-serialization-json instead of gson and move the macros to the main configuration file
This commit is contained in:
parent
c74456c000
commit
d8bbab0ff7
|
@ -11,6 +11,7 @@ buildscript {
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
kotlin("jvm") version "1.5.20-M1"
|
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("com.github.johnrengelman.shadow") version "7.0.0" apply false
|
||||||
id("architectury-plugin") version "[3.0.100, 3.3["
|
id("architectury-plugin") version "[3.0.100, 3.3["
|
||||||
id("dev.architectury.loom") version "0.7.2.110" apply false
|
id("dev.architectury.loom") version "0.7.2.110" apply false
|
||||||
|
@ -31,6 +32,7 @@ val javaVer: String = "8"
|
||||||
val sourceJavaVer: String = "16"
|
val sourceJavaVer: String = "16"
|
||||||
val kotlinVer: String by rootProject
|
val kotlinVer: String by rootProject
|
||||||
val kotlinBaseVer = kotlinVer.substring(0..2)
|
val kotlinBaseVer = kotlinVer.substring(0..2)
|
||||||
|
val serializationVer: String by rootProject
|
||||||
val mcBase: String = rootProject.architectury.minecraft.substring(0..3)
|
val mcBase: String = rootProject.architectury.minecraft.substring(0..3)
|
||||||
val kotlinX: String = "org.jetbrains.kotlinx"
|
val kotlinX: String = "org.jetbrains.kotlinx"
|
||||||
|
|
||||||
|
@ -38,6 +40,7 @@ subprojects {
|
||||||
group = rootProject.group.toString()
|
group = rootProject.group.toString()
|
||||||
lateinit var mappingsDep: Dependency
|
lateinit var mappingsDep: Dependency
|
||||||
apply(plugin = "dev.architectury.loom")
|
apply(plugin = "dev.architectury.loom")
|
||||||
|
apply(plugin = "org.jetbrains.kotlin.plugin.serialization")
|
||||||
extensions.configure<net.fabricmc.loom.LoomGradleExtension>("loom") {
|
extensions.configure<net.fabricmc.loom.LoomGradleExtension>("loom") {
|
||||||
mappingsDep = officialMojangMappings()
|
mappingsDep = officialMojangMappings()
|
||||||
silentMojangMappingsLicense()
|
silentMojangMappingsLicense()
|
||||||
|
@ -49,6 +52,8 @@ subprojects {
|
||||||
repositories { maven(url = "https://jitpack.io"); mavenCentral() }
|
repositories { maven(url = "https://jitpack.io"); mavenCentral() }
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation("$kotlinX:kotlinx-coroutines-core:$kotlinBaseVer.0-RC")
|
implementation("$kotlinX:kotlinx-coroutines-core:$kotlinBaseVer.0-RC")
|
||||||
|
implementation("$kotlinX:kotlinx-serialization-core:$serializationVer")
|
||||||
|
implementation("$kotlinX:kotlinx-serialization-json:$serializationVer")
|
||||||
listOf(
|
listOf(
|
||||||
"$kotlinX:kotlinx-datetime:0.2.1",
|
"$kotlinX:kotlinx-datetime:0.2.1",
|
||||||
"com.typesafe:config:1.4.1",
|
"com.typesafe:config:1.4.1",
|
||||||
|
@ -102,6 +107,11 @@ subprojects {
|
||||||
adaptclassstrings()
|
adaptclassstrings()
|
||||||
"$group.**".also { dontnote(it); dontwarn(it) }
|
"$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
|
// Required for discord-rpc
|
||||||
keepclassmembers("class $group.drpc.** { public * ; }")
|
keepclassmembers("class $group.drpc.** { public * ; }")
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
package fr.username404.snowygui
|
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.EventSnowy.Companion.useKey
|
||||||
import fr.username404.snowygui.config.Configuration.obtained
|
import fr.username404.snowygui.config.Configuration.obtained
|
||||||
import fr.username404.snowygui.gui.feature.ButtonImpl
|
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)))
|
protected fun Class<*>.isValidForButtonCollection(): Boolean = (!((Modifier.isAbstract(javaClass.modifiers)) || javaClass.isAnnotationPresent(Ignored::class.java)))
|
||||||
private val displayInitMessage: Boolean by obtained
|
private val displayInitMessage: Boolean by obtained
|
||||||
companion object {
|
companion object {
|
||||||
val Gson: Gson = GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
protected val FeaturePackage: String = "fr.username404.snowygui.gui.feature"
|
protected val FeaturePackage: String = "fr.username404.snowygui.gui.feature"
|
||||||
lateinit var annotatedButtons: Set<Class<out ButtonImpl>>
|
lateinit var annotatedButtons: Set<Class<out ButtonImpl>>
|
||||||
|
|
|
@ -12,22 +12,26 @@ import io.github.config4k.extract
|
||||||
import kotlinx.coroutines.coroutineScope
|
import kotlinx.coroutines.coroutineScope
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
|
import kotlinx.serialization.decodeFromString
|
||||||
|
import kotlinx.serialization.encodeToString
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
import net.minecraft.client.Minecraft
|
import net.minecraft.client.Minecraft
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.nio.charset.Charset
|
|
||||||
import java.util.function.Supplier
|
import java.util.function.Supplier
|
||||||
|
|
||||||
object Configuration {
|
object Configuration {
|
||||||
val ModifiableValues = mutableMapOf<String, Supplier<ConfigValue>>(
|
val ModifiableValues = mutableMapOf<String, Supplier<ConfigValue>>(
|
||||||
"enabledFeatures" to Supplier { ConfigValueFactory.fromMap(enabledFeatures) },
|
"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()) })
|
); 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 ->
|
private fun Config.withFullModifiableValues() = ModifiableValues.entries.fold(this) { previous, entry ->
|
||||||
previous.withValue(entry.key, entry.value.get())
|
previous.withValue(entry.key, entry.value.get())
|
||||||
}
|
}
|
||||||
private val configDirectory: String = (Minecraft.getInstance().gameDirectory.absolutePath + File.separator + "config").also { File(it).mkdir() }
|
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 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 {
|
private suspend fun writeConfig(c: Config) = coroutineScope {
|
||||||
launch {
|
launch {
|
||||||
file.writeText(
|
file.writeText(
|
||||||
|
@ -64,6 +68,7 @@ object Configuration {
|
||||||
"""
|
"""
|
||||||
|Snowy {
|
|Snowy {
|
||||||
| displayInitMessage = false
|
| displayInitMessage = false
|
||||||
|
| macros = []
|
||||||
| riskyCheats = false
|
| riskyCheats = false
|
||||||
|}
|
|}
|
||||||
""".trimMargin()
|
""".trimMargin()
|
||||||
|
@ -80,9 +85,8 @@ object Configuration {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val foundMacros: Set<Macro> = run {
|
val foundMacros: Set<Macro> = run {
|
||||||
if (!macroFile.exists()) macroFile.createNewFile()
|
obtained.getStringList("macros").map {
|
||||||
macroFile.readLines(Charset.forName("UTF-8")).filterNot { it.isBlank() }.map {
|
Json.decodeFromString<Macro>(it)
|
||||||
Snowy.Gson.fromJson(it, Macro::class.java).copy()
|
|
||||||
}.toSet()
|
}.toSet()
|
||||||
}
|
}
|
||||||
init {
|
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()
|
writeConfig(obtained.withFullModifiableValues()).join()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
package fr.username404.snowygui.gui.feature
|
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 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)
|
@ButtonInfo(Category.MACROS, kind = Type.CLICK)
|
||||||
data class Macro(
|
data class Macro(
|
||||||
@Expose @JvmField var command: String,
|
@JvmField var command: String,
|
||||||
@Expose override var title: String = command
|
override var title: String = command
|
||||||
): ButtonImpl() {
|
): ButtonImpl() {
|
||||||
override fun execAction() = Minecraft.getInstance().player!!.chat("/$command")
|
override fun execAction() = Minecraft.getInstance().player!!.chat("/$command")
|
||||||
}
|
}
|
Loading…
Reference in New Issue