From 4643c712692daacc9ec298af484104e0f622bf8c Mon Sep 17 00:00:00 2001 From: minjaesong Date: Fri, 5 Oct 2018 01:11:12 +0900 Subject: [PATCH] it's not zipping correctly; some fixes on readlayer --- .../torvald/terrarum/gameworld/GameWorld.kt | 42 ++++++++++++++++--- .../modulebasegame/console/ImportLayerData.kt | 17 ++++---- .../gameworld/GameWorldExtension.kt | 7 +++- .../terrarum/modulebasegame/ui/UIRemoCon.kt | 4 +- .../serialise/MarkableFileInputStream.java | 6 ++- .../terrarum/serialise/ReadLayerDataZip.kt | 34 +++++++++++++-- .../terrarum/serialise/WriteLayerDataZip.kt | 3 ++ work_files/DataFormats/Map data format.txt | 2 +- 8 files changed, 92 insertions(+), 23 deletions(-) diff --git a/src/net/torvald/terrarum/gameworld/GameWorld.kt b/src/net/torvald/terrarum/gameworld/GameWorld.kt index 47198e80e..0a7cd0dcb 100644 --- a/src/net/torvald/terrarum/gameworld/GameWorld.kt +++ b/src/net/torvald/terrarum/gameworld/GameWorld.kt @@ -4,12 +4,17 @@ package net.torvald.terrarum.gameworld import com.badlogic.gdx.graphics.Color import net.torvald.terrarum.realestate.LandUtil import net.torvald.terrarum.blockproperties.BlockCodex +import net.torvald.terrarum.serialise.ReadLayerDataZip import org.dyn4j.geometry.Vector2 typealias BlockAddress = Long typealias BlockDamage = Float -open class GameWorld(var worldIndex: Int, val width: Int, val height: Int) { +open class GameWorld { + + var worldIndex: Int + val width: Int + val height: Int //layers @@ -28,8 +33,8 @@ open class GameWorld(var worldIndex: Int, val width: Int, val height: Int) { /** Tilewise spawn point */ var spawnY: Int - val wallDamages = HashMap() - val terrainDamages = HashMap() + val wallDamages: HashMap + val terrainDamages: HashMap //public World physWorld = new World( new Vec2(0, -Terrarum.game.gravitationalAccel) ); //physics @@ -40,13 +45,15 @@ open class GameWorld(var worldIndex: Int, val width: Int, val height: Int) { var averageTemperature = 288f // 15 deg celsius; simulates global warming - - var generatorSeed: Long = 0 + internal set + constructor(worldIndex: Int, width: Int, height: Int) { + this.worldIndex = worldIndex + this.width = width + this.height = height - init { this.spawnX = width / 2 this.spawnY = 200 @@ -56,6 +63,9 @@ open class GameWorld(var worldIndex: Int, val width: Int, val height: Int) { layerTerrainLowBits = PairedMapLayer(width, height) layerWallLowBits = PairedMapLayer(width, height) + wallDamages = HashMap() + terrainDamages = HashMap() + // temperature layer: 2x2 is one cell //layerThermal = MapLayerHalfFloat(width / 2, height / 2, averageTemperature) @@ -63,6 +73,26 @@ open class GameWorld(var worldIndex: Int, val width: Int, val height: Int) { //layerAirPressure = MapLayerHalfFloat(width / 4, height / 8, 13f) // 1013 mBar } + internal constructor(worldIndex: Int, layerData: ReadLayerDataZip.LayerData) { + this.worldIndex = worldIndex + + layerTerrain = layerData.layerTerrain + layerWall = layerData.layerWall + layerWire = layerData.layerWire + layerTerrainLowBits = layerData.layerTerrainLowBits + layerWallLowBits = layerData.layerWallLowBits + + wallDamages = layerData.wallDamages + terrainDamages = layerData.terrainDamages + + spawnX = layerData.spawnX + spawnY = layerData.spawnY + + width = layerTerrain.width + height = layerTerrain.height + } + + /** * Get 2d array data of terrain diff --git a/src/net/torvald/terrarum/modulebasegame/console/ImportLayerData.kt b/src/net/torvald/terrarum/modulebasegame/console/ImportLayerData.kt index 03fdaf1e4..9d24da282 100644 --- a/src/net/torvald/terrarum/modulebasegame/console/ImportLayerData.kt +++ b/src/net/torvald/terrarum/modulebasegame/console/ImportLayerData.kt @@ -3,10 +3,10 @@ package net.torvald.terrarum.modulebasegame.console import net.torvald.terrarum.Terrarum import net.torvald.terrarum.console.ConsoleCommand import net.torvald.terrarum.console.Echo -import net.torvald.terrarum.modulebasegame.Ingame -import net.torvald.terrarum.serialise.ReadLayerData +import net.torvald.terrarum.modulebasegame.gameworld.GameWorldExtension +import net.torvald.terrarum.serialise.ReadLayerDataZip import net.torvald.terrarum.worlddrawer.FeaturesDrawer -import java.io.FileInputStream +import java.io.File /** * Created by minjaesong on 2017-07-18. @@ -18,14 +18,15 @@ object ImportLayerData : ConsoleCommand { return } - //val fis = GZIPInputStream(FileInputStream(args[1])) // this gzip is kaput - val fis = FileInputStream(args[1]) - (Terrarum.ingame!!.world) = ReadLayerData(fis) - (Terrarum.ingame!! as Ingame).actorNowPlaying?.setPosition( + val file = File(args[1]) + val layerData = ReadLayerDataZip(file) + + + Terrarum.ingame!!.world = GameWorldExtension(1, layerData) + Terrarum.ingame!!.actorNowPlaying?.setPosition( (Terrarum.ingame!!.world).spawnY * FeaturesDrawer.TILE_SIZE.toDouble(), (Terrarum.ingame!!.world).spawnX * FeaturesDrawer.TILE_SIZE.toDouble() ) - fis.close() Echo("Successfully loaded ${args[1]}") } diff --git a/src/net/torvald/terrarum/modulebasegame/gameworld/GameWorldExtension.kt b/src/net/torvald/terrarum/modulebasegame/gameworld/GameWorldExtension.kt index 02add8a3c..41651078c 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameworld/GameWorldExtension.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameworld/GameWorldExtension.kt @@ -2,12 +2,17 @@ package net.torvald.terrarum.modulebasegame.gameworld import com.badlogic.gdx.graphics.Color import net.torvald.terrarum.gameworld.* +import net.torvald.terrarum.serialise.ReadLayerDataZip import kotlin.properties.Delegates /** * Created by minjaesong on 2018-07-03. */ -class GameWorldExtension(worldIndex: Int, width: Int, height: Int): GameWorld(worldIndex, width, height) { +class GameWorldExtension: GameWorld { + + constructor(worldIndex: Int, width: Int, height: Int) : super(worldIndex, width, height) + internal constructor(worldIndex: Int, layerData: ReadLayerDataZip.LayerData) : super(worldIndex, layerData) + val time: WorldTime val economy = GameEconomy() diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIRemoCon.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIRemoCon.kt index 7c2c5d9da..819a3b3ce 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIRemoCon.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIRemoCon.kt @@ -115,8 +115,8 @@ open class UIRemoCon(treeRepresentation: QNDTreeNode) : UICanvas() { printdbg(this, 1) val ingame = Ingame(Terrarum.batch) - //ingame.gameLoadInfoPayload = Ingame.NewWorldParameters(2400, 800, HQRNG().nextLong()) - ingame.gameLoadInfoPayload = Ingame.NewWorldParameters(8192, 2048, 0x51621DL) + ingame.gameLoadInfoPayload = Ingame.NewWorldParameters(2400, 800, HQRNG().nextLong()) + //ingame.gameLoadInfoPayload = Ingame.NewWorldParameters(8192, 2048, 0x51621DL) ingame.gameLoadMode = Ingame.GameLoadMode.CREATE_NEW printdbg(this, 2) diff --git a/src/net/torvald/terrarum/serialise/MarkableFileInputStream.java b/src/net/torvald/terrarum/serialise/MarkableFileInputStream.java index 55b3733e4..9a4d5f788 100644 --- a/src/net/torvald/terrarum/serialise/MarkableFileInputStream.java +++ b/src/net/torvald/terrarum/serialise/MarkableFileInputStream.java @@ -12,7 +12,7 @@ import java.nio.channels.FileChannel; */ public class MarkableFileInputStream extends FilterInputStream { private FileChannel myFileChannel; - private long mark = -1; + private long mark = 0; public MarkableFileInputStream(FileInputStream fis) { super(fis); @@ -29,7 +29,9 @@ public class MarkableFileInputStream extends FilterInputStream { try { mark = myFileChannel.position(); } catch (IOException ex) { - mark = -1; + //mark = -1; + System.err.println("MarkableFileInputStream: mark failed"); + ex.printStackTrace(); } } diff --git a/src/net/torvald/terrarum/serialise/ReadLayerDataZip.kt b/src/net/torvald/terrarum/serialise/ReadLayerDataZip.kt index abb0789b5..0a8d67179 100644 --- a/src/net/torvald/terrarum/serialise/ReadLayerDataZip.kt +++ b/src/net/torvald/terrarum/serialise/ReadLayerDataZip.kt @@ -1,11 +1,13 @@ package net.torvald.terrarum.serialise +import net.torvald.terrarum.AppLoader.printdbg import net.torvald.terrarum.gameworld.BlockAddress import net.torvald.terrarum.gameworld.BlockDamage import net.torvald.terrarum.gameworld.MapLayer import net.torvald.terrarum.gameworld.PairedMapLayer import net.torvald.terrarum.realestate.LandUtil import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.DiskSkimmer.Companion.read +import net.torvald.terrarum.toHex import java.io.* import java.nio.charset.Charset import java.util.* @@ -47,6 +49,12 @@ internal object ReadLayerDataZip { val height = inputStream.read(4).toLittleInt() val spawnAddress = inputStream.read(6).toLittleInt48() + printdbg(this, "Version number: $versionNumber") + printdbg(this, "Layers count: $layerCount") + printdbg(this, "Payloads count: $payloadCount") + printdbg(this, "Compression: $compression") + printdbg(this, "Dimension: ${width}x$height") + // read payloads val pldBuffer4 = ByteArray(4) @@ -60,12 +68,14 @@ internal object ReadLayerDataZip { // check payload header if (!pldBuffer4.contentEquals(WriteLayerDataZip.PAYLOAD_HEADER)) - throw InternalError("Payload not found") + throw InternalError("Payload $pldCnt not found -- expected ${WriteLayerDataZip.PAYLOAD_HEADER.toByteString()}, got ${pldBuffer4.toByteString()}") // get payload's name inputStream.read(pldBuffer4) val payloadName = pldBuffer4.toString(Charset.forName("US-ASCII")) + printdbg(this, "Payload $pldCnt name: $payloadName") // maybe maybe related with buffer things? + // get uncompressed size inputStream.read(pldBuffer6) val uncompressedSize = pldBuffer6.toLittleInt48() @@ -79,18 +89,26 @@ internal object ReadLayerDataZip { // loop main while (!pldBuffer8.contentEquals(WriteLayerDataZip.PAYLOAD_FOOTER)) { val aByte = inputStream.read(); deflatedSize += 1 - if (aByte == -1) throw InternalError("Unexpected end-of-file") + if (aByte == -1) throw InternalError("Unexpected end-of-file at payload $pldCnt") pldBuffer8.shiftLeftBy(1, aByte.toByte()) } + // at this point, we should have correct size of deflated bytestream + + printdbg(this, "Payload $pldCnt compressed size: $deflatedSize") + val deflatedBytes = ByteArray(deflatedSize) // FIXME deflated stream cannot be larger than 2 GB inputStream.reset() // go back to marked spot inputStream.read(deflatedBytes) + + // PRO Debug tip: every deflated bytes must begin with 0x789C or 0x78DA + // Thus, \0pLd + [10] must be either of these. + // put constructed payload into a container payloads.put(payloadName, TEMzPayload(uncompressedSize, deflatedBytes)) // skip over to be aligned with the next payload - inputStream.skip(18L + deflatedSize) + inputStream.skip(8) } @@ -239,4 +257,14 @@ internal object ReadLayerDataZip { return i } + + fun ByteArray.toByteString(): String { + val sb = StringBuilder() + this.forEach { + sb.append(it.toUint().toHex().takeLast(2)) + sb.append(' ') + } + sb.deleteCharAt(sb.lastIndex) + return sb.toString() + } } \ No newline at end of file diff --git a/src/net/torvald/terrarum/serialise/WriteLayerDataZip.kt b/src/net/torvald/terrarum/serialise/WriteLayerDataZip.kt index e9dabf0eb..ef4f31379 100644 --- a/src/net/torvald/terrarum/serialise/WriteLayerDataZip.kt +++ b/src/net/torvald/terrarum/serialise/WriteLayerDataZip.kt @@ -100,6 +100,9 @@ internal object WriteLayerDataZip { outputStream.flush() // TERR payload + // PRO Debug tip: every deflated bytes must begin with 0x789C or 0x78DA + // Thus, \0pLd + [10] must be either of these. + wb(PAYLOAD_HEADER); wb("TERR".toByteArray()) wi48(world.width * world.height * 3L / 2) deflater.write(world.terrainArray) diff --git a/work_files/DataFormats/Map data format.txt b/work_files/DataFormats/Map data format.txt index 429c60bb0..aa818a872 100644 --- a/work_files/DataFormats/Map data format.txt +++ b/work_files/DataFormats/Map data format.txt @@ -42,7 +42,7 @@ Ord Hex Description # "\0pLd" Payload header [00, 70, 4C, 64] # [4] Identifier. 4 lettres ASCII string # [6] Uncompressed size of DEFLATEd binary (max size 256 TB) -# [..] DEFLATEd binary +# [..] DEFLATEd binary (begins with one of these: 0x789C, 0x78DA, 0x7801) # "EndPYLd\xFF" Payload footer [45, 6E, 64, 50, 59, 4C, 64, FF]