mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-08 04:41:51 +09:00
more Kotlin, UIs now can open/close with animation, working world time with skybox colour changing (sunlight transition is still WIP), fixed project library settings so that the suggestion in IntelliJ IDEA would work properly
Former-commit-id: 82c857164f2636ad943d5a43f0690a3431ab0f26 Former-commit-id: 7c8b70e6b267f7c5c050d7d0902278739ec18b44
This commit is contained in:
79
src/com/Torvald/Serialise/WriteGameMapData.kt
Normal file
79
src/com/Torvald/Serialise/WriteGameMapData.kt
Normal file
@@ -0,0 +1,79 @@
|
||||
package com.Torvald.Serialise
|
||||
|
||||
import com.Torvald.Terrarum.GameMap.GameMap
|
||||
import com.Torvald.Terrarum.Terrarum
|
||||
import java.io.IOException
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.Paths
|
||||
import java.nio.file.StandardCopyOption
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 16-03-18.
|
||||
*/
|
||||
object WriteGameMapData {
|
||||
|
||||
val META_FILENAME = "worldinfo1"
|
||||
|
||||
val MAGIC: ByteArray = byteArrayOf(
|
||||
'T'.toByte(),
|
||||
'E'.toByte(),
|
||||
'M'.toByte(),
|
||||
'D'.toByte()
|
||||
)
|
||||
|
||||
val BYTE_NULL: Byte = 0
|
||||
|
||||
|
||||
fun write(saveDirectoryName: String): Boolean {
|
||||
val path = Paths.get("${Terrarum.defaultSaveDir}" +
|
||||
"/$saveDirectoryName/${WriteMeta.META_FILENAME}")
|
||||
val tempPath = Files.createTempFile(path.toString(), "_temp")
|
||||
val map = Terrarum.game.map
|
||||
|
||||
// TODO gzip
|
||||
|
||||
// write binary
|
||||
Files.write(tempPath, MAGIC)
|
||||
Files.write(tempPath, byteArrayOf(GameMap.BITS))
|
||||
Files.write(tempPath, byteArrayOf(GameMap.LAYERS))
|
||||
Files.write(tempPath, byteArrayOf(BYTE_NULL))
|
||||
Files.write(tempPath, byteArrayOf(BYTE_NULL))
|
||||
Files.write(tempPath, toByteArray(map.width))
|
||||
Files.write(tempPath, toByteArray(map.height))
|
||||
Files.write(tempPath, toByteArray(map.spawnX))
|
||||
Files.write(tempPath, toByteArray(map.spawnY))
|
||||
map.layerTerrain.forEach(
|
||||
{b -> Files.write(tempPath, byteArrayOf(b))})
|
||||
map.layerWall.forEach(
|
||||
{b -> Files.write(tempPath, byteArrayOf(b))})
|
||||
map.terrainDamage.forEach(
|
||||
{b -> Files.write(tempPath, byteArrayOf(b))})
|
||||
map.wallDamage.forEach(
|
||||
{b -> Files.write(tempPath, byteArrayOf(b))})
|
||||
map.layerWire.forEach(
|
||||
{b -> Files.write(tempPath, byteArrayOf(b))})
|
||||
|
||||
// replace savemeta with tempfile
|
||||
try {
|
||||
Files.copy(tempPath, path, StandardCopyOption.REPLACE_EXISTING)
|
||||
Files.deleteIfExists(tempPath)
|
||||
println("Saved map data '$META_FILENAME' to $saveDirectoryName.")
|
||||
|
||||
return true
|
||||
}
|
||||
catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
fun toByteArray(int: Int): ByteArray {
|
||||
return byteArrayOf(
|
||||
((int ushr 0x18) and 0xFF).toByte(),
|
||||
((int ushr 0x10) and 0xFF).toByte(),
|
||||
((int ushr 0x08) and 0xFF).toByte(),
|
||||
((int ) and 0xFF).toByte()
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user