Fix writeJson and readJson trying to correct negative numbers, when writeVarLong already does this

This commit is contained in:
DBotThePony 2023-06-20 00:23:34 +07:00
parent 09db4ad9e9
commit ddc3850722
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -149,18 +149,6 @@ private const val TYPE_STRING = 0x05
private const val TYPE_ARRAY = 0x06
private const val TYPE_OBJECT = 0x07
private fun fixSignedInt(read: Long): Long {
val sign = read and 0x1L
@Suppress("name_shadowing")
val read = read ushr 1
if (sign == 1L) {
return -read - 1L
} else {
return read
}
}
/**
* Writes binary json to stream in Starbound Object Notation format
*
@ -186,17 +174,9 @@ fun OutputStream.writeJson(element: JsonElement) {
if (element.isNumber) {
val num = element.asNumber
if (num is Int || num is Long) {
if (num is Int || num is Long || num is Short || num is Byte) {
write(TYPE_INT)
var int = num.toLong()
if (int < 0) {
int = int.absoluteValue.shl(1).or(1)
} else {
int.shl(1)
}
writeVarLongLE(int)
writeVarLongLE(num.toLong())
} else if (num is Float || num is Double) {
write(TYPE_DOUBLE)
writeDouble(num.toDouble())
@ -237,7 +217,7 @@ fun InputStream.readJson(sizeLimit: NbtAccounter = NbtAccounter(1L shl 18 /* 256
sizeLimit.accountBytes(1L)
JsonPrimitive(read() > 1)
}
TYPE_INT -> JsonPrimitive(fixSignedInt(readVarLongLE(sizeLimit)))
TYPE_INT -> JsonPrimitive(readVarLongLE(sizeLimit))
TYPE_STRING -> JsonPrimitive(readBinaryString(sizeLimit))
TYPE_ARRAY -> {
val values = readVarIntLE(sizeLimit)