From 85914f188e20fed042faa04214fd2abc343c8d3c Mon Sep 17 00:00:00 2001 From: Thanatos Date: Sat, 30 Dec 2023 00:34:46 +0100 Subject: [PATCH 1/3] Only align if data section size contains files --- src/mercury_engine_data_structures/formats/pkg.py | 10 ++++++---- tests/formats/test_pkg.py | 9 +-------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/mercury_engine_data_structures/formats/pkg.py b/src/mercury_engine_data_structures/formats/pkg.py index 3a038f30..69d48927 100644 --- a/src/mercury_engine_data_structures/formats/pkg.py +++ b/src/mercury_engine_data_structures/formats/pkg.py @@ -74,8 +74,9 @@ def _parse(self, stream, context, path) -> construct.Container: # Get the file headers file_headers = self.file_headers_type._parsereport(stream, context, path) - # Align to 128 bytes - AlignTo(128)._parsereport(stream, context, path) + if file_headers: + # Align to 128 bytes + AlignTo(128)._parsereport(stream, context, path) files = construct.ListContainer() for i, header in enumerate(file_headers): @@ -99,8 +100,9 @@ def _build(self, obj: construct.Container, stream, context, path): # Skip over file headers construct.stream_seek(stream, self.int_size.length + len(obj.files) * file_entry_size, 1, path) - # Align to 128 bytes - AlignTo(128)._build(None, stream, context, path) + if obj.files: + # Align to 128 bytes + AlignTo(128)._build(None, stream, context, path) header_end = construct.stream_tell(stream, path) diff --git a/tests/formats/test_pkg.py b/tests/formats/test_pkg.py index 9c64521c..de934f75 100644 --- a/tests/formats/test_pkg.py +++ b/tests/formats/test_pkg.py @@ -4,14 +4,7 @@ from mercury_engine_data_structures.formats.pkg import Pkg from mercury_engine_data_structures.game_check import Game -_EMPTY_DREAD_PKG = (b'|\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' - b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' - b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' - b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' - b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' - b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' - b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' - b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') +_EMPTY_DREAD_PKG = (b'\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') def test_compare_dread(dread_path): From bb0a046f2ea35d8855dcc869dc7230790da4959d Mon Sep 17 00:00:00 2001 From: Thanatos Date: Sat, 30 Dec 2023 00:42:09 +0100 Subject: [PATCH 2/3] Add test for parse and building all dread pkgs --- tests/formats/test_pkg.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/formats/test_pkg.py b/tests/formats/test_pkg.py index de934f75..0f3f6c3a 100644 --- a/tests/formats/test_pkg.py +++ b/tests/formats/test_pkg.py @@ -8,9 +8,11 @@ def test_compare_dread(dread_path): - parse_and_build_compare( - Pkg.construct_class(Game.DREAD), Game.DREAD, dread_path.joinpath("packs/system/system.pkg") - ) + pkg_files = [f for f in dread_path.rglob("*.pkg")] + for f in pkg_files: + parse_and_build_compare( + Pkg.construct_class(Game.DREAD), Game.DREAD, dread_path.joinpath(f) + ) def test_build_empty_pkg(): From 1a0ac1838551e7fbf98a8ccd837f6f984c79896a Mon Sep 17 00:00:00 2001 From: Thanatos Date: Mon, 8 Jan 2024 16:40:24 +0100 Subject: [PATCH 3/3] Address review for test_pkg.py --- tests/formats/test_pkg.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/tests/formats/test_pkg.py b/tests/formats/test_pkg.py index 0f3f6c3a..73ae21db 100644 --- a/tests/formats/test_pkg.py +++ b/tests/formats/test_pkg.py @@ -1,19 +1,31 @@ +import pytest from construct import Container, ListContainer from tests.test_lib import parse_and_build_compare +from mercury_engine_data_structures import dread_data, samus_returns_data from mercury_engine_data_structures.formats.pkg import Pkg from mercury_engine_data_structures.game_check import Game _EMPTY_DREAD_PKG = (b'\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') +all_dread_pkg = [name for name in dread_data.all_name_to_asset_id().keys() + if name.endswith(".pkg")] -def test_compare_dread(dread_path): - pkg_files = [f for f in dread_path.rglob("*.pkg")] - for f in pkg_files: - parse_and_build_compare( - Pkg.construct_class(Game.DREAD), Game.DREAD, dread_path.joinpath(f) - ) +all_sr_pkg = [name for name in samus_returns_data.all_name_to_asset_id().keys() + if name.endswith(".pkg")] +@pytest.mark.parametrize("pkg_path", all_dread_pkg) +def test_compare_dread(dread_path, pkg_path): + parse_and_build_compare( + Pkg.construct_class(Game.DREAD), Game.DREAD, dread_path.joinpath(pkg_path) + ) + +@pytest.mark.skip("Rebuilding vanilla pkg files is currently not supported for SR") +@pytest.mark.parametrize("pkg_path", all_sr_pkg) +def test_compare_sr(samus_returns_path, pkg_path): + parse_and_build_compare( + Pkg.construct_class(Game.SAMUS_RETURNS), Game.SAMUS_RETURNS, samus_returns_path.joinpath(pkg_path) + ) def test_build_empty_pkg(): pkg = Pkg(Container(files=ListContainer()), Game.DREAD)