material and 5 temporary vectors no longer go into the savegame

This commit is contained in:
minjaesong
2023-05-21 11:20:45 +09:00
parent b0d83325a7
commit 6268b99c1c
53 changed files with 777 additions and 5682 deletions

View File

@@ -1,8 +1,11 @@
package net.torvald.terrarum.serialise
import net.torvald.terrarum.savegame.toBigEndian
import java.util.UUID
import kotlin.math.ceil
/**
* Standard Ascii85 implementation, except that the character sets used are as defined in
* RFC 1924 for JSON-compatibility, and will NOT truncate '00000' into something else;
* Ascii85 implementation with my own character table based on RFC 1924. Will NOT truncate '00000' into something else;
* just gzip the inputstream instead!
*/
object Ascii85 {
@@ -31,9 +34,6 @@ object Ascii85 {
INVERSE_TABLE[CHAR_TABLE[i].toInt()] = i.toLong()
}
/**
* Put -1 ([Ascii85.PAD_BYTE]) for null-bytes
*/
fun encode(i1: Int?, i2: Int?, i3: Int?, i4: Int?): String {
var b1=i1 ?: PAD_BYTE; var b2=i2 ?: PAD_BYTE; var b3=i3 ?: PAD_BYTE; var b4=i4 ?: PAD_BYTE
var padLen = 0
@@ -73,9 +73,6 @@ object Ascii85 {
"${CHAR_TABLE[sum.toInt()]}").substring(0,5 - padLen)
}
/**
* Put '\0' ([Ascii85.PAD_CHAR]) for null-chars
*/
fun decode(x1: Char?, x2: Char?, x3: Char?, x4: Char?, x5: Char?): ByteArray {
var s1=x1 ?: PAD_CHAR; var s2=x2 ?: PAD_CHAR; var s3=x3 ?: PAD_CHAR; var s4=x4 ?: PAD_CHAR; var s5=x5 ?: PAD_CHAR
var padLen = 0
@@ -106,6 +103,47 @@ object Ascii85 {
INVERSE_TABLE[s5.toInt()]
return ByteArray(4 - padLen) { sum.ushr((3 - it) * 8).and(255).toByte() }
}
fun encodeBytes(bytes: ByteArray): String {
val sb = StringBuilder()
for (k in 0..(bytes.size / 4)) {
sb.append(Ascii85.encode(
bytes.getOrNull(k*4)?.toInt(),
bytes.getOrNull(k*4+1)?.toInt(),
bytes.getOrNull(k*4+2)?.toInt(),
bytes.getOrNull(k*4+3)?.toInt()
))
}
return sb.toString()
}
fun decodeBytes(encoded: String): ByteArray {
val bytes = ByteArray(ceil(encoded.length * 0.8).toInt())
var curs = 0
for (k in 0..(encoded.length / 5)) {
Ascii85.decode(
encoded.getOrNull(k*5),
encoded.getOrNull(k*5+1),
encoded.getOrNull(k*5+2),
encoded.getOrNull(k*5+3),
encoded.getOrNull(k*5+4),
).let {
it.forEachIndexed { i, b ->
bytes[curs + i] = b
}
curs += it.size
}
}
return bytes
}
}
fun UUID.toAscii85() =
Ascii85.encodeBytes(this.mostSignificantBits.toBigEndian() + this.leastSignificantBits.toBigEndian())
fun String.ascii85toUUID(): UUID {
val bytes = Ascii85.decodeBytes(this)
val msb = bytes.toBigInt64(0)
val lsb = bytes.toBigInt64(8)
return UUID(msb, lsb)
}
/*fun main(args: Array<String>) {

View File

@@ -38,39 +38,67 @@ fun Long.toULittle48() = byteArrayOf(
fun Double.toLittle() = java.lang.Double.doubleToRawLongBits(this).toLittle()
fun Boolean.toLittle() = byteArrayOf(if (this) 0xFF.toByte() else 0.toByte())
fun ByteArray.toLittleInt() =
if (this.size != 4) throw Error("Array not in size of 4")
else this[0].toUint() or
this[1].toUint().shl(8) or
this[2].toUint().shl(16) or
this[3].toUint().shl(24)
fun ByteArray.toULittleShort() =
if (this.size != 4) throw Error("Array not in size of 2")
else this[0].toUint() or
this[1].toUint().shl(8)
fun ByteArray.toLittleShort() =
if (this.size != 4) throw Error("Array not in size of 2")
else this[0].toUint() or
this[1].toInt().shl(8)
fun ByteArray.toLittleLong() =
if (this.size != 8) throw Error("Array not in size of 8")
else this[0].toUlong() or
this[1].toUlong().shl(8) or
this[2].toUlong().shl(16) or
this[3].toUlong().shl(24) or
this[4].toUlong().shl(32) or
this[5].toUlong().shl(40) or
this[6].toUlong().shl(48) or
this[7].toUlong().shl(56)
fun ByteArray.toLittleInt48() =
if (this.size != 6) throw Error("Array not in size of 6")
else this[0].toUlong() or
this[1].toUlong().shl(8) or
this[2].toUlong().shl(16) or
this[3].toUlong().shl(24) or
this[4].toUlong().shl(32) or
this[5].toUlong().shl(40)
fun ByteArray.toLittleFloat() = java.lang.Float.intBitsToFloat(this.toLittleInt())
fun ByteArray.toLittleInt32(offset: Int = 0) =
this[0 + offset].toUint() or
this[1 + offset].toUint().shl(8) or
this[2 + offset].toUint().shl(16) or
this[3 + offset].toUint().shl(24)
fun ByteArray.toULittleShort(offset: Int = 0) =
this[0 + offset].toUint() or
this[1 + offset].toUint().shl(8)
fun ByteArray.toLittleShort(offset: Int = 0) =
this[0 + offset].toUint() or
this[1 + offset].toInt().shl(8)
fun ByteArray.toLittleInt64(offset: Int = 0) =
this[0 + offset].toUlong() or
this[1 + offset].toUlong().shl(8) or
this[2 + offset].toUlong().shl(16) or
this[3 + offset].toUlong().shl(24) or
this[4 + offset].toUlong().shl(32) or
this[5 + offset].toUlong().shl(40) or
this[6 + offset].toUlong().shl(48) or
this[7 + offset].toUlong().shl(56)
fun ByteArray.toLittleInt48(offset: Int = 0) =
this[0 + offset].toUlong() or
this[1 + offset].toUlong().shl(8) or
this[2 + offset].toUlong().shl(16) or
this[3 + offset].toUlong().shl(24) or
this[4 + offset].toUlong().shl(32) or
this[5 + offset].toUlong().shl(40)
fun ByteArray.toLittleFloat() = java.lang.Float.intBitsToFloat(this.toLittleInt32())
fun ByteArray.toBigInt16(offset: Int = 0): Int {
return this[0 + offset].toUint().shl(8) or
this[1 + offset].toUint()
}
fun ByteArray.toBigInt24(offset: Int = 0): Int {
return this[0 + offset].toUint().shl(16) or
this[1 + offset].toUint().shl(8) or
this[2 + offset].toUint()
}
fun ByteArray.toBigInt32(offset: Int = 0): Int {
return this[0 + offset].toUint().shl(24) or
this[1 + offset].toUint().shl(16) or
this[2 + offset].toUint().shl(8) or
this[3 + offset].toUint()
}
fun ByteArray.toBigInt48(offset: Int = 0): Long {
return this[0 + offset].toUlong().shl(40) or
this[1 + offset].toUlong().shl(32) or
this[2 + offset].toUlong().shl(24) or
this[3 + offset].toUlong().shl(16) or
this[4 + offset].toUlong().shl(8) or
this[5 + offset].toUlong()
}
fun ByteArray.toBigInt64(offset: Int = 0): Long {
return this[0 + offset].toUlong().shl(56) or
this[1 + offset].toUlong().shl(48) or
this[2 + offset].toUlong().shl(40) or
this[3 + offset].toUlong().shl(32) or
this[4 + offset].toUlong().shl(24) or
this[5 + offset].toUlong().shl(16) or
this[6 + offset].toUlong().shl(8) or
this[7 + offset].toUlong()
}
fun Byte.toUlong() = java.lang.Byte.toUnsignedLong(this)
fun Byte.toUint() = java.lang.Byte.toUnsignedInt(this)