Bläddra i källkod

fix bugs in data system

00ll00 5 år sedan
förälder
incheckning
e27e3ac405

+ 13 - 2
src/main/kotlin/bot_luo_core/cli/CmdHandler.kt

@@ -2,6 +2,7 @@ package bot_luo_core.cli
 
 import bot_luo_core.bot.VirtualMessageEvent
 import bot_luo_core.cli.Checker.Companion.order
+import bot_luo_core.cli.commands.DumpFileCmd
 import bot_luo_core.cli.exceptions.*
 import bot_luo_core.data.*
 import bot_luo_core.data.Config.CMD_PREFIX
@@ -93,7 +94,17 @@ object CmdHandler {
         when (context.uploadOutputFile) {
             0 -> {  //不自动上传
                 if (context.getOutput().content.length > MAX_OUTPUT_LEN) {
-                    context.sendOutputWithLog(context.getOutput().limitEnd(MAX_OUTPUT_LEN) + "\n输出过大,考虑使用dumpfile命令获取输出")
+                    val canDump = try {
+                        val cmd = CmdCatalog.findCmdEx("dumpfile","")[0]
+                        cmd.checkers.forEach { it.createInstance().check(cmd, context) }
+                        true
+                    } catch (ignore: CheckerFatal) {
+                        false
+                    }
+                    if (canDump)
+                        context.sendOutputWithLog(context.getOutput().limitEnd(MAX_OUTPUT_LEN) + "\n输出过大,考虑使用dumpfile命令获取输出")
+                    else
+                        context.sendOutputWithLog(context.getOutput().limitEnd(MAX_OUTPUT_LEN))
                 } else {
                     context.sendOutputWithLog()
                 }
@@ -144,7 +155,7 @@ object CmdHandler {
 
                     val res = cmd.executeWith(context, args)
 
-                    Logger.log(cmd, context, "执行完成:${res.success}")
+                    Logger.log(cmd, context, "执行完成:${res}")
 
                     runBlocking { withLockedAccessing(context.user, context.group, Cmds) {
                         if (res.addCount) {

+ 4 - 3
src/main/kotlin/bot_luo_core/cli/commands/DataCmd.kt

@@ -11,6 +11,7 @@ import bot_luo_core.cli.handlers.JsonPathArgHandler
 import bot_luo_core.cli.handlers.UserArgHandler
 import bot_luo_core.data.*
 import bot_luo_core.util.TableBuilder
+import bot_luo_core.util.indices
 import bot_luo_core.util.set
 import com.github.salomonbrys.kotson.add
 import com.github.salomonbrys.kotson.keys
@@ -243,7 +244,7 @@ class DataCmd(context: CmdContext) : Cmd(context) {
         }
 
         private fun arrayGet(array: JsonArray, table: TableBuilder) {
-            for (i in 0 until array.size()) {
+            for (i in array.indices) {
                 table.tr("[$i]").td(formatValue(array[i]))
             }
         }
@@ -276,7 +277,7 @@ class DataCmd(context: CmdContext) : Cmd(context) {
                     }
                     is JsonArray -> {
                         val index = path[0].toIntOrNull() ?: throw NullPointerException()
-                        if (index !in 0 until obj.size()) throw IndexOutOfBoundsException()
+                        if (index !in obj.indices) throw IndexOutOfBoundsException()
                         readByPath(obj[index], path.apply { removeAt(0) }, table)
                     }
                     else -> throw NullPointerException()
@@ -334,7 +335,7 @@ class DataCmd(context: CmdContext) : Cmd(context) {
                         }
                         is JsonArray -> {
                             val index = path[0].toIntOrNull() ?: throw NullPointerException()
-                            if (index !in 0 until obj.size()) throw IndexOutOfBoundsException()
+                            if (index !in obj.indices) throw IndexOutOfBoundsException()
                             writeByPathTyped(obj[index], path.apply { removeAt(0) }, value)
                         }
                         else -> throw NullPointerException()

+ 2 - 1
src/main/kotlin/bot_luo_core/data/DataArray.kt

@@ -1,6 +1,7 @@
 package bot_luo_core.data
 
 import bot_luo_core.util.GSON
+import bot_luo_core.util.clear
 import com.github.salomonbrys.kotson.removeAll
 import com.google.gson.Gson
 import com.google.gson.JsonArray
@@ -97,7 +98,7 @@ abstract class DataArray(
         changed = true
     }
     fun <T: List<*>> setArray(value: T) {
-        element.removeAll()
+        element.clear()
         value.forEach { v -> element.add(GSON.toJsonTree(v)) }
         changed = true
     }

+ 2 - 1
src/main/kotlin/bot_luo_core/data/DataObject.kt

@@ -1,6 +1,7 @@
 package bot_luo_core.data
 
 import bot_luo_core.util.GSON
+import bot_luo_core.util.clear
 import bot_luo_core.util.set
 import com.github.salomonbrys.kotson.put
 import com.github.salomonbrys.kotson.putAll
@@ -100,7 +101,7 @@ abstract class DataObject(
         changed = true
     }
     fun <T: Map<String, *>> setObj(value: T?) {
-        element.removeAll()
+        element.clear()
         value?.forEach{ (k,v) -> element.put(k to GSON.toJsonTree(v)) }
         changed = true
     }

+ 11 - 0
src/main/kotlin/bot_luo_core/util/GsonHelper.kt

@@ -1,5 +1,6 @@
 package bot_luo_core.util
 
+import com.github.salomonbrys.kotson.keys
 import com.google.gson.Gson
 import com.google.gson.GsonBuilder
 import com.google.gson.JsonArray
@@ -19,6 +20,16 @@ operator fun JsonArray.set(key: Int, value: Any?) {
     this.set(key, GSON.toJsonTree(value))
 }
 
+fun JsonObject.clear() {
+    for (k in keys()) remove(k)
+}
+
+fun JsonArray.clear() {
+    for (i in indices.reversed()) remove(i)
+}
+
+val JsonArray.indices get() = 0 until size()
+
 /**
  * # JSON数据读写工具类
  */