mirror of
https://github.com/yairm210/Unciv.git
synced 2025-07-04 07:17:50 +07:00
Fixed user-invisible crash on multiplayer updating thread when resuming game
This commit is contained in:
@ -446,6 +446,8 @@ open class UncivGame(val isConsoleMode: Boolean = false) : Game(), PlatformSpeci
|
||||
val curGameInfo = gameInfo
|
||||
if (curGameInfo != null) files.requestAutoSave(curGameInfo)
|
||||
if (::musicController.isInitialized) musicController.pause()
|
||||
// We stop the *in-game* multiplayer update, so that when we resume(), it doesn't keep working and A. we'll have errors and B. we'll have multiple updaters active
|
||||
if (::onlineMultiplayer.isInitialized) onlineMultiplayer.multiplayerGameUpdater.cancel()
|
||||
super.pause()
|
||||
}
|
||||
|
||||
|
@ -19,6 +19,7 @@ import com.unciv.utils.debug
|
||||
import com.unciv.utils.launchOnThreadPool
|
||||
import com.unciv.utils.withGLContext
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.flow
|
||||
@ -51,9 +52,15 @@ class OnlineMultiplayer {
|
||||
private val lastCurGameRefresh: AtomicReference<Instant?> = AtomicReference()
|
||||
|
||||
val games: Set<OnlineMultiplayerGame> get() = savedGames.values.toSet()
|
||||
val multiplayerGameUpdater: Job
|
||||
|
||||
init {
|
||||
flow<Unit> {
|
||||
/** We have 2 'async processes' that update the multiplayer games:
|
||||
* A. This one, which as part of *this process* runs refreshes for all OS's
|
||||
* B. MultiplayerTurnCheckWorker, which *as an Android worker* runs refreshes *even when the game is closed*.
|
||||
* Only for Android, obviously
|
||||
*/
|
||||
multiplayerGameUpdater = flow<Unit> {
|
||||
while (true) {
|
||||
delay(500)
|
||||
|
||||
@ -134,8 +141,7 @@ class OnlineMultiplayer {
|
||||
*/
|
||||
suspend fun addGame(gameId: String, gameName: String? = null) {
|
||||
val saveFileName = if (gameName.isNullOrBlank()) gameId else gameName
|
||||
var gamePreview: GameInfoPreview
|
||||
gamePreview = try {
|
||||
val gamePreview: GameInfoPreview = try {
|
||||
multiplayerServer.tryDownloadGamePreview(gameId)
|
||||
} catch (_: MultiplayerFileNotFoundException) {
|
||||
// Game is so old that a preview could not be found on dropbox lets try the real gameInfo instead
|
||||
|
Reference in New Issue
Block a user