Uninstall only what the launcher put in its folder, so a kept game folder inside it stays.

Uninstall ran rmdir /s on the whole launcher folder, which holds the default game folder (<launcher>\game) and, with
the setup's --dest, whatever else the player keeps there: "Remove the Built Game" Off would have deleted the game and
its saves. It now deletes NX2navi.exe, python, app, pydeps, data, toolchain and python.old-*, then the folder only
when that left it empty, and for a toolchain in C:\NX2navi that toolchain, then C:\NX2navi only when empty.

The command also never ran: it went to cmd.exe as a list, whose quotes Python escapes as \", which cmd.exe does not
understand, so every rmdir failed and nothing was removed. It is now one command line. tests/launcher_test.py runs it
on a folder with spaces and a character outside ASCII.

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 b142b7a71a
commit aaea991191
3 changed files with 89 additions and 12 deletions

View File

@@ -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 `<launcher>\game`. The dump is never touched.
## Command line

View File

@@ -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-<n>). Uninstall deletes
# only these: the default game folder is <root>\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)

View File

@@ -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):