mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-11 11:04:05 +09:00
com.torvald → net.torvald
Former-commit-id: 375604da8a20a6ba7cd0a8d05a44add02b2d04f4 Former-commit-id: 287287c5920b07618174d7a7573f049d350ded66
This commit is contained in:
65
src/net/torvald/serialise/WriteCSV.kt
Normal file
65
src/net/torvald/serialise/WriteCSV.kt
Normal file
@@ -0,0 +1,65 @@
|
||||
package net.torvald.serialise
|
||||
|
||||
import net.torvald.CSVFetcher
|
||||
import net.torvald.terrarum.itemproperties.ItemPropCodex
|
||||
import net.torvald.terrarum.itemproperties.MaterialPropCodex
|
||||
import net.torvald.terrarum.Terrarum
|
||||
import net.torvald.terrarum.tileproperties.TilePropCodex
|
||||
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 WriteCSV {
|
||||
val META_FILENAME_TILE = "worldinfo2"
|
||||
val META_FILENAME_ITEM = "worldinfo3"
|
||||
val META_FILENAME_MAT = "worldinfo4"
|
||||
|
||||
fun write(saveDirectoryName: String): Boolean {
|
||||
val tileCSV = CSVFetcher.readCSVasString(TilePropCodex.CSV_PATH)
|
||||
val itemCSV = CSVFetcher.readCSVasString(ItemPropCodex.CSV_PATH)
|
||||
val matCSV = CSVFetcher.readCSVasString(MaterialPropCodex.CSV_PATH)
|
||||
|
||||
val pathTile = Paths.get("${Terrarum.defaultSaveDir}" +
|
||||
"/$saveDirectoryName/${META_FILENAME_TILE}")
|
||||
val pathItem = Paths.get("${Terrarum.defaultSaveDir}" +
|
||||
"/$saveDirectoryName/${META_FILENAME_ITEM}")
|
||||
val pathMat = Paths.get("${Terrarum.defaultSaveDir}" +
|
||||
"/$saveDirectoryName/${META_FILENAME_MAT}")
|
||||
val tempPathTile = Files.createTempFile(pathTile.toString(), "_temp")
|
||||
val tempPathItem = Files.createTempFile(pathItem.toString(), "_temp")
|
||||
val tempPathMat = Files.createTempFile(pathMat.toString(), "_temp")
|
||||
|
||||
// TODO gzip
|
||||
|
||||
// write CSV to path
|
||||
Files.write(tempPathTile, tileCSV.toByteArray(Charsets.UTF_8))
|
||||
Files.write(tempPathItem, itemCSV.toByteArray(Charsets.UTF_8))
|
||||
Files.write(tempPathMat, matCSV.toByteArray(Charsets.UTF_8))
|
||||
|
||||
// replace savemeta with tempfile
|
||||
try {
|
||||
Files.copy(tempPathTile, pathTile, StandardCopyOption.REPLACE_EXISTING)
|
||||
Files.deleteIfExists(tempPathTile)
|
||||
|
||||
Files.copy(tempPathItem, pathItem, StandardCopyOption.REPLACE_EXISTING)
|
||||
Files.deleteIfExists(tempPathItem)
|
||||
|
||||
Files.copy(tempPathMat, pathMat, StandardCopyOption.REPLACE_EXISTING)
|
||||
Files.deleteIfExists(tempPathMat)
|
||||
|
||||
println("Saved map data '${WriteGameMapData.META_FILENAME}' to $saveDirectoryName.")
|
||||
|
||||
return true
|
||||
}
|
||||
catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
}
|
||||
79
src/net/torvald/serialise/WriteGameMapData.kt
Normal file
79
src/net/torvald/serialise/WriteGameMapData.kt
Normal file
@@ -0,0 +1,79 @@
|
||||
package net.torvald.serialise
|
||||
|
||||
import net.torvald.terrarum.gamemap.GameMap
|
||||
import net.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()
|
||||
)
|
||||
}
|
||||
}
|
||||
101
src/net/torvald/serialise/WriteMeta.kt
Normal file
101
src/net/torvald/serialise/WriteMeta.kt
Normal file
@@ -0,0 +1,101 @@
|
||||
package net.torvald.serialise
|
||||
|
||||
import net.torvald.terrarum.mapgenerator.MapGenerator
|
||||
import net.torvald.terrarum.mapgenerator.RoguelikeRandomiser
|
||||
import net.torvald.terrarum.Terrarum
|
||||
import net.torvald.terrarum.tileproperties.TilePropCodex
|
||||
import org.apache.commons.codec.digest.DigestUtils
|
||||
import java.io.FileInputStream
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import java.nio.file.*
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 16-03-15.
|
||||
*/
|
||||
object WriteMeta {
|
||||
|
||||
val META_FILENAME = "world"
|
||||
|
||||
val MAGIC: ByteArray = byteArrayOf(
|
||||
'T'.toByte(),
|
||||
'E'.toByte(),
|
||||
'S'.toByte(),
|
||||
'V'.toByte()
|
||||
)
|
||||
|
||||
val BYTE_NULL: Byte = 0
|
||||
|
||||
val terraseed: Long = MapGenerator.SEED
|
||||
val rogueseed: Long = RoguelikeRandomiser.seed
|
||||
|
||||
/**
|
||||
* Write save meta to specified directory. Returns false if something went wrong.
|
||||
* @param saveDirectoryName
|
||||
* @param savegameName -- Nullable. If the value is not specified, saveDirectoryName will be used instead.
|
||||
*/
|
||||
fun write(saveDirectoryName: String, savegameName: String?): Boolean {
|
||||
val hashArray: ArrayList<ByteArray> = ArrayList()
|
||||
val savenameAsByteArray: ByteArray =
|
||||
(savegameName ?: saveDirectoryName).toByteArray(Charsets.UTF_8)
|
||||
|
||||
// define files to get hash
|
||||
val fileArray: Array<File> = arrayOf(
|
||||
File(TilePropCodex.CSV_PATH)
|
||||
//, File(ItemPropCodex.CSV_PATH)
|
||||
//, File(MaterialPropCodex.CSV_PATH)
|
||||
//,
|
||||
)
|
||||
|
||||
// get and store hash from fileArray
|
||||
for (file in fileArray) {
|
||||
val inputStream = FileInputStream(file)
|
||||
val hash = DigestUtils.sha256(inputStream)
|
||||
|
||||
hashArray.add(hash)
|
||||
}
|
||||
|
||||
// open file and delete it
|
||||
val metaPath = Paths.get("${Terrarum.defaultSaveDir}" +
|
||||
"/$saveDirectoryName/$META_FILENAME")
|
||||
val metaTempPath = Files.createTempFile(metaPath.toString(), "_temp")
|
||||
|
||||
// TODO gzip
|
||||
|
||||
// write bytes in tempfile
|
||||
Files.write(metaTempPath, MAGIC)
|
||||
Files.write(metaTempPath, savenameAsByteArray)
|
||||
Files.write(metaTempPath, byteArrayOf(BYTE_NULL))
|
||||
Files.write(metaTempPath, toByteArray(terraseed))
|
||||
Files.write(metaTempPath, toByteArray(rogueseed))
|
||||
for (hash in hashArray)
|
||||
Files.write(metaTempPath, hash)
|
||||
|
||||
// replace savemeta with tempfile
|
||||
try {
|
||||
Files.copy(metaTempPath, metaPath, StandardCopyOption.REPLACE_EXISTING)
|
||||
Files.deleteIfExists(metaTempPath)
|
||||
println("Saved metadata to $saveDirectoryName.")
|
||||
|
||||
return true
|
||||
}
|
||||
catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
fun toByteArray(long: Long): ByteArray {
|
||||
return byteArrayOf(
|
||||
(long.ushr(0x38) and 0xFF).toByte(),
|
||||
(long.ushr(0x30) and 0xFF).toByte(),
|
||||
(long.ushr(0x28) and 0xFF).toByte(),
|
||||
(long.ushr(0x20) and 0xFF).toByte(),
|
||||
(long.ushr(0x18) and 0xFF).toByte(),
|
||||
(long.ushr(0x10) and 0xFF).toByte(),
|
||||
(long.ushr(0x08) and 0xFF).toByte(),
|
||||
(long and 0xFF).toByte()
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user