seemingly working savewriter with fixed TEVD

This commit is contained in:
minjaesong
2019-02-23 05:03:20 +09:00
parent 58f017e264
commit 73dfab206e
8 changed files with 124 additions and 40 deletions

View File

@@ -1,7 +1,7 @@
package net.torvald.terrarum.serialise
import com.badlogic.gdx.Gdx
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import net.torvald.terrarum.AppLoader
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.gameactors.AVKey
@@ -24,8 +24,8 @@ object SavegameWriter {
private lateinit var playerName: String
operator fun invoke(): Boolean {
playerName = "${Terrarum.ingame!!.actorGamer!!.actorValue[AVKey.NAME]}"
operator fun invoke(pnameOverride: String? = null): Boolean {
playerName = pnameOverride ?: "${Terrarum.ingame!!.actorGamer!!.actorValue[AVKey.NAME]}"
if (playerName.isEmpty()) playerName = "Test subject ${Math.random().times(0x7FFFFFFF).roundInt()}"
try {
@@ -58,6 +58,14 @@ object SavegameWriter {
throw Error("Serialising world failed")
}
if (!worldBytes.sliceArray(0..3).contentEquals(WriteLayerDataZip.MAGIC)) {
worldBytes.forEach {
print(it.toUInt().and(255u).toString(16).toUpperCase().padStart(2, '0'))
print(' ')
}; println()
throw Error()
}
// add current world (stage) to the disk
VDUtil.registerFile(disk, DiskEntry(
gameworld.worldIndex, ROOT,
@@ -91,7 +99,7 @@ object SavegameWriter {
// actors
ingame.actorContainerActive.forEach {
VDUtil.registerFile(disk, DiskEntry(
gameworld.worldIndex, ROOT,
it.referenceID!!, ROOT,
it.referenceID!!.toString(16).toUpperCase().toByteArray(charset),
creationDate, creationDate,
EntryFile(serialiseActor(it))
@@ -99,7 +107,7 @@ object SavegameWriter {
}
ingame.actorContainerInactive.forEach {
VDUtil.registerFile(disk, DiskEntry(
gameworld.worldIndex, ROOT,
it.referenceID!!, ROOT,
it.referenceID!!.toString(16).toUpperCase().toByteArray(charset),
creationDate, creationDate,
EntryFile(serialiseActor(it))
@@ -109,7 +117,7 @@ object SavegameWriter {
// items
ItemCodex.dynamicItemDescription.forEach { dynamicID, item ->
VDUtil.registerFile(disk, DiskEntry(
gameworld.worldIndex, ROOT,
item.dynamicID, ROOT,
dynamicID.toString(16).toUpperCase().toByteArray(charset),
creationDate, creationDate,
EntryFile(serialiseItem(item))
@@ -125,13 +133,26 @@ object SavegameWriter {
TODO()
}
fun getJsonBuilder() = if (AppLoader.IS_DEVELOPMENT_BUILD) {
GsonBuilder()
.setPrettyPrinting()
.serializeNulls()
.create()
}
else {
GsonBuilder()
.serializeNulls()
.create()
}
private fun serialiseActor(a: Actor): ByteArray64 {
val gson = Gson().toJsonTree(a).toString().toByteArray(charset)
val gson = getJsonBuilder().toJson(a).toByteArray(charset)
return ByteArray64.fromByteArray(gson)
}
private fun serialiseItem(i: GameItem): ByteArray64 {
val gson = Gson().toJsonTree(i).toString().toByteArray(charset)
val gson = getJsonBuilder().toJson(i).toByteArray(charset)
return ByteArray64.fromByteArray(gson)
}
}

View File

@@ -4,7 +4,6 @@ import net.torvald.terrarum.gameworld.GameWorld
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.ByteArray64
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.ByteArray64GrowableOutputStream
import net.torvald.terrarum.realestate.LandUtil
import java.io.IOException
import java.util.zip.Deflater
import java.util.zip.DeflaterOutputStream
@@ -99,12 +98,13 @@ internal object WriteLayerDataZip {
wi48(LandUtil.getBlockAddr(world, world.spawnX, world.spawnY))
// write payloads //
outputStream.flush()
// TERR payload
// PRO Debug tip: every deflated bytes must begin with 0x789C or 0x78DA
// Thus, \0pLd + [10] must be either of these.
// TODO serialised payloads have bit too much zeros, should I be worried?
wb(PAYLOAD_HEADER); wb("TERR".toByteArray())
wi48(world.width * world.height * 3L / 2)
deflater = DeflaterOutputStream(outputStream, Deflater(Deflater.BEST_COMPRESSION), true)
@@ -187,8 +187,6 @@ internal object WriteLayerDataZip {
wb(PAYLOAD_FOOTER)
// write footer
wb(FILE_FOOTER)
@@ -197,21 +195,10 @@ internal object WriteLayerDataZip {
// END OF WRITE //
//////////////////
try {
outputStream.flush()
outputStream.close()
outputStream.flush()
outputStream.close()
return outputStream.toByteArray64()
}
catch (e: IOException) {
e.printStackTrace()
}
finally {
outputStream.close()
}
return null
return outputStream.toByteArray64()
}