Add CLI argument to specify the data directory

This commit is contained in:
yairm210
2024-07-27 23:32:52 +03:00
parent 4ce6536871
commit d0a3a0834c
2 changed files with 22 additions and 16 deletions

View File

@ -8,7 +8,6 @@ import com.badlogic.gdx.utils.JsonReader
import com.badlogic.gdx.utils.SerializationException
import com.unciv.Constants
import com.unciv.UncivGame
import com.unciv.json.fromJsonFile
import com.unciv.json.json
import com.unciv.logic.CompatibilityVersion
import com.unciv.logic.GameInfo
@ -379,17 +378,6 @@ class UncivFiles(
return field
}
/** Specialized function to access settings before Gdx is initialized.
*
* @param base Path to the directory where the file should be - if not set, the OS current directory is used (which is "/" on Android)
*/
fun getSettingsForPlatformLaunchers(base: String = "."): GameSettings {
// FileHandle is Gdx, but the class and JsonParser are not dependent on app initialization
// In fact, at this point Gdx.app or Gdx.files are null but this still works.
val file = FileHandle(base + File.separator + SETTINGS_FILE_NAME)
return if (file.exists()) json().fromJsonFile(GameSettings::class.java, file)
else GameSettings().apply { isFreshlyCreated = true }
}
/** @throws IncompatibleGameInfoVersionException if the [gameData] was created by a version of this game that is incompatible with the current one. */
fun gameInfoFromString(gameData: String): GameInfo {

View File

@ -4,9 +4,11 @@ import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration
import com.badlogic.gdx.files.FileHandle
import com.badlogic.gdx.graphics.glutils.HdpiMode
import com.unciv.app.desktop.DesktopScreenMode.Companion.getMaximumWindowBounds
import com.unciv.json.fromJsonFile
import com.unciv.json.json
import com.unciv.logic.files.SETTINGS_FILE_NAME
import com.unciv.logic.files.UncivFiles
import com.unciv.models.metadata.GameSettings
import com.unciv.models.metadata.GameSettings.ScreenSize
import com.unciv.models.metadata.GameSettings.WindowState
import com.unciv.models.ruleset.Ruleset
@ -18,6 +20,7 @@ import com.unciv.ui.components.fonts.Fonts
import com.unciv.ui.screens.basescreen.BaseScreen
import com.unciv.utils.Display
import com.unciv.utils.Log
import java.io.File
import kotlin.system.exitProcess
internal object DesktopLauncher {
@ -43,6 +46,9 @@ internal object DesktopLauncher {
exitProcess(if (errors.any { it.errorSeverityToReport == RulesetErrorSeverity.Error }) 1 else 0)
}
val customDataDirPrefix="--data-dir="
val customDataDir = arg.find { it.startsWith(customDataDirPrefix) }?.removePrefix(customDataDirPrefix)
// Setup Desktop logging
Log.backend = DesktopLogBackend()
@ -72,11 +78,15 @@ internal object DesktopLauncher {
// LibGDX not yet configured, use regular java class
val maximumWindowBounds = getMaximumWindowBounds()
val settings = UncivFiles.getSettingsForPlatformLaunchers()
val settingsFileLocation = if (customDataDir == null) SETTINGS_FILE_NAME
else customDataDir + File.separator + SETTINGS_FILE_NAME
val settings = getSettingsForPlatformLaunchers(settingsFileLocation)
if (settings.isFreshlyCreated) {
settings.screenSize = ScreenSize.Large // By default we guess that Desktops have larger screens
settings.windowState = WindowState(maximumWindowBounds)
FileHandle(SETTINGS_FILE_NAME).writeString(json().toJson(settings), false, Charsets.UTF_8.name()) // so when we later open the game we get fullscreen
FileHandle(settingsFileLocation).writeString(json().toJson(settings), false, Charsets.UTF_8.name()) // so when we later open the game we get fullscreen
}
// Kludge! This is a workaround - the matching call in DesktopDisplay doesn't "take" quite permanently,
// the window might revert to the "config" values when the user moves the window - worse if they
@ -92,11 +102,19 @@ internal object DesktopLauncher {
}
val customDataDirPrefix="--data-dir="
val customDataDir = arg.find { it.startsWith(customDataDirPrefix) }?.removePrefix(customDataDirPrefix)
// HardenGdxAudio extends Lwjgl3Application, and the Lwjgl3Application constructor runs as long as the game runs
HardenGdxAudio(DesktopGame(config, customDataDir), config)
exitProcess(0)
}
/** Specialized function to access settings before Gdx is initialized.
*/
private fun getSettingsForPlatformLaunchers(settingsFileLocation: String): GameSettings {
// FileHandle is Gdx, but the class and JsonParser are not dependent on app initialization
// In fact, at this point Gdx.app or Gdx.files are null but this still works.
val file = FileHandle(settingsFileLocation)
return if (file.exists()) json().fromJsonFile(GameSettings::class.java, file)
else GameSettings().apply { isFreshlyCreated = true }
}
}