Skip to content

Commit

Permalink
more features for ModMeta
Browse files Browse the repository at this point in the history
  • Loading branch information
liplum committed Dec 21, 2023
1 parent 2175746 commit 4b2c2b1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
48 changes: 40 additions & 8 deletions main/src/mindustry/model/ModMeta.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<String, Any?>) {
this.info.fillMissingWith(info)
}

fun fillMissingWithDefault() {
this.info.fillMissingWith(defaultMeta)
}

override fun toString(): String {
return toHjson()
}
Expand Down Expand Up @@ -122,15 +142,15 @@ class ModMeta private constructor(

@JvmStatic
fun by(vararg metas: Map.Entry<String, Any?>) =
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<String, Any?>) =
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 =
Expand All @@ -145,8 +165,20 @@ class ModMeta private constructor(

@JvmStatic
internal
fun <T> T.fillDefaultValue(): T where T : MutableMap<String, Any?> {
for ((dk, dv) in defaultMeta) {
fun <T> T.fillMissingWithDefault(): T where T : MutableMap<String, Any?> {
return fillMissingWith(defaultMeta)
}

@JvmStatic
internal
fun <T> T.fillMissingWith(other: ModMeta): T where T : MutableMap<String, Any?> {
return fillMissingWith(other.info)
}

@JvmStatic
internal
fun <T> T.fillMissingWith(info: Map<String, Any?>): T where T : MutableMap<String, Any?> {
for ((dk, dv) in info) {
this.putIfAbsent(dk, dv)
}
return this
Expand Down
2 changes: 1 addition & 1 deletion main/src/task/ModHjsonGenerate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.gradle.api.tasks.TaskAction

open class ModHjsonGenerate : DefaultTask() {
val modMeta = project.prop<ModMeta>()
@Input @Optional get
@Input get
val output = project.fileProp()
@OutputFile get

Expand Down

0 comments on commit 4b2c2b1

Please sign in to comment.