From 36e6e5f08acc57c37c6d4701692697aca064e7ab Mon Sep 17 00:00:00 2001 From: derrod Date: Tue, 25 Oct 2022 15:46:34 +0200 Subject: [PATCH] [core/models] Make manifest merge part of Manifest class --- legendary/core.py | 3 +-- legendary/models/manifest.py | 39 ++++++++++++++++++++++++++++++++++++ legendary/utils/manifests.py | 39 ------------------------------------ 3 files changed, 40 insertions(+), 41 deletions(-) delete mode 100644 legendary/utils/manifests.py diff --git a/legendary/core.py b/legendary/core.py index 6b0290f..78b3890 100644 --- a/legendary/core.py +++ b/legendary/core.py @@ -39,7 +39,6 @@ from legendary.lfs.eos import EOSOverlayApp, query_registry_entries from legendary.utils.game_workarounds import is_opt_enabled, update_workarounds, get_exe_override from legendary.utils.savegame_helper import SaveGameHelper from legendary.utils.selective_dl import games as sdl_games -from legendary.utils.manifests import combine_manifests from legendary.lfs.wine_helpers import read_registry, get_shell_folders, case_insensitive_path_search @@ -1325,7 +1324,7 @@ class LegendaryCore: self.log.info(f'Using optimized delta manifest to upgrade from build ' f'"{old_manifest.meta.build_id}" to ' f'"{new_manifest.meta.build_id}"...') - combine_manifests(new_manifest, delta_manifest) + new_manifest.apply_delta_manifest(delta_manifest) else: self.log.debug(f'No Delta manifest received from CDN.') diff --git a/legendary/models/manifest.py b/legendary/models/manifest.py index 0cbefb4..a9d8688 100644 --- a/legendary/models/manifest.py +++ b/legendary/models/manifest.py @@ -1,5 +1,7 @@ # coding: utf-8 +from __future__ import annotations + import hashlib import logging import struct @@ -164,6 +166,43 @@ class Manifest: return bio.tell() if fp else bio.getvalue() + def apply_delta_manifest(self, delta_manifest: Manifest): + added = set() + # overwrite file elements with the ones from the delta manifest + for idx, file_elem in enumerate(self.file_manifest_list.elements): + try: + delta_file = delta_manifest.file_manifest_list.get_file_by_path(file_elem.filename) + self.file_manifest_list.elements[idx] = delta_file + added.add(delta_file.filename) + except ValueError: + pass + + # add other files that may be missing + for delta_file in delta_manifest.file_manifest_list.elements: + if delta_file.filename not in added: + self.file_manifest_list.elements.append(delta_file) + # update count and clear map + self.file_manifest_list.count = len(self.file_manifest_list.elements) + self.file_manifest_list._path_map = None + + # ensure guid map exists (0 will most likely yield no result, so ignore ValueError) + try: + self.chunk_data_list.get_chunk_by_guid(0) + except ValueError: + pass + + # add new chunks from delta manifest to main manifest and again clear maps and update count + existing_chunk_guids = self.chunk_data_list._guid_int_map.keys() + + for chunk in delta_manifest.chunk_data_list.elements: + if chunk.guid_num not in existing_chunk_guids: + self.chunk_data_list.elements.append(chunk) + + self.chunk_data_list.count = len(self.chunk_data_list.elements) + self.chunk_data_list._guid_map = None + self.chunk_data_list._guid_int_map = None + self.chunk_data_list._path_map = None + class ManifestMeta: serialisation_version = 0 diff --git a/legendary/utils/manifests.py b/legendary/utils/manifests.py deleted file mode 100644 index 64032fc..0000000 --- a/legendary/utils/manifests.py +++ /dev/null @@ -1,39 +0,0 @@ -from legendary.models.manifest import Manifest - - -def combine_manifests(base_manifest: Manifest, delta_manifest: Manifest): - added = set() - # overwrite file elements with the ones from the delta manifest - for idx, file_elem in enumerate(base_manifest.file_manifest_list.elements): - try: - delta_file = delta_manifest.file_manifest_list.get_file_by_path(file_elem.filename) - base_manifest.file_manifest_list.elements[idx] = delta_file - added.add(delta_file.filename) - except ValueError: - pass - - # add other files that may be missing - for delta_file in delta_manifest.file_manifest_list.elements: - if delta_file.filename not in added: - base_manifest.file_manifest_list.elements.append(delta_file) - # update count and clear map - base_manifest.file_manifest_list.count = len(base_manifest.file_manifest_list.elements) - base_manifest.file_manifest_list._path_map = None - - # ensure guid map exists - try: - base_manifest.chunk_data_list.get_chunk_by_guid(0) - except: - pass - - # add new chunks from delta manifest to main manifest and again clear maps and update count - existing_chunk_guids = base_manifest.chunk_data_list._guid_int_map.keys() - - for chunk in delta_manifest.chunk_data_list.elements: - if chunk.guid_num not in existing_chunk_guids: - base_manifest.chunk_data_list.elements.append(chunk) - - base_manifest.chunk_data_list.count = len(base_manifest.chunk_data_list.elements) - base_manifest.chunk_data_list._guid_map = None - base_manifest.chunk_data_list._guid_int_map = None - base_manifest.chunk_data_list._path_map = None