mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-21 07:59:58 -04:00
Merge the save export and import for bug reports into the test line.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -7,9 +7,14 @@ no account yet; the import makes one as the game would. Saves are committed thro
|
||||
a finished commit (ready-<n>) is newer than save/<account>: an import clears that folder, so the game loads the
|
||||
imported save.
|
||||
|
||||
<game>/saves is the player's side. Import Saves takes a save put there first (its files, or the folder that holds
|
||||
them); otherwise the player picks a folder. Before the save is replaced it is copied to saves/backup <time>, and a
|
||||
save taken from saves/ moves to saves/imported <time>, so nothing is lost and nothing is imported twice.
|
||||
<game>/saves is the player's side. Import Saves takes a save put there first (a shared save's zip, its files, or the
|
||||
folder that holds them); otherwise the player picks a zip or a folder. Before the save is replaced it is copied to
|
||||
saves/backup <time>, and a save taken from saves/ moves to saves/imported <time>, so nothing is lost and nothing is
|
||||
imported twice.
|
||||
|
||||
A shared save is one zip (export_save, import_zip; section "sharing a save" below): a player attaches it to a bug
|
||||
report, and whoever fixes it imports it to reach the same place in the game. The game's settings menu exports and
|
||||
imports the same zip; its import waits in <game>/profile/save-import for the game's next start.
|
||||
|
||||
A save folder can come from a Switch save backup (the save's files), an emulator (nested folders named by user and
|
||||
title ids, gone down while each holds only one folder), another NX2navi game folder or its profile folder (the
|
||||
@@ -19,11 +24,15 @@ A whole save is account.sav, option.sav and the folders slot_00 to slot_05, each
|
||||
folder with progress.sav at its top is one slot: it replaces that slot (the slot named by its folder, else slot_00) and
|
||||
the rest of the current save stays, as the game reads progress.sav only inside a slot folder.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import stat
|
||||
import tempfile
|
||||
import time
|
||||
import zipfile
|
||||
import zlib
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
@@ -40,13 +49,14 @@ def _rmtree(path):
|
||||
|
||||
README = '''NX2navi saves folder
|
||||
|
||||
To import a save, put it here and select Import Saves in the NX2navi launcher (next to Game Folder):
|
||||
To import a save, put it here and select Saves, then Import a Save, in the NX2navi launcher:
|
||||
- a save someone shared: the NX2navi-save-....zip that NX2navi exported
|
||||
- the files of a Switch save backup, or the folder that holds them
|
||||
- one save slot (a folder such as slot_00): only that slot is replaced
|
||||
- an emulator's save folder for the game
|
||||
- the profile folder (or the whole game folder) of another NX2navi install
|
||||
|
||||
You can also select Import Saves and choose the folder instead.
|
||||
You can also select Import a Save and choose the zip or the folder instead.
|
||||
|
||||
Before a save is imported, the game's current save is copied here into a folder named "backup" and the date. An
|
||||
imported save moves into a folder named "imported" and the date. The game itself keeps its save in profile\\save.
|
||||
@@ -71,7 +81,11 @@ def ensure_saves_dir(game):
|
||||
d = saves_dir(game)
|
||||
d.mkdir(parents=True, exist_ok=True)
|
||||
readme = d / 'README.txt'
|
||||
if not readme.is_file():
|
||||
try:
|
||||
current = readme.read_text(encoding='utf-8')
|
||||
except (OSError, ValueError):
|
||||
current = None
|
||||
if current != README: # missing, or an older launcher's
|
||||
readme.write_text(README, encoding='utf-8', newline='\r\n')
|
||||
return d
|
||||
|
||||
@@ -139,7 +153,8 @@ def _one_slot(src):
|
||||
|
||||
|
||||
def waiting(game):
|
||||
"""(source, names to leave out) of a save put into <game>/saves, or (None, ())."""
|
||||
"""(source, names to leave out) of a save put into <game>/saves, or (None, ()). A zip there (export_save's) comes
|
||||
first, the newest one; its source is the zip file."""
|
||||
d = saves_dir(game)
|
||||
if not d.is_dir():
|
||||
return None, ()
|
||||
@@ -147,6 +162,9 @@ def waiting(game):
|
||||
entries = _entries(d, skip)
|
||||
if not entries:
|
||||
return None, ()
|
||||
zips = [e for e in entries if e.is_file() and e.suffix.lower() == '.zip']
|
||||
if zips:
|
||||
return max(zips, key=lambda e: e.stat().st_mtime), ()
|
||||
if len(entries) == 1 and entries[0].is_dir():
|
||||
return find_save(entries[0])
|
||||
return (d, tuple(sorted(skip))) if any(e.is_file() or _has_file(e) for e in entries) else (None, ())
|
||||
@@ -202,23 +220,64 @@ def game_running(game):
|
||||
return running(game)
|
||||
|
||||
|
||||
def pending_import(game):
|
||||
"""The save the game's menu took from a shared zip and left in <game>/profile/save-import for its next start
|
||||
(runtime/fs_host.c import_pending), or None."""
|
||||
p = Path(game) / 'profile' / PENDING
|
||||
return p if p.is_dir() else None
|
||||
|
||||
|
||||
def import_save(game, folder=None, log=print):
|
||||
"""Make the save in folder (None: the one waiting in <game>/saves) the game's save. Returns {'files', 'source',
|
||||
'backup'}; backup is the folder the replaced save went to, or None when there was none."""
|
||||
"""Make the save in folder (None: the one waiting in <game>/saves) the game's save. folder may also be a zip of
|
||||
export_save (import_zip). Returns {'files', 'source', 'backup', 'slot'}; backup is the folder the replaced save
|
||||
went to, or None when there was none."""
|
||||
game = Path(game)
|
||||
if folder is not None and Path(folder).resolve() == saves_dir(game).resolve():
|
||||
folder = None # the saves folder itself, picked in the dialog: what waits in it, as the button takes it
|
||||
if folder is not None and Path(folder).is_file():
|
||||
return import_zip(game, folder, log=log)
|
||||
from_saves = folder is None
|
||||
src, skip = waiting(game) if from_saves else find_save(folder)
|
||||
if src is None:
|
||||
raise SaveError('the saves folder holds no save' if from_saves else f'{folder} holds no save files')
|
||||
stamp = time.strftime('%Y-%m-%d %H.%M.%S')
|
||||
if Path(src).is_file(): # a shared save's zip put into saves/: it moves out once imported, as a folder does
|
||||
r = import_zip(game, src, log=log, stamp=stamp)
|
||||
_put_away(game, [Path(src).name], stamp)
|
||||
return r
|
||||
prof = (game / 'profile').resolve()
|
||||
if Path(src).resolve() == prof or prof in Path(src).resolve().parents:
|
||||
raise SaveError('that is the game\'s own save')
|
||||
files = _files(src, set(skip))
|
||||
slot = _one_slot(src)
|
||||
account, backup = _replace(game, src, files, slot, stamp, log)
|
||||
if from_saves: # what was put into saves/ moves out whole, so the next import does not take any of it again
|
||||
saves = saves_dir(game)
|
||||
if Path(src) == saves:
|
||||
top = sorted({rel.parts[0] for rel in files})
|
||||
else: # the save sits in a folder that was put there (a game folder, an emulator's id folders)
|
||||
top = [Path(src).relative_to(saves).parts[0]]
|
||||
_put_away(game, top, stamp)
|
||||
log(f'saves: imported {len(files)} files from {src} into {account / slot if slot else account}' +
|
||||
(f'; the old save is in {backup}' if backup else ''))
|
||||
return {'files': len(files), 'source': Path(src), 'backup': backup, 'slot': slot}
|
||||
|
||||
|
||||
def _put_away(game, names, stamp):
|
||||
"""Move names (in <game>/saves) into saves/imported <stamp>."""
|
||||
saves = saves_dir(game)
|
||||
done = _fresh(saves, f'imported {stamp}')
|
||||
done.mkdir()
|
||||
for name in names:
|
||||
shutil.move(str(saves / name), str(done / name))
|
||||
|
||||
|
||||
def _replace(game, src, files, slot, stamp, log):
|
||||
"""Copy the files of src (relative paths) into the game's save: the whole save, or with slot only that slot folder
|
||||
and the rest of the current save kept. The save the game would load now goes to saves/backup <stamp> first.
|
||||
Returns (account folder, backup folder or None)."""
|
||||
account = account_dir(game)
|
||||
saves = ensure_saves_dir(game)
|
||||
stamp = time.strftime('%Y-%m-%d %H.%M.%S')
|
||||
backup = None
|
||||
current = _newest(game, account)
|
||||
if current is not None and _has_file(current):
|
||||
@@ -228,7 +287,6 @@ def import_save(game, folder=None, log=print):
|
||||
tx.mkdir(parents=True, exist_ok=True)
|
||||
stage = tx / 'import'
|
||||
shutil.rmtree(stage, ignore_errors=True)
|
||||
slot = _one_slot(src)
|
||||
if slot and current is not None: # the rest of the save stays; a slot's files left at the top by mistake do not
|
||||
shutil.copytree(current, stage, ignore=lambda d, _names: SLOT_FILES if Path(d) == Path(current) else ())
|
||||
_rmtree(stage / slot)
|
||||
@@ -279,15 +337,188 @@ def import_save(game, folder=None, log=print):
|
||||
raise
|
||||
if gone is not None:
|
||||
_rmtree(gone)
|
||||
if from_saves: # what was put into saves/ moves out whole, so the next import does not take any of it again
|
||||
done = _fresh(saves, f'imported {stamp}')
|
||||
done.mkdir()
|
||||
if Path(src) == saves:
|
||||
top = sorted({rel.parts[0] for rel in files})
|
||||
else: # the save sits in a folder that was put there (a game folder, an emulator's id folders)
|
||||
top = [Path(src).relative_to(saves).parts[0]]
|
||||
for name in top:
|
||||
shutil.move(str(saves / name), str(done / name))
|
||||
log(f'saves: imported {len(files)} files from {src} into {account / slot if slot else account}' +
|
||||
# A save the game's menu took for its next start would replace this one then: this import is the newer choice.
|
||||
for name in (PENDING, PENDING + '.part'):
|
||||
if (game / 'profile' / name).exists():
|
||||
log(f'saves: the save import the game\'s menu left for its next start ({name}) is dropped; '
|
||||
'this import replaces it')
|
||||
_rmtree(game / 'profile' / name)
|
||||
return account, backup
|
||||
|
||||
|
||||
# ---- sharing a save: one zip with the save and a manifest (export_save, import_zip)
|
||||
#
|
||||
# NX2navi-save-<date>_<time>[-bug-report].zip:
|
||||
# manifest.json FORMAT, FORMAT_VERSION, the game's title id and version, the NX2navi version, the export time
|
||||
# save/... the save's files as the game keeps them (account.sav, option.sav, slot_00/progress.sav, ...)
|
||||
# bug-report/... with bug_report: the game's latest log and nx2navi.ini; an import leaves them out
|
||||
# The game's own menu writes and reads the same zip (runtime/save_share.c), with the same manifest keys.
|
||||
|
||||
FORMAT, FORMAT_VERSION = 'nx2navi-save', 1
|
||||
MANIFEST, SAVE_DIR, REPORT_DIR = 'manifest.json', 'save/', 'bug-report/'
|
||||
GAME, GAME_VERSION = 'The Legend of Zelda: Ocarina of Time', '1.1.1'
|
||||
PENDING = 'save-import' # profile/save-import: what the game's menu took from a zip, imported at the game's next start
|
||||
ZIP_MAX_FILES, ZIP_MAX_BYTES = 4000, 256 << 20
|
||||
SAVE_FILE = re.compile(r'^(account\.sav|option\.sav|slot_\d\d/progress\.sav)$') # what makes a folder this game's save
|
||||
|
||||
|
||||
def _title_hex():
|
||||
from .gamefiles import TITLE_ID
|
||||
return f'{TITLE_ID:016x}'
|
||||
|
||||
|
||||
def _key_file(rel):
|
||||
"""Key files never go into a zip, wherever a stray one was copied (a save folder taken from another tool)."""
|
||||
n = Path(rel).name.lower()
|
||||
return n.endswith('.keys') or n.endswith('.keys.txt')
|
||||
|
||||
|
||||
def default_export_dir():
|
||||
"""The player's Downloads folder (where Windows keeps it), else the Desktop, else the home folder."""
|
||||
from .winsys import known_folder
|
||||
for p in (known_folder('downloads'), Path.home() / 'Downloads', known_folder('desktop'), Path.home() / 'Desktop'):
|
||||
if p and Path(p).is_dir():
|
||||
return Path(p)
|
||||
return Path.home()
|
||||
|
||||
|
||||
def _fresh_file(folder, stem, suffix):
|
||||
"""folder/<stem><suffix>, or "<stem> (2)<suffix>" and so on when that is taken."""
|
||||
p, n = folder / f'{stem}{suffix}', 2
|
||||
while p.exists():
|
||||
p, n = folder / f'{stem} ({n}){suffix}', n + 1
|
||||
return p
|
||||
|
||||
|
||||
def export_save(game, dest=None, bug_report=False, version='', log=print):
|
||||
"""Write the save the game would load now into one zip in dest (default_export_dir when None), for a bug report
|
||||
or another player. bug_report adds the game's latest log and nx2navi.ini. Never a key file. Returns {'path',
|
||||
'files', 'extras'} (extras: the names of the bug report files that were there)."""
|
||||
game = Path(game)
|
||||
if not (game / 'profile' / 'account.uid').is_file():
|
||||
raise SaveError('the game has no save yet: play until it saves, then export')
|
||||
account = account_dir(game)
|
||||
dest = Path(dest) if dest else default_export_dir()
|
||||
dest.mkdir(parents=True, exist_ok=True)
|
||||
stem = time.strftime('NX2navi-save-%Y-%m-%d_%H-%M-%S') + ('-bug-report' if bug_report else '')
|
||||
extras = [f for f in (game / 'logs' / 'nx2navi.log', game / 'nx2navi.ini') if bug_report and f.is_file()]
|
||||
for attempt in range(3):
|
||||
# a running game may swap a commit in while its files are read (runtime/fs_host.c swap_in): read again
|
||||
current = _newest(game, account)
|
||||
if current is None or not _has_file(current):
|
||||
raise SaveError('the game has no save yet: play until it saves, then export')
|
||||
files = [rel for rel in _files(current, set()) if not _key_file(rel)]
|
||||
out = _fresh_file(dest, stem, '.zip')
|
||||
tmp = out.with_name(out.name + '.part')
|
||||
manifest = {'format': FORMAT, 'format_version': FORMAT_VERSION, 'game': GAME, 'title_id': _title_hex(),
|
||||
'game_version': GAME_VERSION, 'nx2navi': version,
|
||||
'exported': time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()), 'exported_by': 'launcher',
|
||||
'files': len(files), 'bug_report': bool(bug_report)}
|
||||
try:
|
||||
with zipfile.ZipFile(tmp, 'w', zipfile.ZIP_DEFLATED) as z:
|
||||
z.writestr(MANIFEST, json.dumps(manifest, indent=1) + '\n')
|
||||
for rel in files:
|
||||
z.write(current / rel, SAVE_DIR + rel.as_posix())
|
||||
for f in extras:
|
||||
z.write(f, REPORT_DIR + f.name)
|
||||
os.replace(tmp, out)
|
||||
except FileNotFoundError:
|
||||
tmp.unlink(missing_ok=True)
|
||||
if attempt == 2:
|
||||
raise
|
||||
time.sleep(0.2)
|
||||
continue
|
||||
except BaseException:
|
||||
tmp.unlink(missing_ok=True)
|
||||
raise
|
||||
log(f'saves: exported {len(files)} files from {current} to {out}' +
|
||||
(f' with {", ".join(f.name for f in extras)}' if extras else ''))
|
||||
return {'path': out, 'files': len(files), 'extras': [f.name for f in extras]}
|
||||
|
||||
|
||||
def _member_path(name):
|
||||
"""The relative path of a zip member below save/, or SaveError when it could land outside the save."""
|
||||
parts = name.split('/')
|
||||
if '\\' in name or name.startswith('/') or any(p in ('', '.', '..') for p in parts) or ':' in name:
|
||||
raise SaveError(f'it holds a file outside the save ({SAVE_DIR}{name})')
|
||||
return Path(*parts)
|
||||
|
||||
|
||||
def check_manifest(m):
|
||||
"""SaveError unless m is the manifest of an NX2navi save of this game that this version can read."""
|
||||
if not isinstance(m, dict) or m.get('format') != FORMAT:
|
||||
raise SaveError(f'its {MANIFEST} is not an NX2navi save manifest')
|
||||
v = m.get('format_version')
|
||||
if type(v) is not int or v < 1:
|
||||
raise SaveError(f'its {MANIFEST} has no format version')
|
||||
if v > FORMAT_VERSION:
|
||||
raise SaveError(f'it was made by a newer NX2navi ({m.get("nx2navi") or "format " + str(v)}): update NX2navi '
|
||||
'first')
|
||||
if str(m.get('title_id', '')).lower() != _title_hex():
|
||||
raise SaveError(f'it is a save of another game ({m.get("game") or m.get("title_id") or "not named"})')
|
||||
|
||||
|
||||
def read_zip(path):
|
||||
"""The manifest of a zip of export_save and its save files as [(ZipInfo, relative Path)], checked: the manifest
|
||||
(check_manifest), every path inside the save, no links, at most ZIP_MAX_FILES files and ZIP_MAX_BYTES, and among
|
||||
them the files of this game's save (SAVE_FILE). SaveError otherwise."""
|
||||
name = Path(path).name
|
||||
try:
|
||||
z = zipfile.ZipFile(path)
|
||||
except (zipfile.BadZipFile, OSError) as e:
|
||||
raise SaveError(f'{name} cannot be read as a zip file ({e})') from None
|
||||
with z:
|
||||
try:
|
||||
info = z.getinfo(MANIFEST)
|
||||
except KeyError:
|
||||
raise SaveError(f'{name} is not a save exported by NX2navi (it has no {MANIFEST}); to import a save '
|
||||
'folder, unpack the zip and choose the folder') from None
|
||||
try:
|
||||
if info.file_size > 1 << 16:
|
||||
raise ValueError('too large')
|
||||
m = json.loads(z.read(info).decode('utf-8'))
|
||||
except (ValueError, zipfile.BadZipFile, OSError, EOFError, zlib.error) as e:
|
||||
raise SaveError(f'the {MANIFEST} of {name} cannot be read ({e})') from None
|
||||
check_manifest(m)
|
||||
members, total = [], 0
|
||||
for info in z.infolist():
|
||||
if not info.filename.startswith(SAVE_DIR) or info.is_dir():
|
||||
continue # the manifest, bug report files
|
||||
rel = _member_path(info.filename[len(SAVE_DIR):])
|
||||
if (info.external_attr >> 16) & 0o170000 == 0o120000:
|
||||
raise SaveError(f'it holds a link ({info.filename})')
|
||||
if _key_file(rel):
|
||||
continue
|
||||
total += info.file_size
|
||||
members.append((info, rel))
|
||||
if len(members) > ZIP_MAX_FILES or total > ZIP_MAX_BYTES:
|
||||
raise SaveError(f'it holds more than a save ({len(members)} files, {total / 1e6:.0f} MB so far)')
|
||||
if not any(SAVE_FILE.match(rel.as_posix()) for _, rel in members):
|
||||
raise SaveError(f'it holds no save of {GAME}' if members else 'it holds no save files')
|
||||
return m, members
|
||||
|
||||
|
||||
def import_zip(game, path, log=print, stamp=None):
|
||||
"""Make the save in a zip of export_save the game's save (a whole save: every slot), backed up first as
|
||||
import_save does. Returns import_save's dict with the zip's manifest under 'manifest'."""
|
||||
game = Path(game)
|
||||
m, members = read_zip(path)
|
||||
stage = Path(tempfile.mkdtemp(prefix='nx2navi-save-'))
|
||||
try:
|
||||
try:
|
||||
with zipfile.ZipFile(path) as z:
|
||||
for info, rel in members:
|
||||
out = stage / rel
|
||||
out.parent.mkdir(parents=True, exist_ok=True)
|
||||
with z.open(info) as f, open(out, 'wb') as g: # the CRC is checked as the member is read
|
||||
shutil.copyfileobj(f, g)
|
||||
except (zipfile.BadZipFile, EOFError, zlib.error) as e:
|
||||
raise SaveError(f'{Path(path).name} is damaged ({e})') from None
|
||||
files = [rel for _, rel in members]
|
||||
account, backup = _replace(game, stage, files, None, stamp or time.strftime('%Y-%m-%d %H.%M.%S'), log)
|
||||
finally:
|
||||
_rmtree(stage)
|
||||
log(f'saves: imported {len(files)} files from {path} (exported {m.get("exported", "?")} by NX2navi '
|
||||
f'{m.get("nx2navi") or "?"}, game {m.get("game_version", "?")}) into {account}' +
|
||||
(f'; the old save is in {backup}' if backup else ''))
|
||||
return {'files': len(files), 'source': Path(src), 'backup': backup, 'slot': slot}
|
||||
return {'files': len(files), 'source': Path(path), 'backup': backup, 'slot': None, 'manifest': m}
|
||||
|
||||
@@ -13,6 +13,8 @@ DEFAULTS = {
|
||||
'hide_while_playing': True, # hide the window while the game runs, show it again when it exits
|
||||
'keep_cache': True, # keep build-cache/ (the lift and objects) so an update recompiles only changes
|
||||
'desktop_shortcut': True,
|
||||
'save_export_dir': '', # where Saves > Export Save writes its zip; empty: the Downloads folder
|
||||
'export_with_log': True, # Export Save adds the game's latest log and nx2navi.ini (for a bug report)
|
||||
'feed_repo': '', # owner/name overriding feed.json, for testing a fork's releases
|
||||
'github_token': '', # only for a private release repository; stored as plain text
|
||||
'last_check': 0.0,
|
||||
|
||||
@@ -8,7 +8,8 @@ The window is one Tk canvas at 1000 x 640 logical pixels, scaled to the display.
|
||||
flat cells and the orange choices are canvas items. Screens:
|
||||
|
||||
first_run the dump, the install folder, three choices and Begin Installation
|
||||
home Play, Updates, the game folder, Rebuild the Game, Settings
|
||||
home Play, Updates, the game folder, Saves, Rebuild the Game, Settings
|
||||
saves Export Save (a zip for a bug report or another player), Import a Save (such a zip), a save folder
|
||||
job the steps of an install, update or rebuild, with their progress, and Cancel; after a failure Try Again
|
||||
and Copy Report
|
||||
settings the launcher's choices, the folders, the logs and Uninstall
|
||||
@@ -153,7 +154,8 @@ class App:
|
||||
enabled=not self.layout.dev and not args.selftest)
|
||||
self.row_gap = GAP
|
||||
self.message = ''
|
||||
self.save_message = '' # the last Import Saves result, shown in its description
|
||||
self.save_message = '' # the last import's result, shown in the import rows' descriptions
|
||||
self.export_message = '' # the last Export Save result, shown in its description
|
||||
self.dumps = None
|
||||
self.dump_search = None
|
||||
self.uninstall_game = False
|
||||
@@ -424,6 +426,8 @@ class App:
|
||||
self.show('home')
|
||||
elif self.screen == 'uninstall':
|
||||
self.show('settings')
|
||||
elif self.screen == 'saves':
|
||||
self.show('home', next((i for i, r in enumerate(self.rows_home()) if r.label == 'Saves'), None))
|
||||
elif self.screen == 'job' and self.job and self.job.result is None:
|
||||
self.cancel_job()
|
||||
elif self.screen == 'job':
|
||||
@@ -755,7 +759,7 @@ class App:
|
||||
Row('divider'),
|
||||
Row('value', 'Game Folder', gd, on=lambda: self.open(gd),
|
||||
desc=('Open the game folder. The game keeps your save in its profile folder, its settings in '
|
||||
'nx2navi.ini and its logs in logs. Its saves folder is for saves to import.',)),
|
||||
'nx2navi.ini and its logs in logs. Its saves folder holds saves to import and backups.',)),
|
||||
self.saves_row(gd),
|
||||
Row('value', 'Rebuild the Game', rebuild_value, rebuild_style, on=self.start_rebuild,
|
||||
desc=('Build the game again from the files of the last build. Only what changed is compiled. Use it '
|
||||
@@ -764,24 +768,107 @@ class App:
|
||||
desc=('Updates, the game folder, the logs and uninstalling.',)),
|
||||
]
|
||||
|
||||
def saves_row(self, gd):
|
||||
"""Import Saves: a save put into the game folder's saves folder, else a folder the player picks."""
|
||||
ready = False
|
||||
def save_waiting(self, gd):
|
||||
"""A save put into the game folder's saves folder (a shared zip, a save folder), waiting to be imported."""
|
||||
if gd and Path(gd).is_dir():
|
||||
try:
|
||||
saves.ensure_saves_dir(gd)
|
||||
ready = saves.waiting(gd)[0] is not None
|
||||
return saves.waiting(gd)[0] is not None
|
||||
except OSError:
|
||||
pass
|
||||
desc = ('Bring in a save from a Switch save backup, an emulator or another NX2navi install. Put it into the '
|
||||
'saves folder of the game folder and select this row, or select this row and choose its folder.',
|
||||
'Your current save is copied to the saves folder first.')
|
||||
if self.save_message:
|
||||
desc = (self.save_message,) + desc
|
||||
return Row('value', 'Import Saves', 'From the Saves Folder' if ready else 'Choose a Folder',
|
||||
'orange_arrow' if ready else 'arrow', on=self.import_saves, desc=desc)
|
||||
return False
|
||||
|
||||
def import_saves(self):
|
||||
def saves_row(self, gd):
|
||||
"""Saves: the screen that exports and imports saves; orange while a save waits in the saves folder."""
|
||||
ready = self.save_waiting(gd)
|
||||
desc = ('Export your save into one zip, to attach to a bug report or give to another player, or import a '
|
||||
'save someone shared, a Switch save backup or an emulator\'s save.',)
|
||||
if ready:
|
||||
desc += ('A save waits in the saves folder of the game folder: select this row, then Import a Save.',)
|
||||
return Row('value', 'Saves', 'Import Waiting' if ready else '', 'orange_arrow' if ready else 'arrow',
|
||||
on=lambda: self.show('saves'), desc=desc)
|
||||
|
||||
# ---- saves
|
||||
|
||||
def export_dir(self):
|
||||
d = self.settings.get('save_export_dir')
|
||||
return Path(d) if d and Path(d).is_dir() else saves.default_export_dir()
|
||||
|
||||
def rows_saves(self):
|
||||
gd = self.settings.get('game_dir')
|
||||
ready = self.save_waiting(gd)
|
||||
out = self.export_dir()
|
||||
export_desc = ('Write your save into one zip file, to attach to a bug report or give to another player: every '
|
||||
'save slot and a note of the NX2navi and game versions, never your keys or game files. The '
|
||||
'folder opens with the zip selected.',)
|
||||
if self.export_message:
|
||||
export_desc = (self.export_message,) + export_desc
|
||||
import_desc = ('Make a save someone shared (an NX2navi-save zip) your save: every slot is replaced. Put the zip '
|
||||
'into the saves folder of the game folder and select this row, or select this row and choose '
|
||||
'the zip.', 'Your current save is copied to the saves folder first.')
|
||||
if gd and saves.pending_import(gd):
|
||||
import_desc += ('A save imported in the game\'s menu waits for the game\'s next start; an import here '
|
||||
'replaces it.',)
|
||||
folder_desc = ('Bring in a save folder: a Switch save backup, an emulator\'s save, another NX2navi install, or '
|
||||
'one save slot (a folder named slot_00 to slot_05: only that slot is replaced).',
|
||||
'Your current save is copied to the saves folder first.')
|
||||
if self.save_message:
|
||||
import_desc, folder_desc = (self.save_message,) + import_desc, (self.save_message,) + folder_desc
|
||||
|
||||
def report(v):
|
||||
self.settings['export_with_log'] = bool(v)
|
||||
self.settings.save()
|
||||
return [
|
||||
Row('value', 'Export Save', out.name or str(out), on=self.export_save, desc=export_desc),
|
||||
Row('toggle', 'Add Log and Settings', on=report, get=lambda: bool(self.settings.get('export_with_log')),
|
||||
desc=('On: the zip also holds the game\'s latest log (logs\\nx2navi.log) and its settings '
|
||||
'(nx2navi.ini), which show what happened and with which settings. Keep it on for a bug report.',
|
||||
'Off: the save alone, to give to another player.')),
|
||||
Row('value', 'Export To', str(out), on=self.choose_export_dir,
|
||||
desc=(f'Export Save writes into {out}.', 'Select this row to choose another folder.')),
|
||||
Row('divider'),
|
||||
Row('value', 'Import a Save', 'From the Saves Folder' if ready else 'Choose a Zip',
|
||||
'orange_arrow' if ready else 'arrow', on=lambda: self.import_saves(folder=False), desc=import_desc),
|
||||
Row('value', 'Import a Save Folder', 'Choose a Folder', 'arrow',
|
||||
on=lambda: self.import_saves(folder=True), desc=folder_desc),
|
||||
Row('value', 'Open Saves Folder', '', 'arrow',
|
||||
on=lambda: gd and Path(gd).is_dir() and self.open(saves.ensure_saves_dir(gd)),
|
||||
desc=('The saves folder of the game folder: saves to import, and a backup of each save an import '
|
||||
'replaced.',)),
|
||||
Row('action', 'Back', on=lambda: self.show('home'), height=40, desc=('Back to the main menu.',)),
|
||||
]
|
||||
|
||||
def choose_export_dir(self):
|
||||
d = filedialog.askdirectory(parent=self.root, title='Choose where Export Save writes its zip',
|
||||
initialdir=str(self.export_dir()), mustexist=True)
|
||||
if d:
|
||||
self.settings['save_export_dir'] = str(Path(d))
|
||||
self.settings.save()
|
||||
self.show('saves', self.focus)
|
||||
|
||||
def export_save(self):
|
||||
gd = self.settings.get('game_dir')
|
||||
if not gd or not Path(gd).is_dir():
|
||||
self.export_message = 'Install the game first.'
|
||||
else:
|
||||
try:
|
||||
r = saves.export_save(gd, self.export_dir(), bug_report=bool(self.settings.get('export_with_log')),
|
||||
version=versions.display(build.built_version(gd) or self.layout.version),
|
||||
log=self.ctx.log)
|
||||
except (saves.SaveError, OSError) as e:
|
||||
self.export_message = f'The save was not exported: {e}'
|
||||
else:
|
||||
extra = ' with the game\'s log and settings' if r['extras'] else ''
|
||||
self.export_message = f'Exported {r["files"]} files{extra} to {r["path"]}.'
|
||||
try:
|
||||
winsys.reveal(r['path'])
|
||||
except OSError:
|
||||
pass
|
||||
self.show('saves', self.focus)
|
||||
|
||||
def import_saves(self, folder):
|
||||
"""Import a Save (folder False): what waits in the saves folder, else a zip the player picks. Import a Save
|
||||
Folder (folder True): a folder the player picks; the saves folder itself takes what waits there."""
|
||||
gd = self.settings.get('game_dir')
|
||||
if not gd or not Path(gd).is_dir():
|
||||
self.save_message = 'Install the game first.'
|
||||
@@ -789,20 +876,37 @@ class App:
|
||||
self.save_message = 'Quit the game first: it keeps its save open while it runs.'
|
||||
else:
|
||||
try:
|
||||
folder = None
|
||||
if saves.waiting(gd)[0] is None:
|
||||
folder = filedialog.askdirectory(parent=self.root, title='Choose the folder with the save',
|
||||
initialdir=str(saves.ensure_saves_dir(gd)), mustexist=True)
|
||||
if not folder:
|
||||
src = None
|
||||
if folder:
|
||||
src = filedialog.askdirectory(parent=self.root, title='Choose the folder with the save',
|
||||
initialdir=str(saves.ensure_saves_dir(gd)), mustexist=True)
|
||||
if not src:
|
||||
return
|
||||
r = saves.import_save(gd, folder, log=self.ctx.log)
|
||||
elif saves.waiting(gd)[0] is None:
|
||||
src = filedialog.askopenfilename(parent=self.root, title='Choose the save to import',
|
||||
initialdir=str(self.export_dir()),
|
||||
filetypes=[('NX2navi save', '*.zip'), ('All files', '*.*')])
|
||||
if not src:
|
||||
return
|
||||
r = saves.import_save(gd, src, log=self.ctx.log)
|
||||
except (saves.SaveError, OSError) as e:
|
||||
self.save_message = f'The save was not imported: {e}'
|
||||
else:
|
||||
self.save_message = ((f'Save imported into {r["slot"]}.' if r['slot'] else
|
||||
f'Save imported: {r["files"]} files from {r["source"].name}.')
|
||||
+ (f' Your old save is in saves\\{r["backup"].name}.' if r['backup'] else ''))
|
||||
self.show('home', self.focus)
|
||||
self.save_message = self.imported_text(r)
|
||||
self.show('saves', self.focus)
|
||||
|
||||
@staticmethod
|
||||
def imported_text(r):
|
||||
"""What an import did, for the import rows' descriptions."""
|
||||
m = r.get('manifest')
|
||||
if m:
|
||||
text = (f'Save imported from {r["source"].name} (exported {str(m.get("exported", ""))[:10]} with NX2navi '
|
||||
f'{m.get("nx2navi") or "?"}).')
|
||||
elif r['slot']:
|
||||
text = f'Save imported into {r["slot"]}.'
|
||||
else:
|
||||
text = f'Save imported: {r["files"]} files from {r["source"].name}.'
|
||||
return text + (f' Your old save is in saves\\{r["backup"].name}.' if r['backup'] else '')
|
||||
|
||||
def update_desc(self):
|
||||
r = self.release
|
||||
|
||||
@@ -163,10 +163,17 @@ def states(app):
|
||||
app.show('job')
|
||||
|
||||
def import_saves():
|
||||
app.release, app.message = None, ''
|
||||
app.save_message = ('Save imported: 3 files from 0100000000010000. Your old save is in '
|
||||
'saves\\backup 2026-11-08 17.30.12.')
|
||||
app.show('home', next(i for i, r in enumerate(app.rows_home()) if r.label == 'Import Saves'))
|
||||
app.release, app.message, app.export_message = None, '', ''
|
||||
app.save_message = ('Save imported from NX2navi-save-2026-11-09_17-30-05.zip (exported 2026-11-09 with NX2navi '
|
||||
'0.6.1). Your old save is in saves' + chr(92) + 'backup 2026-11-09 18.02.44.')
|
||||
app.show('saves', next(i for i, r in enumerate(app.rows_saves()) if r.label == 'Import a Save'))
|
||||
|
||||
def export_save():
|
||||
app.save_message = ''
|
||||
app.export_message = ('Exported 14 files with the game' + chr(39) + 's log and settings to '
|
||||
+ str(Path.home() / 'Downloads' / 'NX2navi-save-2026-11-09_17-30-05-bug-report.zip')
|
||||
+ '.')
|
||||
app.show('saves', 0)
|
||||
|
||||
def update_staged(): # a release downloaded in the background, waiting for the game to be closed
|
||||
rel = fake_release(ahead_of(ctx.version))
|
||||
@@ -178,7 +185,7 @@ def states(app):
|
||||
return [('1-first-run', first_run), ('2-installing', installing), ('3-home', home),
|
||||
('4-update-ready', update_ready), ('5-updating', updating), ('6-settings', settings),
|
||||
('7-build-failed', failed), ('8-uninstall', uninstall), ('9-import-saves', import_saves),
|
||||
('10-shaders', shaders), ('11-update-staged', update_staged)]
|
||||
('10-shaders', shaders), ('11-update-staged', update_staged), ('12-export-save', export_save)]
|
||||
|
||||
|
||||
def run(app, shots):
|
||||
|
||||
@@ -149,6 +149,48 @@ def open_path(path):
|
||||
subprocess.Popen(['xdg-open', path], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
|
||||
|
||||
def reveal(path):
|
||||
"""Show a file in its folder, selected in Explorer (a file to drag into a bug report); elsewhere open the folder."""
|
||||
path = Path(path)
|
||||
if os.name == 'nt':
|
||||
subprocess.Popen(['explorer', f'/select,{path}']) # noqa: S603, S607
|
||||
else:
|
||||
open_path(path.parent)
|
||||
|
||||
|
||||
KNOWN_FOLDERS = {'downloads': '{374DE290-123F-4565-9164-39C4925E467B}',
|
||||
'desktop': '{B4BFCC3A-DB2C-424C-B029-7FE99A87C641}'}
|
||||
|
||||
|
||||
def known_folder(name):
|
||||
"""Windows: the folder the shell keeps for name ('downloads', 'desktop'), also when the player moved it to another
|
||||
drive; None elsewhere or when the shell names none."""
|
||||
if os.name != 'nt' or name not in KNOWN_FOLDERS:
|
||||
return None
|
||||
try:
|
||||
from ctypes import wintypes
|
||||
|
||||
class GUID(ctypes.Structure):
|
||||
_fields_ = [('d1', wintypes.DWORD), ('d2', wintypes.WORD), ('d3', wintypes.WORD),
|
||||
('d4', ctypes.c_ubyte * 8)]
|
||||
ole32, shell32 = ctypes.WinDLL('ole32'), ctypes.WinDLL('shell32') # private handles: argtypes stay here
|
||||
ole32.CLSIDFromString.argtypes = (wintypes.LPCWSTR, ctypes.POINTER(GUID))
|
||||
ole32.CoTaskMemFree.argtypes = (ctypes.c_void_p,)
|
||||
shell32.SHGetKnownFolderPath.argtypes = (ctypes.POINTER(GUID), wintypes.DWORD, wintypes.HANDLE,
|
||||
ctypes.POINTER(ctypes.c_void_p))
|
||||
g, out = GUID(), ctypes.c_void_p()
|
||||
if ole32.CLSIDFromString(KNOWN_FOLDERS[name], ctypes.byref(g)) != 0:
|
||||
return None
|
||||
if shell32.SHGetKnownFolderPath(ctypes.byref(g), 0, None, ctypes.byref(out)) != 0:
|
||||
return None
|
||||
try:
|
||||
return Path(ctypes.wstring_at(out.value))
|
||||
finally:
|
||||
ole32.CoTaskMemFree(out)
|
||||
except (AttributeError, OSError):
|
||||
return None
|
||||
|
||||
|
||||
def start_game(exe):
|
||||
"""Start the game from its own folder; returns the process to watch. ocarina.exe is a console program and the
|
||||
launcher has no console, so without CREATE_NO_WINDOW Windows opened an empty console window beside the game,
|
||||
|
||||
Reference in New Issue
Block a user