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

Version checking #232

Merged
merged 10 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
42 changes: 42 additions & 0 deletions src/mercury_engine_data_structures/_dread_data_construct.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,46 @@
return self._build_construct._build(list(obj.items()), stream, context, path)


class VersionedHashesDict(construct.Construct):
def __init__(self):
super().__init__()
self._build_construct = construct.PrefixedArray(
construct.Int32un,
construct.Sequence(
construct.PascalString(construct.Int16un, "ascii"), # key
construct.Int64un, # hash
construct.Int16un, # versions
),
)

def _parse(self, stream, context, path) -> dict[str, int]:
key_struct = struct.Struct("=H")
value_struct = struct.Struct("=QH")

count = construct.Int32un._parse(stream, None, "")

result = {}
for _ in range(count):
key = stream.read(key_struct.unpack(stream.read(2))[0]).decode()
value, versions = value_struct.unpack(stream.read(10))
result[key] = {"crc": value, "versions": versions}

return result

def _build(self, obj: dict[str, dict], stream, context, path):
ver_to_val = context.versions
all_vers = sum([v for v in ver_to_val.values()])
for a in obj.values():
vers = a.get("versions")
if vers is not None:
a["versions"] = sum([ver_to_val[v] for v in vers])

Check warning on line 84 in src/mercury_engine_data_structures/_dread_data_construct.py

View check run for this annotation

Codecov / codecov/patch

src/mercury_engine_data_structures/_dread_data_construct.py#L79-L84

Added lines #L79 - L84 were not covered by tests
else:
a["versions"] = all_vers

Check warning on line 86 in src/mercury_engine_data_structures/_dread_data_construct.py

View check run for this annotation

Codecov / codecov/patch

src/mercury_engine_data_structures/_dread_data_construct.py#L86

Added line #L86 was not covered by tests

return self._build_construct._build(

Check warning on line 88 in src/mercury_engine_data_structures/_dread_data_construct.py

View check run for this annotation

Codecov / codecov/patch

src/mercury_engine_data_structures/_dread_data_construct.py#L88

Added line #L88 was not covered by tests
list([(k, v["crc"], v["versions"]) for k, v in obj.items()]), stream, context, path
)


KnownHashes = CompressedZSTD(HashesDict(), 15)
VersionedHashes = CompressedZSTD(VersionedHashesDict(), 15)
38 changes: 28 additions & 10 deletions src/mercury_engine_data_structures/dread_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import typing
from pathlib import Path

from mercury_engine_data_structures._dread_data_construct import KnownHashes
from mercury_engine_data_structures._dread_data_construct import KnownHashes, VersionedHashes
from mercury_engine_data_structures.game_check import Game, GameVersion

_root = Path(__file__).parent
DREAD_VERSIONS = GameVersion.versions_for_game(Game.DREAD)
ALL_VERSIONS_BITMASK = -1


@functools.lru_cache
Expand All @@ -18,23 +21,38 @@


@functools.lru_cache
def all_name_to_asset_id() -> dict[str, int]:
def all_name_to_asset_id_and_version() -> dict[str, dict[str, int]]:
bin_path = _root.joinpath("dread_resource_names.bin")
if bin_path.exists():
return dict(KnownHashes.parse_file(bin_path))
return dict(VersionedHashes.parse_file(bin_path))

path = _root.joinpath("dread_resource_names.json")
with path.open() as names_file:
return json.load(names_file)
data: dict[str, dict] = json.load(names_file)

Check warning on line 31 in src/mercury_engine_data_structures/dread_data.py

View check run for this annotation

Codecov / codecov/patch

src/mercury_engine_data_structures/dread_data.py#L31

Added line #L31 was not covered by tests

for a in data.values():
vers = a.get("versions")
if vers is not None:
a["versions"] = sum([DREAD_VERSIONS[v].bitmask for v in vers])

Check warning on line 36 in src/mercury_engine_data_structures/dread_data.py

View check run for this annotation

Codecov / codecov/patch

src/mercury_engine_data_structures/dread_data.py#L33-L36

Added lines #L33 - L36 were not covered by tests
else:
a["versions"] = ALL_VERSIONS_BITMASK

Check warning on line 38 in src/mercury_engine_data_structures/dread_data.py

View check run for this annotation

Codecov / codecov/patch

src/mercury_engine_data_structures/dread_data.py#L38

Added line #L38 was not covered by tests

return data

Check warning on line 40 in src/mercury_engine_data_structures/dread_data.py

View check run for this annotation

Codecov / codecov/patch

src/mercury_engine_data_structures/dread_data.py#L40

Added line #L40 was not covered by tests


@functools.lru_cache
def all_name_to_asset_id(ver: GameVersion | None = None) -> dict[str, int]:
bitmask = ver.bitmask if ver else ALL_VERSIONS_BITMASK
return {k: v["crc"] for k, v in all_name_to_asset_id_and_version().items() if v["versions"] & bitmask != 0}


@functools.lru_cache
def all_asset_id_to_name() -> dict[int, str]:
return {asset_id: name for name, asset_id in all_name_to_asset_id().items()}
def all_asset_id_to_name(ver: GameVersion | None = None) -> dict[int, str]:
return {asset_id: name for name, asset_id in all_name_to_asset_id(ver).items()}


def name_for_asset_id(asset_id: int) -> str | None:
return all_asset_id_to_name().get(asset_id)
def name_for_asset_id(asset_id: int, ver: GameVersion | None = None) -> str | None:
return all_asset_id_to_name(ver).get(asset_id)

Check warning on line 55 in src/mercury_engine_data_structures/dread_data.py

View check run for this annotation

Codecov / codecov/patch

src/mercury_engine_data_structures/dread_data.py#L55

Added line #L55 was not covered by tests


@functools.lru_cache
Expand All @@ -55,11 +73,11 @@
return {asset_id: name for name, asset_id in names.items()}


def all_files_ending_with(ext: str, exclusions: list[str] | None = None) -> list[str]:
def all_files_ending_with(ext: str, exclusions: list[str] = None, ver: GameVersion | None = None) -> list[str]:
if not ext.startswith("."):
ext = "." + ext

if exclusions is None:
exclusions = []

return [name for name in all_name_to_asset_id().keys() if name.endswith(ext) and name not in exclusions]
return [name for name in all_name_to_asset_id(ver).keys() if name.endswith(ext) and name not in exclusions]
Loading