bytearray64reader: read length of zero and EOF are properly distinguished (gdx is somewhat pedantic); changed an ascii85 charset; working meta (de)serialisation

This commit is contained in:
minjaesong
2021-09-01 17:23:12 +09:00
parent 07f26a7716
commit f08296b3be
11 changed files with 109 additions and 315 deletions

View File

@@ -3,7 +3,6 @@ package net.torvald.terrarum.serialise
import com.badlogic.gdx.utils.Json
import com.badlogic.gdx.utils.JsonValue
import com.badlogic.gdx.utils.JsonWriter
import net.torvald.terrarum.AppLoader.printdbg
import net.torvald.terrarum.console.EchoError
import net.torvald.terrarum.gameworld.BlockLayer
import net.torvald.terrarum.gameworld.GameWorld
@@ -272,215 +271,3 @@ object Common {
return unzipdBytes
}
}
class ByteArray64Writer(val charset: Charset) : Writer() {
private val acceptableCharsets = arrayOf(Charsets.UTF_8, Charset.forName("CP437"))
init {
if (!acceptableCharsets.contains(charset))
throw UnsupportedCharsetException(charset.name())
}
private val ba64 = ByteArray64()
private var closed = false
private var surrogateBuf = 0
init {
this.lock = ba64
}
private fun checkOpen() {
if (closed) throw ClosedChannelException()
}
private fun Int.isSurroHigh() = this.ushr(10) == 0b110110
private fun Int.isSurroLow() = this.ushr(10) == 0b110111
private fun Int.toUcode() = 'u' + this.toString(16).uppercase().padStart(4,'0')
/**
* @param c not a freakin' codepoint; just a Java's Char casted into Int
*/
override fun write(c: Int) {
checkOpen()
when (charset) {
Charsets.UTF_8 -> {
if (surrogateBuf == 0 && !c.isSurroHigh() && !c.isSurroLow())
writeUtf8Codepoint(c)
else if (surrogateBuf == 0 && c.isSurroHigh())
surrogateBuf = c
else if (surrogateBuf != 0 && c.isSurroLow())
writeUtf8Codepoint(65536 + surrogateBuf.and(1023).shl(10) or c.and(1023))
// invalid surrogate pair input
else
throw IllegalStateException("Surrogate high: ${surrogateBuf.toUcode()}, surrogate low: ${c.toUcode()}")
}
Charset.forName("CP437") -> {
ba64.add(c.toByte())
}
else -> throw UnsupportedCharsetException(charset.name())
}
}
fun writeUtf8Codepoint(codepoint: Int) {
when (codepoint) {
in 0..127 -> ba64.add(codepoint.toByte())
in 128..2047 -> {
ba64.add((0xC0 or codepoint.ushr(6).and(31)).toByte())
ba64.add((0x80 or codepoint.and(63)).toByte())
}
in 2048..65535 -> {
ba64.add((0xE0 or codepoint.ushr(12).and(15)).toByte())
ba64.add((0x80 or codepoint.ushr(6).and(63)).toByte())
ba64.add((0x80 or codepoint.and(63)).toByte())
}
in 65536..1114111 -> {
ba64.add((0xF0 or codepoint.ushr(18).and(7)).toByte())
ba64.add((0x80 or codepoint.ushr(12).and(63)).toByte())
ba64.add((0x80 or codepoint.ushr(6).and(63)).toByte())
ba64.add((0x80 or codepoint.and(63)).toByte())
}
else -> throw IllegalArgumentException("Not a unicode code point: U+${codepoint.toString(16).uppercase()}")
}
}
override fun write(cbuf: CharArray) {
checkOpen()
write(String(cbuf))
}
override fun write(str: String) {
checkOpen()
str.toByteArray(charset).forEach { ba64.add(it) }
}
override fun write(cbuf: CharArray, off: Int, len: Int) {
write(cbuf.copyOfRange(off, off + len))
}
override fun write(str: String, off: Int, len: Int) {
write(str.substring(off, off + len))
}
override fun close() { closed = true }
override fun flush() {}
fun toByteArray64() = if (closed) ba64 else throw IllegalAccessException("Writer not closed")
}
class ByteArray64Reader(val ba: ByteArray64, val charset: Charset) : Reader() {
private val acceptableCharsets = arrayOf(Charsets.UTF_8, Charset.forName("CP437"))
init {
if (!acceptableCharsets.contains(charset))
throw UnsupportedCharsetException(charset.name())
}
private var readCursor = 0L
private val remaining
get() = ba.size - readCursor
/**
* U+0000 .. U+007F 0xxxxxxx
* U+0080 .. U+07FF 110xxxxx 10xxxxxx
* U+0800 .. U+FFFF 1110xxxx 10xxxxxx 10xxxxxx
* U+10000 .. U+10FFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
*/
private fun utf8GetCharLen(head: Byte) = when (head.toInt() and 255) {
in 0b11110_000..0b11110_111 -> 4
in 0b1110_0000..0b1110_1111 -> 3
in 0b110_00000..0b110_11111 -> 2
in 0b0_0000000..0b0_1111111 -> 1
else -> throw IllegalArgumentException("Invalid UTF-8 Character head byte: ${head.toInt() and 255}")
}
/**
* @param list of bytes that encodes one unicode character. Get required byte length using [utf8GetCharLen].
* @return A codepoint of the character.
*/
private fun utf8decode(bytes0: List<Byte>): Int {
val bytes = bytes0.map { it.toInt() and 255 }
var ret = when (bytes.size) {
4 -> (bytes[0] and 7) shl 18
3 -> (bytes[0] and 15) shl 12
2 -> (bytes[0] and 31) shl 6
1 -> (bytes[0] and 127)
else -> throw IllegalArgumentException("Expected bytes size: 1..4, got ${bytes.size}")
}
bytes.tail().reversed().forEachIndexed { index, byte ->
ret = ret or (byte and 63).shl(6 * index)
}
return ret
}
private var surrogateLeftover = ' '
override fun read(cbuf: CharArray, off: Int, len: Int): Int {
var readCount = 0
when (charset) {
Charsets.UTF_8 -> {
while (readCount < len && remaining > 0) {
if (surrogateLeftover != ' ') {
cbuf[off + readCount] = surrogateLeftover
readCount += 1
surrogateLeftover = ' '
}
else {
val bbuf = (0 until minOf(4L, remaining)).map { ba[readCursor + it] }
val charLen = utf8GetCharLen(bbuf[0])
val codePoint = utf8decode(bbuf.subList(0, charLen))
if (codePoint < 65536) {
cbuf[off + readCount] = codePoint.toChar()
readCount += 1
readCursor += charLen
}
else {
/*
* U' = yyyyyyyyyyxxxxxxxxxx // U - 0x10000
* W1 = 110110yyyyyyyyyy // 0xD800 + yyyyyyyyyy
* W2 = 110111xxxxxxxxxx // 0xDC00 + xxxxxxxxxx
*/
val codPoin = codePoint - 65536
val surroLead = (0xD800 or codPoin.ushr(10)).toChar()
val surroTrail = (0xDC00 or codPoin.and(1023)).toChar()
cbuf[off + readCount] = surroLead
if (off + readCount + 1 < cbuf.size) {
cbuf[off + readCount + 1] = surroTrail
readCount += 2
readCursor += 4
}
else {
readCount += 1
readCursor += 4
surrogateLeftover = surroTrail
}
}
}
}
}
Charset.forName("CP437") -> {
for (i in 0 until minOf(len.toLong(), remaining)) {
cbuf[(off + i).toInt()] = ba[readCursor].toChar()
readCursor += 1
readCount += 1
}
}
else -> throw UnsupportedCharsetException(charset.name())
}
return if (readCount == 0) -1 else readCount
}
override fun close() { readCursor = 0L }
override fun reset() { readCursor = 0L }
override fun markSupported() = false
}