Skip to content

Commit

Permalink
Merge pull request #112 from ThanatosGit/empty-pkg-parse
Browse files Browse the repository at this point in the history
Only align if data section size contains files
  • Loading branch information
ThanatosGit authored Jan 8, 2024
2 parents 450602f + 1a0ac18 commit 94a3182
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
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

0 comments on commit 94a3182

Please sign in to comment.