From 88bee220109664d83558af53f979c6d7bf4096e2 Mon Sep 17 00:00:00 2001 From: nikky Date: Tue, 26 Jun 2018 03:51:17 +0200 Subject: [PATCH] this really should not break --- core/src/main/kotlin/matterlink/Util.kt | 7 ++++--- core/src/main/kotlin/matterlink/config/BaseConfig.kt | 11 ++++++++--- .../main/kotlin/matterlink/config/PermissionConfig.kt | 3 +-- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/core/src/main/kotlin/matterlink/Util.kt b/core/src/main/kotlin/matterlink/Util.kt index cdd5023..a6252a2 100644 --- a/core/src/main/kotlin/matterlink/Util.kt +++ b/core/src/main/kotlin/matterlink/Util.kt @@ -56,7 +56,8 @@ val Exception.stackTraceString: String return sw.toString() } -inline fun JsonObject.getOrDefault(key: String, default: T, comment: String? = null): T { - instance.info("type: ${T::class.java.name} key: $key default: $default") - return putDefault(key, default, comment)!! +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)!! } diff --git a/core/src/main/kotlin/matterlink/config/BaseConfig.kt b/core/src/main/kotlin/matterlink/config/BaseConfig.kt index 5eeb62a..3c57638 100644 --- a/core/src/main/kotlin/matterlink/config/BaseConfig.kt +++ b/core/src/main/kotlin/matterlink/config/BaseConfig.kt @@ -10,6 +10,7 @@ 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 @@ -358,16 +359,16 @@ data class BaseConfig(val rootDir: File) { jankson.load(configFile) } catch (e: SyntaxError) { instance.error("error loading config: ${e.completeMessage}") - Marshaller.getFallback().serialize(MatterLinkConfig()) as JsonObject + jankson.marshaller.serialize(MatterLinkConfig()) as JsonObject } catch (e: FileNotFoundException) { instance.error("creating config file $configFile") configFile.createNewFile() - Marshaller.getFallback().serialize(MatterLinkConfig()) as JsonObject + jankson.marshaller.serialize(MatterLinkConfig()) as JsonObject } instance.info("finished loading $jsonObject") val tmpCfg = try { - cfgDirectory.resolve("debug.matterlink.hjson").writeText(jsonObject.toJson(false, true)) + //cfgDirectory.resolve("debug.matterlink.hjson").writeText(jsonObject.toJson(false, true)) jankson.fromJson(jsonObject, MatterLinkConfig::class.java).apply { configFile.writeText(jsonObject.toJson(true, true)) instance.info("loaded config: $this") @@ -378,6 +379,10 @@ data class BaseConfig(val rootDir: File) { } catch (e: IllegalStateException) { instance.error(e.stackTraceString) MatterLinkConfig() + } catch (e: NullPointerException) { + instance.error("error loading config: ${e.stackTraceString}") + MatterLinkConfig() + exitProcess(-1) } // val defaultJsonObject = jankson.load("{}") diff --git a/core/src/main/kotlin/matterlink/config/PermissionConfig.kt b/core/src/main/kotlin/matterlink/config/PermissionConfig.kt index dc4281e..c1d6675 100644 --- a/core/src/main/kotlin/matterlink/config/PermissionConfig.kt +++ b/core/src/main/kotlin/matterlink/config/PermissionConfig.kt @@ -4,7 +4,6 @@ import blue.endless.jankson.Jankson import blue.endless.jankson.JsonObject import blue.endless.jankson.impl.Marshaller import blue.endless.jankson.impl.SyntaxError -import matterlink.getOrDefault import matterlink.instance import java.io.File import java.io.FileNotFoundException @@ -84,7 +83,7 @@ object PermissionConfig { fun add(platform: String, userid: String, powerlevel: Double, comment: String? = null) { val platformObject = jsonObject.getObject(platform) ?: JsonObject() - platformObject.getOrDefault(userid, powerlevel, comment) + platformObject.putDefault(userid, powerlevel, comment) jsonObject[platform] = platformObject perms.clear()