Refactor UncivGame initialization - add UncivGameParameters. (#2779)

This commit is contained in:
Alexander Korolyov 2020-06-29 19:05:58 +02:00 committed by GitHub
parent 79ff8c2ecf
commit 5356e63249
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 13 deletions

View file

@ -7,6 +7,7 @@ import androidx.work.WorkManager
import com.badlogic.gdx.backends.android.AndroidApplication
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration
import com.unciv.UncivGame
import com.unciv.UncivGameParameters
import com.unciv.logic.GameSaver
import java.io.File
@ -23,12 +24,13 @@ class AndroidLauncher : AndroidApplication() {
}
val config = AndroidApplicationConfiguration().apply { useImmersiveMode = true }
val game = UncivGame (
val androidParameters = UncivGameParameters(
version = BuildConfig.VERSION_NAME,
crashReportSender = CrashReportSenderAndroid(this),
exitEvent = this::finish,
fontImplementation = NativeFontAndroid(45)
)
val game = UncivGame ( androidParameters )
initialize(game, config)
}

View file

@ -18,15 +18,16 @@ import com.unciv.ui.worldscreen.WorldScreen
import java.util.*
import kotlin.concurrent.thread
class UncivGame(
val version: String,
private val crashReportSender: CrashReportSender? = null,
val exitEvent: (()->Unit)? = null,
val cancelDiscordEvent: (()->Unit)? = null,
val fontImplementation: NativeFontImplementation? = null
) : Game() {
class UncivGame(parameters: UncivGameParameters) : Game() {
// we need this secondary constructor because Java code for iOS can't handle Kotlin lambda parameters
constructor(version: String) : this(version, null)
constructor(version: String) : this(UncivGameParameters(version, null))
val version = parameters.version
private val crashReportSender = parameters.crashReportSender
val exitEvent = parameters.exitEvent
val cancelDiscordEvent = parameters.cancelDiscordEvent
val fontImplementation = parameters.fontImplementation
val consoleMode = parameters.consoleMode
lateinit var gameInfo: GameInfo
fun isGameInfoInitialized() = this::gameInfo.isInitialized

View file

@ -0,0 +1,12 @@
package com.unciv
import com.unciv.ui.utils.CrashReportSender
import com.unciv.ui.utils.NativeFontImplementation
class UncivGameParameters(val version: String,
val crashReportSender: CrashReportSender? = null,
val exitEvent: (()->Unit)? = null,
val cancelDiscordEvent: (()->Unit)? = null,
val fontImplementation: NativeFontImplementation? = null,
val consoleMode: Boolean = false) {
}

View file

@ -9,6 +9,7 @@ import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.tools.texturepacker.TexturePacker
import com.unciv.UncivGame
import com.unciv.UncivGameParameters
import com.unciv.models.translations.tr
import io.ktor.application.call
import io.ktor.http.HttpStatusCode
@ -41,7 +42,14 @@ internal object DesktopLauncher {
val versionFromJar = DesktopLauncher.javaClass.`package`.specificationVersion ?: "Desktop"
val game = UncivGame ( versionFromJar, null, { exitProcess(0) }, { discordTimer?.cancel() }, NativeFontDesktop(45) )
val desktopParameters = UncivGameParameters(
versionFromJar,
exitEvent = { exitProcess(0) },
cancelDiscordEvent = { discordTimer?.cancel() },
fontImplementation = NativeFontDesktop(45)
)
val game = UncivGame ( desktopParameters )
if(!RaspberryPiDetector.isRaspberryPi()) // No discord RPC for Raspberry Pi, see https://github.com/yairm210/Unciv/issues/1624
tryActivateDiscord(game)

View file

@ -3,6 +3,7 @@ package com.unciv.testing
import com.badlogic.gdx.Gdx
import com.unciv.UncivGame
import com.unciv.UncivGameParameters
import com.unciv.models.ruleset.Ruleset
import com.unciv.models.ruleset.RulesetCache
import com.unciv.models.ruleset.unit.BaseUnit
@ -35,13 +36,16 @@ class BasicTests {
@Test
fun gameIsNotRunWithDebugModes() {
val game = UncivGame("", null, null)
val params = UncivGameParameters("", null, null)
val game = UncivGame(params)
Assert.assertTrue("This test will only pass if the game is not run with debug modes",
!game.superchargedForDebug
&& !game.viewEntireMapForDebug
&& game.simulateUntilTurnForDebug <= 0
&& !game.simulateUntilWin
&& !game.scenarioDebugSwitch)
&& !game.scenarioDebugSwitch
&& !game.consoleMode
)
}
// If there's a unit that obsoletes with no upgrade then when it obsoletes