Read launcher.json saved with a BOM, and keep a copy of one that is not valid JSON.

The docs ask players to edit the file by hand (github_token, feed_repo). A BOM, which an editor may add, made the
file unreadable and every setting, the game folder included, fell back to its default, so the launcher showed the
first run again; a typo did the same and the next save overwrote what the player wrote. The file is now read with
utf-8-sig, and a file that is still no JSON is copied to launcher.json.bad and named in the log.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-08 08:51:35 -05:00
parent aaea991191
commit c9f75ad0b2
3 changed files with 29 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
"""data/launcher.json: the player's choices and what the launcher remembers between runs."""
import json
import os
import shutil
from pathlib import Path
DEFAULTS = {
@@ -22,12 +23,21 @@ class Settings(dict):
def __init__(self, path):
super().__init__(DEFAULTS)
self.path = Path(path)
self.unreadable = False # the file is there but is no JSON; a copy of it is kept as launcher.json.bad
try:
data = json.loads(self.path.read_text(encoding='utf-8'))
# utf-8-sig: the docs ask players to edit the file by hand, and an editor may save it with a BOM
data = json.loads(self.path.read_text(encoding='utf-8-sig'))
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):
except OSError:
pass
except ValueError:
# a typo made by hand: keep what the player wrote before the next save() replaces it with the defaults
self.unreadable = True
try:
shutil.copyfile(self.path, self.path.with_name(self.path.name + '.bad'))
except OSError:
pass
def save(self):
self.path.parent.mkdir(parents=True, exist_ok=True)