Game now saves save files in external storage on Android when possible.

May the lord have mercy on our souls.
This commit is contained in:
Yair Morgenstern
2020-05-31 19:48:36 +03:00
parent 434ded6070
commit 43f8fa89c0
2 changed files with 14 additions and 2 deletions

View File

@ -7,6 +7,7 @@ import androidx.work.WorkManager
import com.badlogic.gdx.backends.android.AndroidApplication import com.badlogic.gdx.backends.android.AndroidApplication
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration
import com.unciv.UncivGame import com.unciv.UncivGame
import com.unciv.logic.GameSaver
import java.io.File import java.io.File
class AndroidLauncher : AndroidApplication() { class AndroidLauncher : AndroidApplication() {
@ -17,6 +18,7 @@ class AndroidLauncher : AndroidApplication() {
// Only allow mods on KK+, to avoid READ_EXTERNAL_STORAGE permission earlier versions need // Only allow mods on KK+, to avoid READ_EXTERNAL_STORAGE permission earlier versions need
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
copyMods() copyMods()
GameSaver.externalFilesDirForAndroid = getExternalFilesDir(null)!!.path
} }
val config = AndroidApplicationConfiguration().apply { useImmersiveMode = true } val config = AndroidApplicationConfiguration().apply { useImmersiveMode = true }

View File

@ -14,16 +14,26 @@ object GameSaver {
private const val multiplayerFilesFolder = "MultiplayerGames" private const val multiplayerFilesFolder = "MultiplayerGames"
private const val settingsFileName = "GameSettings.json" private const val settingsFileName = "GameSettings.json"
/** When set, we know we're on Android and can save to the app's personal external file directory
* See https://developer.android.com/training/data-storage/app-specific#external-access-files */
var externalFilesDirForAndroid = ""
fun json() = Json().apply { setIgnoreDeprecated(true); ignoreUnknownFields = true } // Json() is NOT THREAD SAFE so we need to create a new one for each function fun json() = Json().apply { setIgnoreDeprecated(true); ignoreUnknownFields = true } // Json() is NOT THREAD SAFE so we need to create a new one for each function
fun getSubfolder(multiplayer: Boolean=false) = if(multiplayer) multiplayerFilesFolder else saveFilesFolder fun getSubfolder(multiplayer: Boolean=false) = if(multiplayer) multiplayerFilesFolder else saveFilesFolder
fun getSave(GameName: String, multiplayer: Boolean = false): FileHandle { fun getSave(GameName: String, multiplayer: Boolean = false): FileHandle {
return Gdx.files.local("${getSubfolder(multiplayer)}/$GameName") val localfile = Gdx.files.local("${getSubfolder(multiplayer)}/$GameName")
if(externalFilesDirForAndroid=="" || !Gdx.files.isExternalStorageAvailable) return localfile
val externalFile = Gdx.files.absolute(externalFilesDirForAndroid+"/${getSubfolder(multiplayer)}/$GameName")
if(localfile.exists() && !externalFile.exists()) return localfile
return externalFile
} }
fun getSaves(multiplayer: Boolean = false): List<String> { fun getSaves(multiplayer: Boolean = false): List<String> {
return Gdx.files.local(getSubfolder(multiplayer)).list().map { it.name() } val localSaves = Gdx.files.local(getSubfolder(multiplayer)).list().map { it.name() }
if(externalFilesDirForAndroid=="" || !Gdx.files.isExternalStorageAvailable) return localSaves
return localSaves + Gdx.files.absolute(externalFilesDirForAndroid+"/${getSubfolder(multiplayer)}").list().map { it.name() }
} }
fun saveGame(game: GameInfo, GameName: String, multiplayer: Boolean = false) { fun saveGame(game: GameInfo, GameName: String, multiplayer: Boolean = false) {