more world save and virtualdisk lib update

This commit is contained in:
minjaesong
2021-08-30 13:57:54 +09:00
parent 7ae862dd05
commit fb140ce2da
6 changed files with 61 additions and 84 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -682,62 +682,3 @@ inline class FluidType(val value: Int) {
fun abs() = this.value.absoluteValue
}
/**
* @param b a BlockLayer
* @return Bytes in [b] which are GZip'd then Ascii85-encoded
*/
fun blockLayerToStr(b: BlockLayer): String {
val sb = StringBuilder()
val bo = ByteArray64GrowableOutputStream()
val zo = GZIPOutputStream(bo)
b.bytesIterator().forEachRemaining {
zo.write(it.toInt())
}
zo.flush(); zo.close()
val ba = bo.toByteArray64()
var bai = 0
val buf = IntArray(4) { Ascii85.PAD_BYTE }
ba.forEach {
if (bai > 0 && bai % 4 == 0) {
sb.append(Ascii85.encode(buf[0], buf[1], buf[2], buf[3]))
buf.fill(Ascii85.PAD_BYTE)
}
buf[bai % 4] = it.toInt() and 255
bai += 1
}; sb.append(Ascii85.encode(buf[0], buf[1], buf[2], buf[3]))
return sb.toString()
}
/**
* @param b a BlockLayer
* @return Bytes in [b] which are LZMA'd then Ascii85-encoded
*/
fun blockLayerToStr2(b: BlockLayer): String {
val sb = StringBuilder()
val bi = UnsafePtrInputStream(b.ptr)
val bo = ByteArray64GrowableOutputStream()
Lzma.compress(bi, bo); bo.flush(); bo.close()
val ba = bo.toByteArray64()
var bai = 0
val buf = IntArray(4) { Ascii85.PAD_BYTE }
ba.forEach {
if (bai > 0 && bai % 4 == 0) {
sb.append(Ascii85.encode(buf[0], buf[1], buf[2], buf[3]))
buf.fill(Ascii85.PAD_BYTE)
}
buf[bai % 4] = it.toInt() and 255
bai += 1
}; sb.append(Ascii85.encode(buf[0], buf[1], buf[2], buf[3]))
return sb.toString()
}

View File

@@ -41,20 +41,22 @@ object Save : ConsoleCommand {
val disk = VDUtil.createNewDisk(1L shl 60, savename, Charsets.UTF_8)
// NOTE: don't bother with the entryID of DiskEntries; it will be overwritten anyway
val metaContent = EntryFile(WriteMeta(ingame).encodeToByteArray64())
val meta = DiskEntry(32768, 0, "savegame.json".toByteArray(), creation_t, time_t, metaContent)
VDUtil.addFile(disk, 0, meta, false)
val meta = DiskEntry(0, 0, "savegame".toByteArray(), creation_t, time_t, metaContent)
VDUtil.addFile(disk, 0, meta)
val worldContent = EntryFile(WriteWorld(ingame).encodeToByteArray64())
val world = DiskEntry(ingame.world.worldIndex, 0, "world${ingame.world.worldIndex}.json".toByteArray(), creation_t, time_t, worldContent)
VDUtil.addFile(disk, 0, world, false)
val world = DiskEntry(0, 0, "world${ingame.world.worldIndex}".toByteArray(), creation_t, time_t, worldContent)
VDUtil.addFile(disk, 0, world)
listOf(ingame.actorContainerActive, ingame.actorContainerInactive).forEach { actors ->
actors.forEach {
if (acceptable(it)) {
val actorContent = EntryFile(WriteActor.encodeToByteArray64(it))
val actor = DiskEntry(it.referenceID, 0, "actor${it.referenceID}.json".toByteArray(), creation_t, time_t, actorContent)
VDUtil.addFile(disk, 0, actor, false)
val actor = DiskEntry(0, 0, "actor${it.referenceID}".toByteArray(), creation_t, time_t, actorContent)
VDUtil.addFile(disk, 0, actor)
}
}
}

View File

@@ -10,9 +10,12 @@ import net.torvald.terrarum.gameworld.WorldTime
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.ByteArray64
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.ByteArray64GrowableOutputStream
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.ByteArray64InputStream
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.ByteArray64OutputStream
import net.torvald.terrarum.utils.*
import org.apache.commons.codec.digest.DigestUtils
import java.io.Writer
import java.math.BigInteger
import java.nio.channels.ClosedChannelException
import java.util.zip.GZIPInputStream
import java.util.zip.GZIPOutputStream
@@ -188,7 +191,6 @@ object Common {
var bai = 0
val buf = IntArray(4) { Ascii85.PAD_BYTE }
ba.forEach {
// b.bytesIterator().forEachRemaining {
if (bai > 0 && bai % 4 == 0) {
sb.append(Ascii85.encode(buf[0], buf[1], buf[2], buf[3]))
buf.fill(Ascii85.PAD_BYTE)
@@ -235,7 +237,6 @@ object Common {
var writeCursor = 0L
val sb = StringBuilder()
unzipdBytes.forEach {
// unasciidBytes.forEach {
if (writeCursor < layer.ptr.size) {
if (writeCursor < 1024) {
@@ -262,4 +263,46 @@ object Common {
return layer
}
}
class ByteArray64Writer() : Writer() {
private var closed = false
private val ba64 = ByteArray64()
init {
this.lock = ba64
}
private fun checkOpen() {
if (closed) throw ClosedChannelException()
}
override fun write(c: Int) {
checkOpen()
"${c.toChar()}".toByteArray().forEach { ba64.add(it) }
}
override fun write(cbuf: CharArray) {
checkOpen()
write(String(cbuf))
}
override fun write(str: String) {
checkOpen()
str.toByteArray().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")
}

View File

@@ -2,6 +2,7 @@ package net.torvald.terrarum.serialise
import net.torvald.terrarum.modulebasegame.TerrarumIngame
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.ByteArray64
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.ByteArray64GrowableOutputStream
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.ByteArray64OutputStream
import java.io.Writer
@@ -18,31 +19,21 @@ open class WriteWorld(val ingame: TerrarumIngame) {
}
fun encodeToByteArray64(): ByteArray64 {
/*val world = ingame.world
val world = ingame.world
world.genver = Common.GENVER
world.comp = Common.COMP_GZIP
val ba = ByteArray64()
val bao = ByteArray64OutputStream(ba)
val wr = object : Writer() {
override fun close() {
}
val baw = ByteArray64Writer()
override fun flush() {
}
Common.jsoner.toJson(world, baw)
baw.flush(); baw.close()
override fun write(cbuf: CharArray, off: Int, len: Int) {
bao.write(cbuf.copyOfRange(off, off + len).toString().toByteArray())
}
}
Common.jsoner.toJson(world, wr)
wr.flush(); wr.close()
return baw.toByteArray64()
return ba*/
val ba = ByteArray64()
/*val ba = ByteArray64()
this.invoke().toByteArray().forEach { ba.add(it) }
return ba
return ba*/
}
}