diff --git a/src/main/kotlin/EditorConstants.kt b/src/main/kotlin/EditorConstants.kt index 1b21830..dcde34b 100644 --- a/src/main/kotlin/EditorConstants.kt +++ b/src/main/kotlin/EditorConstants.kt @@ -4,7 +4,7 @@ import javax.swing.* import java.awt.event.* object EditorConstants { - var BUILD_NUMBER = "2.0.1" + var BUILD_NUMBER = "2.1" var DARK_MODE = true diff --git a/src/main/kotlin/Editors.kt b/src/main/kotlin/Editors.kt index 8c67b32..3a3e2a8 100644 --- a/src/main/kotlin/Editors.kt +++ b/src/main/kotlin/Editors.kt @@ -62,13 +62,7 @@ enum class Editors(val data: EditorData) { t.put("description",table.description) dtables.add(t) } - - - val manager = ScriptEngineManager() - val scriptEngine = manager.getEngineByName("JavaScript") - scriptEngine.put("jsonString", dtables.toJSONString()) - scriptEngine.eval("result = JSON.stringify(JSON.parse(jsonString), null, 2)") - val prettyPrintedJson = scriptEngine["result"] as String + val prettyPrintedJson = generateJSON(dtables) try { FileWriter(EditorConstants.CONFIG_PATH + File.separator + fileName).use { file -> @@ -108,7 +102,6 @@ enum class Editors(val data: EditorData) { override fun save() { val array = JSONArray() TableData.npcConfigs.forEach { array.add(it) } - val prettyPrintedJson = generateJSON(array) try { @@ -145,7 +138,6 @@ enum class Editors(val data: EditorData) { TableData.itemConfigs.forEach { array.add(it) } val prettyPrintedJson = generateJSON(array) - try { FileWriter(EditorConstants.CONFIG_PATH + File.separator + fileName).use { file -> file.write(prettyPrintedJson) @@ -170,16 +162,7 @@ enum class Editors(val data: EditorData) { override fun save() { val array = JSONArray() TableData.objConfigs.forEach { array.add(it) } - val sortedList = array - .map { it as JSONObject } - .sortedBy { (it["ids"] as String) } - val sortedArray = JSONArray() - sortedList.forEach { sortedArray.add(it) } - val manager = ScriptEngineManager() - val scriptEngine = manager.getEngineByName("JavaScript") - scriptEngine.put("jsonString", sortedArray.toJSONString()) - scriptEngine.eval("result = JSON.stringify(JSON.parse(jsonString), null, 2)") - val prettyPrintedJson = scriptEngine["result"] as String + val prettyPrintedJson = generateJSON(array) try { FileWriter(EditorConstants.CONFIG_PATH + File.separator + fileName).use { file -> @@ -238,13 +221,7 @@ enum class Editors(val data: EditorData) { sh.put("force_shared", shop.forceShared.toString()) shs.add(sh) } - - - val manager = ScriptEngineManager() - val scriptEngine = manager.getEngineByName("JavaScript") - scriptEngine.put("jsonString", shs.toJSONString()) - scriptEngine.eval("result = JSON.stringify(JSON.parse(jsonString), null, 2)") - val prettyPrintedJson = scriptEngine["result"] as String + val prettyPrintedJson = generateJSON(shs) try { FileWriter(EditorConstants.CONFIG_PATH + File.separator + fileName).use { file -> @@ -300,11 +277,7 @@ enum class Editors(val data: EditorData) { spawn["loc_data"] = spawnString.toString() spawnsJsonArray.add(spawn) } - val manager = ScriptEngineManager() - val scriptEngine = manager.getEngineByName("JavaScript") - scriptEngine.put("jsonString", spawnsJsonArray.toJSONString()) - scriptEngine.eval("result = JSON.stringify(JSON.parse(jsonString), null, 2)") - val prettyPrintedJson = scriptEngine["result"] as String + val prettyPrintedJson = generateJSON(spawnsJsonArray) try { FileWriter(EditorConstants.CONFIG_PATH + File.separator + fileName).use { file -> @@ -360,11 +333,7 @@ enum class Editors(val data: EditorData) { spawn["loc_data"] = spawnString.toString() spawnsJsonArray.add(spawn) } - val manager = ScriptEngineManager() - val scriptEngine = manager.getEngineByName("JavaScript") - scriptEngine.put("jsonString", spawnsJsonArray.toJSONString()) - scriptEngine.eval("result = JSON.stringify(JSON.parse(jsonString), null, 2)") - val prettyPrintedJson = scriptEngine["result"] as String + val prettyPrintedJson = generateJSON(spawnsJsonArray) try { FileWriter(EditorConstants.CONFIG_PATH + File.separator + fileName).use { file -> @@ -416,46 +385,59 @@ private fun saveTable(table: WeightBasedTable): JSONArray{ } /** - * Sorts and generates a string from an unsorted JSONArray. - * Currently this is only used by NPC_Config or item_config as it assumes each entry has 1 ID. + * Sorts an unsorted JSONArray. * Most other tables have some collection of IDs * ID name and examine will be first if present then everything else will be alphabetical * If other things wish to be sorted add them to the when statement - * @param array the JSONArray from npc_config or item_config + * @param array the JSONArray * @return The string ready to write out */ -private fun generateJSON(array: JSONArray): String{ - val sortedArray = array - .map{ it as JSONObject } - // Sort IDs - .sortedBy { (it["id"] as String).toIntOrNull() ?: Int.MAX_VALUE } - .map { itemJSONObj -> - val sortedKeys = itemJSONObj.keys +private fun sortJSON(value: Any?): Any? = + when (value) { + is JSONObject -> { + val sortedKeys = value.keys .asSequence() - .map { it as String} - .sortedWith(compareBy { key -> - when (key) { - "id" -> 0 - "name" -> 1 - "examine" -> 2 - else -> 3 - } - }.thenBy { it }) - .toList() + .map { it as String } + .sortedWith( + compareBy { + when (it) { + "id" -> 0 + "ids" -> 0 + "npc_id" -> 0 + "name" -> 1 + "examine" -> 2 + else -> 3 + } + }.thenBy { it } + ) - val orderedItem = LinkedHashMap() - for (key in sortedKeys){ - orderedItem[key] = itemJSONObj[key] + LinkedHashMap().apply { + for (key in sortedKeys) { + this[key] = sortJSON(value[key]) + } } - orderedItem } - val sortedJSONArray = JSONArray() - sortedArray.forEach { sortedJSONArray.add(it) } + is JSONArray -> { + JSONArray().apply { + addAll(value.map { sortJSON(it) }) + } + } + + else -> value + } + +/** + * Generates a string from an unsorted JSONArray. + * @param array the JSONArray + * @return The string ready to write out + */ +private fun generateJSON(array: JSONArray): String { + val sortedJSONArray = sortJSON(array) as JSONArray val manager = ScriptEngineManager() val scriptEngine = manager.getEngineByName("JavaScript") scriptEngine.put("jsonString", sortedJSONArray.toJSONString()) scriptEngine.eval("result = JSON.stringify(JSON.parse(jsonString), null, 2)") val prettyPrintedJson = scriptEngine["result"] as String return prettyPrintedJson -} \ No newline at end of file +}