diff --git a/main/src/mindustry/model/ModMeta.kt b/main/src/mindustry/model/ModMeta.kt index 6785fbb..9da8e34 100644 --- a/main/src/mindustry/model/ModMeta.kt +++ b/main/src/mindustry/model/ModMeta.kt @@ -4,6 +4,7 @@ package io.github.liplum.mindustry import groovy.json.JsonOutput import io.github.liplum.dsl.toMutableMap +import io.github.liplum.mindustry.ModMeta.Companion.fillMissingWithDefault import org.hjson.JsonObject import org.hjson.Stringify import java.io.File @@ -86,9 +87,28 @@ class ModMeta private constructor( info[property] = value } - /** - * [ModMeta.toHjson] - */ +// fun loadFrom(path: String) { +// loadFrom(File(path)) +// } +// +// fun loadFrom(file: File) { +// runCatching { +// +// }.getOrElse { ModMeta() } +// } + + fun fillMissingWith(other: ModMeta) { + this.info.fillMissingWith(other) + } + + fun fillMissingWith(info: Map) { + this.info.fillMissingWith(info) + } + + fun fillMissingWithDefault() { + this.info.fillMissingWith(defaultMeta) + } + override fun toString(): String { return toHjson() } @@ -122,15 +142,15 @@ class ModMeta private constructor( @JvmStatic fun by(vararg metas: Map.Entry) = - ModMeta(HashMap(metas.associate { Pair(it.key, it.value) }).fillDefaultValue()) + ModMeta(HashMap(metas.associate { Pair(it.key, it.value) }).fillMissingWithDefault()) @JvmStatic fun by(vararg metas: Pair) = - ModMeta(mutableMapOf(*metas).fillDefaultValue()) + ModMeta(mutableMapOf(*metas).fillMissingWithDefault()) @JvmStatic fun fromHjson(hjson: String): ModMeta = - ModMeta(JsonObject.readHjson(hjson).toMutableMap().fillDefaultValue()) + ModMeta(JsonObject.readHjson(hjson).toMutableMap().fillMissingWithDefault()) @JvmStatic fun fromHjson(file: File): ModMeta = @@ -145,8 +165,20 @@ class ModMeta private constructor( @JvmStatic internal - fun T.fillDefaultValue(): T where T : MutableMap { - for ((dk, dv) in defaultMeta) { + fun T.fillMissingWithDefault(): T where T : MutableMap { + return fillMissingWith(defaultMeta) + } + + @JvmStatic + internal + fun T.fillMissingWith(other: ModMeta): T where T : MutableMap { + return fillMissingWith(other.info) + } + + @JvmStatic + internal + fun T.fillMissingWith(info: Map): T where T : MutableMap { + for ((dk, dv) in info) { this.putIfAbsent(dk, dv) } return this diff --git a/main/src/task/ModHjsonGenerate.kt b/main/src/task/ModHjsonGenerate.kt index 4baddef..205b56d62 100644 --- a/main/src/task/ModHjsonGenerate.kt +++ b/main/src/task/ModHjsonGenerate.kt @@ -12,7 +12,7 @@ import org.gradle.api.tasks.TaskAction open class ModHjsonGenerate : DefaultTask() { val modMeta = project.prop() - @Input @Optional get + @Input get val output = project.fileProp() @OutputFile get