mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-13 03:54:06 +09:00
game load wip
This commit is contained in:
@@ -6,17 +6,21 @@ import net.torvald.terrarum.modulebasegame.TerrarumIngame
|
||||
import net.torvald.terrarum.modulebasegame.worldgenerator.RoguelikeRandomiser
|
||||
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.ByteArray64
|
||||
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.ByteArray64Reader
|
||||
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.EntryFile
|
||||
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.VirtualDisk
|
||||
import net.torvald.terrarum.utils.MetaModuleCSVPair
|
||||
import net.torvald.terrarum.utils.ZipCodedStr
|
||||
import net.torvald.terrarum.weather.WeatherMixer
|
||||
import java.io.StringReader
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 2021-08-23.
|
||||
*/
|
||||
open class WriteMeta(val ingame: TerrarumIngame) {
|
||||
object WriteMeta {
|
||||
|
||||
open fun invoke(): String {
|
||||
operator fun invoke(ingame: TerrarumIngame, currentPlayTime_t: Long): String {
|
||||
val world = ingame.world
|
||||
|
||||
|
||||
val meta = WorldMeta(
|
||||
genver = Common.GENVER,
|
||||
savename = world.worldName,
|
||||
@@ -28,34 +32,26 @@ open class WriteMeta(val ingame: TerrarumIngame) {
|
||||
playerid = ingame.actorGamer.referenceID,
|
||||
creation_t = world.creationTime,
|
||||
lastplay_t = world.lastPlayTime,
|
||||
playtime_t = world.totalPlayTime,
|
||||
blocks = StringBuilder().let {
|
||||
ModMgr.getFilesFromEveryMod("blocks/blocks.csv").forEach { (modname, file) ->
|
||||
it.append(modnameToOrnamentalHeader(modname))
|
||||
it.append(file.readText())
|
||||
}
|
||||
zipStrAndEnascii(it.toString())
|
||||
playtime_t = world.totalPlayTime + currentPlayTime_t,
|
||||
blocks = ModMgr.getFilesFromEveryMod("blocks/blocks.csv").fold(MetaModuleCSVPair()) {
|
||||
map, (modname, file) ->
|
||||
map[modname] = ZipCodedStr(file.readText ())
|
||||
/*return*/map
|
||||
},
|
||||
items = StringBuilder().let {
|
||||
ModMgr.getFilesFromEveryMod("items/itemid.csv").forEach { (modname, file) ->
|
||||
it.append(modnameToOrnamentalHeader(modname))
|
||||
it.append(file.readText())
|
||||
}
|
||||
zipStrAndEnascii(it.toString())
|
||||
items = ModMgr.getFilesFromEveryMod("items/itemid.csv").fold(MetaModuleCSVPair()) {
|
||||
map, (modname, file) ->
|
||||
map[modname] = ZipCodedStr(file.readText ())
|
||||
/*return*/map
|
||||
},
|
||||
wires = StringBuilder().let {
|
||||
ModMgr.getFilesFromEveryMod("wires/wires.csv").forEach { (modname, file) ->
|
||||
it.append(modnameToOrnamentalHeader(modname))
|
||||
it.append(file.readText())
|
||||
}
|
||||
zipStrAndEnascii(it.toString())
|
||||
wires = ModMgr.getFilesFromEveryMod("wires/wires.csv").fold(MetaModuleCSVPair()) {
|
||||
map, (modname, file) ->
|
||||
map[modname] = ZipCodedStr(file.readText ())
|
||||
/*return*/map
|
||||
},
|
||||
materials = StringBuilder().let {
|
||||
ModMgr.getFilesFromEveryMod("materials/materials.csv").forEach { (modname, file) ->
|
||||
it.append(modnameToOrnamentalHeader(modname))
|
||||
it.append(file.readText())
|
||||
}
|
||||
zipStrAndEnascii(it.toString())
|
||||
materials = ModMgr.getFilesFromEveryMod("materials/materials.csv").fold(MetaModuleCSVPair()) {
|
||||
map, (modname, file) ->
|
||||
map[modname] = ZipCodedStr(file.readText ())
|
||||
/*return*/map
|
||||
},
|
||||
loadorder = ModMgr.loadOrder.toTypedArray(),
|
||||
worlds = ingame.gameworldIndices.toTypedArray()
|
||||
@@ -64,9 +60,9 @@ open class WriteMeta(val ingame: TerrarumIngame) {
|
||||
return Common.jsoner.toJson(meta)
|
||||
}
|
||||
|
||||
fun encodeToByteArray64(): ByteArray64 {
|
||||
fun encodeToByteArray64(ingame: TerrarumIngame, currentPlayTime_t: Long): ByteArray64 {
|
||||
val ba = ByteArray64()
|
||||
this.invoke().toByteArray(Common.CHARSET).forEach { ba.add(it) }
|
||||
this.invoke(ingame, currentPlayTime_t).toByteArray(Common.CHARSET).forEach { ba.add(it) }
|
||||
return ba
|
||||
}
|
||||
|
||||
@@ -82,10 +78,10 @@ open class WriteMeta(val ingame: TerrarumIngame) {
|
||||
val creation_t: Long = 0,
|
||||
val lastplay_t: Long = 0,
|
||||
val playtime_t: Long = 0,
|
||||
val blocks: String = "",
|
||||
val items: String = "",
|
||||
val wires: String = "",
|
||||
val materials: String = "",
|
||||
val blocks: MetaModuleCSVPair = MetaModuleCSVPair(),
|
||||
val items: MetaModuleCSVPair = MetaModuleCSVPair(),
|
||||
val wires: MetaModuleCSVPair = MetaModuleCSVPair(),
|
||||
val materials: MetaModuleCSVPair = MetaModuleCSVPair(),
|
||||
val loadorder: Array<String> = arrayOf(), // do not use list; Could not instantiate instance of class: java.util.Collections$SingletonList
|
||||
val worlds: Array<Int> = arrayOf() // do not use list; Could not instantiate instance of class: java.util.Collections$SingletonList
|
||||
) {
|
||||
@@ -94,23 +90,22 @@ open class WriteMeta(val ingame: TerrarumIngame) {
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private fun modnameToOrnamentalHeader(s: String) =
|
||||
"\n\n${"#".repeat(16 + s.length)}\n" +
|
||||
"## module: $s ##\n" +
|
||||
"${"#".repeat(16 + s.length)}\n\n"
|
||||
|
||||
/**
|
||||
* @param [s] a String
|
||||
* @return UTF-8 encoded [s] which are GZip'd then Ascii85-encoded
|
||||
*/
|
||||
fun zipStrAndEnascii(s: String): String {
|
||||
return Common.bytesToZipdStr(s.toByteArray(Common.CHARSET).iterator())
|
||||
}
|
||||
|
||||
fun unasciiAndUnzipStr(s: String): String {
|
||||
return ByteArray64Reader(Common.strToBytes(StringReader(s)), Common.CHARSET).readText()
|
||||
}
|
||||
}
|
||||
private fun modnameToOrnamentalHeader(s: String) =
|
||||
"\n\n${"#".repeat(16 + s.length)}\n" +
|
||||
"## module: $s ##\n" +
|
||||
"${"#".repeat(16 + s.length)}\n\n"
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 2021-09-03.
|
||||
*/
|
||||
object ReadMeta {
|
||||
|
||||
operator fun invoke(savefile: VirtualDisk): WriteMeta.WorldMeta {
|
||||
val metaFile = savefile.entries[-1]!!
|
||||
val metaReader = ByteArray64Reader((metaFile.contents as EntryFile).getContent(), Common.CHARSET)
|
||||
return Common.jsoner.fromJson(WriteMeta.WorldMeta::class.java, metaReader)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user