diff --git a/docs/installer.md b/docs/installer.md index 82aeaa3b..14d6a6e2 100644 --- a/docs/installer.md +++ b/docs/installer.md @@ -38,7 +38,9 @@ The install folder (`main.c` `portable_paths()` reads this layout; it stays port The player runs a newer installer on the same folder. It reads `totk-install.json`, keeps `romfs/`, `exefs/`, `shared-fonts/`, `profile/` and `totk.ini` after checking their hashes, rebuilds `totk.exe` and re-prepares the -shaders. A newer release that changes nothing game-derived skips the rebuild. +shaders. A newer release that changes nothing game-derived skips the rebuild. The window starts with the last install folder +it ran on, kept in `%LOCALAPPDATA%\TotK Recomp\setup.json` (`$XDG_STATE_HOME/totk-recomp/setup.json` elsewhere), so an update +needs no other input. Given an update file other than the recorded one (1.1.0, then 1.2.1, then 1.4.3), the installer extracts and builds again from it and keeps `profile/`. Saves carry over both ways between 1.2.1 and 1.4.3: 1.4.3 converts a 1.2.1 save on load, and 1.2.1 diff --git a/installer/gui.py b/installer/gui.py index 84b014c8..f04a0f43 100644 --- a/installer/gui.py +++ b/installer/gui.py @@ -16,6 +16,7 @@ import json import os import subprocess import sys +import tempfile from pathlib import Path from . import dpi @@ -34,6 +35,7 @@ except ImportError: from . import ini from . import layout as layouts +from . import recent from .guilogic import (DONE, FAILED, PENDING, RUNNING, SKIPPED, UI_LANGUAGE_LABELS, UI_LANGUAGES, Catalog, Form, Model, Process, amount, default_game_language, describe_event, parse_line, windows_ui_language) @@ -113,6 +115,10 @@ class App: self.bind_text(self.title, 'text', 'ui.rebuild_title') self.vars['dest'].set(str(rebuild)) root.after(POLL_MS, self.start) + else: + last = recent.load() # an update needs only the folder: refresh_form shows the hint and the recorded language + if last: + self.vars['dest'].set(last) # ---- text ----------------------------------------------------------------------------------------------------- @@ -358,6 +364,7 @@ class App: self.fit() return self.form_error.configure(text='') + recent.save(form.dest) # in a repository the Python running this window: the tools' embeddable Python there has no ._pth entry for # the repository, so `-m installer` would not find the package python = Path(sys.executable) if self.layout.dev else self.layout.python @@ -606,6 +613,7 @@ def grab(root, path): def selftest(layout, shots=None): """Every screen in every language; the window closes itself. Returns 0 when nothing raised.""" fix_tcl_paths() + os.environ[recent.ENV] = os.path.join(tempfile.mkdtemp(prefix='totk-selftest-'), 'setup.json') root = tk.Tk() root.geometry('+60+40') app = App(root, 'en', layout) diff --git a/installer/recent.py b/installer/recent.py new file mode 100644 index 00000000..7b9e1700 --- /dev/null +++ b/installer/recent.py @@ -0,0 +1,52 @@ +"""The last install folder, in a per-user file, so an update starts with the folder already filled in. + + {"format": 1, "dest": ""} + +Windows: %LOCALAPPDATA%\TotK Recomp\setup.json; elsewhere $XDG_STATE_HOME/totk-recomp/setup.json (default +~/.local/state/totk-recomp/setup.json). TOTK_SETUP_RECENT names another file (tests, --selftest). Nothing else is +stored: the language comes from the install's own totk-install.json, and no key or key file path is kept. +""" +import json +import os +import sys +from pathlib import Path + +from . import state + +FORMAT = 1 +ENV = 'TOTK_SETUP_RECENT' + + +def path(): + override = os.environ.get(ENV) + if override: + return Path(override) + if sys.platform == 'win32': + base = os.environ.get('LOCALAPPDATA') or str(Path.home() / 'AppData' / 'Local') + return Path(base) / 'TotK Recomp' / 'setup.json' + base = os.environ.get('XDG_STATE_HOME') or str(Path.home() / '.local' / 'state') + return Path(base) / 'totk-recomp' / 'setup.json' + + +def load(): + """The recorded install folder when it still holds totk-install.json, else None. Never raises.""" + try: + data = json.loads(path().read_text(encoding='utf-8')) + dest = data['dest'] + if not isinstance(dest, str) or not dest: + return None + return dest if (Path(dest) / state.NAME).is_file() else None + except (OSError, ValueError, KeyError, TypeError): + return None + + +def save(dest): + """Records dest atomically; a failure to write is ignored, the window works without it.""" + target = path() + tmp = target.with_name(target.name + '.tmp') + try: + target.parent.mkdir(parents=True, exist_ok=True) + tmp.write_text(json.dumps({'format': FORMAT, 'dest': os.path.abspath(dest)}), encoding='utf-8') + os.replace(tmp, target) + except OSError: + pass diff --git a/tests/installer_gui_test.py b/tests/installer_gui_test.py index e8289727..b301421b 100644 --- a/tests/installer_gui_test.py +++ b/tests/installer_gui_test.py @@ -29,6 +29,7 @@ ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT)) from installer import layout as layouts # noqa: E402 from installer import dpi # noqa: E402 +from installer import recent # noqa: E402 from installer import state as statefile # noqa: E402 from installer.events import Reporter # noqa: E402 from installer.runner import Context # noqa: E402 @@ -381,6 +382,42 @@ class DpiTest(unittest.TestCase): self.assertEqual((int(pad), int(wrap)), (round(14 * float(scale)), round(620 * float(scale)))) +class RecentTest(unittest.TestCase): + def setUp(self): + self.tmp = Path(tempfile.mkdtemp()) + self.addCleanup(shutil.rmtree, self.tmp, True) + self.file = self.tmp / 'sub' / 'setup.json' + old = os.environ.get(recent.ENV) + os.environ[recent.ENV] = str(self.file) + self.addCleanup(lambda: os.environ.pop(recent.ENV, None) if old is None else os.environ.update({recent.ENV: old})) + self.dest = self.tmp / 'TotK' + self.dest.mkdir() + + def test_override_is_honoured(self): + self.assertEqual(recent.path(), self.file) + + def test_missing_file(self): + self.assertIsNone(recent.load()) + + def test_folder_without_state(self): + recent.save(self.dest) + self.assertIsNone(recent.load()) + + def test_round_trip(self): + (self.dest / statefile.NAME).write_text('{}', encoding='utf-8') + recent.save(self.dest) + self.assertEqual(json.loads(self.file.read_text(encoding='utf-8')), + {'format': 1, 'dest': os.path.abspath(self.dest)}) + self.assertEqual(recent.load(), os.path.abspath(self.dest)) + self.assertFalse(self.file.with_name('setup.json.tmp').exists()) + + def test_garbage_file(self): + self.file.parent.mkdir() + for text in ('not json', '[]', '{"dest": 5}', '{"format": 1}'): + self.file.write_text(text, encoding='utf-8') + self.assertIsNone(recent.load(), text) + + class SmokeTest(unittest.TestCase): def test_every_screen_in_both_languages(self): try: