Удаляем дубликаты функций из binary json

This commit is contained in:
DBotThePony 2023-02-09 16:09:21 +07:00
parent 005aa354df
commit a5668b57e6
Signed by: DBot
GPG Key ID: DCC23B5715498507
5 changed files with 31 additions and 98 deletions

View File

@ -22,25 +22,13 @@ abstract class ArmorItemPrototype : ItemPrototype(), IArmorItemDefinition {
}
@JsonBuilder
class HeadArmorItemPrototype : ArmorItemPrototype() {
@JsonIgnoreProperty
override val armorType: ArmorPieceType = ArmorPieceType.HEAD
}
class HeadArmorItemPrototype : ArmorItemPrototype()
@JsonBuilder
class ChestArmorItemPrototype : ArmorItemPrototype() {
@JsonIgnoreProperty
override val armorType: ArmorPieceType = ArmorPieceType.CHEST
}
class ChestArmorItemPrototype : ArmorItemPrototype()
@JsonBuilder
class LegsArmorItemPrototype : ArmorItemPrototype() {
@JsonIgnoreProperty
override val armorType: ArmorPieceType = ArmorPieceType.LEGS
}
class LegsArmorItemPrototype : ArmorItemPrototype()
@JsonBuilder
class BackArmorItemPrototype : ArmorItemPrototype() {
@JsonIgnoreProperty
override val armorType: ArmorPieceType = ArmorPieceType.BACK
}
class BackArmorItemPrototype : ArmorItemPrototype()

View File

@ -86,7 +86,7 @@ class BTreeDB(val path: File) {
val rootNodeIndex get() = if (useNodeTwo) rootNode2Index else rootNode1Index
val rootNodeIsLeaf get() = if (useNodeTwo) rootNode2IsLeaf else rootNode1IsLeaf
fun readBlockType() = TreeBlockType[reader.readASCIIString(2)]
fun readBlockType() = TreeBlockType[reader.readString(2)]
fun findAllKeys(index: Long = rootNodeIndex): List<ByteArray> {
seekBlock(index)

View File

@ -1,7 +1,6 @@
package ru.dbotthepony.kstarbound.io
import it.unimi.dsi.fastutil.bytes.ByteArrayList
import java.io.DataInputStream
import java.io.IOException
import java.io.InputStream
import java.io.RandomAccessFile
@ -132,7 +131,7 @@ fun InputStream.readVarInt(): Int {
return result
}
fun RandomAccessFile.readASCIIString(length: Int): String {
fun RandomAccessFile.readString(length: Int): String {
require(length >= 0) { "Invalid length $length" }
val bytes = ByteArray(length)
@ -145,7 +144,7 @@ fun RandomAccessFile.readASCIIString(length: Int): String {
return bytes.toString(Charsets.UTF_8)
}
fun InputStream.readASCIIString(length: Int): String {
fun InputStream.readString(length: Int): String {
require(length >= 0) { "Invalid length $length" }
val bytes = ByteArray(length)

View File

@ -180,7 +180,15 @@ class StarboundPak(val path: File, callback: (finished: Boolean, status: String)
}
// сразу за INDEX идут метаданные в формате Binary Json
val metadata = BinaryJsonReader.readObject(reader)
val metadata = BinaryJsonReader.readObject(DataInputStream(object : InputStream() {
override fun read(): Int {
return reader.read()
}
override fun read(b: ByteArray, off: Int, len: Int): Int {
return reader.read(b, off, len)
}
}))
// сразу за метаданными идёт количество файлов внутри данного pak в формате Big Endian variable int
val indexNodeCount = reader.readVarLong()
@ -203,7 +211,7 @@ class StarboundPak(val path: File, callback: (finished: Boolean, status: String)
callback(false, "Reading index node $i")
val readLength = stream.readVarInt()
name = stream.readASCIIString(readLength)
name = stream.readString(readLength)
require(name[0] == '/') { "index node at $i with '$name' appears to be not an absolute filename" }
val offset = stream.readLong()
val length = stream.readLong()

View File

@ -9,13 +9,12 @@ import com.google.gson.JsonPrimitive
import com.google.gson.JsonSyntaxException
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonToken
import ru.dbotthepony.kstarbound.io.readASCIIString
import ru.dbotthepony.kstarbound.io.readString
import ru.dbotthepony.kstarbound.io.readVarInt
import ru.dbotthepony.kstarbound.io.readVarLong
import java.io.DataInputStream
import java.io.EOFException
import java.io.InputStream
import java.io.RandomAccessFile
import java.io.Reader
import java.util.LinkedList
@ -60,7 +59,7 @@ class BinaryJsonReader(private val stream: DataInputStream) : JsonReader(unreada
if (last.readingName) {
last.readingName = false
stack.addLast(NameReader(stream.readASCIIString(stream.readVarInt())))
stack.addLast(NameReader(stream.readString(stream.readVarInt())))
} else {
last.readingName = true
last.readPairs++
@ -159,7 +158,7 @@ class BinaryJsonReader(private val stream: DataInputStream) : JsonReader(unreada
}
private inner class StringReader(val length: Int) : NothingReader("string", JsonToken.STRING) {
private val value by lazy(LazyThreadSafetyMode.NONE) { stream.readASCIIString(length) }
private val value by lazy(LazyThreadSafetyMode.NONE) { stream.readString(length) }
override fun skipValue() {
popstack(this)
@ -355,22 +354,6 @@ class BinaryJsonReader(private val stream: DataInputStream) : JsonReader(unreada
}
}
/**
* Позволяет читать двоичный JSON прямиком в [JsonElement]
*/
fun readElement(reader: RandomAccessFile): JsonElement {
return when (val id = reader.read()) {
TYPE_NULL -> JsonNull.INSTANCE
TYPE_DOUBLE -> JsonPrimitive(reader.readDouble())
TYPE_BOOLEAN -> JsonPrimitive(reader.readBoolean())
TYPE_INT -> JsonPrimitive(fixSignedInt(reader.readVarLong()))
TYPE_STRING -> JsonPrimitive(reader.readASCIIString(reader.readVarInt()))
TYPE_ARRAY -> readArray(reader)
TYPE_OBJECT -> readObject(reader)
else -> throw JsonParseException("Unknown element type $id")
}
}
/**
* Позволяет читать двоичный JSON прямиком в [JsonElement]
*/
@ -380,57 +363,26 @@ class BinaryJsonReader(private val stream: DataInputStream) : JsonReader(unreada
TYPE_DOUBLE -> JsonPrimitive(reader.readDouble())
TYPE_BOOLEAN -> JsonPrimitive(reader.readBoolean())
TYPE_INT -> JsonPrimitive(fixSignedInt(reader.readVarLong()))
TYPE_STRING -> JsonPrimitive(reader.readASCIIString(reader.readVarInt()))
TYPE_STRING -> JsonPrimitive(reader.readString(reader.readVarInt()))
TYPE_ARRAY -> readArray(reader)
TYPE_OBJECT -> readObject(reader)
else -> throw JsonParseException("Unknown element type $id")
}
}
/**
* Позволяет читать двоичный JSON объект прямиком в [JsonObject]
*/
fun readObject(reader: RandomAccessFile): JsonObject {
val values = reader.readVarInt() - 1
if (values == -1) return JsonObject()
if (values < -1) throw JsonSyntaxException("Tried to read json object with $values elements in it")
val build = JsonObject()
for (i in 0 .. values) {
val key: String
try {
key = reader.readASCIIString(reader.readVarInt())
} catch(err: Throwable) {
throw JsonSyntaxException("Reading json object at $i", err)
}
try {
build.add(key, readElement(reader))
} catch(err: Throwable) {
throw JsonSyntaxException("Reading json object at $i with name $key", err)
}
}
return build
}
/**
* Позволяет читать двоичный JSON объект прямиком в [JsonObject]
*/
fun readObject(reader: DataInputStream): JsonObject {
val values = reader.readVarInt() - 1
if (values == -1) return JsonObject()
if (values < -1) throw JsonSyntaxException("Tried to read json object with $values elements in it")
val values = reader.readVarInt()
if (values == 0) return JsonObject()
if (values < 0) throw JsonSyntaxException("Tried to read json object with $values elements in it")
val build = JsonObject()
for (i in 0 .. values) {
val key: String
try {
key = reader.readASCIIString(reader.readVarInt())
for (i in 0 until values) {
val key = try {
reader.readString(reader.readVarInt())
} catch(err: Throwable) {
throw JsonSyntaxException("Reading json object at $i", err)
}
@ -445,31 +397,17 @@ class BinaryJsonReader(private val stream: DataInputStream) : JsonReader(unreada
return build
}
/**
* Позволяет читать двоичный JSON массив прямиком в [JsonArray]
*/
fun readArray(reader: RandomAccessFile): JsonArray {
val values = reader.readVarInt() - 1
if (values == -1) return JsonArray()
if (values < -1) throw JsonSyntaxException("Tried to read json array with $values elements in it")
val build = JsonArray(values)
for (i in 0 .. values) build.add(readElement(reader))
return build
}
/**
* Позволяет читать двоичный JSON массив прямиком в [JsonArray]
*/
fun readArray(reader: DataInputStream): JsonArray {
val values = reader.readVarInt() - 1
val values = reader.readVarInt()
if (values == -1) return JsonArray()
if (values < -1) throw JsonSyntaxException("Tried to read json array with $values elements in it")
if (values == 0) return JsonArray()
if (values < 0) throw JsonSyntaxException("Tried to read json array with $values elements in it")
val build = JsonArray(values)
for (i in 0 .. values) build.add(readElement(reader))
for (i in 0 until values) build.add(readElement(reader))
return build
}