Skip to content
Open
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
7 changes: 3 additions & 4 deletions src/pycrimson/_files/_pathc.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,8 @@ def from_file(cls, path: Path):
return cls(f)

def get_file_header(self, path: str) -> bytes:
checksum = _crypto.calculate_checksum(
f"/{path}" if not path.startswith("/") else path
)
normalized_path = f"/{path}" if not path.startswith("/") else path
checksum = _crypto.calculate_checksum(normalized_path)

entry = self._entries.get(checksum)
assert entry is not None
Expand All @@ -101,7 +100,7 @@ def get_file_header(self, path: str) -> bytes:
header = self._headers[entry.texture_header_index]
compressed_block_infos = entry.compressed_block_infos
else:
collision_entry = self._hash_collision_entries.get(path)
collision_entry = self._hash_collision_entries.get(normalized_path)
assert collision_entry is not None

header = self._headers[collision_entry.texture_header_index]
Expand Down
56 changes: 56 additions & 0 deletions tests/test_pathc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import unittest
from types import SimpleNamespace

from pycrimson._crypto import calculate_checksum
from pycrimson._files._pathc import (
PackTextureHeaderCollection,
_PackTextureHeaderCollectionCollisionEntry,
_PackTextureHeaderCollectionEntry,
)


class TestPackTextureHeaderCollection(unittest.TestCase):
def _make_collection(self, normalized_path: str) -> PackTextureHeaderCollection:
checksum = calculate_checksum(normalized_path)

collection = PackTextureHeaderCollection.__new__(PackTextureHeaderCollection)
collection._header = SimpleNamespace(header_size=0x80)
collection._headers = [b"A" * 0x80, b"B" * 0x80]
collection._entries = {
checksum: _PackTextureHeaderCollectionEntry(
texture_header_index=0xFFFF,
collision_start_index=0,
collision_end_index=0,
compressed_block_infos=b"\x00" * 16,
)
}
collection._hash_collision_entries = {
normalized_path: _PackTextureHeaderCollectionCollisionEntry(
filename_offset=0,
texture_header_index=1,
unknown0=0,
compressed_block_infos=b"\x00" * 16,
)
}
return collection

def test_collision_lookup_normalizes_leading_slash(self):
path = "ui/texture/image/worldmap/cd_worldmap_road_sdf_32768x32768_6_8.dds"
normalized_path = f"/{path}"
collection = self._make_collection(normalized_path)

header = collection.get_file_header(path)

self.assertEqual(header, b"B" * 0x80)

def test_collision_lookup_accepts_already_normalized_path(self):
normalized_path = "/ui/texture/image/worldmap/cd_worldmap_road_sdf_32768x32768_6_8.dds"
collection = self._make_collection(normalized_path)

header = collection.get_file_header(normalized_path)

self.assertEqual(header, b"B" * 0x80)


if __name__ == "__main__":
unittest.main()