diff --git a/Jankson b/Jankson index 90b600e..8771387 160000 --- a/Jankson +++ b/Jankson @@ -1 +1 @@ -Subproject commit 90b600e2acdbba48a9a3c39d85a49a9ec24e8db1 +Subproject commit 8771387a0568da140879c0104da32ba58e3bb717 diff --git a/core/src/main/kotlin/matterlink/Util.kt b/core/src/main/kotlin/matterlink/Util.kt index a6252a2..873adee 100644 --- a/core/src/main/kotlin/matterlink/Util.kt +++ b/core/src/main/kotlin/matterlink/Util.kt @@ -57,7 +57,6 @@ val Exception.stackTraceString: String } fun JsonObject.getOrDefault(key: String, default: T, comment: String? = null): T { - instance.info("type: ${default.javaClass.name} key: $key json: >>>${this.getObject(key)?.toJson()}<<< default: $default") - putDefault(key, default, comment)!! - return get(default.javaClass, key)!! +// instance.info("type: ${default.javaClass.name} key: $key json: >>>${this.getObject(key)?.toJson()}<<< default: $default") + return putDefault(key, default, comment)!! } diff --git a/core/src/main/kotlin/matterlink/bridge/command/PermCommand.kt b/core/src/main/kotlin/matterlink/bridge/command/PermCommand.kt index 2bf5a1b..4b5f552 100644 --- a/core/src/main/kotlin/matterlink/bridge/command/PermCommand.kt +++ b/core/src/main/kotlin/matterlink/bridge/command/PermCommand.kt @@ -36,7 +36,7 @@ object PermCommand : IBridgeCommand { if(currentPowerlevel < 0.0) { MessageHandlerInst.transmit( ApiMessage( - text = "Your poermission level is $currentPowerlevel seems like someone banned you from making any more requests" + text = "Your level is $currentPowerlevel seems like someone banned you from making any more requests" ) ) return true diff --git a/core/src/main/kotlin/matterlink/config/BaseConfig.kt b/core/src/main/kotlin/matterlink/config/BaseConfig.kt index 3c57638..bcb92ae 100644 --- a/core/src/main/kotlin/matterlink/config/BaseConfig.kt +++ b/core/src/main/kotlin/matterlink/config/BaseConfig.kt @@ -10,7 +10,6 @@ import matterlink.instance import matterlink.stackTraceString import java.io.File import java.io.FileNotFoundException -import kotlin.system.exitProcess lateinit var cfg: BaseConfig.MatterLinkConfig lateinit var baseCfg: BaseConfig @@ -374,15 +373,18 @@ data class BaseConfig(val rootDir: File) { instance.info("loaded config: $this") } } catch (e: SyntaxError) { - instance.error("error parsing config: ${e.completeMessage} ${e.stackTraceString}") + instance.error("error parsing config: ${e.completeMessage} ") + instance.error(e.stackTraceString) + cfgDirectory.resolve("error.matterlink.hjson").writeText(jsonObject.toJson(false, true)) MatterLinkConfig() } catch (e: IllegalStateException) { instance.error(e.stackTraceString) + cfgDirectory.resolve("error.matterlink.hjson").writeText(jsonObject.toJson(false, true)) MatterLinkConfig() } catch (e: NullPointerException) { instance.error("error loading config: ${e.stackTraceString}") + cfgDirectory.resolve("error.matterlink.hjson").writeText(jsonObject.toJson(false, true)) MatterLinkConfig() - exitProcess(-1) } // val defaultJsonObject = jankson.load("{}") diff --git a/core/src/main/kotlin/matterlink/config/CommandConfig.kt b/core/src/main/kotlin/matterlink/config/CommandConfig.kt index e93330c..cb3734a 100644 --- a/core/src/main/kotlin/matterlink/config/CommandConfig.kt +++ b/core/src/main/kotlin/matterlink/config/CommandConfig.kt @@ -6,6 +6,7 @@ import blue.endless.jankson.JsonPrimitive import blue.endless.jankson.impl.SyntaxError import matterlink.bridge.command.CommandType import matterlink.bridge.command.CustomCommand +import matterlink.getOrDefault import matterlink.instance import java.io.File import java.io.FileNotFoundException @@ -111,7 +112,7 @@ object CommandConfig { jsonObject.forEach { key, element -> instance.trace("loading command '$key'") val command = jsonObject.get(CustomCommand::class.java, key) - if(command != null) + if (command != null) commands[key] = command else { instance.error("could not parse key: $key, value: '$element' as CustomCommand") @@ -124,20 +125,22 @@ object CommandConfig { val command = commands[k] if (command == null || command.defaultCommand == true) { commands[k] = defCommand - val element = jankson.marshaller.serialize(defCommand) - jsonObject.putDefault(k, element, comment) + jsonObject.getOrDefault(k, defCommand, comment) } } instance.debug("loaded jsonObj: $jsonObject") instance.debug("loaded commandMap: $commands") - val nonDefaultJsonObj = jsonObject.getDelta(jankson.marshaller.serialize(default.mapValues { it.value.second }) as JsonObject) - + val defaultJsonObject = jankson.marshaller.serialize(CustomCommand.DEFAULT) as JsonObject + val nonDefaultJsonObj = jsonObject.clone() + jsonObject.forEach { key, element -> + if (element is JsonObject) { + nonDefaultJsonObj[key] = element.getDelta(defaultJsonObject) + } + } configFile.writeText(nonDefaultJsonObj.toJson(true, true)) return true } - - } \ No newline at end of file diff --git a/core/src/main/kotlin/matterlink/config/PermissionConfig.kt b/core/src/main/kotlin/matterlink/config/PermissionConfig.kt index c1d6675..02bebd4 100644 --- a/core/src/main/kotlin/matterlink/config/PermissionConfig.kt +++ b/core/src/main/kotlin/matterlink/config/PermissionConfig.kt @@ -41,51 +41,41 @@ object PermissionConfig { fun loadPermFile(): Boolean { permissionRequests.clear() + val defaultJsonObject = JsonObject().apply { + default.forEach { platform, userMap -> + val jsonUserMap = this.getOrDefault(platform, JsonObject()) + if (jsonUserMap is JsonObject) { + userMap.forEach { user, (powerlevel, comment) -> + instance.trace("loading platform: $platform user: $user powerlevel: $powerlevel") + val element = Marshaller.getFallback().serialize(powerlevel) + jsonUserMap.putDefault(user, element, comment.takeUnless { it.isBlank() }) + } + this[platform] = jsonUserMap + } else { + instance.error("cannot parse platform: $platform , value: '$jsonUserMap' as Map, skipping") + } + } + } + + var save = true jsonObject = try { jankson.load(configFile) } catch (e: SyntaxError) { instance.error("error parsing config: ${e.completeMessage}") - JsonObject() + save = false + defaultJsonObject } catch (e: FileNotFoundException) { + instance.error("cannot find config: $configFile .. creating sample permissions mapping") configFile.createNewFile() - JsonObject() + defaultJsonObject } - default.forEach { platform, userMap -> - val jsonUserMap = jsonObject.getOrDefault(platform, JsonObject()) - if(jsonUserMap is JsonObject) { - userMap.forEach { user, (powerlevel, comment) -> - instance.trace("loading platform: $platform user: $user powerlevel: $powerlevel") - val element = Marshaller.getFallback().serialize(powerlevel) - jsonUserMap.putDefault(user, element, comment.takeUnless { it.isBlank() }) - } - jsonObject[platform] = jsonUserMap - } else { - instance.error("cannot parse platform: $platform , value: '$jsonUserMap' as Map, skipping") - } - } - - jsonObject.forEach { platform, jsonUserMap -> - val userMap = perms[platform] ?: mutableMapOf() - if (jsonUserMap is JsonObject) { - jsonUserMap.forEach { user, powerlevel -> - instance.info("$platform $user $powerlevel") - userMap[user] = jsonUserMap.get(Double::class.java, user) ?: 0.0 - } - } - perms[platform] = userMap - } - - configFile.writeText(jsonObject.toJson(true, true)) + load(save) return true } - fun add(platform: String, userid: String, powerlevel: Double, comment: String? = null) { - val platformObject = jsonObject.getObject(platform) ?: JsonObject() - platformObject.putDefault(userid, powerlevel, comment) - jsonObject[platform] = platformObject - + private fun load(save: Boolean = true) { perms.clear() jsonObject.forEach { platform, jsonUserMap -> val userMap = perms[platform] ?: mutableMapOf() @@ -99,6 +89,15 @@ object PermissionConfig { } instance.info("Permissions reloaded") - configFile.writeText(jsonObject.toJson(true, true)) + if (save) + configFile.writeText(jsonObject.toJson(true, true)) + } + + fun add(platform: String, userid: String, powerlevel: Double, comment: String? = null) { + val platformObject = jsonObject.getObject(platform) ?: JsonObject() + platformObject.putDefault(userid, powerlevel, comment) + jsonObject[platform] = platformObject + + load() } } \ No newline at end of file