Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

invalid json but works with RTM may not be loaded. #217

Merged
merged 2 commits into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/main/java/com/anatawa12/fixRtm/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,55 @@ fun <T> 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>): 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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
71 changes: 71 additions & 0 deletions src/test/java/com/anatawa12/fixRtm/UtilsKtTest.kt
Original file line number Diff line number Diff line change
@@ -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<Nothing>(), // 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()))
}
}