Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only align if data section size contains files #112

Merged
merged 3 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/mercury_engine_data_structures/formats/pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)

Expand Down
27 changes: 17 additions & 10 deletions tests/formats/test_pkg.py
Original file line number Diff line number Diff line change
@@ -1,24 +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'|\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')

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):
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("packs/system/system.pkg")
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)
Expand Down