Skip to content

Commit

Permalink
Thinking about how FileSystem should be installed, serializableCompon…
Browse files Browse the repository at this point in the history
…ents seems to really need a builder pattern
  • Loading branch information
0ffz committed Dec 24, 2022
1 parent 11e80f1 commit 82ce31c
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class GearyArchetypeModule(

override val engine: ArchetypeEngine = ArchetypeEngine(tickDuration)
override val eventRunner: ArchetypeEventRunner = ArchetypeEventRunner()
override val addons: DIContext = DIContext()
override val pipeline: Pipeline get() = TODO("Not yet implemented")
override val systems: SystemProvider = UnorderedSystemProvider()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.mineinabyss.geary.modules

import com.mineinabyss.ding.DI
import com.mineinabyss.geary.addons.GearyPhase
import com.mineinabyss.geary.addons.Namespaced
import com.mineinabyss.geary.addons.dsl.GearyAddon
import com.mineinabyss.geary.addons.dsl.GearyAddonWithDefault
import com.mineinabyss.geary.addons.dsl.GearyDSL

@GearyDSL
class GearyConfiguration {
inline fun <T : GearyAddonWithDefault<Module>, reified Module: Any> install(
addon: T,
) = install(addon, addon.default())

inline fun <T : GearyAddon<Module>, reified Module: Any> install(
addon: T,
module: Module,
) {
DI.add(module)
with(addon) { module.install() }
}

fun namespace(namespace: String, configure: Namespaced.() -> Unit) {
Namespaced(namespace, TODO(), this).configure()
}
/**
* Allows defining actions that should run at a specific phase during startup
*
* Within its context, invoke a [GearyPhase] to run something during it, ex:
*
* ```
* GearyLoadPhase.ENABLE {
* // run code here
* }
* ```
*/

fun on(phase: GearyPhase, run: () -> Unit) {
geary.pipeline.intercept(phase, run)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ package com.mineinabyss.geary.modules

import co.touchlab.kermit.Logger
import com.mineinabyss.ding.DI
import com.mineinabyss.geary.addons.GearyPhase
import com.mineinabyss.geary.addons.Namespaced
import com.mineinabyss.geary.addons.dsl.GearyAddon
import com.mineinabyss.geary.addons.dsl.GearyAddonWithDefault
import com.mineinabyss.geary.addons.dsl.GearyDSL
import com.mineinabyss.geary.engine.*

Expand All @@ -31,35 +27,8 @@ interface GearyModule {
fun inject()
fun start()

// operator fun invoke(configure: GearyModule.() -> Unit)
operator fun invoke(configure: GearyConfiguration.() -> Unit) {
GearyConfiguration().apply(configure)
}
}

fun geary(configure: GearyConfiguration.() -> Unit) {
}

@GearyDSL
interface GearyConfiguration {
fun <T : GearyAddonWithDefault<Module>, Module> install(
addon: T,
) = install(addon, addon.default())

fun <T : GearyAddon<Module>, Module> install(
addon: T,
module: Module,
)

fun namespace(namespace: String, configure: Namespaced.() -> Unit) = Namespaced(namespace, TODO(), this).configure()

/**
* Allows defining actions that should run at a specific phase during startup
*
* Within its context, invoke a [GearyPhase] to run something during it, ex:
*
* ```
* GearyLoadPhase.ENABLE {
* // run code here
* }
* ```
*/
fun on(phase: GearyPhase, run: () -> Unit)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.modules.SerializersModule
import kotlin.reflect.KClass

interface Serializers {
interface ComponentSerializers {
//TODO allow this to work for all registered classes, not just components
fun getClassFor(serialName: String): KClass<out Component>
fun isRegistered(serialName: String): Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.SerializationStrategy
import okio.Path

interface PrefabFormat {
interface Format {
val ext: String

fun <T> decodeFromFile(deserializer: DeserializationStrategy<T>, path: Path): T
fun <T> encodeToFile(serializer: SerializationStrategy<T>, value: T, path: Path)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mineinabyss.geary.serialization


typealias GearySerializers = Serializers
typealias GearySerializers = ComponentSerializers
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import okio.FileSystem
val serializableComponents by DI.observe<SerializableComponents>()

interface SerializableComponents {
val serializers: Serializers
val serializers: ComponentSerializers
val formats: Formats
val fileSystem: FileSystem

companion object Plugin : GearyAddonWithDefault<SerializableComponents> {
override fun default(): SerializableComponents = object : SerializableComponents {
Expand All @@ -24,14 +23,17 @@ interface SerializableComponents {

override fun SerializableComponents.install() {
geary.pipeline.intercept(GearyPhase.INIT_COMPONENTS) {

serializers
}
}
}
}

object FileSystemAddon : GearyAddon<FileSystem> {
override fun FileSystem.install() {
TODO("Not yet implemented")
val fileSystem by DI.observe<FileSystem>()

interface FileSystemAddon {
companion object: GearyAddon<FileSystem> {
override fun FileSystem.install() {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ class SerializableComponentsDSL(
module { polymorphic(Component::class) { init() } }
}

fun format(format: (SerializersModule) -> Format) {
serializableComponents.formats.register(ext, format)
}

fun format(ext: String, format: (SerializersModule) -> PrefabFormat) {
fun format(ext: String, format: (SerializersModule) -> Format) {
serializableComponents.formats.register(ext, format)
}

Expand Down Expand Up @@ -61,4 +64,4 @@ class SerializableComponentsDSL(

@GearyDSL
fun Namespaced.serialization(configure: SerializableComponentsDSL.() -> Unit) =
gearyConf.install(SerializableComponents).also { SerializableComponentsDSL().configure() }
gearyConf.install(SerializableComponents).also { SerializableComponentsDSL(this).configure() }
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.overwriteWith
import kotlin.reflect.KClass

class SerializersByMap : Serializers {
class SerializersByMap : ComponentSerializers {
private val addonToModuleMap = mutableMapOf<String, SerializersModule>()
private val serialName2Component: MutableMap<String, KClass<out Component>> = mutableMapOf()
private val component2serialName: MutableMap<KClass<out Component>, String> = mutableMapOf()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.mineinabyss.geary.serialization.formats

import com.mineinabyss.geary.serialization.PrefabFormat
import com.mineinabyss.geary.serialization.Format
import kotlinx.serialization.cbor.Cbor
import kotlinx.serialization.modules.SerializersModule

interface Formats {
/** Gets a registered [PrefabFormat] for a file with extension [ext]. */
operator fun get(ext: String): PrefabFormat?
/** Gets a registered [Format] for a file with extension [ext]. */
operator fun get(ext: String): Format?

/** Registers a [PrefabFormat] for a file with extension [ext]. */
fun register(ext: String, format: (SerializersModule) -> PrefabFormat)
/** Registers a [Format] for a file with extension [ext]. */
fun register(ext: String, format: (SerializersModule) -> Format)

/** The format to use for encoding binary data (usually not to files) */
val binaryFormat: Cbor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.mineinabyss.geary.serialization.formats

import com.mineinabyss.geary.modules.geary
import com.mineinabyss.geary.datatypes.Component
import com.mineinabyss.geary.serialization.PrefabFormat
import com.mineinabyss.geary.serialization.Format
import com.mineinabyss.geary.serialization.serializableComponents
import kotlinx.serialization.cbor.Cbor

Expand All @@ -14,19 +14,18 @@ import kotlinx.serialization.cbor.Cbor
*/
class SimpleFormats : Formats {
private val serializers = serializableComponents.serializers
private val formatMap = mutableMapOf<String, PrefabFormat>()
private val formatMap = mutableMapOf<String, Format>()

override val binaryFormat: Cbor by lazy {
Cbor {
serializers.serializers
serializersModule = geary.serializers.module
serializersModule = serializers.module
encodeDefaults = false
}
}

override operator fun get(ext: String): PrefabFormat? = formatMap[ext]
override operator fun get(ext: String): Format? = formatMap[ext]

override fun register(ext: String, format: PrefabFormat) {
override fun register(ext: String, format: Format) {
formatMap[ext] = format
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package com.mineinabyss.serialization.formats

import com.charleskorn.kaml.Yaml
import com.charleskorn.kaml.YamlConfiguration
import com.mineinabyss.geary.serialization.PrefabFormat
import com.mineinabyss.geary.serialization.Format
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.SerializationStrategy
import kotlinx.serialization.modules.SerializersModule
import okio.FileSystem
import okio.Path

class YamlFormat(
module: SerializersModule
) : PrefabFormat {
) : Format {
override val ext = "yml"

private val yaml = Yaml(
serializersModule = module,
configuration = YamlConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import io.ktor.server.netty.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import okio.FileSystem
import okio.Path.Companion.toOkioPath
import org.bukkit.Bukkit
import java.util.*
import kotlin.io.path.isDirectory
Expand Down Expand Up @@ -62,7 +63,7 @@ class GearyPluginImpl : GearyPlugin() {
.forEach { folder ->
namespace(folder.name) {
prefabs {
fromRecursive(folder)
fromRecursive(folder.toOkioPath())
}
}
}
Expand Down

0 comments on commit 82ce31c

Please sign in to comment.