mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-17 07:09:57 -04:00
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:
@@ -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')
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user