diff --git a/core/src/com/unciv/logic/files/UncivFiles.kt b/core/src/com/unciv/logic/files/UncivFiles.kt index f3ab20b40a..62fc8cd3d1 100644 --- a/core/src/com/unciv/logic/files/UncivFiles.kt +++ b/core/src/com/unciv/logic/files/UncivFiles.kt @@ -59,7 +59,10 @@ class UncivFiles( val localFile = files.local(location) val externalFile = files.external(location) - val toReturn = if (preferExternalStorage && files.isExternalStorageAvailable && (externalFile.exists() || !localFile.exists())) { + val toReturn = if (files.isExternalStorageAvailable && ( + preferExternalStorage && (externalFile.exists() || !localFile.exists()) + || !preferExternalStorage && (externalFile.exists() && !localFile.exists()) + ) ) { externalFile } else { localFile @@ -97,7 +100,7 @@ class UncivFiles( debug("Getting saves from folder %s, externalStoragePath: %s", saveFolder, files.externalStoragePath) val localFiles = files.local(saveFolder).list().asSequence() - val externalFiles = if (files.isExternalStorageAvailable) { + val externalFiles = if (files.isExternalStorageAvailable && files.local("").file().absolutePath != files.external("").file().absolutePath) { files.external(saveFolder).list().asSequence() } else { emptySequence() diff --git a/core/src/com/unciv/ui/screens/savescreens/LoadGameScreen.kt b/core/src/com/unciv/ui/screens/savescreens/LoadGameScreen.kt index 96c25b4c17..080c3fd5bd 100644 --- a/core/src/com/unciv/ui/screens/savescreens/LoadGameScreen.kt +++ b/core/src/com/unciv/ui/screens/savescreens/LoadGameScreen.kt @@ -96,7 +96,7 @@ class LoadGameScreen : LoadOrSaveScreen() { override fun onExistingSaveSelected(saveGameFile: FileHandle) { copySavedGameToClipboardButton.enable() rightSideButton.isVisible = true - rightSideButton.setText("Load [$selectedSave]".tr()) + rightSideButton.setText("Load [${saveGameFile.name()}]".tr()) rightSideButton.enable() } @@ -115,14 +115,14 @@ class LoadGameScreen : LoadOrSaveScreen() { } private fun onLoadGame() { - if (selectedSave.isEmpty()) return + if (selectedSave == null) return val loadingPopup = Popup( this) loadingPopup.addGoodSizedLabel(Constants.loading) loadingPopup.open() Concurrency.run(loadGame) { try { // This is what can lead to ANRs - reading the file and setting the transients, that's why this is in another thread - val loadedGame = game.files.loadGameByName(selectedSave) + val loadedGame = game.files.loadGameFromFile(selectedSave!!) game.loadGame(loadedGame, true) } catch (notAPlayer: UncivShowableException) { launchOnGLThread { @@ -183,9 +183,10 @@ class LoadGameScreen : LoadOrSaveScreen() { private fun getCopyExistingSaveToClipboardButton(): TextButton { val copyButton = copyExistingSaveToClipboard.toTextButton() copyButton.onActivation { + if (selectedSave == null) return@onActivation Concurrency.run(copyExistingSaveToClipboard) { try { - val gameText = game.files.getSave(selectedSave).readString() + val gameText = selectedSave!!.readString() Gdx.app.clipboard.contents = if (gameText[0] == '{') Gzip.zip(gameText) else gameText } catch (ex: Throwable) { ex.printStackTrace() diff --git a/core/src/com/unciv/ui/screens/savescreens/LoadOrSaveScreen.kt b/core/src/com/unciv/ui/screens/savescreens/LoadOrSaveScreen.kt index eaa9bff61b..4341e8cbcb 100644 --- a/core/src/com/unciv/ui/screens/savescreens/LoadOrSaveScreen.kt +++ b/core/src/com/unciv/ui/screens/savescreens/LoadOrSaveScreen.kt @@ -34,7 +34,7 @@ abstract class LoadOrSaveScreen( abstract fun onExistingSaveSelected(saveGameFile: FileHandle) abstract fun doubleClickAction() - protected var selectedSave = "" + protected var selectedSave: FileHandle? = null private set private val savesScrollPane = VerticalFileListScrollPane() @@ -79,19 +79,20 @@ abstract class LoadOrSaveScreen( } private fun onDeleteClicked() { - if (selectedSave.isEmpty()) return + if (selectedSave == null) return + val name = selectedSave!!.name() ConfirmPopup(this, "Are you sure you want to delete this save?", "Delete save") { val result = try { - if (game.files.deleteSave(selectedSave)) { + if (game.files.deleteSave(selectedSave!!)) { resetWindowState() - "[$selectedSave] deleted successfully." + "[$name] deleted successfully." } else { - "Failed to delete [$selectedSave]." + "Failed to delete [$name]." } } catch (ex: SecurityException) { - "Insufficient permissions to delete [$selectedSave]." + "Insufficient permissions to delete [$name]." } catch (ex: Throwable) { - "Failed to delete [$selectedSave]." + "Failed to delete [$name]." } descriptionLabel.setText(result.tr()) }.open() @@ -104,7 +105,7 @@ abstract class LoadOrSaveScreen( private fun selectExistingSave(saveGameFile: FileHandle) { deleteSaveButton.enable() - selectedSave = saveGameFile.name() + selectedSave = saveGameFile showSaveInfo(saveGameFile) rightSideButton.isVisible = true onExistingSaveSelected(saveGameFile)