From 106ed16a4994486c9129c1ad9750462e9466baae Mon Sep 17 00:00:00 2001 From: derrod Date: Thu, 21 Oct 2021 14:27:08 +0200 Subject: [PATCH] [cli/core] Do not delete merely incomplete saves unless user says so --- legendary/cli.py | 5 ++++- legendary/core.py | 24 ++++++++++++++++-------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/legendary/cli.py b/legendary/cli.py index ba16e21..442fccf 100644 --- a/legendary/cli.py +++ b/legendary/cli.py @@ -408,7 +408,7 @@ class LegendaryCLI: logger.error('Login failed! Cannot continue with download process.') exit(1) logger.info(f'Cleaning saves...') - self.core.clean_saves(self._resolve_aliases(args.app_name)) + self.core.clean_saves(self._resolve_aliases(args.app_name), args.delete_incomplete) def sync_saves(self, args): if not self.core.login(): @@ -1941,6 +1941,9 @@ def main(): sync_saves_parser.add_argument('--disable-filters', dest='disable_filters', action='store_true', help='Disable save game file filtering') + clean_saves_parser.add_argument('--delete-incomplete', dest='delete_incomplete', action='store_true', + help='Delete incomplete save files') + import_parser.add_argument('--disable-check', dest='disable_check', action='store_true', help='Disables completeness check of the to-be-imported game installation ' '(useful if the imported game is a much older version or missing files)') diff --git a/legendary/core.py b/legendary/core.py index f2f98bd..6b78ae3 100644 --- a/legendary/core.py +++ b/legendary/core.py @@ -851,7 +851,7 @@ class LegendaryCore: self.log.info('Successfully completed savegame download.') - def clean_saves(self, app_name=''): + def clean_saves(self, app_name='', delete_incomplete=False): savegames = self.egs.get_user_cloud_saves(app_name=app_name) files = savegames['files'] deletion_list = [] @@ -886,18 +886,26 @@ class LegendaryCore: m = self.load_manifest(r.content) # check if all required chunks are present chunk_fnames = set() + missing_chunks = 0 + total_chunks = m.chunk_data_list.count for chunk in m.chunk_data_list.elements: cpath_p = fname.split('/', 3)[:3] cpath_p.append(chunk.path) cpath = '/'.join(cpath_p) + chunk_fnames.add(cpath) if cpath not in files: - self.log.error(f'Chunk missing, marking manifest for deletion.') - deletion_list.append(fname) - break - else: - chunk_fnames.add(cpath) - else: - used_chunks |= chunk_fnames + missing_chunks += 1 + + if (0 < missing_chunks < total_chunks and delete_incomplete) or missing_chunks == total_chunks: + self.log.error(f'Chunk(s) missing, marking manifest for deletion.') + deletion_list.append(fname) + continue + elif 0 < missing_chunks < total_chunks: + self.log.error(f'Some chunk(s) missing, optionally run "legendary download-saves" to obtain a backup ' + f'of the corrupted save, then re-run this command with "--delete-incomplete" to remove ' + f'it from the cloud save service.') + + used_chunks |= chunk_fnames # check for orphaned chunks (not used in any manifests) for fname, f in files.items():