Skip to content
This repository has been archived by the owner on Sep 4, 2023. It is now read-only.

Commit

Permalink
Removed deprecated config
Browse files Browse the repository at this point in the history
Changed names in PluginConfig
Used JvmStatic
Used WeakHashMap for saving PluginConfigs
  • Loading branch information
kittech0 committed Aug 16, 2023
1 parent e9c174c commit cb3652a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 138 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
116 changes: 0 additions & 116 deletions src/main/kotlin/io/github/mosaicmc/mosaiccoder/api/Config.kt

This file was deleted.

49 changes: 32 additions & 17 deletions src/main/kotlin/io/github/mosaicmc/mosaiccoder/api/PluginConfig.kt
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -30,17 +32,17 @@ private constructor(private val file: File, private val codec: Codec<T>, private
*
* @return A [DataResult] containing the reloaded configuration data or an error.
*/
fun reload(): DataResult<T> = wrapResult { forceRead() }
fun reloadData(): DataResult<T> = wrapResult { forceReadData() }

/**
* Writes new configuration data to the file.
*
* @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<T> = wrapResult {
fun writeData(newData: T): DataResult<T> = wrapResult {
if (!file.exists()) {
val createdData = create()
val createdData = createFile()
if (createdData.error().isPresent) return@wrapResult createdData
}

Expand All @@ -60,15 +62,15 @@ private constructor(private val file: File, private val codec: Codec<T>, private
*/
fun getData(): DataResult<T> = 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<T> = wrapResult {
if (!file.exists()) onFileNotExists()
inline private fun forceReadData(): DataResult<T> = wrapResult {
if (!file.exists()) createData()

val readFile = JsonParser.parseReader(FileReader(file)).asJsonObject
val parsedData = codec.parse(JsonOps.INSTANCE, readFile)
Expand All @@ -79,28 +81,29 @@ private constructor(private val file: File, private val codec: Codec<T>, private
parsedData
}

private fun create(): DataResult<T> = wrapResult {
inline private fun createFile(): DataResult<T> = wrapResult {
file.parentFile.mkdirs()
file.createNewFile()

val parsed = codec.parse(JsonOps.INSTANCE, default.asJsonObject)

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<T> = wrapResult {
val createdData = create()
inline private fun createData(): DataResult<T> = wrapResult {
val createdData = createFile()
if (createdData.error().isPresent) return@wrapResult createdData
data = createdData.result().get()
createdData
}

companion object {
private val configs = mutableMapOf<File, PluginConfig<*>>()

@JvmStatic private val configs = WeakHashMap<File, PluginConfig<*>>()
/**
* Retrieves or creates a configuration instance for a plugin.
*
Expand All @@ -109,14 +112,26 @@ private constructor(private val file: File, private val codec: Codec<T>, private
* @param default The default configuration data.
* @return A [PluginConfig] instance for the specified plugin and configuration file.
*/
@JvmStatic
fun <T : Any> PluginContainer.getConfig(
fileName: String,
codec: Codec<T>,
default: T
): PluginConfig<T> {
val file = configDir.resolve(fileName)

return configs.getOrDefault(file, PluginConfig(file, codec, default)) as PluginConfig<T>
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 <T : Any> PluginContainer.getConfig(
fileName: String,
): PluginConfig<T>? = configs.getExt(configDir.resolve(fileName))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ internal fun PluginContainer.`get test config`(): DataResult<TestJson> =
getConfig("test.json", testCoded, testJson).getData()

internal fun PluginContainer.`reload test config`(): DataResult<TestJson> =
getConfig("test.json", testCoded, testJson).reload()
getConfig("test.json", testCoded, testJson).reloadData()

internal fun PluginContainer.`write test config`(): DataResult<TestJson> =
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`)
Expand Down
20 changes: 18 additions & 2 deletions src/main/kotlin/io/github/mosaicmc/mosaiccoder/internal/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
@file:JvmName("Utils")
@file:Suppress("NOTHING_TO_INLINE", "UNCHECKED_CAST")

package io.github.mosaicmc.mosaiccoder.internal

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 <T : Any> wrapResult(wrapped: () -> DataResult<T>): DataResult<T> =
inline internal fun <T> wrapResult(wrapped: () -> DataResult<T>): DataResult<T> =
try {
wrapped()
} catch (e: Exception) {
Expand All @@ -31,5 +36,16 @@ internal fun <T : Any> wrapResult(wrapped: () -> DataResult<T>): DataResult<T> =

val gson: Gson = GsonBuilder().setPrettyPrinting().create()

val <T : Any> T.asJsonObject: JsonObject
val <T> 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 <TKey, TValue> Map<TKey, *>.getExt(key: TKey): TValue? = this[key] as? TValue

inline internal fun <TKey, TValue> Map<TKey, *>.getOrDefaultExt(
key: TKey,
default: TValue
): TValue = (this[key] ?: default) as TValue

0 comments on commit cb3652a

Please sign in to comment.