From 035e23b964c1293d20bdb846537121ce009799bd Mon Sep 17 00:00:00 2001 From: derrod Date: Sat, 25 Apr 2020 23:02:46 +0200 Subject: [PATCH] [core/lfs] Clean up local manifest saving/loading --- legendary/core.py | 7 ++++--- legendary/lfs/lgndry.py | 26 ++++++++++---------------- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/legendary/core.py b/legendary/core.py index 5dc7122..7d77080 100644 --- a/legendary/core.py +++ b/legendary/core.py @@ -313,7 +313,7 @@ class LegendaryCore: old_manifest = self.load_manfiest(old_manifest_data) elif not disable_patching and not force and self.is_installed(game.app_name): igame = self.get_installed_game(game.app_name) - if old_bytes := self.lgd.get_manifest(game.app_name, igame.version): + if old_bytes := self.lgd.load_manifest(game.app_name, igame.version): old_manifest = self.load_manfiest(old_bytes) base_urls = list(game.base_urls) # copy list for manipulation @@ -357,9 +357,9 @@ class LegendaryCore: self.log.debug(f'Base urls: {base_urls}') new_manifest = self.load_manfiest(new_manifest_data) self.lgd.save_manifest(game.app_name, new_manifest_data) - # save manifest with version name in "old" folder as well for testing/downgrading/etc. + # save manifest with version name as well for testing/downgrading/etc. self.lgd.save_manifest(game.app_name, new_manifest_data, - filename=f'{game.app_name}_{new_manifest.meta.build_version}', old=True) + version=new_manifest.meta.build_version) if not game_folder: if game.is_dlc: @@ -374,6 +374,7 @@ class LegendaryCore: install_path = os.path.join(base_path, game_folder) + # todo move this somewhere else so the directory only gets created once the download is started if not os.path.exists(install_path): os.makedirs(install_path) diff --git a/legendary/lfs/lgndry.py b/legendary/lfs/lgndry.py index c49c59b..5b7d42f 100644 --- a/legendary/lfs/lgndry.py +++ b/legendary/lfs/lgndry.py @@ -116,29 +116,23 @@ class LGDLFS: open(os.path.join(self.path, 'assets.json'), 'w'), indent=2, sort_keys=True) - def get_manifest(self, app_name, version=''): + def _get_manifest_filename(self, app_name, version=''): if not version: - manifest_file = os.path.join(self.path, 'manifests', f'{app_name}.manifest') + return os.path.join(self.path, 'manifests', f'{app_name}.manifest') else: # if a version is specified load it from the versioned directory fname = clean_filename(f'{app_name}_{version}') - manifest_file = os.path.join(self.path, 'manifests', 'old', - f'{fname}.manifest') + return os.path.join(self.path, 'manifests', 'old', f'{fname}.manifest') - if os.path.exists(manifest_file): - return open(manifest_file, 'rb').read() - else: + def load_manifest(self, app_name, version=''): + try: + return open(self._get_manifest_filename(app_name, version), 'rb').read() + except FileNotFoundError: # all other errors should propagate return None - def save_manifest(self, app_name, manifest_data, filename=None, old=False): - if not filename: - manifest_file = os.path.join(self.path, 'manifests', f'{app_name}.manifest') - elif filename and old: - manifest_file = os.path.join(self.path, 'manifests', f'old/{clean_filename(filename)}.manifest') - else: - manifest_file = os.path.join(self.path, 'manifests', f'{clean_filename(filename)}.manifest') - - open(manifest_file, 'wb').write(manifest_data) + def save_manifest(self, app_name, manifest_data, version=''): + with open(self._get_manifest_filename(app_name, version), 'wb') as f: + f.write(manifest_data) def get_game_meta(self, app_name): _meta = self._game_metadata.get(app_name, None)