From c9f75ad0b2c7e0cbe1530c06277331cf95016c73 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Sun, 8 Nov 2026 08:51:35 -0500 Subject: [PATCH] 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 --- launcher/context.py | 3 +++ launcher/settings.py | 14 ++++++++++++-- tests/launcher_test.py | 14 ++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/launcher/context.py b/launcher/context.py index 332cf300..4bcd2a94 100644 --- a/launcher/context.py +++ b/launcher/context.py @@ -29,6 +29,9 @@ class Context: self._file = None self.log(f'--- NX2navi launcher {self.version} ({"checkout" if self.layout.dev else "release"}) in ' f'{self.layout.root}') + if self.settings.unreadable: + self.log(f'{self.layout.settings} is not valid JSON: the settings start from their defaults; the file as ' + 'it was is kept as launcher.json.bad') def log(self, line): stamp = time.strftime('%H:%M:%S') diff --git a/launcher/settings.py b/launcher/settings.py index d9cd89ee..08e6e1fe 100644 --- a/launcher/settings.py +++ b/launcher/settings.py @@ -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) diff --git a/tests/launcher_test.py b/tests/launcher_test.py index ac61cd3a..3ca39587 100644 --- a/tests/launcher_test.py +++ b/tests/launcher_test.py @@ -268,6 +268,20 @@ class SettingsTest(Tmp): self.assertEqual((s['auto_check'], s['channel']), (True, 'preview')) self.assertNotIn('junk', s) + def test_bom_and_typo(self): + # a file saved by an editor with a BOM reads as written; one with a typo keeps a copy before a save replaces it + self.layout.settings.write_bytes(b'\xef\xbb\xbf' + json.dumps({'game_dir': 'D:/g'}).encode()) + s = settings.Settings(self.layout.settings) + self.assertEqual(s['game_dir'], 'D:/g') + self.assertFalse(s.unreadable) + typo = '{"game_dir": "D:/g", "github_token": "t",}' + self.layout.settings.write_text(typo, encoding='utf-8') + s = settings.Settings(self.layout.settings) + self.assertTrue(s.unreadable) + self.assertEqual(s['game_dir'], '') + s.save() + self.assertEqual(self.layout.settings.with_name('launcher.json.bad').read_text(encoding='utf-8'), typo) + class UninstallTest(Tmp): def tree(self, root, rels):