fix NPE in Jankson and finetune hjson loading

This commit is contained in:
nikky 2018-06-26 19:08:36 +02:00
parent 88bee22010
commit 1356e3682f
6 changed files with 52 additions and 49 deletions

@ -1 +1 @@
Subproject commit 90b600e2acdbba48a9a3c39d85a49a9ec24e8db1 Subproject commit 8771387a0568da140879c0104da32ba58e3bb717

View File

@ -57,7 +57,6 @@ val Exception.stackTraceString: String
} }
fun <T : Any> JsonObject.getOrDefault(key: String, default: T, comment: String? = null): T { fun <T : Any> 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") // instance.info("type: ${default.javaClass.name} key: $key json: >>>${this.getObject(key)?.toJson()}<<< default: $default")
putDefault(key, default, comment)!! return putDefault(key, default, comment)!!
return get(default.javaClass, key)!!
} }

View File

@ -36,7 +36,7 @@ object PermCommand : IBridgeCommand {
if(currentPowerlevel < 0.0) { if(currentPowerlevel < 0.0) {
MessageHandlerInst.transmit( MessageHandlerInst.transmit(
ApiMessage( 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 return true

View File

@ -10,7 +10,6 @@ import matterlink.instance
import matterlink.stackTraceString import matterlink.stackTraceString
import java.io.File import java.io.File
import java.io.FileNotFoundException import java.io.FileNotFoundException
import kotlin.system.exitProcess
lateinit var cfg: BaseConfig.MatterLinkConfig lateinit var cfg: BaseConfig.MatterLinkConfig
lateinit var baseCfg: BaseConfig lateinit var baseCfg: BaseConfig
@ -374,15 +373,18 @@ data class BaseConfig(val rootDir: File) {
instance.info("loaded config: $this") instance.info("loaded config: $this")
} }
} catch (e: SyntaxError) { } 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() MatterLinkConfig()
} catch (e: IllegalStateException) { } catch (e: IllegalStateException) {
instance.error(e.stackTraceString) instance.error(e.stackTraceString)
cfgDirectory.resolve("error.matterlink.hjson").writeText(jsonObject.toJson(false, true))
MatterLinkConfig() MatterLinkConfig()
} catch (e: NullPointerException) { } catch (e: NullPointerException) {
instance.error("error loading config: ${e.stackTraceString}") instance.error("error loading config: ${e.stackTraceString}")
cfgDirectory.resolve("error.matterlink.hjson").writeText(jsonObject.toJson(false, true))
MatterLinkConfig() MatterLinkConfig()
exitProcess(-1)
} }
// val defaultJsonObject = jankson.load("{}") // val defaultJsonObject = jankson.load("{}")

View File

@ -6,6 +6,7 @@ import blue.endless.jankson.JsonPrimitive
import blue.endless.jankson.impl.SyntaxError import blue.endless.jankson.impl.SyntaxError
import matterlink.bridge.command.CommandType import matterlink.bridge.command.CommandType
import matterlink.bridge.command.CustomCommand import matterlink.bridge.command.CustomCommand
import matterlink.getOrDefault
import matterlink.instance import matterlink.instance
import java.io.File import java.io.File
import java.io.FileNotFoundException import java.io.FileNotFoundException
@ -111,7 +112,7 @@ object CommandConfig {
jsonObject.forEach { key, element -> jsonObject.forEach { key, element ->
instance.trace("loading command '$key'") instance.trace("loading command '$key'")
val command = jsonObject.get(CustomCommand::class.java, key) val command = jsonObject.get(CustomCommand::class.java, key)
if(command != null) if (command != null)
commands[key] = command commands[key] = command
else { else {
instance.error("could not parse key: $key, value: '$element' as CustomCommand") instance.error("could not parse key: $key, value: '$element' as CustomCommand")
@ -124,20 +125,22 @@ object CommandConfig {
val command = commands[k] val command = commands[k]
if (command == null || command.defaultCommand == true) { if (command == null || command.defaultCommand == true) {
commands[k] = defCommand commands[k] = defCommand
val element = jankson.marshaller.serialize(defCommand) jsonObject.getOrDefault(k, defCommand, comment)
jsonObject.putDefault(k, element, comment)
} }
} }
instance.debug("loaded jsonObj: $jsonObject") instance.debug("loaded jsonObj: $jsonObject")
instance.debug("loaded commandMap: $commands") 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)) configFile.writeText(nonDefaultJsonObj.toJson(true, true))
return true return true
} }
} }

View File

@ -41,51 +41,41 @@ object PermissionConfig {
fun loadPermFile(): Boolean { fun loadPermFile(): Boolean {
permissionRequests.clear() permissionRequests.clear()
jsonObject = try { val defaultJsonObject = JsonObject().apply {
jankson.load(configFile)
} catch (e: SyntaxError) {
instance.error("error parsing config: ${e.completeMessage}")
JsonObject()
} catch (e: FileNotFoundException) {
configFile.createNewFile()
JsonObject()
}
default.forEach { platform, userMap -> default.forEach { platform, userMap ->
val jsonUserMap = jsonObject.getOrDefault(platform, JsonObject()) val jsonUserMap = this.getOrDefault(platform, JsonObject())
if(jsonUserMap is JsonObject) { if (jsonUserMap is JsonObject) {
userMap.forEach { user, (powerlevel, comment) -> userMap.forEach { user, (powerlevel, comment) ->
instance.trace("loading platform: $platform user: $user powerlevel: $powerlevel") instance.trace("loading platform: $platform user: $user powerlevel: $powerlevel")
val element = Marshaller.getFallback().serialize(powerlevel) val element = Marshaller.getFallback().serialize(powerlevel)
jsonUserMap.putDefault(user, element, comment.takeUnless { it.isBlank() }) jsonUserMap.putDefault(user, element, comment.takeUnless { it.isBlank() })
} }
jsonObject[platform] = jsonUserMap this[platform] = jsonUserMap
} else { } else {
instance.error("cannot parse platform: $platform , value: '$jsonUserMap' as Map, skipping") 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)) var save = true
jsonObject = try {
jankson.load(configFile)
} catch (e: SyntaxError) {
instance.error("error parsing config: ${e.completeMessage}")
save = false
defaultJsonObject
} catch (e: FileNotFoundException) {
instance.error("cannot find config: $configFile .. creating sample permissions mapping")
configFile.createNewFile()
defaultJsonObject
}
load(save)
return true return true
} }
fun add(platform: String, userid: String, powerlevel: Double, comment: String? = null) { private fun load(save: Boolean = true) {
val platformObject = jsonObject.getObject(platform) ?: JsonObject()
platformObject.putDefault(userid, powerlevel, comment)
jsonObject[platform] = platformObject
perms.clear() perms.clear()
jsonObject.forEach { platform, jsonUserMap -> jsonObject.forEach { platform, jsonUserMap ->
val userMap = perms[platform] ?: mutableMapOf() val userMap = perms[platform] ?: mutableMapOf()
@ -99,6 +89,15 @@ object PermissionConfig {
} }
instance.info("Permissions reloaded") instance.info("Permissions reloaded")
if (save)
configFile.writeText(jsonObject.toJson(true, true)) 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()
}
} }