"""data/launcher.json: the player's choices and what the launcher remembers between runs.""" import json import os from pathlib import Path DEFAULTS = { 'game_dir': '', # the built game; empty until the first install picks one 'dump': '', # the .nup (or the folder with exefs/ and romfs/) the game was built from 'auto_check': True, # look for a new version when the launcher opens 'channel': 'stable', # 'stable' or 'preview' (GitHub pre-releases) 'hide_while_playing': True, # hide the window while the game runs, show it again when it exits 'keep_cache': True, # keep build-cache/ (the lift and objects) so an update recompiles only changes 'desktop_shortcut': True, 'feed_repo': '', # owner/name overriding feed.json, for testing a fork's releases 'github_token': '', # only for a private release repository; stored as plain text 'last_check': 0.0, 'last_seen': '', # the newest version the player was told about } class Settings(dict): def __init__(self, path): super().__init__(DEFAULTS) self.path = Path(path) try: data = json.loads(self.path.read_text(encoding='utf-8')) if isinstance(data, dict): self.update({k: v for k, v in data.items() if k in DEFAULTS and type(v) is type(DEFAULTS[k])}) except (OSError, ValueError): pass def save(self): self.path.parent.mkdir(parents=True, exist_ok=True) tmp = self.path.with_name(self.path.name + '.tmp') tmp.write_text(json.dumps(dict(self), indent=1), encoding='utf-8') os.replace(tmp, self.path) def reset(self): """Default Settings: the choices go back to their defaults; the game folder, dump and history stay.""" keep = {k: self[k] for k in ('game_dir', 'dump', 'last_check', 'last_seen', 'feed_repo', 'github_token')} self.clear() self.update(DEFAULTS) self.update(keep)