Удаляем дубликаты функций из binary json
This commit is contained in:
parent
005aa354df
commit
a5668b57e6
@ -22,25 +22,13 @@ abstract class ArmorItemPrototype : ItemPrototype(), IArmorItemDefinition {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@JsonBuilder
|
@JsonBuilder
|
||||||
class HeadArmorItemPrototype : ArmorItemPrototype() {
|
class HeadArmorItemPrototype : ArmorItemPrototype()
|
||||||
@JsonIgnoreProperty
|
|
||||||
override val armorType: ArmorPieceType = ArmorPieceType.HEAD
|
|
||||||
}
|
|
||||||
|
|
||||||
@JsonBuilder
|
@JsonBuilder
|
||||||
class ChestArmorItemPrototype : ArmorItemPrototype() {
|
class ChestArmorItemPrototype : ArmorItemPrototype()
|
||||||
@JsonIgnoreProperty
|
|
||||||
override val armorType: ArmorPieceType = ArmorPieceType.CHEST
|
|
||||||
}
|
|
||||||
|
|
||||||
@JsonBuilder
|
@JsonBuilder
|
||||||
class LegsArmorItemPrototype : ArmorItemPrototype() {
|
class LegsArmorItemPrototype : ArmorItemPrototype()
|
||||||
@JsonIgnoreProperty
|
|
||||||
override val armorType: ArmorPieceType = ArmorPieceType.LEGS
|
|
||||||
}
|
|
||||||
|
|
||||||
@JsonBuilder
|
@JsonBuilder
|
||||||
class BackArmorItemPrototype : ArmorItemPrototype() {
|
class BackArmorItemPrototype : ArmorItemPrototype()
|
||||||
@JsonIgnoreProperty
|
|
||||||
override val armorType: ArmorPieceType = ArmorPieceType.BACK
|
|
||||||
}
|
|
||||||
|
@ -86,7 +86,7 @@ class BTreeDB(val path: File) {
|
|||||||
val rootNodeIndex get() = if (useNodeTwo) rootNode2Index else rootNode1Index
|
val rootNodeIndex get() = if (useNodeTwo) rootNode2Index else rootNode1Index
|
||||||
val rootNodeIsLeaf get() = if (useNodeTwo) rootNode2IsLeaf else rootNode1IsLeaf
|
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> {
|
fun findAllKeys(index: Long = rootNodeIndex): List<ByteArray> {
|
||||||
seekBlock(index)
|
seekBlock(index)
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package ru.dbotthepony.kstarbound.io
|
package ru.dbotthepony.kstarbound.io
|
||||||
|
|
||||||
import it.unimi.dsi.fastutil.bytes.ByteArrayList
|
import it.unimi.dsi.fastutil.bytes.ByteArrayList
|
||||||
import java.io.DataInputStream
|
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
import java.io.RandomAccessFile
|
import java.io.RandomAccessFile
|
||||||
@ -132,7 +131,7 @@ fun InputStream.readVarInt(): Int {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
fun RandomAccessFile.readASCIIString(length: Int): String {
|
fun RandomAccessFile.readString(length: Int): String {
|
||||||
require(length >= 0) { "Invalid length $length" }
|
require(length >= 0) { "Invalid length $length" }
|
||||||
|
|
||||||
val bytes = ByteArray(length)
|
val bytes = ByteArray(length)
|
||||||
@ -145,7 +144,7 @@ fun RandomAccessFile.readASCIIString(length: Int): String {
|
|||||||
return bytes.toString(Charsets.UTF_8)
|
return bytes.toString(Charsets.UTF_8)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun InputStream.readASCIIString(length: Int): String {
|
fun InputStream.readString(length: Int): String {
|
||||||
require(length >= 0) { "Invalid length $length" }
|
require(length >= 0) { "Invalid length $length" }
|
||||||
|
|
||||||
val bytes = ByteArray(length)
|
val bytes = ByteArray(length)
|
||||||
|
@ -180,7 +180,15 @@ class StarboundPak(val path: File, callback: (finished: Boolean, status: String)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// сразу за INDEX идут метаданные в формате Binary Json
|
// сразу за 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
|
// сразу за метаданными идёт количество файлов внутри данного pak в формате Big Endian variable int
|
||||||
val indexNodeCount = reader.readVarLong()
|
val indexNodeCount = reader.readVarLong()
|
||||||
@ -203,7 +211,7 @@ class StarboundPak(val path: File, callback: (finished: Boolean, status: String)
|
|||||||
callback(false, "Reading index node $i")
|
callback(false, "Reading index node $i")
|
||||||
|
|
||||||
val readLength = stream.readVarInt()
|
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" }
|
require(name[0] == '/') { "index node at $i with '$name' appears to be not an absolute filename" }
|
||||||
val offset = stream.readLong()
|
val offset = stream.readLong()
|
||||||
val length = stream.readLong()
|
val length = stream.readLong()
|
||||||
|
@ -9,13 +9,12 @@ import com.google.gson.JsonPrimitive
|
|||||||
import com.google.gson.JsonSyntaxException
|
import com.google.gson.JsonSyntaxException
|
||||||
import com.google.gson.stream.JsonReader
|
import com.google.gson.stream.JsonReader
|
||||||
import com.google.gson.stream.JsonToken
|
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.readVarInt
|
||||||
import ru.dbotthepony.kstarbound.io.readVarLong
|
import ru.dbotthepony.kstarbound.io.readVarLong
|
||||||
import java.io.DataInputStream
|
import java.io.DataInputStream
|
||||||
import java.io.EOFException
|
import java.io.EOFException
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
import java.io.RandomAccessFile
|
|
||||||
import java.io.Reader
|
import java.io.Reader
|
||||||
import java.util.LinkedList
|
import java.util.LinkedList
|
||||||
|
|
||||||
@ -60,7 +59,7 @@ class BinaryJsonReader(private val stream: DataInputStream) : JsonReader(unreada
|
|||||||
|
|
||||||
if (last.readingName) {
|
if (last.readingName) {
|
||||||
last.readingName = false
|
last.readingName = false
|
||||||
stack.addLast(NameReader(stream.readASCIIString(stream.readVarInt())))
|
stack.addLast(NameReader(stream.readString(stream.readVarInt())))
|
||||||
} else {
|
} else {
|
||||||
last.readingName = true
|
last.readingName = true
|
||||||
last.readPairs++
|
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 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() {
|
override fun skipValue() {
|
||||||
popstack(this)
|
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]
|
* Позволяет читать двоичный JSON прямиком в [JsonElement]
|
||||||
*/
|
*/
|
||||||
@ -380,57 +363,26 @@ class BinaryJsonReader(private val stream: DataInputStream) : JsonReader(unreada
|
|||||||
TYPE_DOUBLE -> JsonPrimitive(reader.readDouble())
|
TYPE_DOUBLE -> JsonPrimitive(reader.readDouble())
|
||||||
TYPE_BOOLEAN -> JsonPrimitive(reader.readBoolean())
|
TYPE_BOOLEAN -> JsonPrimitive(reader.readBoolean())
|
||||||
TYPE_INT -> JsonPrimitive(fixSignedInt(reader.readVarLong()))
|
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_ARRAY -> readArray(reader)
|
||||||
TYPE_OBJECT -> readObject(reader)
|
TYPE_OBJECT -> readObject(reader)
|
||||||
else -> throw JsonParseException("Unknown element type $id")
|
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]
|
* Позволяет читать двоичный JSON объект прямиком в [JsonObject]
|
||||||
*/
|
*/
|
||||||
fun readObject(reader: DataInputStream): JsonObject {
|
fun readObject(reader: DataInputStream): JsonObject {
|
||||||
val values = reader.readVarInt() - 1
|
val values = reader.readVarInt()
|
||||||
if (values == -1) return JsonObject()
|
if (values == 0) return JsonObject()
|
||||||
if (values < -1) throw JsonSyntaxException("Tried to read json object with $values elements in it")
|
if (values < 0) throw JsonSyntaxException("Tried to read json object with $values elements in it")
|
||||||
|
|
||||||
val build = JsonObject()
|
val build = JsonObject()
|
||||||
|
|
||||||
for (i in 0 .. values) {
|
for (i in 0 until values) {
|
||||||
val key: String
|
val key = try {
|
||||||
|
reader.readString(reader.readVarInt())
|
||||||
try {
|
|
||||||
key = reader.readASCIIString(reader.readVarInt())
|
|
||||||
} catch(err: Throwable) {
|
} catch(err: Throwable) {
|
||||||
throw JsonSyntaxException("Reading json object at $i", err)
|
throw JsonSyntaxException("Reading json object at $i", err)
|
||||||
}
|
}
|
||||||
@ -445,31 +397,17 @@ class BinaryJsonReader(private val stream: DataInputStream) : JsonReader(unreada
|
|||||||
return build
|
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]
|
* Позволяет читать двоичный JSON массив прямиком в [JsonArray]
|
||||||
*/
|
*/
|
||||||
fun readArray(reader: DataInputStream): JsonArray {
|
fun readArray(reader: DataInputStream): JsonArray {
|
||||||
val values = reader.readVarInt() - 1
|
val values = reader.readVarInt()
|
||||||
|
|
||||||
if (values == -1) return JsonArray()
|
if (values == 0) return JsonArray()
|
||||||
if (values < -1) throw JsonSyntaxException("Tried to read json array with $values elements in it")
|
if (values < 0) throw JsonSyntaxException("Tried to read json array with $values elements in it")
|
||||||
|
|
||||||
val build = JsonArray(values)
|
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
|
return build
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user