diff --git a/src/main/java/com/anatawa12/fixRtm/Utils.kt b/src/main/java/com/anatawa12/fixRtm/Utils.kt index 76caac08..63eee2b1 100644 --- a/src/main/java/com/anatawa12/fixRtm/Utils.kt +++ b/src/main/java/com/anatawa12/fixRtm/Utils.kt @@ -174,3 +174,55 @@ fun T.addEntityCrashInfoAboutModelSet( } fun arrayOfItemStack(size: Int) = Array(size) { ItemStack.EMPTY } + +private const val START = 0 +private const val KEY = 1 +private const val STR_BS = 2 +private const val STR_BS_U0 = 3 // \u|0000 +private const val STR_BS_U1 = 4 // \u0|000 +private const val STR_BS_U2 = 5 // \u00|00 +private const val STR_BS_U3 = 6 // \u000|0 +private const val STR = 7 + +fun joinLinesForJsonReading(lines: List): String = buildString { + var stat = START + var shouldAddNewLine = false + for (line in lines) { + for (c in line) { + val preStat = stat + when (stat) { + START -> { + if (c in '0'..'9' || c in 'a'..'z' || c in 'A'..'Z' || c == '+' || c == '-' || c == '.') + stat = KEY + else if (c == '"') + stat = STR + } + KEY -> { + if (c in '0'..'9' || c in 'a'..'z' || c in 'A'..'Z' || c == '+' || c == '-' || c == '.') + stat = KEY + else + stat = START + } + STR -> { + when (c) { + '\\' -> stat = STR_BS + '"' -> stat = START + } + } + STR_BS -> { + when (c) { + 'u' -> stat = STR_BS_U0 + else -> stat = STR + } + } + STR_BS_U0, STR_BS_U1, STR_BS_U2, STR_BS_U3 -> { + stat++ + } + } + if (shouldAddNewLine && (preStat == START || stat == START)) append('\n') + shouldAddNewLine = false + append(c) + } + shouldAddNewLine = true + } +} diff --git a/src/main/rtm-patches/jp/ngt/rtm/modelpack/ModelPackManager.java.pm.patch b/src/main/rtm-patches/jp/ngt/rtm/modelpack/ModelPackManager.java.pm.patch index ac1268cf..ed4f5253 100644 --- a/src/main/rtm-patches/jp/ngt/rtm/modelpack/ModelPackManager.java.pm.patch +++ b/src/main/rtm-patches/jp/ngt/rtm/modelpack/ModelPackManager.java.pm.patch @@ -87,7 +87,7 @@ + "file and file is exists.", file.getAbsolutePath()); - throw new ModelPackException("Can't load model", file.getAbsolutePath()); -+ ResourceConfig resourceconfig = (ResourceConfig)NGTJson.getObjectFromJson(NGTText.append(json, true), type.cfgClass); ++ ResourceConfig resourceconfig = (ResourceConfig)NGTJson.getObjectFromJson(com.anatawa12.fixRtm.UtilsKt.joinLinesForJsonReading(json), type.cfgClass); + resourceconfig.file = file; + return this.registerResourceSet(type, resourceconfig, NGTText.append(json, false)); } diff --git a/src/test/java/com/anatawa12/fixRtm/UtilsKtTest.kt b/src/test/java/com/anatawa12/fixRtm/UtilsKtTest.kt new file mode 100644 index 00000000..c5af27b3 --- /dev/null +++ b/src/test/java/com/anatawa12/fixRtm/UtilsKtTest.kt @@ -0,0 +1,71 @@ +package com.anatawa12.fixRtm + +import net.minecraftforge.common.ForgeVersion +import net.minecraftforge.fml.common.Loader +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test +import java.io.File + +internal class UtilsKtTest { + companion object { + init { + Loader.injectData( + ForgeVersion.majorVersion.toString(), // major + ForgeVersion.minorVersion.toString(), // minor + ForgeVersion.revisionVersion.toString(), // rev + ForgeVersion.buildVersion.toString(), // build + ForgeVersion.mcVersion, // mccversion + ForgeVersion.mcpVersion, // mcpversion + File("eclipse"), // minecraftDir + mutableListOf(), // injectedContainers + ) + Loader::class.java.getDeclaredMethod("initializeLoader").also { it.isAccessible = true } + .invoke(Loader.instance()) + } + } + + @Test + fun joinLinesForJsonReading() { + val validJson = """ + { + "integer": 10, + "number": 0.0, + "number-expo": 0.0e+0, + "signed-number": -0.0e+0, + "string": "some string", + "escaped": "\"\r\n", + "end": 0 + } + """.trimIndent() + val validJsonTokens = """ + "integer": 0, + "number": 0.0, + "number-expo": 0.0e+0, + "signed-number": -0.0e+0, + "string": "some string", + "escaped": "\"\r\n", + "end": 0, + """.trimIndent() + val invalidJson = """ + { + "integer": 1 + 0, + "number": 0. + 0, + "number-expo": 0.0 + e+0, + "signed-number": - + 0.0e+0, + "string": "some + string", + "escaped": "\ + "\r\n", + "end": 0 + } + """.trimIndent() + + assertEquals(validJson, joinLinesForJsonReading(validJson.lines())) + assertEquals(validJsonTokens, joinLinesForJsonReading(validJsonTokens.lines())) + assertEquals(validJson, joinLinesForJsonReading(invalidJson.lines())) + } +}