Skip to content

Commit

Permalink
CTJS: Use kotlinx serialization instead of gson
Browse files Browse the repository at this point in the history
This also adds author as an alias for creator
  • Loading branch information
camnwalter authored and mattco98 committed Dec 27, 2023
1 parent 39e9587 commit 730d6c7
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/com/chattriggers/ctjs/CTJS.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import com.chattriggers.ctjs.internal.console.ConsoleHostProcess
import com.chattriggers.ctjs.internal.engine.CTEvents
import com.chattriggers.ctjs.internal.engine.module.ModuleManager
import com.chattriggers.ctjs.internal.utils.Initializer
import com.google.gson.Gson
import kotlinx.serialization.json.Json
import net.fabricmc.api.ClientModInitializer
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper
import net.fabricmc.loader.api.FabricLoader
Expand Down Expand Up @@ -97,7 +97,7 @@ class CTJS : ClientModInitializer {
internal val sounds = mutableListOf<Sound>()
internal val isDevelopment = FabricLoader.getInstance().isDevelopmentEnvironment

internal val gson = Gson()
internal val json = Json { useAlternativeNames = true }

@JvmOverloads
internal fun makeWebRequest(url: String, userAgent: String? = "Mozilla/5.0 (ChatTriggers)"): URLConnection =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.chattriggers.ctjs.engine.LogType
import com.chattriggers.ctjs.engine.printToConsole
import com.chattriggers.ctjs.internal.engine.JSContextFactory
import com.chattriggers.ctjs.internal.engine.JSLoader
import kotlinx.serialization.json.Json
import org.apache.commons.io.FileUtils
import org.mozilla.javascript.Context
import java.io.File
Expand Down Expand Up @@ -94,10 +93,7 @@ object ModuleManager {

if (metadataFile.exists()) {
try {
// A lot of older modules have "author" instead of "creator", and it's easy enough to
// support that here
val text = metadataFile.readText().replace("\"author\"", "\"creator\"")
metadata = Json.decodeFromString(text)
metadata = CTJS.json.decodeFromString(metadataFile.readText())
} catch (e: Exception) {
"Module $directory has invalid metadata.json".printToConsole(LogType.ERROR)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.chattriggers.ctjs.internal.engine.module

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonNames

@Serializable
@OptIn(ExperimentalSerializationApi::class)
data class ModuleMetadata(
val name: String? = null,
val version: String? = null,
var entry: String? = null,
var mixinEntry: String? = null,
val tags: ArrayList<String>? = null,
val pictureLink: String? = null,
@JsonNames("author")
val creator: String? = null,
val description: String? = null,
val requires: ArrayList<String>? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ object ModuleUpdater : Initializer {
val connection = CTJS.makeWebRequest(url)

val newMetadataText = connection.getInputStream().bufferedReader().readText()
val newMetadata = CTJS.gson.fromJson(newMetadataText, ModuleMetadata::class.java)
val newMetadata = CTJS.json.decodeFromString<ModuleMetadata>(newMetadataText)

if (newMetadata.version == null) {
("Remote version of module ${metadata.name} has no version numbers, so it will " +
Expand All @@ -75,7 +75,7 @@ object ModuleUpdater : Initializer {
"Updated module ${metadata.name}".printToConsole()

module.metadata = File(module.folder, "metadata.json").let {
CTJS.gson.fromJson(it.readText(), ModuleMetadata::class.java)
CTJS.json.decodeFromString<ModuleMetadata>(it.readText())
}

if (Config.moduleChangelog && module.metadata.changelog != null) {
Expand Down

0 comments on commit 730d6c7

Please sign in to comment.