Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
some improvements
Browse files Browse the repository at this point in the history
Signed-off-by: reosfire <2.com9761721@gmail.com>
  • Loading branch information
reosfire committed May 15, 2024
1 parent b7acf2f commit 247f00e
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 84 deletions.
61 changes: 25 additions & 36 deletions common/src/main/kotlin/com/daylifecraft/common/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,19 @@ import kotlin.reflect.full.primaryConstructor
import kotlin.reflect.jvm.jvmErasure

inline fun <reified T : Any> load(
firstLoader: Provider,
vararg loaders: Provider,
basePath: ConfigPath = ConfigPath(),
): T {
val l = mutableListOf(firstLoader)
l.addAll(loaders)
return load(T::class, basePath, l)
}
): T = load(T::class, basePath, loaders.toList())

fun <T : Any> load(
kClass: KClass<T>,
basePath: ConfigPath,
loaders: List<Provider>,
): T {
if (!kClass.isData) throw IllegalArgumentException("T must be data class, but was ${kClass.simpleName}")
require(kClass.isData) { "${kClass.simpleName} must be data class, to be loadable by reflection" }

val primaryConstructor = kClass.primaryConstructor ?: throw IllegalArgumentException("Primary constructor of data class not found!?")
val primaryConstructor = kClass.primaryConstructor
require(primaryConstructor != null) { "${kClass.simpleName} must have a primary constructor" }

val arguments = mutableMapOf<KParameter, Any?>()

Expand Down Expand Up @@ -73,14 +69,30 @@ private fun loadList(
): List<Any> {
val listSize = loaders.tryGet { listSize(basePath) } ?: 0
return when (listElementType) {
String::class -> loadList(listSize, basePath) {
loaders.tryGet { string(basePath) }
}

Int::class -> loadList(listSize, basePath) {
loaders.tryGet { int(basePath) }
}

Long::class -> loadList(listSize, basePath) {
loaders.tryGet { long(basePath) }
}

Float::class -> loadList(listSize, basePath) {
loaders.tryGet { float(basePath) }
}

Double::class -> loadList(listSize, basePath) {
loaders.tryGet { double(basePath) }
}

Boolean::class -> loadList(listSize, basePath) {
loaders.tryGet { boolean(basePath) }
}

String::class -> loadList(listSize, basePath) {
loaders.tryGet { string(basePath) }
}

else -> loadList(listSize, basePath) { path ->
load(listElementType, path, loaders)
}
Expand All @@ -98,33 +110,10 @@ private inline fun loadList(
return@MutableList got ?: error("List element not found")
}

inline fun <T> List<Provider>.tryGet(block: Provider.() -> T): T? {
private inline fun <T> List<Provider>.tryGet(block: Provider.() -> T): T? {
for (loader in this) {
val res = block.invoke(loader)
if (res != null) return res
}
return null
}

data class Cfg(
val cfgA: Int,
val cfgB: String,
val cfgC: C,
val cfgD: Int? = null,
val cfgE: Int?,
val cfgF: Int = 1,
)

data class C(
val a: Int,
val b: String,
val li: List<Int>,
val ls: List<String>,
val lo: List<D>,
)

data class D(
val v: Int,
val w: String,
val cfg: Cfg,
)
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class YamlProvider(yamlStream: InputStream) : Provider {

override fun listSize(configPath: ConfigPath): Int? = (any(configPath, data) as List<*>?)?.size

private fun any(path: ConfigPath, currentData: Map<String, Any?>): Any? {
private fun any(path: ConfigPath, currentData: Map<*, *>): Any? {
val pathToken = path.first()
if (pathToken !is StringToken) error("path wasn't a StringToken $path")
return moveDeeper(path, currentData[pathToken.value])
Expand All @@ -70,15 +70,15 @@ class YamlProvider(yamlStream: InputStream) : Provider {
return moveDeeper(path, currentData[firstToken.value])
}

@Suppress("UNCHECKED_CAST")
private fun moveDeeper(path: ConfigPath, current: Any?): Any? {
if (path.size == 1) return current
if (current == null) return null

val removed = path.removeFirst()
val next = when (current) {
is List<*> -> any(path, current)
else -> any(path, current as Map<String, Any>)
is Map<*, *> -> any(path, current)
else -> return null
}
path.addFirst(removed)
return next
Expand Down
29 changes: 25 additions & 4 deletions runner/src/main/kotlin/com/daylifecraft/minigames/Init.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.daylifecraft.minigames

import com.daylifecraft.common.config.Cfg
import com.daylifecraft.common.config.load
import com.daylifecraft.common.config.providers.EnvProvider
import com.daylifecraft.common.config.providers.yamlProvidersOf
Expand Down Expand Up @@ -38,6 +37,29 @@ import java.nio.charset.StandardCharsets
import java.util.UUID
import kotlin.system.exitProcess

data class Cfg(
val cfgA: Int,
val cfgB: String,
val cfgC: C,
val cfgD: Int? = null,
val cfgE: Int?,
val cfgF: Int = 1,
)

data class C(
val a: Int,
val b: String,
val li: List<Int>,
val ls: List<String>,
val lo: List<D>,
)

data class D(
val v: Int,
val w: String,
val cfg: Cfg,
)

private const val SERVER_IP = "::"

object Init {
Expand Down Expand Up @@ -93,19 +115,18 @@ object Init {
EnvProvider(),
*yamlProvidersOf(
FilesUtil.getResourceStreamByPath("server.yml"),
FilesUtil.getResourceStreamByPath("another.yml"),
),
)
println(main)
val cfg = load<Cfg>(
EnvProvider(),
*yamlProvidersOf(
FilesUtil.getResourceStreamByPath("server.yml"),
FilesUtil.getResourceStreamByPath("test.yml"),
FilesUtil.getResourceStreamByPath("another.yml"),
),
)
val config = load<MiniGameSettingsConfig>(
yamlProvidersOf(FilesUtil.getResourceStreamByPath("games/testMiniGame/config.yml")).first(),
*yamlProvidersOf(FilesUtil.getResourceStreamByPath("games/testMiniGame/config.yml")),
)
println(config)
println(cfg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class MiniGamesSettingManager {

private fun loadConfigurationFile(folderName: String, inputStream: InputStream) {
val config = load<MiniGameSettingsConfig>(
yamlProvidersOf(inputStream).first(),
*yamlProvidersOf(inputStream),
)

val generalGameSettings = GeneralGameSettings(
Expand Down
40 changes: 36 additions & 4 deletions runner/src/main/resources/another.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
cfgB: "asdf"
cfgA: 11
cfgA: "aaaa"
cfgC:
a: 321
b: "bim bim bam bam"
a: 111
b: "fdsa"
li:
- 1
- 2
- 3
- 4
ls:
- "asdf"
- "fdsa"
- "vibe"
lo:
- v: 1
w: "one"
cfg:
cfgA: 1
cfgB: "b"
cfgC:
a: 1
b: "b"
li: []
ls: []
lo: []

- v: 2
w: "two"
cfg:
cfgA: 2
cfgB: "bb"
cfgC:
a: 2
b: "bb"
li: [ 1, 2, 3 ]
ls: [ ]
lo: [ ]
36 changes: 0 additions & 36 deletions runner/src/main/resources/server.yml
Original file line number Diff line number Diff line change
@@ -1,39 +1,3 @@
cfgC:
b: "fdsa"
li:
- 1
- 2
- 3
- 4
ls:
- "asdf"
- "fdsa"
- "vibe"
lo:
- v: 1
w: "one"
cfg:
cfgA: 1
cfgB: "b"
cfgC:
a: 1
b: "b"
li: []
ls: []
lo: []

- v: 2
w: "two"
cfg:
cfgA: 2
cfgB: "bb"
cfgC:
a: 2
b: "bb"
li: [ 1, 2, 3 ]
ls: [ ]
lo: [ ]

# MongoDB collections
collections:
- players
Expand Down
36 changes: 36 additions & 0 deletions runner/src/main/resources/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
cfgB: "bbbbb"
cfgC:
b: "fdsa"
li:
- 1
- 2
- 3
- 4
ls:
- "asdf"
- "kkkkkk"
- "vibe"
lo:
- v: 1
w: "one"
cfg:
cfgA: 1
cfgB: "b"
cfgC:
a: 1
b: "b"
li: []
ls: []
lo: []

- v: 2
w: "two"
cfg:
cfgA: 2
cfgB: "bb"
cfgC:
a: 2
b: "bb"
li: [ 1, 2, 3 ]
ls: [ ]
lo: [ ]

0 comments on commit 247f00e

Please sign in to comment.