Skip to content

Commit 8c1506f

Browse files
committed
feat: add base classes for YAML and JSON Sponge configuration management
1 parent d092db0 commit 8c1506f

File tree

4 files changed

+165
-3
lines changed

4 files changed

+165
-3
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package dev.slne.surf.surfapi.core.api.config
2+
3+
import dev.slne.surf.surfapi.core.api.config.manager.SpongeConfigManager
4+
import java.nio.file.Path
5+
6+
/**
7+
* Base convenience wrapper around a [SpongeConfigManager] for a specific config type [C].
8+
*
9+
* This class is intended to be extended by companion objects of config data classes.
10+
* It provides a simple, type-safe API to:
11+
* - access the current config instance via [getConfig]
12+
* - persist changes via [save]
13+
* - reload the config from disk via [reloadFromFile]
14+
* - perform in-place mutations via [edit]
15+
*
16+
* The actual manager instance is provided by subclasses and typically created by
17+
* a central configuration API (e.g. [surfConfigApi]).
18+
*
19+
* @param C the type of the configuration data object.
20+
* @param configClass the Java class of [C], used by underlying config frameworks
21+
* to create and map configuration instances.
22+
*/
23+
sealed class SpongeConfigClass<C>(configClass: Class<C>) {
24+
/**
25+
* Folder where the configuration file is stored.
26+
*
27+
* Implementations should point this to a plugin- or module-specific config directory.
28+
*/
29+
protected abstract val configFolder: Path
30+
31+
/**
32+
* The name of the configuration file, including its extension
33+
* (for example `settings.yml` or `settings.json`).
34+
*/
35+
protected abstract val fileName: String
36+
37+
/**
38+
* The underlying configuration manager responsible for loading, saving,
39+
* and tracking the config instance of type [C].
40+
*/
41+
abstract val manager: SpongeConfigManager<C>
42+
43+
/**
44+
* Persists the current configuration to disk.
45+
*
46+
* Delegates to [SpongeConfigManager.save].
47+
*/
48+
fun save() = manager.save()
49+
50+
/**
51+
* Reloads the configuration from disk and replaces the current in-memory instance.
52+
*
53+
* Delegates to [SpongeConfigManager.reloadFromFile].
54+
* Use this when external changes to the config file should be picked up at runtime.
55+
*/
56+
fun reloadFromFile() = manager.reloadFromFile()
57+
58+
/**
59+
* Applies mutations to the current config instance in a safe way.
60+
*
61+
* The [block] receives the current config instance as receiver and can freely
62+
* modify its properties. After the block completes, the config will be saved
63+
* automatically if [save] is `true`.
64+
*
65+
* Example:
66+
* ```kotlin
67+
* MyConfig.edit {
68+
* someField = "new value"
69+
* }
70+
* ```
71+
*
72+
* @param save whether to persist the config to disk after applying [block]. Defaults to `true`.
73+
* @param block mutation block executed on the current config instance.
74+
*/
75+
inline fun edit(save: Boolean = true, block: C.() -> Unit) = manager.edit(save, block)
76+
77+
/**
78+
* Returns the current in-memory configuration instance.
79+
*
80+
* The returned object is managed by the underlying [SpongeConfigManager] and
81+
* will be replaced when [reloadFromFile] is called.
82+
*/
83+
fun getConfig(): C = manager.config
84+
85+
/**
86+
* Dummy initializer to force class loading and companion object initialization.
87+
*
88+
* This is useful in plugin entry points (e.g. `onLoad`) to ensure that:
89+
* - the config manager is constructed
90+
* - the configuration is loaded before first access via [getConfig]
91+
*
92+
* This method is a no-op and can be safely called multiple times.
93+
*/
94+
fun init() = Unit
95+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package dev.slne.surf.surfapi.core.api.config
2+
3+
/**
4+
* Convenience base class for JSON-backed Sponge configuration classes.
5+
*
6+
* This class wires the common configuration metadata ([configFolder], [fileName])
7+
* to a JSON-based [dev.slne.surf.surfapi.core.api.config.manager.SpongeConfigManager] instance created by [surfConfigApi].
8+
*
9+
* Typical usage is via a companion object on a `@ConfigSerializable` data class:
10+
* ```kotlin
11+
* @ConfigSerializable
12+
* data class MyJsonConfig(
13+
* var someField: String = "value"
14+
* ) {
15+
* companion object : SpongeJsonConfigClass<MyJsonConfig>(MyJsonConfig::class.java) {
16+
* override val configFolder = Path("config/my-plugin")
17+
* override val fileName = "my-config.json"
18+
* }
19+
* }
20+
* ```
21+
*
22+
* @param C the type of the configuration data object.
23+
* @param configClass the Java class of [C], used by the underlying config framework.
24+
*/
25+
abstract class SpongeJsonConfigClass<C>(configClass: Class<C>) : SpongeConfigClass<C>(configClass) {
26+
27+
/**
28+
* JSON-backed configuration manager for this config type.
29+
*
30+
* The manager is created using [SurfConfigApi.createSpongeJsonConfigManager]
31+
* with [configClass], [configFolder] and [fileName].
32+
*/
33+
override val manager =
34+
surfConfigApi.createSpongeJsonConfigManager(configClass, configFolder, fileName)
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package dev.slne.surf.surfapi.core.api.config
2+
3+
/**
4+
* Convenience base class for YAML-backed Sponge configuration classes.
5+
*
6+
* This class wires the common configuration metadata ([configFolder], [fileName])
7+
* to a YAML-based [dev.slne.surf.surfapi.core.api.config.manager.SpongeConfigManager] instance created by [surfConfigApi].
8+
*
9+
* Typical usage is via a companion object on a `@ConfigSerializable` data class:
10+
* ```kotlin
11+
* @ConfigSerializable
12+
* data class MyConfig(
13+
* var someField: String = "value"
14+
* ) {
15+
* companion object : SpongeYmlConfigClass<MyConfig>(MyConfig::class.java) {
16+
* override val configFolder = Path("config/my-plugin")
17+
* override val fileName = "my-config.yml"
18+
* }
19+
* }
20+
* ```
21+
*
22+
* @param C the type of the configuration data object.
23+
* @param configClass the Java class of [C], used by the underlying config framework.
24+
*/
25+
abstract class SpongeYmlConfigClass<C>(configClass: Class<C>) : SpongeConfigClass<C>(configClass) {
26+
27+
/**
28+
* YAML-backed configuration manager for this config type.
29+
*
30+
* The manager is created using [SurfConfigApi.createSpongeYmlConfigManager]
31+
* with [configClass], [configFolder] and [fileName].
32+
*/
33+
override val manager =
34+
surfConfigApi.createSpongeYmlConfigManager(configClass, configFolder, fileName)
35+
}

surf-api-gradle-plugin/build.gradle.kts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import gradle.kotlin.dsl.accessors._243eb5039d4da502111cc46640837e78.compileOnly
2-
import gradle.kotlin.dsl.accessors._243eb5039d4da502111cc46640837e78.implementation
3-
import gradle.kotlin.dsl.accessors._243eb5039d4da502111cc46640837e78.sourceSets
41
import java.nio.file.Files
52

63
// region properties

0 commit comments

Comments
 (0)