-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
181 additions
and
32 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
common/src/main/java/io/github/moulberry/moulconfig/managed/GsonMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.github.moulberry.moulconfig.managed | ||
|
||
import com.google.gson.GsonBuilder | ||
import io.github.moulberry.moulconfig.observer.PropertyTypeAdapterFactory | ||
|
||
class GsonMapper<T>(val clazz: Class<T>) : DataMapper<T> { | ||
val gsonBuilder = GsonBuilder() | ||
.registerTypeAdapterFactory(PropertyTypeAdapterFactory()) | ||
private val gson by lazy { gsonBuilder.create() } | ||
override fun serialize(value: T): String { | ||
return gson.toJson(value) | ||
} | ||
|
||
override fun createDefault(): T { | ||
return clazz.newInstance() | ||
} | ||
|
||
override fun deserialize(string: String): T { | ||
return gson.fromJson(string, clazz) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 16 additions & 16 deletions
32
common/src/main/java/io/github/moulberry/moulconfig/managed/ManagedDataFileBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,30 @@ | ||
package io.github.moulberry.moulconfig.managed | ||
|
||
import java.io.File | ||
import java.util.function.BiConsumer | ||
import java.util.function.Consumer | ||
|
||
class ManagedDataFileBuilder<T> internal constructor( | ||
open class ManagedDataFileBuilder<T>( | ||
var file: File, | ||
var mapper: DataMapper<T>, | ||
val clazz: Class<T> | ||
) { | ||
|
||
fun throwOnFailure() { | ||
loadFailed = Consumer { throw it } | ||
saveFailed = Consumer { throw it } | ||
loadFailed = BiConsumer { _, ex -> throw ex } | ||
saveFailed = BiConsumer { _, ex -> throw ex } | ||
} | ||
|
||
var loadFailed: Consumer<Exception> = Consumer {} | ||
var saveFailed: Consumer<Exception> = Consumer {} | ||
var beforeLoad: Runnable = Runnable {} | ||
var afterLoad: Runnable = Runnable {} | ||
var beforeSave: Runnable = Runnable {} | ||
var afterSave: Runnable = Runnable {} | ||
var mapper: DataMapper<T> = GsonMapper(clazz) | ||
|
||
fun build(): ManagedDataFile<T> { | ||
return ManagedDataFile( | ||
file, | ||
mapper, | ||
loadFailed, saveFailed, beforeLoad, afterLoad, beforeSave, afterSave | ||
) | ||
@JvmOverloads | ||
fun jsonMapper(function: GsonMapper<T>.() -> Unit = {}) { | ||
mapper = GsonMapper(clazz).also(function) | ||
} | ||
|
||
open var loadFailed: BiConsumer<ManagedDataFile<T>, Exception> = BiConsumer { _, _ -> } | ||
open var saveFailed: BiConsumer<ManagedDataFile<T>, Exception> = BiConsumer { _, _ -> } | ||
open var beforeLoad: Consumer<ManagedDataFile<T>> = Consumer {} | ||
open var afterLoad: Consumer<ManagedDataFile<T>> = Consumer {} | ||
open var beforeSave: Consumer<ManagedDataFile<T>> = Consumer {} | ||
open var afterSave: Consumer<ManagedDataFile<T>> = Consumer {} | ||
} |
78 changes: 78 additions & 0 deletions
78
legacy/src/main/java/io/github/moulberry/moulconfig/managed/ManagedConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package io.github.moulberry.moulconfig.managed | ||
|
||
import io.github.moulberry.moulconfig.Config | ||
import io.github.moulberry.moulconfig.gui.GuiOptionEditor | ||
import io.github.moulberry.moulconfig.gui.GuiScreenElementWrapper | ||
import io.github.moulberry.moulconfig.gui.MoulConfigEditor | ||
import io.github.moulberry.moulconfig.processor.BuiltinMoulConfigGuis | ||
import io.github.moulberry.moulconfig.processor.ConfigProcessorDriver | ||
import io.github.moulberry.moulconfig.processor.MoulConfigProcessor | ||
import io.github.moulberry.moulconfig.processor.ProcessedOption | ||
import net.minecraft.client.Minecraft | ||
import net.minecraft.client.gui.GuiScreen | ||
import java.io.File | ||
import java.util.function.BiFunction | ||
import java.util.function.Consumer | ||
|
||
class ManagedConfig<T : Config>(private val builder: ManagedConfigBuilder<T>) : ManagedDataFile<T>(builder.apply { | ||
afterLoad = Consumer<ManagedDataFile<T>> { (it as ManagedConfig<T>).rebuildConfigProcessor(builder) } | ||
.andThen(afterLoad) | ||
}) { | ||
|
||
companion object { | ||
@JvmStatic | ||
@JvmOverloads | ||
fun <T : Config> create( | ||
file: File, | ||
clazz: Class<T>, | ||
consumer: (ManagedConfigBuilder<T>.() -> Unit) = {} | ||
): ManagedConfig<T> { | ||
return ManagedConfig(ManagedConfigBuilder(file, clazz).apply(consumer)) | ||
} | ||
} | ||
|
||
lateinit var processor: MoulConfigProcessor<T> | ||
private set | ||
|
||
|
||
fun rebuildConfigProcessor() { | ||
rebuildConfigProcessor(builder) | ||
} | ||
private fun rebuildConfigProcessor(builder: ManagedConfigBuilder<T>) { | ||
processor = buildProcessor(builder) | ||
} | ||
|
||
/** | ||
* Helper function to introduce the A type parameter so that two objects can be cast to be that same A variable. | ||
*/ | ||
@Suppress("NOTHING_TO_INLINE") | ||
private inline fun <A : Annotation> cast(processor: MoulConfigProcessor<T>, annotation: Class<A>, method: Any) { | ||
@Suppress("UNCHECKED_CAST") | ||
processor.registerConfigEditor(annotation, method as BiFunction<ProcessedOption, A, GuiOptionEditor>) | ||
} | ||
|
||
private fun buildProcessor(builder: ManagedConfigBuilder<T>): MoulConfigProcessor<T> { | ||
val processor = MoulConfigProcessor(this.instance) | ||
if (builder.useDefaultProcessors) { | ||
BuiltinMoulConfigGuis.addProcessors(processor) | ||
} | ||
builder.customProcessors.forEach { (annotation, method) -> | ||
cast(processor, annotation, method) | ||
} | ||
ConfigProcessorDriver.processConfig(this.instance.javaClass, this.instance, processor) | ||
return processor | ||
} | ||
|
||
fun getEditor(): MoulConfigEditor<T> { | ||
return MoulConfigEditor(processor) | ||
} | ||
|
||
fun getGui(): GuiScreen { | ||
return GuiScreenElementWrapper(getEditor()) | ||
} | ||
|
||
fun openConfigGui() { | ||
Minecraft.getMinecraft().displayGuiScreen(getGui()) | ||
} | ||
} | ||
|
34 changes: 34 additions & 0 deletions
34
legacy/src/main/java/io/github/moulberry/moulconfig/managed/ManagedConfigBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.github.moulberry.moulconfig.managed | ||
|
||
import io.github.moulberry.moulconfig.gui.GuiOptionEditor | ||
import io.github.moulberry.moulconfig.processor.ProcessedOption | ||
import java.io.File | ||
import java.util.function.BiFunction | ||
|
||
class ManagedConfigBuilder<T>(file: File, clazz: Class<T>) : ManagedDataFileBuilder<T>(file, clazz) { | ||
var useDefaultProcessors = true | ||
internal val customProcessors = | ||
mutableListOf<Pair<Class<out Annotation>, BiFunction<ProcessedOption, Annotation, GuiOptionEditor>>>() | ||
|
||
fun clearCustomProcessors() { | ||
customProcessors.clear() | ||
} | ||
|
||
inline fun <reified A : Annotation> customProcessor(noinline editorGenerator: (ProcessedOption, A) -> GuiOptionEditor) { | ||
customProcessor(A::class.java, editorGenerator) | ||
} | ||
|
||
fun <A : Annotation> customProcessor( | ||
annotation: Class<A>, | ||
editorGenerator: BiFunction<ProcessedOption, in A, GuiOptionEditor> | ||
) { | ||
@Suppress("UNCHECKED_CAST") | ||
customProcessors.add( | ||
Pair( | ||
annotation, | ||
editorGenerator as BiFunction<ProcessedOption, Annotation, GuiOptionEditor> | ||
) | ||
) | ||
} | ||
|
||
} |