From 16a0b286968237de389de5ef36d27bc62cdd23f3 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Tue, 14 Sep 2021 23:24:21 +0900 Subject: [PATCH] able to "lock" the toggle key of the UI to prevent them from being closed while saving --- .../modulebasegame/ui/UIInventoryEscMenu.kt | 8 +++- .../terrarum/serialise/GameSavingThread.kt | 42 ++++++++++++------- .../terrarum/serialise/WriteSavegame.kt | 8 +--- src/net/torvald/terrarum/ui/UIHandler.kt | 11 ++++- 4 files changed, 46 insertions(+), 23 deletions(-) diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryEscMenu.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryEscMenu.kt index 1937b9bed..40df2ab9b 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryEscMenu.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryEscMenu.kt @@ -83,8 +83,13 @@ class UIInventoryEscMenu(val full: UIInventoryFull) : UICanvas() { when (new) { 0 -> { screen = 3; gameMenuButtons.deselect() + full.handler.lockToggle() // save the game - WriteSavegame(Terrarum.ingame!!.savegameArchive, File(App.defaultSaveDir, "${App.getTIME_T()}"), Terrarum.ingame!! as TerrarumIngame) + WriteSavegame(Terrarum.ingame!!.savegameArchive, File(App.defaultSaveDir, "${App.getTIME_T()}"), Terrarum.ingame!! as TerrarumIngame) { + // callback: + screen = 0 + full.handler.unlockToggle() + } } 4 -> { screen = 2; gameMenuButtons.deselect() @@ -127,7 +132,6 @@ class UIInventoryEscMenu(val full: UIInventoryFull) : UICanvas() { }, { delta: Float -> savingUI.update(delta) - // TODO make UI not closable until saving is done } ) private val screenRenders = arrayOf( diff --git a/src/net/torvald/terrarum/serialise/GameSavingThread.kt b/src/net/torvald/terrarum/serialise/GameSavingThread.kt index a10ca048a..5038e56c3 100644 --- a/src/net/torvald/terrarum/serialise/GameSavingThread.kt +++ b/src/net/torvald/terrarum/serialise/GameSavingThread.kt @@ -13,7 +13,7 @@ import java.util.zip.GZIPOutputStream /** * Created by minjaesong on 2021-09-14. */ -class GameSavingThread(val disk: VirtualDisk, val outFile: File, val ingame: TerrarumIngame) : Runnable { +class GameSavingThread(val disk: VirtualDisk, val outFile: File, val ingame: TerrarumIngame, val callback: () -> Unit) : Runnable { /** * Will happily overwrite existing entry @@ -25,11 +25,25 @@ class GameSavingThread(val disk: VirtualDisk, val outFile: File, val ingame: Ter if (!dir.contains(file.entryID)) dir.add(file.entryID) } + private val chunkProgressMultiplier = 1f + private val actorProgressMultiplier = 1f + override fun run() { while (!IngameRenderer.fboRGBexportedLatch) { Thread.sleep(1L) } + val actorsList = listOf(ingame.actorContainerActive, ingame.actorContainerInactive).flatMap { it.filter { WriteWorld.actorAcceptable(it) } } + val layers = intArrayOf(0,1).map { ingame.world.getLayer(it) } + val cw = ingame.world.width / LandUtil.CHUNK_W + val ch = ingame.world.height / LandUtil.CHUNK_H + + WriteSavegame.saveProgress = 0f + WriteSavegame.saveProgressMax = 2f + + (cw * ch * layers.size) * chunkProgressMultiplier + + actorsList.size * actorProgressMultiplier + + val tgaout = ByteArray64GrowableOutputStream() val gzout = GZIPOutputStream(tgaout) @@ -51,6 +65,8 @@ class GameSavingThread(val disk: VirtualDisk, val outFile: File, val ingame: Ter val thumb = DiskEntry(-2, 0, creation_t, time_t, thumbContent) addFile(disk, thumb) + WriteSavegame.saveProgress += 1f + // Write BlockCodex// // val blockCodexContent = EntryFile(zip(ByteArray64.fromByteArray(Common.jsoner.toJson(BlockCodex).toByteArray(Common.CHARSET)))) @@ -92,11 +108,8 @@ class GameSavingThread(val disk: VirtualDisk, val outFile: File, val ingame: Ter val world = DiskEntry(worldNum.toLong(), 0, creation_t, time_t, worldMeta) addFile(disk, world) - val layers = intArrayOf(0,1).map { ingame.world.getLayer(it) } - val cw = ingame.world.width / LandUtil.CHUNK_W - val ch = ingame.world.height / LandUtil.CHUNK_H + WriteSavegame.saveProgress += 1f - WriteSavegame.saveProgressMax = (cw * ch * layers.size).toFloat() for (layer in layers.indices) { for (cx in 0 until cw) { @@ -113,7 +126,7 @@ class GameSavingThread(val disk: VirtualDisk, val outFile: File, val ingame: Ter // "W1L0-92,15" addFile(disk, entry) - WriteSavegame.saveProgress += 1 + WriteSavegame.saveProgress += chunkProgressMultiplier } } } @@ -121,14 +134,12 @@ class GameSavingThread(val disk: VirtualDisk, val outFile: File, val ingame: Ter Echo("Writing actors...") // Write Actors // - listOf(ingame.actorContainerActive, ingame.actorContainerInactive).forEach { actors -> - actors.forEach { - if (WriteWorld.actorAcceptable(it)) { - val actorContent = EntryFile(WriteActor.encodeToByteArray64(it)) - val actor = DiskEntry(it.referenceID.toLong(), 0, creation_t, time_t, actorContent) - addFile(disk, actor) - } - } + actorsList.forEach { + val actorContent = EntryFile(WriteActor.encodeToByteArray64(it)) + val actor = DiskEntry(it.referenceID.toLong(), 0, creation_t, time_t, actorContent) + addFile(disk, actor) + + WriteSavegame.saveProgress += actorProgressMultiplier } disk.capacity = 0 @@ -141,5 +152,8 @@ class GameSavingThread(val disk: VirtualDisk, val outFile: File, val ingame: Ter IngameRenderer.fboRGBexportedLatch = false WriteSavegame.savingStatus = 255 + + + callback() } } \ No newline at end of file diff --git a/src/net/torvald/terrarum/serialise/WriteSavegame.kt b/src/net/torvald/terrarum/serialise/WriteSavegame.kt index 25e4c826c..d2db01cc0 100644 --- a/src/net/torvald/terrarum/serialise/WriteSavegame.kt +++ b/src/net/torvald/terrarum/serialise/WriteSavegame.kt @@ -1,7 +1,6 @@ package net.torvald.terrarum.serialise import com.badlogic.gdx.graphics.Pixmap -import net.torvald.gdx.graphics.PixmapIO2 import net.torvald.terrarum.* import net.torvald.terrarum.App.printdbg import net.torvald.terrarum.console.Echo @@ -13,12 +12,9 @@ import net.torvald.terrarum.modulebasegame.TerrarumIngame import net.torvald.terrarum.modulebasegame.gameactors.IngamePlayer import net.torvald.terrarum.realestate.LandUtil import net.torvald.terrarum.serialise.Common.getUnzipInputStream -import net.torvald.terrarum.serialise.Common.zip -import net.torvald.terrarum.serialise.WriteWorld.actorAcceptable import net.torvald.terrarum.tvda.* import java.io.File import java.io.Reader -import java.util.zip.GZIPOutputStream /** * It's your responsibility to create a new VirtualDisk if your save is new, and create a backup for modifying existing save. @@ -41,7 +37,7 @@ object WriteSavegame { if (!dir.contains(file.entryID)) dir.add(file.entryID) } - operator fun invoke(disk: VirtualDisk, outFile: File, ingame: TerrarumIngame) { + operator fun invoke(disk: VirtualDisk, outFile: File, ingame: TerrarumIngame, callback: () -> Unit = {}) { savingStatus = 0 Echo("Save queued") @@ -61,7 +57,7 @@ object WriteSavegame { } IngameRenderer.fboRGBexportRequested = true - val savingThread = Thread(GameSavingThread(disk, outFile, ingame), "TerrarumBasegameGameSaveThread") + val savingThread = Thread(GameSavingThread(disk, outFile, ingame, callback), "TerrarumBasegameGameSaveThread") savingThread.start() diff --git a/src/net/torvald/terrarum/ui/UIHandler.kt b/src/net/torvald/terrarum/ui/UIHandler.kt index 7d8ce20c3..077e04a8f 100644 --- a/src/net/torvald/terrarum/ui/UIHandler.kt +++ b/src/net/torvald/terrarum/ui/UIHandler.kt @@ -117,6 +117,8 @@ void main() { private val shader = App.loadShaderInline(SHADER_PROG_VERT, SHADER_PROG_FRAG) + private var uiToggleLocked = false + init { //UI.handler = this } @@ -150,11 +152,18 @@ void main() { subUIs.remove(ui) } + fun lockToggle() { + uiToggleLocked = true + } + fun unlockToggle() { + uiToggleLocked = false + } + fun update(ui: UICanvas, delta: Float) { // open/close UI by key pressed // some UIs will pause the game, and they still need to be closed - if (Terrarum.ingame?.consoleOpened == false && (Terrarum.ingame?.paused == false || isOpened)) { + if (!uiToggleLocked && (Terrarum.ingame?.consoleOpened == false && (Terrarum.ingame?.paused == false || isOpened))) { if (toggleKey != null && Gdx.input.isKeyJustPressed(toggleKey!!)) { if (isClosed) setAsOpen()