From cb3652a20d85104e5abab6da7a3cd2e171845d7e Mon Sep 17 00:00:00 2001 From: justfoxx Date: Wed, 16 Aug 2023 22:48:41 +0200 Subject: [PATCH] Removed deprecated config Changed names in PluginConfig Used JvmStatic Used WeakHashMap for saving PluginConfigs --- gradle.properties | 2 +- .../github/mosaicmc/mosaiccoder/api/Config.kt | 116 ------------------ .../mosaicmc/mosaiccoder/api/PluginConfig.kt | 49 +++++--- .../mosaicmc/mosaiccoder/internal/Test.kt | 4 +- .../mosaicmc/mosaiccoder/internal/Utils.kt | 20 ++- 5 files changed, 53 insertions(+), 138 deletions(-) delete mode 100644 src/main/kotlin/io/github/mosaicmc/mosaiccoder/api/Config.kt diff --git a/gradle.properties b/gradle.properties index edab653..1e1e657 100644 --- a/gradle.properties +++ b/gradle.properties @@ -13,7 +13,7 @@ ktfmt_version=0.12.0 # Mod Properties -mod_version = 0.2.0-beta +mod_version = 0.2.1-beta maven_group = io.github.mosaicmc mod_id = mosaiccoder diff --git a/src/main/kotlin/io/github/mosaicmc/mosaiccoder/api/Config.kt b/src/main/kotlin/io/github/mosaicmc/mosaiccoder/api/Config.kt deleted file mode 100644 index 373d1b6..0000000 --- a/src/main/kotlin/io/github/mosaicmc/mosaiccoder/api/Config.kt +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright (c) 2023. JustFoxx - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ -@file:JvmName("Config") -@file:Suppress("unused") - -package io.github.mosaicmc.mosaiccoder.api - -import com.google.gson.JsonObject -import com.google.gson.JsonParser -import com.mojang.serialization.Codec -import com.mojang.serialization.DataResult -import com.mojang.serialization.JsonOps -import io.github.mosaicmc.mosaiccoder.internal.asJsonObject -import io.github.mosaicmc.mosaiccoder.internal.gson -import io.github.mosaicmc.mosaiccoder.internal.wrapResult -import io.github.mosaicmc.mosaiccore.api.plugin.PluginContainer -import io.github.mosaicmc.mosaiccore.api.plugin.name -import java.io.File -import java.io.FileReader -import java.io.FileWriter -import net.fabricmc.loader.impl.FabricLoaderImpl - -/** Represents the directory where configuration files are stored. */ -val PluginContainer.configDir: File - get() = FabricLoaderImpl.INSTANCE.configDir.resolve(name).toFile() - -@Deprecated("Use PluginConfig instead.", ReplaceWith("PluginConfig")) -fun PluginContainer.writeConfig( - fileName: String, - dataToWrite: JsonObject, -): DataResult = wrapResult { - val file = configDir.resolve(fileName) - if (!file.exists()) { - return@wrapResult DataResult.error { "file not found" } - } - FileWriter(file).use { gson.toJson(dataToWrite, it) } - return@wrapResult DataResult.success(dataToWrite) -} - -@Deprecated("Use PluginConfig instead.", ReplaceWith("PluginConfig")) -fun PluginContainer.writeConfig( - fileName: String, - dataToWrite: T, - codec: Codec, -): DataResult = wrapResult { - val file = configDir.resolve(fileName) - if (!file.exists()) { - return@wrapResult DataResult.error { "file not found" } - } - val parsed = codec.parse(JsonOps.INSTANCE, dataToWrite.asJsonObject) - if (parsed.error().isPresent) return@wrapResult parsed - FileWriter(file).use { gson.toJson(parsed.result().get(), it) } - return@wrapResult parsed -} - -@Deprecated("Use PluginConfig instead.", ReplaceWith("PluginConfig")) -fun PluginContainer.readConfig( - fileName: String = "common.json", - codec: Codec -): DataResult = wrapResult { - val file = configDir.resolve(fileName) - if (!file.exists()) { - return@wrapResult DataResult.error { "file not found" } - } - - val readFile = JsonParser.parseReader(FileReader(file)).asJsonObject - return@wrapResult codec.parse(JsonOps.INSTANCE, readFile) -} - -@Deprecated("Use PluginConfig instead.", ReplaceWith("PluginConfig")) -fun PluginContainer.createConfig( - fileName: String = "common.json", - codec: Codec, - defaultObject: JsonObject = JsonObject() -): DataResult = wrapResult { - val file = configDir.resolve(fileName) - if (!file.exists()) { - file.parentFile.mkdirs() - file.createNewFile() - } else { - return@wrapResult DataResult.error { "file already exists" } - } - val parsed = codec.parse(JsonOps.INSTANCE, defaultObject) - if (parsed.error().isPresent) return@wrapResult parsed - FileWriter(file).use { gson.toJson(parsed.result().get(), it) } - return@wrapResult parsed -} - -@Deprecated("Use PluginConfig instead.", ReplaceWith("PluginConfig")) -fun PluginContainer.createOrReadConfig( - fileName: String = "common.json", - codec: Codec, - defaultObject: JsonObject = JsonObject(), -): DataResult = - if (configDir.resolve(fileName).exists()) readConfig(fileName, codec) - else createConfig(fileName, codec, defaultObject) - -@Deprecated("Use PluginConfig instead.", ReplaceWith("PluginConfig")) -fun PluginContainer.createOrReadConfig( - fileName: String = "common.json", - codec: Codec, - defaultObject: T, -): DataResult = createOrReadConfig(fileName, codec, defaultObject.asJsonObject) diff --git a/src/main/kotlin/io/github/mosaicmc/mosaiccoder/api/PluginConfig.kt b/src/main/kotlin/io/github/mosaicmc/mosaiccoder/api/PluginConfig.kt index dd3272f..622d6c4 100644 --- a/src/main/kotlin/io/github/mosaicmc/mosaiccoder/api/PluginConfig.kt +++ b/src/main/kotlin/io/github/mosaicmc/mosaiccoder/api/PluginConfig.kt @@ -1,16 +1,18 @@ +@file:Suppress("NOTHING_TO_INLINE") + package io.github.mosaicmc.mosaiccoder.api import com.google.gson.JsonParser import com.mojang.serialization.Codec import com.mojang.serialization.DataResult import com.mojang.serialization.JsonOps -import io.github.mosaicmc.mosaiccoder.internal.asJsonObject -import io.github.mosaicmc.mosaiccoder.internal.gson +import io.github.mosaicmc.mosaiccoder.internal.* import io.github.mosaicmc.mosaiccoder.internal.wrapResult import io.github.mosaicmc.mosaiccore.api.plugin.PluginContainer import java.io.File import java.io.FileReader import java.io.FileWriter +import java.util.WeakHashMap /** * A utility class for managing and interacting with plugin configuration files. @@ -30,7 +32,7 @@ private constructor(private val file: File, private val codec: Codec, private * * @return A [DataResult] containing the reloaded configuration data or an error. */ - fun reload(): DataResult = wrapResult { forceRead() } + fun reloadData(): DataResult = wrapResult { forceReadData() } /** * Writes new configuration data to the file. @@ -38,9 +40,9 @@ private constructor(private val file: File, private val codec: Codec, private * @param newData The new configuration data to be written. * @return A [DataResult] indicating success or an error during the write operation. */ - fun write(newData: T): DataResult = wrapResult { + fun writeData(newData: T): DataResult = wrapResult { if (!file.exists()) { - val createdData = create() + val createdData = createFile() if (createdData.error().isPresent) return@wrapResult createdData } @@ -60,15 +62,15 @@ private constructor(private val file: File, private val codec: Codec, private */ fun getData(): DataResult = wrapResult { if (data == null) { - val forceReadData = forceRead() + val forceReadData = forceReadData() if (forceReadData.error().isPresent) return@wrapResult forceReadData - data = forceRead().result().get() + data = forceReadData().result().get() } DataResult.success(data!!) } - private fun forceRead(): DataResult = wrapResult { - if (!file.exists()) onFileNotExists() + inline private fun forceReadData(): DataResult = wrapResult { + if (!file.exists()) createData() val readFile = JsonParser.parseReader(FileReader(file)).asJsonObject val parsedData = codec.parse(JsonOps.INSTANCE, readFile) @@ -79,7 +81,7 @@ private constructor(private val file: File, private val codec: Codec, private parsedData } - private fun create(): DataResult = wrapResult { + inline private fun createFile(): DataResult = wrapResult { file.parentFile.mkdirs() file.createNewFile() @@ -87,20 +89,21 @@ private constructor(private val file: File, private val codec: Codec, private if (parsed.error().isPresent) return@wrapResult parsed - FileWriter(file).use { gson.toJson(parsed.result().get(), it) } + val fileWriter = FileWriter(file) + gson.toJson(parsed.result().get(), fileWriter) + parsed } - private fun onFileNotExists(): DataResult = wrapResult { - val createdData = create() + inline private fun createData(): DataResult = wrapResult { + val createdData = createFile() if (createdData.error().isPresent) return@wrapResult createdData data = createdData.result().get() createdData } companion object { - private val configs = mutableMapOf>() - + @JvmStatic private val configs = WeakHashMap>() /** * Retrieves or creates a configuration instance for a plugin. * @@ -109,14 +112,26 @@ private constructor(private val file: File, private val codec: Codec, private * @param default The default configuration data. * @return A [PluginConfig] instance for the specified plugin and configuration file. */ + @JvmStatic fun PluginContainer.getConfig( fileName: String, codec: Codec, default: T ): PluginConfig { val file = configDir.resolve(fileName) - - return configs.getOrDefault(file, PluginConfig(file, codec, default)) as PluginConfig + val pluginConfig = PluginConfig(file, codec, default) + return configs.getOrDefaultExt(file, pluginConfig) } + + /** + * Retrieves a configuration instance for a plugin if exists. + * + * @param fileName The name of the configuration file. + * @return A [PluginConfig] instance for the specified plugin and configuration file. + */ + @JvmStatic + fun PluginContainer.getConfig( + fileName: String, + ): PluginConfig? = configs.getExt(configDir.resolve(fileName)) } } diff --git a/src/main/kotlin/io/github/mosaicmc/mosaiccoder/internal/Test.kt b/src/main/kotlin/io/github/mosaicmc/mosaiccoder/internal/Test.kt index 601dd97..3b450c9 100644 --- a/src/main/kotlin/io/github/mosaicmc/mosaiccoder/internal/Test.kt +++ b/src/main/kotlin/io/github/mosaicmc/mosaiccoder/internal/Test.kt @@ -54,10 +54,10 @@ internal fun PluginContainer.`get test config`(): DataResult = getConfig("test.json", testCoded, testJson).getData() internal fun PluginContainer.`reload test config`(): DataResult = - getConfig("test.json", testCoded, testJson).reload() + getConfig("test.json", testCoded, testJson).reloadData() internal fun PluginContainer.`write test config`(): DataResult = - getConfig("test.json", testCoded, testJson).write(TestJson(2, "z")) + getConfig("test.json", testCoded, testJson).writeData(TestJson(2, "z")) internal fun PluginContainer.test() { logger.printResult(::`get test config`) diff --git a/src/main/kotlin/io/github/mosaicmc/mosaiccoder/internal/Utils.kt b/src/main/kotlin/io/github/mosaicmc/mosaiccoder/internal/Utils.kt index 17888dd..ffd6b49 100644 --- a/src/main/kotlin/io/github/mosaicmc/mosaiccoder/internal/Utils.kt +++ b/src/main/kotlin/io/github/mosaicmc/mosaiccoder/internal/Utils.kt @@ -14,6 +14,7 @@ * along with this program. If not, see . */ @file:JvmName("Utils") +@file:Suppress("NOTHING_TO_INLINE", "UNCHECKED_CAST") package io.github.mosaicmc.mosaiccoder.internal @@ -21,8 +22,12 @@ import com.google.gson.Gson import com.google.gson.GsonBuilder import com.google.gson.JsonObject import com.mojang.serialization.DataResult +import io.github.mosaicmc.mosaiccore.api.plugin.PluginContainer +import io.github.mosaicmc.mosaiccore.api.plugin.name +import java.io.File +import net.fabricmc.loader.impl.FabricLoaderImpl -internal fun wrapResult(wrapped: () -> DataResult): DataResult = +inline internal fun wrapResult(wrapped: () -> DataResult): DataResult = try { wrapped() } catch (e: Exception) { @@ -31,5 +36,16 @@ internal fun wrapResult(wrapped: () -> DataResult): DataResult = val gson: Gson = GsonBuilder().setPrettyPrinting().create() -val T.asJsonObject: JsonObject +val T.asJsonObject: JsonObject get() = gson.toJsonTree(this).asJsonObject + +/** Represents the directory where configuration files are stored. */ +val PluginContainer.configDir: File + get() = FabricLoaderImpl.INSTANCE.configDir.resolve(name).toFile() + +inline internal fun Map.getExt(key: TKey): TValue? = this[key] as? TValue + +inline internal fun Map.getOrDefaultExt( + key: TKey, + default: TValue +): TValue = (this[key] ?: default) as TValue