mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-17 07:09:57 -04:00
Import a single save slot into its own slot folder and keep the rest of the save.
A slot folder holds only progress.sav and caption.sav, and Import Saves took those two files for a whole save: it replaced the save and left them at its top, where the game does not read them, so the game started a new one. A folder with progress.sav at its top now replaces that slot alone. A backup made by the import no longer brings its save-tx folder back with it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -145,8 +145,9 @@ Import Saves (home, under Game Folder; `launcher/saves.py`) makes another save t
|
||||
a `saves` folder with a README for it: a save put there (its files, or the folder that holds them) is taken first,
|
||||
and the row then reads "From the Saves Folder"; otherwise selecting the row asks for the folder. It recognises a
|
||||
Switch save backup (the save's files), an emulator's save folder (nested folders named by user and title ids), and
|
||||
another NX2navi game folder or its profile folder (the account folder inside). A folder with more than 4,000 files
|
||||
or 1 GB is refused as picked by mistake.
|
||||
another NX2navi game folder or its profile folder (the account folder inside). A folder with `progress.sav` at its
|
||||
top is one save slot: it replaces that slot (`slot_00` to `slot_05`, by the folder's name, else `slot_00`) and the rest
|
||||
of the current save stays. A folder with more than 4,000 files or 1 GB is refused as picked by mistake.
|
||||
|
||||
The game keeps its save in `profile/save/<account>`, named after `profile/account.uid` (made as the game would when
|
||||
it never ran). The import copies the save the game would load now (a finished commit still waiting in
|
||||
|
||||
@@ -14,6 +14,10 @@ save taken from saves/ moves to saves/imported <time>, so nothing is lost and no
|
||||
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
|
||||
account folder inside).
|
||||
|
||||
A whole save is account.sav, option.sav and the folders slot_00 to slot_05, each with progress.sav and caption.sav. A
|
||||
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 os
|
||||
import re
|
||||
@@ -38,6 +42,7 @@ README = '''NX2navi saves folder
|
||||
|
||||
To import a save, put it here and select Import Saves in the NX2navi launcher (next to Game Folder):
|
||||
- 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
|
||||
|
||||
@@ -47,6 +52,8 @@ Before a save is imported, the game's current save is copied here into a folder
|
||||
imported save moves into a folder named "imported" and the date. The game itself keeps its save in profile\\save.
|
||||
'''
|
||||
HEX32 = re.compile(r'^[0-9a-fA-F]{32}$')
|
||||
SLOT = re.compile(r'^slot_\d\d$')
|
||||
SLOT_FILES = ('progress.sav', 'caption.sav')
|
||||
MAX_FILES, MAX_BYTES = 4000, 1 << 30 # a save is a few files of a few MB; more is a folder picked by mistake
|
||||
OURS = re.compile(r'^(backup|imported) \d{4}-\d\d-\d\d') # what this module writes into saves/
|
||||
|
||||
@@ -119,7 +126,16 @@ def find_save(folder):
|
||||
if len(entries) != 1 or len(dirs) != 1:
|
||||
break
|
||||
p = dirs[0]
|
||||
return (p, ()) if _has_file(p) else (None, ())
|
||||
# a backup this module wrote keeps the replaced save's journal beside it, which is not part of the save
|
||||
return (p, ('save-tx',) if (p / 'save-tx').is_dir() else ()) if _has_file(p) else (None, ())
|
||||
|
||||
|
||||
def _one_slot(src):
|
||||
"""The slot folder the files of src belong in when src is a single slot, None when it is a whole save."""
|
||||
p = Path(src)
|
||||
if not (p / 'progress.sav').is_file() or any(e.is_dir() and SLOT.match(e.name) for e in p.iterdir()):
|
||||
return None
|
||||
return p.name if SLOT.match(p.name) else 'slot_00'
|
||||
|
||||
|
||||
def waiting(game):
|
||||
@@ -212,11 +228,16 @@ 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)
|
||||
target = stage / slot if slot else stage
|
||||
for rel in files:
|
||||
(stage / rel).parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copy2(Path(src) / rel, stage / rel)
|
||||
(target / rel).parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copy2(Path(src) / rel, target / rel)
|
||||
# a save copied from read-only media keeps the attribute through copy2; the game rewrites its files
|
||||
os.chmod(stage / rel, stat.S_IREAD | stat.S_IWRITE)
|
||||
os.chmod(target / rel, stat.S_IREAD | stat.S_IWRITE)
|
||||
# The journal of the replaced save goes out of save-tx (the game would load a finished commit first, or complete an
|
||||
# interrupted one): what holds files moves into the backup, beside the save it was copied from, so a copy the
|
||||
# backup's own choice missed (an interrupted commit's other half) is kept too.
|
||||
@@ -267,6 +288,6 @@ def import_save(game, folder=None, log=print):
|
||||
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}' + (f'; the old save is in {backup}' if backup
|
||||
else ''))
|
||||
return {'files': len(files), 'source': Path(src), 'backup': backup}
|
||||
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}
|
||||
|
||||
@@ -717,7 +717,8 @@ class App:
|
||||
except (saves.SaveError, OSError) as e:
|
||||
self.save_message = f'The save was not imported: {e}'
|
||||
else:
|
||||
self.save_message = (f'Save imported: {r["files"]} files from {r["source"].name}.'
|
||||
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)
|
||||
|
||||
|
||||
@@ -1178,6 +1178,58 @@ class SavesTest(unittest.TestCase):
|
||||
self.assertTrue((self.game / 'saves' / 'README.txt').is_file())
|
||||
self.assertTrue((src / 'save.dat').is_file()) # a picked folder is left as it was
|
||||
|
||||
def test_one_slot_replaces_only_that_slot(self):
|
||||
acct = saves.account_dir(self.game)
|
||||
self.files(acct, {'account.sav': b'a', 'option.sav': b'o', 'slot_00/progress.sav': b'old0',
|
||||
'slot_01/progress.sav': b'old1', 'slot_01/extra.sav': b'e'})
|
||||
src = self.files(self.tmp / 'slot_01', {'slot_01/progress.sav': b'new1', 'slot_01/caption.sav': b'c1'}) # nested
|
||||
r = saves.import_save(self.game, src, log=lambda m: None)
|
||||
self.assertEqual(r['slot'], 'slot_01')
|
||||
self.assertEqual((acct / 'slot_01' / 'progress.sav').read_bytes(), b'new1')
|
||||
self.assertEqual((acct / 'slot_01' / 'caption.sav').read_bytes(), b'c1')
|
||||
self.assertFalse((acct / 'slot_01' / 'extra.sav').exists()) # the slot is replaced, not mixed
|
||||
self.assertFalse((acct / 'progress.sav').exists()) # the game reads it only inside a slot
|
||||
for name, data in (('slot_00/progress.sav', b'old0'), ('account.sav', b'a'), ('option.sav', b'o')):
|
||||
self.assertEqual((acct / name).read_bytes(), data)
|
||||
self.assertEqual((r['backup'] / 'slot_01' / 'progress.sav').read_bytes(), b'old1')
|
||||
|
||||
def test_a_slot_under_another_name_is_the_first_slot(self):
|
||||
src = self.files(self.tmp / 'my save', {'progress.sav': b'p', 'caption.sav': b'c'})
|
||||
r = saves.import_save(self.game, src, log=lambda m: None) # a game that never ran has no save to keep
|
||||
acct = saves.account_dir(self.game)
|
||||
self.assertEqual(r['slot'], 'slot_00')
|
||||
self.assertIsNone(r['backup'])
|
||||
self.assertEqual((acct / 'slot_00' / 'progress.sav').read_bytes(), b'p')
|
||||
|
||||
def test_a_slot_clears_a_slot_left_at_the_top_of_the_save(self):
|
||||
acct = saves.account_dir(self.game)
|
||||
self.files(acct, {'progress.sav': b'stray', 'caption.sav': b'stray', 'option.sav': b'o'})
|
||||
src = self.files(self.tmp / 'slot_02', {'progress.sav': b'p'})
|
||||
r = saves.import_save(self.game, src, log=lambda m: None)
|
||||
self.assertFalse((acct / 'progress.sav').exists())
|
||||
self.assertFalse((acct / 'caption.sav').exists())
|
||||
self.assertEqual((acct / 'option.sav').read_bytes(), b'o')
|
||||
self.assertEqual((acct / 'slot_02' / 'progress.sav').read_bytes(), b'p')
|
||||
self.assertEqual((r['backup'] / 'progress.sav').read_bytes(), b'stray')
|
||||
|
||||
def test_a_whole_save_replaces_the_save(self):
|
||||
acct = saves.account_dir(self.game)
|
||||
self.files(acct, {'slot_00/progress.sav': b'old', 'slot_03/progress.sav': b'old3', 'option.sav': b'o'})
|
||||
src = self.files(self.tmp / 'whole', {'account.sav': b'a', 'option.sav': b'n', 'slot_00/progress.sav': b'new'})
|
||||
r = saves.import_save(self.game, src, log=lambda m: None)
|
||||
self.assertIsNone(r['slot'])
|
||||
self.assertEqual((acct / 'slot_00' / 'progress.sav').read_bytes(), b'new')
|
||||
self.assertEqual((acct / 'option.sav').read_bytes(), b'n')
|
||||
self.assertFalse((acct / 'slot_03').exists())
|
||||
|
||||
def test_a_backup_comes_back_without_its_journal(self):
|
||||
src = self.files(self.tmp / 'backup 2026-11-09 05.42.06', {'option.sav': b'o', 'slot_00/progress.sav': b'p',
|
||||
'save-tx/work/option.sav': b'w'})
|
||||
saves.import_save(self.game, src, log=lambda m: None)
|
||||
acct = saves.account_dir(self.game)
|
||||
self.assertEqual((acct / 'slot_00' / 'progress.sav').read_bytes(), b'p')
|
||||
self.assertFalse((acct / 'save-tx').exists())
|
||||
|
||||
def test_import_from_the_saves_folder(self):
|
||||
d = saves.ensure_saves_dir(self.game)
|
||||
self.assertIsNone(saves.waiting(self.game)[0]) # README.txt alone is no save
|
||||
|
||||
Reference in New Issue
Block a user