mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-13 23:26:07 +09:00
more world save and virtualdisk lib update
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -682,62 +682,3 @@ inline class FluidType(val value: Int) {
|
|||||||
fun abs() = this.value.absoluteValue
|
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()
|
|
||||||
}
|
|
||||||
@@ -41,20 +41,22 @@ object Save : ConsoleCommand {
|
|||||||
|
|
||||||
val disk = VDUtil.createNewDisk(1L shl 60, savename, Charsets.UTF_8)
|
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 metaContent = EntryFile(WriteMeta(ingame).encodeToByteArray64())
|
||||||
val meta = DiskEntry(32768, 0, "savegame.json".toByteArray(), creation_t, time_t, metaContent)
|
val meta = DiskEntry(0, 0, "savegame".toByteArray(), creation_t, time_t, metaContent)
|
||||||
VDUtil.addFile(disk, 0, meta, false)
|
VDUtil.addFile(disk, 0, meta)
|
||||||
|
|
||||||
val worldContent = EntryFile(WriteWorld(ingame).encodeToByteArray64())
|
val worldContent = EntryFile(WriteWorld(ingame).encodeToByteArray64())
|
||||||
val world = DiskEntry(ingame.world.worldIndex, 0, "world${ingame.world.worldIndex}.json".toByteArray(), creation_t, time_t, worldContent)
|
val world = DiskEntry(0, 0, "world${ingame.world.worldIndex}".toByteArray(), creation_t, time_t, worldContent)
|
||||||
VDUtil.addFile(disk, 0, world, false)
|
VDUtil.addFile(disk, 0, world)
|
||||||
|
|
||||||
listOf(ingame.actorContainerActive, ingame.actorContainerInactive).forEach { actors ->
|
listOf(ingame.actorContainerActive, ingame.actorContainerInactive).forEach { actors ->
|
||||||
actors.forEach {
|
actors.forEach {
|
||||||
if (acceptable(it)) {
|
if (acceptable(it)) {
|
||||||
val actorContent = EntryFile(WriteActor.encodeToByteArray64(it))
|
val actorContent = EntryFile(WriteActor.encodeToByteArray64(it))
|
||||||
val actor = DiskEntry(it.referenceID, 0, "actor${it.referenceID}.json".toByteArray(), creation_t, time_t, actorContent)
|
val actor = DiskEntry(0, 0, "actor${it.referenceID}".toByteArray(), creation_t, time_t, actorContent)
|
||||||
VDUtil.addFile(disk, 0, actor, false)
|
VDUtil.addFile(disk, 0, actor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.ByteArray64
|
||||||
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.ByteArray64GrowableOutputStream
|
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.ByteArray64GrowableOutputStream
|
||||||
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.ByteArray64InputStream
|
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.ByteArray64InputStream
|
||||||
|
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.ByteArray64OutputStream
|
||||||
import net.torvald.terrarum.utils.*
|
import net.torvald.terrarum.utils.*
|
||||||
import org.apache.commons.codec.digest.DigestUtils
|
import org.apache.commons.codec.digest.DigestUtils
|
||||||
|
import java.io.Writer
|
||||||
import java.math.BigInteger
|
import java.math.BigInteger
|
||||||
|
import java.nio.channels.ClosedChannelException
|
||||||
import java.util.zip.GZIPInputStream
|
import java.util.zip.GZIPInputStream
|
||||||
import java.util.zip.GZIPOutputStream
|
import java.util.zip.GZIPOutputStream
|
||||||
|
|
||||||
@@ -188,7 +191,6 @@ object Common {
|
|||||||
var bai = 0
|
var bai = 0
|
||||||
val buf = IntArray(4) { Ascii85.PAD_BYTE }
|
val buf = IntArray(4) { Ascii85.PAD_BYTE }
|
||||||
ba.forEach {
|
ba.forEach {
|
||||||
// b.bytesIterator().forEachRemaining {
|
|
||||||
if (bai > 0 && bai % 4 == 0) {
|
if (bai > 0 && bai % 4 == 0) {
|
||||||
sb.append(Ascii85.encode(buf[0], buf[1], buf[2], buf[3]))
|
sb.append(Ascii85.encode(buf[0], buf[1], buf[2], buf[3]))
|
||||||
buf.fill(Ascii85.PAD_BYTE)
|
buf.fill(Ascii85.PAD_BYTE)
|
||||||
@@ -235,7 +237,6 @@ object Common {
|
|||||||
var writeCursor = 0L
|
var writeCursor = 0L
|
||||||
val sb = StringBuilder()
|
val sb = StringBuilder()
|
||||||
unzipdBytes.forEach {
|
unzipdBytes.forEach {
|
||||||
// unasciidBytes.forEach {
|
|
||||||
if (writeCursor < layer.ptr.size) {
|
if (writeCursor < layer.ptr.size) {
|
||||||
|
|
||||||
if (writeCursor < 1024) {
|
if (writeCursor < 1024) {
|
||||||
@@ -262,4 +263,46 @@ object Common {
|
|||||||
|
|
||||||
return layer
|
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")
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,7 @@ package net.torvald.terrarum.serialise
|
|||||||
|
|
||||||
import net.torvald.terrarum.modulebasegame.TerrarumIngame
|
import net.torvald.terrarum.modulebasegame.TerrarumIngame
|
||||||
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.ByteArray64
|
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 net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.ByteArray64OutputStream
|
||||||
import java.io.Writer
|
import java.io.Writer
|
||||||
|
|
||||||
@@ -18,31 +19,21 @@ open class WriteWorld(val ingame: TerrarumIngame) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun encodeToByteArray64(): ByteArray64 {
|
fun encodeToByteArray64(): ByteArray64 {
|
||||||
/*val world = ingame.world
|
val world = ingame.world
|
||||||
world.genver = Common.GENVER
|
world.genver = Common.GENVER
|
||||||
world.comp = Common.COMP_GZIP
|
world.comp = Common.COMP_GZIP
|
||||||
|
|
||||||
val ba = ByteArray64()
|
val baw = ByteArray64Writer()
|
||||||
val bao = ByteArray64OutputStream(ba)
|
|
||||||
val wr = object : Writer() {
|
|
||||||
override fun close() {
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun flush() {
|
Common.jsoner.toJson(world, baw)
|
||||||
}
|
baw.flush(); baw.close()
|
||||||
|
|
||||||
override fun write(cbuf: CharArray, off: Int, len: Int) {
|
return baw.toByteArray64()
|
||||||
bao.write(cbuf.copyOfRange(off, off + len).toString().toByteArray())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Common.jsoner.toJson(world, wr)
|
|
||||||
wr.flush(); wr.close()
|
|
||||||
|
|
||||||
return ba*/
|
|
||||||
|
|
||||||
val ba = ByteArray64()
|
/*val ba = ByteArray64()
|
||||||
this.invoke().toByteArray().forEach { ba.add(it) }
|
this.invoke().toByteArray().forEach { ba.add(it) }
|
||||||
return ba
|
return ba*/
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user