diff --git a/docs/launcher.md b/docs/launcher.md index ba2fc390..bdeaaf59 100644 --- a/docs/launcher.md +++ b/docs/launcher.md @@ -164,9 +164,11 @@ it), and what to check: the date, time and time zone, or security software that ## Uninstalling -Settings > Uninstall NX2navi, or Settings > Apps > NX2navi (which runs `NX2navi.exe --uninstall`): the launcher -folder, its toolchain, the shortcuts and the Apps entry go; the built game and its saves stay unless "Remove the -Built Game" is On. The dump is never touched. +Settings > Uninstall NX2navi, or Settings > Apps > NX2navi (which runs `NX2navi.exe --uninstall`): what the launcher +put in its folder (`NX2navi.exe`, `python`, `app`, `pydeps`, `data`, `toolchain`; `launcher/winsys.py` `OWNED`), the +toolchain in `C:\NX2navi` when it went there, the shortcuts and the Apps entry go; the launcher's folder itself goes +only when that leaves it empty. The built game and its saves stay unless "Remove the Built Game" is On, also when the +game folder is the default `\game`. The dump is never touched. ## Command line diff --git a/launcher/winsys.py b/launcher/winsys.py index 9669d21d..d5f0fe58 100644 --- a/launcher/winsys.py +++ b/launcher/winsys.py @@ -198,23 +198,55 @@ def keep_awake(on): pass +# What the launcher puts in its folder (launcher/paths.py, the stub's python.next and python.old-). Uninstall deletes +# only these: the default game folder is \game, and a folder given to the setup with --dest may hold other things. +OWNED = ('NX2navi.exe', 'app', 'python', 'python.next', 'pydeps', 'data', 'toolchain') + + +def uninstall_plan(layout, remove_game=''): + """(paths to delete with all they hold, folders to delete only when that left them empty), in order. A kept game + folder stays whether or not it lies inside the launcher's folder.""" + root = Path(layout.root) + gone = [Path(remove_game)] if remove_game else [] + gone += [root / n for n in OWNED] + try: + gone += sorted(p for p in root.glob('python.old-*')) + except OSError: + pass + empty = [root] + if Path(layout.toolchain).parent != root: # the toolchain went to C:\NX2navi (launcher/paths.py ansi_safe) + gone.append(Path(layout.toolchain)) + empty.append(Path(layout.toolchain).parent) + return gone, empty + + +def uninstall_script(gone, empty): + """The cmd.exe line that deletes them once the launcher has exited: a file with del, a folder with rmdir /s, the + folders that must be empty with a plain rmdir (which leaves a folder that still holds something).""" + steps = ['ping -n 3 127.0.0.1 >nul'] + for p in gone: + steps.append(f'(if exist "{p}\\*" (rmdir /s /q "{p}") else if exist "{p}" (del /f /q "{p}"))') + steps += [f'rmdir "{p}" 2>nul' for p in empty] + return ' & '.join(steps) + + def uninstall(layout, remove_game='', log=print): - """Remove the shortcuts, the uninstall entry and, once this process has exited, the launcher's folder (and the - game folder when remove_game names it). Saves inside a kept game folder stay.""" + """Remove the shortcuts, the uninstall entry and, once this process has exited, what the launcher put in its folder + (and the game folder when remove_game names it). A kept game folder and its saves stay, also when it lies inside + the launcher's folder.""" stub_run(layout, ['--shortcuts', 'off']) + gone, empty = uninstall_plan(layout, remove_game) if os.name == 'nt': try: import winreg winreg.DeleteKey(winreg.HKEY_CURRENT_USER, UNINSTALL_KEY) except OSError: pass - targets = [str(layout.root)] + ([str(remove_game)] if remove_game else []) - if layout.toolchain.parent != layout.root: - targets.append(str(layout.toolchain.parent)) - script = 'ping -n 3 127.0.0.1 >nul & ' + ' & '.join(f'rmdir /s /q "{t}"' for t in targets) - start_detached(['cmd.exe', '/c', script], cwd=os.environ.get('TEMP', 'C:\\')) + # one command line as cmd.exe reads it: a list would have its quotes escaped as \", which cmd.exe does not + # understand, and no rmdir would find its folder + start_detached(f'cmd.exe /c "{uninstall_script(gone, empty)}"', cwd=os.environ.get('TEMP', 'C:\\')) else: - log('uninstall: remove these folders yourself: ' + ', '.join([str(layout.root), str(remove_game)])) + log('uninstall: remove these yourself: ' + ', '.join(str(p) for p in gone if p.exists())) # ---------------------------------------------------------------- gamepad (XInput) diff --git a/tests/launcher_test.py b/tests/launcher_test.py index 2150634e..ac61cd3a 100644 --- a/tests/launcher_test.py +++ b/tests/launcher_test.py @@ -38,7 +38,7 @@ ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT)) sys.path.insert(0, str(ROOT / 'tools')) -from launcher import build, feed, gamefiles, jobs, net, saves, settings, toolchain, updater, versions # noqa: E402 +from launcher import build, feed, gamefiles, jobs, net, saves, settings, toolchain, updater, versions, winsys # noqa: E402 from launcher.context import Context # noqa: E402 from launcher.paths import Layout # noqa: E402 @@ -269,6 +269,49 @@ class SettingsTest(Tmp): self.assertNotIn('junk', s) +class UninstallTest(Tmp): + def tree(self, root, rels): + for rel in rels: + p = root / rel + p.parent.mkdir(parents=True, exist_ok=True) + p.write_text('x') + + def test_plan_keeps_the_game_and_what_is_not_ours(self): + from types import SimpleNamespace + root = self.tmp / 'NX2navi' + self.tree(root, ('NX2navi.exe', 'app/current.txt', 'python.old-3/x', 'game/profile/save/1/a.sav', 'mine.txt')) + layout = SimpleNamespace(root=root, toolchain=root / 'toolchain') + gone, empty = winsys.uninstall_plan(layout) + self.assertNotIn(root / 'game', gone) + self.assertNotIn(root, gone) + self.assertIn(root / 'python.old-3', gone) + self.assertEqual(empty, [root]) + gone, _ = winsys.uninstall_plan(layout, str(root / 'game')) + self.assertEqual(gone[0], root / 'game') + # the toolchain moved to C:\NX2navi: that folder goes only when nothing else is left in it + other = self.tmp / 'C NX2navi' / 'toolchain' + gone, empty = winsys.uninstall_plan(SimpleNamespace(root=root, toolchain=other)) + self.assertIn(other, gone) + self.assertNotIn(other.parent, gone) + self.assertEqual(empty, [root, other.parent]) + + @unittest.skipUnless(os.name == 'nt', 'runs the cmd.exe script') + def test_script_on_windows(self): + from types import SimpleNamespace + root = self.tmp / 'NX2navi ΓΏ x' # spaces and a character outside ASCII + self.tree(root, ('NX2navi.exe', 'app/1/VERSION', 'python/pythonw.exe', 'python.old-12/x', 'pydeps/z', + 'data/launcher.json', 'toolchain/msys64/a', 'game/profile/save/1/a.sav', 'mine.txt')) + layout = SimpleNamespace(root=root, toolchain=root / 'toolchain') + script = winsys.uninstall_script(*winsys.uninstall_plan(layout)).replace('ping -n 3 127.0.0.1 >nul', 'echo') + subprocess.run(f'cmd.exe /c "{script}"', capture_output=True) + self.assertEqual(sorted(p.name for p in root.iterdir()), ['game', 'mine.txt']) + self.assertTrue((root / 'game/profile/save/1/a.sav').is_file()) + (root / 'mine.txt').unlink() + script = winsys.uninstall_script(*winsys.uninstall_plan(layout, str(root / 'game'))) + subprocess.run(f'cmd.exe /c "{script.replace("ping -n 3 127.0.0.1 >nul", "echo")}"', capture_output=True) + self.assertFalse(root.exists()) + + # ---------------------------------------------------------------- toolchain class ToolchainTest(Tmp):