diff --git a/core/src/com/unciv/logic/files/UncivFiles.kt b/core/src/com/unciv/logic/files/UncivFiles.kt index 5269da0a72..91a9355220 100644 --- a/core/src/com/unciv/logic/files/UncivFiles.kt +++ b/core/src/com/unciv/logic/files/UncivFiles.kt @@ -8,6 +8,7 @@ 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 @@ -378,6 +379,17 @@ class UncivFiles( return field } + /** Specialized function to access settings before Gdx is initialized. + * + * @param baseDirectory Path to the directory where the file should be - if not set, the OS current directory is used (which is "/" on Android) + */ + fun getSettingsForPlatformLaunchers(baseDirectory: 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(baseDirectory + 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 { diff --git a/desktop/src/com/unciv/app/desktop/DesktopLauncher.kt b/desktop/src/com/unciv/app/desktop/DesktopLauncher.kt index 41c586d7d8..23c8843498 100644 --- a/desktop/src/com/unciv/app/desktop/DesktopLauncher.kt +++ b/desktop/src/com/unciv/app/desktop/DesktopLauncher.kt @@ -4,11 +4,9 @@ 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 @@ -78,15 +76,14 @@ internal object DesktopLauncher { // LibGDX not yet configured, use regular java class val maximumWindowBounds = getMaximumWindowBounds() - val settingsFileLocation = if (customDataDir == null) SETTINGS_FILE_NAME - else customDataDir + File.separator + SETTINGS_FILE_NAME + val settingsDirectory = customDataDir ?: "." - val settings = getSettingsForPlatformLaunchers(settingsFileLocation) + val settings = UncivFiles.getSettingsForPlatformLaunchers(settingsDirectory) if (settings.isFreshlyCreated) { settings.screenSize = ScreenSize.Large // By default we guess that Desktops have larger screens settings.windowState = WindowState(maximumWindowBounds) - FileHandle(settingsFileLocation).writeString(json().toJson(settings), false, Charsets.UTF_8.name()) // so when we later open the game we get fullscreen + FileHandle(settingsDirectory + File.separator + SETTINGS_FILE_NAME).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 @@ -107,14 +104,4 @@ internal object DesktopLauncher { 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 } - } }