it's not zipping correctly; some fixes on readlayer

This commit is contained in:
minjaesong
2018-10-05 01:11:12 +09:00
parent 374070e9b9
commit 1d67a9a9ce
8 changed files with 92 additions and 23 deletions

View File

@@ -4,12 +4,17 @@ package net.torvald.terrarum.gameworld
import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.Color
import net.torvald.terrarum.realestate.LandUtil import net.torvald.terrarum.realestate.LandUtil
import net.torvald.terrarum.blockproperties.BlockCodex import net.torvald.terrarum.blockproperties.BlockCodex
import net.torvald.terrarum.serialise.ReadLayerDataZip
import org.dyn4j.geometry.Vector2 import org.dyn4j.geometry.Vector2
typealias BlockAddress = Long typealias BlockAddress = Long
typealias BlockDamage = Float 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 //layers
@@ -28,8 +33,8 @@ open class GameWorld(var worldIndex: Int, val width: Int, val height: Int) {
/** Tilewise spawn point */ /** Tilewise spawn point */
var spawnY: Int var spawnY: Int
val wallDamages = HashMap<BlockAddress, BlockDamage>() val wallDamages: HashMap<BlockAddress, BlockDamage>
val terrainDamages = HashMap<BlockAddress, BlockDamage>() val terrainDamages: HashMap<BlockAddress, BlockDamage>
//public World physWorld = new World( new Vec2(0, -Terrarum.game.gravitationalAccel) ); //public World physWorld = new World( new Vec2(0, -Terrarum.game.gravitationalAccel) );
//physics //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 averageTemperature = 288f // 15 deg celsius; simulates global warming
var generatorSeed: Long = 0 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.spawnX = width / 2
this.spawnY = 200 this.spawnY = 200
@@ -56,6 +63,9 @@ open class GameWorld(var worldIndex: Int, val width: Int, val height: Int) {
layerTerrainLowBits = PairedMapLayer(width, height) layerTerrainLowBits = PairedMapLayer(width, height)
layerWallLowBits = PairedMapLayer(width, height) layerWallLowBits = PairedMapLayer(width, height)
wallDamages = HashMap<BlockAddress, BlockDamage>()
terrainDamages = HashMap<BlockAddress, BlockDamage>()
// temperature layer: 2x2 is one cell // temperature layer: 2x2 is one cell
//layerThermal = MapLayerHalfFloat(width / 2, height / 2, averageTemperature) //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 //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 * Get 2d array data of terrain

View File

@@ -3,10 +3,10 @@ package net.torvald.terrarum.modulebasegame.console
import net.torvald.terrarum.Terrarum import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.console.ConsoleCommand import net.torvald.terrarum.console.ConsoleCommand
import net.torvald.terrarum.console.Echo import net.torvald.terrarum.console.Echo
import net.torvald.terrarum.modulebasegame.Ingame import net.torvald.terrarum.modulebasegame.gameworld.GameWorldExtension
import net.torvald.terrarum.serialise.ReadLayerData import net.torvald.terrarum.serialise.ReadLayerDataZip
import net.torvald.terrarum.worlddrawer.FeaturesDrawer import net.torvald.terrarum.worlddrawer.FeaturesDrawer
import java.io.FileInputStream import java.io.File
/** /**
* Created by minjaesong on 2017-07-18. * Created by minjaesong on 2017-07-18.
@@ -18,14 +18,15 @@ object ImportLayerData : ConsoleCommand {
return return
} }
//val fis = GZIPInputStream(FileInputStream(args[1])) // this gzip is kaput val file = File(args[1])
val fis = FileInputStream(args[1]) val layerData = ReadLayerDataZip(file)
(Terrarum.ingame!!.world) = ReadLayerData(fis)
(Terrarum.ingame!! as Ingame).actorNowPlaying?.setPosition(
Terrarum.ingame!!.world = GameWorldExtension(1, layerData)
Terrarum.ingame!!.actorNowPlaying?.setPosition(
(Terrarum.ingame!!.world).spawnY * FeaturesDrawer.TILE_SIZE.toDouble(), (Terrarum.ingame!!.world).spawnY * FeaturesDrawer.TILE_SIZE.toDouble(),
(Terrarum.ingame!!.world).spawnX * FeaturesDrawer.TILE_SIZE.toDouble() (Terrarum.ingame!!.world).spawnX * FeaturesDrawer.TILE_SIZE.toDouble()
) )
fis.close()
Echo("Successfully loaded ${args[1]}") Echo("Successfully loaded ${args[1]}")
} }

View File

@@ -2,12 +2,17 @@ package net.torvald.terrarum.modulebasegame.gameworld
import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.Color
import net.torvald.terrarum.gameworld.* import net.torvald.terrarum.gameworld.*
import net.torvald.terrarum.serialise.ReadLayerDataZip
import kotlin.properties.Delegates import kotlin.properties.Delegates
/** /**
* Created by minjaesong on 2018-07-03. * 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 time: WorldTime
val economy = GameEconomy() val economy = GameEconomy()

View File

@@ -115,8 +115,8 @@ open class UIRemoCon(treeRepresentation: QNDTreeNode<String>) : UICanvas() {
printdbg(this, 1) printdbg(this, 1)
val ingame = Ingame(Terrarum.batch) val ingame = Ingame(Terrarum.batch)
//ingame.gameLoadInfoPayload = Ingame.NewWorldParameters(2400, 800, HQRNG().nextLong()) ingame.gameLoadInfoPayload = Ingame.NewWorldParameters(2400, 800, HQRNG().nextLong())
ingame.gameLoadInfoPayload = Ingame.NewWorldParameters(8192, 2048, 0x51621DL) //ingame.gameLoadInfoPayload = Ingame.NewWorldParameters(8192, 2048, 0x51621DL)
ingame.gameLoadMode = Ingame.GameLoadMode.CREATE_NEW ingame.gameLoadMode = Ingame.GameLoadMode.CREATE_NEW
printdbg(this, 2) printdbg(this, 2)

View File

@@ -12,7 +12,7 @@ import java.nio.channels.FileChannel;
*/ */
public class MarkableFileInputStream extends FilterInputStream { public class MarkableFileInputStream extends FilterInputStream {
private FileChannel myFileChannel; private FileChannel myFileChannel;
private long mark = -1; private long mark = 0;
public MarkableFileInputStream(FileInputStream fis) { public MarkableFileInputStream(FileInputStream fis) {
super(fis); super(fis);
@@ -29,7 +29,9 @@ public class MarkableFileInputStream extends FilterInputStream {
try { try {
mark = myFileChannel.position(); mark = myFileChannel.position();
} catch (IOException ex) { } catch (IOException ex) {
mark = -1; //mark = -1;
System.err.println("MarkableFileInputStream: mark failed");
ex.printStackTrace();
} }
} }

View File

@@ -1,11 +1,13 @@
package net.torvald.terrarum.serialise package net.torvald.terrarum.serialise
import net.torvald.terrarum.AppLoader.printdbg
import net.torvald.terrarum.gameworld.BlockAddress import net.torvald.terrarum.gameworld.BlockAddress
import net.torvald.terrarum.gameworld.BlockDamage import net.torvald.terrarum.gameworld.BlockDamage
import net.torvald.terrarum.gameworld.MapLayer import net.torvald.terrarum.gameworld.MapLayer
import net.torvald.terrarum.gameworld.PairedMapLayer import net.torvald.terrarum.gameworld.PairedMapLayer
import net.torvald.terrarum.realestate.LandUtil import net.torvald.terrarum.realestate.LandUtil
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.DiskSkimmer.Companion.read import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.DiskSkimmer.Companion.read
import net.torvald.terrarum.toHex
import java.io.* import java.io.*
import java.nio.charset.Charset import java.nio.charset.Charset
import java.util.* import java.util.*
@@ -47,6 +49,12 @@ internal object ReadLayerDataZip {
val height = inputStream.read(4).toLittleInt() val height = inputStream.read(4).toLittleInt()
val spawnAddress = inputStream.read(6).toLittleInt48() 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 // read payloads
val pldBuffer4 = ByteArray(4) val pldBuffer4 = ByteArray(4)
@@ -60,12 +68,14 @@ internal object ReadLayerDataZip {
// check payload header // check payload header
if (!pldBuffer4.contentEquals(WriteLayerDataZip.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 // get payload's name
inputStream.read(pldBuffer4) inputStream.read(pldBuffer4)
val payloadName = pldBuffer4.toString(Charset.forName("US-ASCII")) val payloadName = pldBuffer4.toString(Charset.forName("US-ASCII"))
printdbg(this, "Payload $pldCnt name: $payloadName") // maybe maybe related with buffer things?
// get uncompressed size // get uncompressed size
inputStream.read(pldBuffer6) inputStream.read(pldBuffer6)
val uncompressedSize = pldBuffer6.toLittleInt48() val uncompressedSize = pldBuffer6.toLittleInt48()
@@ -79,18 +89,26 @@ internal object ReadLayerDataZip {
// loop main // loop main
while (!pldBuffer8.contentEquals(WriteLayerDataZip.PAYLOAD_FOOTER)) { while (!pldBuffer8.contentEquals(WriteLayerDataZip.PAYLOAD_FOOTER)) {
val aByte = inputStream.read(); deflatedSize += 1 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()) pldBuffer8.shiftLeftBy(1, aByte.toByte())
} }
// at this point, we should have correct size of deflated bytestream // 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 val deflatedBytes = ByteArray(deflatedSize) // FIXME deflated stream cannot be larger than 2 GB
inputStream.reset() // go back to marked spot inputStream.reset() // go back to marked spot
inputStream.read(deflatedBytes) 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 // put constructed payload into a container
payloads.put(payloadName, TEMzPayload(uncompressedSize, deflatedBytes)) payloads.put(payloadName, TEMzPayload(uncompressedSize, deflatedBytes))
// skip over to be aligned with the next payload // 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 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()
}
} }

View File

@@ -100,6 +100,9 @@ internal object WriteLayerDataZip {
outputStream.flush() outputStream.flush()
// TERR payload // 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()) wb(PAYLOAD_HEADER); wb("TERR".toByteArray())
wi48(world.width * world.height * 3L / 2) wi48(world.width * world.height * 3L / 2)
deflater.write(world.terrainArray) deflater.write(world.terrainArray)

View File

@@ -42,7 +42,7 @@ Ord Hex Description
# "\0pLd" Payload header [00, 70, 4C, 64] # "\0pLd" Payload header [00, 70, 4C, 64]
# [4] Identifier. 4 lettres ASCII string # [4] Identifier. 4 lettres ASCII string
# [6] Uncompressed size of DEFLATEd binary (max size 256 TB) # [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] # "EndPYLd\xFF" Payload footer [45, 6E, 64, 50, 59, 4C, 64, FF]