mirror of
https://github.com/yairm210/Unciv.git
synced 2025-07-13 17:28:57 +07:00
Refactor: change GameSaver from singleton to single instance pattern & move autosave logic into GameSaver (#6846)
* Refactor: change GameSaver from singleton to single instance pattern & move autosave logic info GameSaver Singleton just doesn't make sense anymore when we have to `init(..)` with different arguments, then we should just make a normal class out of it * Fix not correctly checking for missing external files dir * Refactor: use more appropriate library method * Add logging for external files dir
This commit is contained in:
@ -10,7 +10,6 @@ import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration
|
||||
import com.unciv.UncivGame
|
||||
import com.unciv.UncivGameParameters
|
||||
import com.unciv.logic.GameSaver
|
||||
import com.unciv.models.metadata.GameSettings
|
||||
import com.unciv.ui.utils.Fonts
|
||||
import java.io.File
|
||||
|
||||
@ -24,14 +23,12 @@ open class AndroidLauncher : AndroidApplication() {
|
||||
MultiplayerTurnCheckWorker.createNotificationChannels(applicationContext)
|
||||
|
||||
copyMods()
|
||||
val externalFilesDir = getExternalFilesDir(null)
|
||||
if (externalFilesDir != null) GameSaver.externalFilesDirForAndroid = externalFilesDir.path
|
||||
|
||||
val config = AndroidApplicationConfiguration().apply {
|
||||
useImmersiveMode = true
|
||||
}
|
||||
|
||||
val settings = GameSettings.getSettingsForPlatformLaunchers(filesDir.path)
|
||||
val settings = GameSaver.getSettingsForPlatformLaunchers(filesDir.path)
|
||||
val fontFamily = settings.fontFamily
|
||||
|
||||
// Manage orientation lock
|
||||
@ -73,8 +70,8 @@ open class AndroidLauncher : AndroidApplication() {
|
||||
if (UncivGame.isCurrentInitialized()
|
||||
&& UncivGame.Current.isGameInfoInitialized()
|
||||
&& UncivGame.Current.settings.multiplayerTurnCheckerEnabled
|
||||
&& GameSaver.getSaves(true).any()) {
|
||||
MultiplayerTurnCheckWorker.startTurnChecker(applicationContext, GameSaver, UncivGame.Current.gameInfo, UncivGame.Current.settings)
|
||||
&& UncivGame.Current.gameSaver.getMultiplayerSaves().any()) {
|
||||
MultiplayerTurnCheckWorker.startTurnChecker(applicationContext, UncivGame.Current.gameSaver, UncivGame.Current.gameInfo, UncivGame.Current.settings)
|
||||
}
|
||||
super.onPause()
|
||||
}
|
||||
|
@ -27,14 +27,14 @@ class CustomSaveLocationHelperAndroid(private val activity: Activity) : CustomSa
|
||||
@GuardedBy("this")
|
||||
private val callbacks = ArrayList<IndexedCallback>()
|
||||
|
||||
override fun saveGame(gameInfo: GameInfo, gameName: String, forcePrompt: Boolean, saveCompleteCallback: ((Exception?) -> Unit)?) {
|
||||
override fun saveGame(gameSaver: GameSaver, gameInfo: GameInfo, gameName: String, forcePrompt: Boolean, saveCompleteCallback: ((Exception?) -> Unit)?) {
|
||||
val callbackIndex = synchronized(this) {
|
||||
val index = callbackIndex++
|
||||
callbacks.add(IndexedCallback(
|
||||
index,
|
||||
{ uri ->
|
||||
if (uri != null) {
|
||||
saveGame(gameInfo, uri)
|
||||
saveGame(gameSaver, gameInfo, uri)
|
||||
saveCompleteCallback?.invoke(null)
|
||||
} else {
|
||||
saveCompleteCallback?.invoke(RuntimeException("Uri was null"))
|
||||
@ -68,16 +68,16 @@ class CustomSaveLocationHelperAndroid(private val activity: Activity) : CustomSa
|
||||
}
|
||||
}
|
||||
|
||||
private fun saveGame(gameInfo: GameInfo, uri: Uri) {
|
||||
private fun saveGame(gameSaver: GameSaver, gameInfo: GameInfo, uri: Uri) {
|
||||
gameInfo.customSaveLocation = uri.toString()
|
||||
activity.contentResolver.openOutputStream(uri, "rwt")
|
||||
?.writer()
|
||||
?.use {
|
||||
it.write(GameSaver.gameInfoToString(gameInfo))
|
||||
it.write(gameSaver.gameInfoToString(gameInfo))
|
||||
}
|
||||
}
|
||||
|
||||
override fun loadGame(loadCompleteCallback: (GameInfo?, Exception?) -> Unit) {
|
||||
override fun loadGame(gameSaver: GameSaver, loadCompleteCallback: (GameInfo?, Exception?) -> Unit) {
|
||||
val callbackIndex = synchronized(this) {
|
||||
val index = callbackIndex++
|
||||
callbacks.add(IndexedCallback(
|
||||
@ -90,7 +90,7 @@ class CustomSaveLocationHelperAndroid(private val activity: Activity) : CustomSa
|
||||
?.reader()
|
||||
?.readText()
|
||||
?.run {
|
||||
GameSaver.gameInfoFromString(this)
|
||||
gameSaver.gameInfoFromString(this)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
exception = e
|
||||
|
@ -182,7 +182,7 @@ class MultiplayerTurnCheckWorker(appContext: Context, workerParams: WorkerParame
|
||||
|
||||
fun startTurnChecker(applicationContext: Context, gameSaver: GameSaver, currentGameInfo: GameInfo, settings: GameSettings) {
|
||||
Log.i(LOG_TAG, "startTurnChecker")
|
||||
val gameFiles = gameSaver.getSaves(true)
|
||||
val gameFiles = gameSaver.getMultiplayerSaves()
|
||||
val gameIds = Array(gameFiles.count()) {""}
|
||||
val gameNames = Array(gameFiles.count()) {""}
|
||||
|
||||
@ -255,14 +255,16 @@ class MultiplayerTurnCheckWorker(appContext: Context, workerParams: WorkerParame
|
||||
*/
|
||||
private val notFoundRemotely = mutableMapOf<String, Boolean>()
|
||||
|
||||
private val gameSaver = GameSaver
|
||||
private val gameSaver: GameSaver
|
||||
init {
|
||||
// We can't use Gdx.files since that is only initialized within a com.badlogic.gdx.backends.android.AndroidApplication.
|
||||
// Worker instances may be stopped & recreated by the Android WorkManager, so no AndroidApplication and thus no Gdx.files available
|
||||
val files = DefaultAndroidFiles(applicationContext.assets, ContextWrapper(applicationContext), false)
|
||||
// GDX's AndroidFileHandle uses Gdx.files internally, so we need to set that to our new instance
|
||||
Gdx.files = files
|
||||
gameSaver.init(files, null)
|
||||
val externalFilesDirForAndroid = applicationContext.getExternalFilesDir(null)?.path
|
||||
Log.d(LOG_TAG, "Creating new GameSaver with externalFilesDir=[${externalFilesDirForAndroid}]")
|
||||
gameSaver = GameSaver(files, null, externalFilesDirForAndroid)
|
||||
}
|
||||
|
||||
override fun doWork(): Result = runBlocking {
|
||||
|
@ -29,4 +29,8 @@ Sources for Info about current orientation in case need:
|
||||
// Comparison ensures ActivityTaskManager.getService().setRequestedOrientation isn't called unless necessary
|
||||
if (activity.requestedOrientation != orientation) activity.requestedOrientation = orientation
|
||||
}
|
||||
|
||||
override fun getExternalFilesDir(): String? {
|
||||
return activity.getExternalFilesDir(null)?.path
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user