Skip to content

Game.PSI.PdxSdk.LauncherSettings

Assembly: Game
Namespace: Game.PSI.PdxSdk

Type: static class

Base: System.Object

Summary:
LauncherSettings is a small static helper that loads and saves launcher-specific configuration stored in "launcher-settings.json" in the user's data path. It bridges a JSON representation (the nested Settings struct) and the game's SharedSettings and LocalizationManager. It supports reading the launcher file, applying values (language, display mode, vSync, resolution, refresh rate, display index) into SharedSettings, and merging current SharedSettings back into the JSON structure for saving. All I/O and (de)serialization use Colossal's JSON helpers and the Colossal logging API for error/info reporting.


Fields

  • private static readonly string kLauncherSettingsFileName = "launcher-settings.json"
    Name of the launcher settings file stored in the user data folder.

  • private static readonly string kLauncherSettingsPath = EnvPath.kUserDataPath + "/" + kLauncherSettingsFileName
    Full path used to read/write the launcher settings file. EnvPath.kUserDataPath is used as the base directory.

  • private static ILog log = LogManager.GetLogger("PdxSdk")
    Logger used to report load/save success and failures.

  • private struct Settings (nested)
    Container that maps to the JSON structure. It contains:

  • public struct System with fields:

    • public string language — locale id or null
    • public string display_mode — string name of display mode (e.g. "fullscreen", "windowed", "borderless_fullscreen")
    • public bool vsync — vSync enabled flag
    • public string fullscreen_resolution — resolution string for fullscreen (e.g. "1920x1080")
    • public string windowed_resolution — resolution string for windowed
    • public double refreshRate — refresh rate stored as double; later converted to RefreshRate numerator/denominator
    • public string display_index — display index as string
  • private enum LauncherDisplayMode { fullscreen, borderless_fullscreen, windowed }
    Internal enum used for mapping between JSON string and the game's DisplayMode.

The Settings struct also provides methods to Merge and Apply settings (see Methods section).

Properties

  • This type is a static class; it does not expose properties.

Constructors

  • No public or instance constructors. LauncherSettings is a static class and provides only static members.

Methods

  • public static void LoadSettings(LocalizationManager localizationManager, SharedSettings gameSettings)
    Loads launcher-settings.json (if present) and applies its values into the provided SharedSettings by calling Settings.Apply. If the file does not exist or deserialization fails, nothing is applied; failures are logged.

  • public static void SaveSettings(SharedSettings gameSettings)
    If launcher-settings.json can be loaded (or created in memory), this will merge current SharedSettings into the Settings structure and write the JSON file back to kLauncherSettingsPath. Uses JSON.Dump to serialize. Exceptions during file write or serialization are caught and logged.

  • private static bool TryGetLauncherSettings(out Settings launcherSettings)
    Attempts to read and parse the JSON file. Returns true and outputs a populated Settings on success. On failure (file missing, parse error), returns false, sets launcherSettings to default(Settings), and logs the condition.

  • Settings.Merge(SharedSettings gameSettings)
    Merges values from SharedSettings into the Settings data:

  • Sets system.language unless SharedSettings userInterface.locale equals "os" (in which case language is left null).
  • Converts DisplayMode to the string representation of LauncherDisplayMode and stores in system.display_mode.
  • Copies vSync into system.vsync.
  • Formats resolution into "WIDTHxHEIGHT" and stores into windowed_resolution or fullscreen_resolution depending on the DisplayMode.
  • Stores system.refreshRate from gameSettings.graphics.resolution.refreshRate.value.
  • Stores display index as string.

  • Settings.Apply(LocalizationManager localizationManager, SharedSettings settings)
    Applies stored system values into SharedSettings:

  • If localizationManager.SupportsLocale(system.language) returns true, sets settings.userInterface.locale.
  • Parses system.display_mode into the internal LauncherDisplayMode enum and converts to the game's DisplayMode; sets settings.graphics.displayMode and sets a flag indicating a resolution/display-mode change.
  • Sets settings.graphics.vSync.
  • Attempts to parse the appropriate resolution string (windowed or fullscreen) via TryFormatResolution and, if successful, sets settings.graphics.resolution and flags a resolution change.
  • Parses system.display_index into settings.graphics.displayIndex (falls back to 0).
  • If a display mode/resolution change occurred, calls settings.graphics.ApplyResolution() to commit the change.

  • Settings.TryFormatResolution(DisplayMode displayMode, out ScreenResolution resolution) : bool
    Parses a stored resolution string of the form "WIDTHxHEIGHT". If parsing succeeds, constructs a ScreenResolution with width/height and a RefreshRate where:

  • numerator = (uint)(system.refreshRate * 1000.0)
  • denominator = 1000u Returns true if the constructed resolution is valid (resolution.isValid), otherwise false. On failure, outputs default(ScreenResolution).

  • Settings.FormatResolutionStr(ScreenResolution resolution) : string (private static)
    Formats a ScreenResolution as "WIDTHxHEIGHT". Used when merging SharedSettings into the JSON structure.

Notes on behavior and robustness: - JSON parsing uses Colossal.Json JSON.Load and Variant.Make() to map to the nested struct. Malformed JSON or missing fields will be caught and logged in TryGetLauncherSettings. - SaveSettings first obtains the existing JSON structure (if present) via TryGetLauncherSettings, merges current game settings into it, then writes the file. If the file didn't exist, TryGetLauncherSettings will return false and SaveSettings will not attempt to write. - Logging uses the "PdxSdk" logger; both successful operations and exceptions are logged at Info level.

Usage Example

// Load launcher settings at startup and apply to current game settings:
LauncherSettings.LoadSettings(localizationManager, gameSettings);

// ... later, when user changes settings in game and you want to persist
LauncherSettings.SaveSettings(gameSettings);

Example JSON layout written/read by this code (approximate)

{
  "system": {
    "language": "en",
    "display_mode": "fullscreen",
    "vsync": true,
    "fullscreen_resolution": "1920x1080",
    "windowed_resolution": "1280x720",
    "refreshRate": 60.0,
    "display_index": "0"
  }
}

Implementation remarks: - Resolution parsing expects an "WxH" string with lowercase "x" and integer width/height. - Refresh rate is stored as a double and reconstructed as a RefreshRate with numerator/denominator for the game's ScreenResolution type.