Skip to content

Commit

Permalink
Merge branch 'main' into brfld-layers
Browse files Browse the repository at this point in the history
  • Loading branch information
MayberryZoom authored Oct 14, 2024
2 parents b7d9c1d + 430c6ef commit c51f244
Show file tree
Hide file tree
Showing 32 changed files with 181 additions and 160 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
python-version: "3.12"

- name: Install Python packages
run: python -m pip install --upgrade build
Expand Down Expand Up @@ -57,7 +57,8 @@ jobs:
python:
- { version: "3.10" }
- { version: "3.11" }
- { version: "3.12.0-beta - 3.12.0" }
- { version: "3.12" }
- { version: "3.13" }

steps:
- name: Checkout
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.6.8
rev: v0.6.9
hooks:
- id: ruff
args: [ --fix, --exit-non-zero-on-fix ]
Expand Down
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ classifiers = [
"Intended Audience :: Developers",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
]
requires-python = ">=3.10"
dynamic = ["version"]
Expand All @@ -27,7 +29,6 @@ dependencies = [
"zstandard",
]


[project.readme]
file = "README.md"
content-type = "text/markdown"
Expand Down Expand Up @@ -67,7 +68,7 @@ lint.select = ["E", "F", "W", "C90", "I", "UP"]
src = ["src"]

# Version to target for generated code.
target-version = "py38"
target-version = "py310"

[tool.ruff.lint.mccabe]
# Flag errors (`C901`) whenever the complexity level exceeds 25.
Expand Down
5 changes: 2 additions & 3 deletions src/mercury_engine_data_structures/_dread_data_construct.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import struct
import typing

import construct

Expand Down Expand Up @@ -30,7 +29,7 @@ def __init__(self):
),
)

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

Expand All @@ -44,7 +43,7 @@ def _parse(self, stream, context, path) -> typing.Dict[str, int]:

return result

def _build(self, obj: typing.Dict[str, int], stream, context, path):
def _build(self, obj: dict[str, int], stream, context, path):
return self._build_construct._build(list(obj.items()), stream, context, path)


Expand Down
12 changes: 6 additions & 6 deletions src/mercury_engine_data_structures/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import typing
from concurrent.futures import ProcessPoolExecutor
from pathlib import Path
from typing import Optional

from mercury_engine_data_structures import formats
from mercury_engine_data_structures.construct_extensions.json import convert_to_raw_python
from mercury_engine_data_structures.file_tree_editor import FileTreeEditor, OutputFormat
from mercury_engine_data_structures.game_check import Game
from mercury_engine_data_structures.romfs import ExtractedRomFs


def game_argument_type(s: str) -> Game:
Expand Down Expand Up @@ -135,7 +135,7 @@ def do_decode_from_pkg(args):
root: Path = args.root
asset_name: str = args.asset_name

pkg_editor = FileTreeEditor(root, args.game)
pkg_editor = FileTreeEditor(ExtractedRomFs(root), args.game)
asset = pkg_editor.get_parsed_asset(asset_name)
print(asset.raw)

Expand Down Expand Up @@ -174,7 +174,7 @@ def find_pkg_for(args):
asset_id: int = args.asset_id
asset_name: str = args.asset_name

pkg_editor = FileTreeEditor(root, args.game)
pkg_editor = FileTreeEditor(ExtractedRomFs(root), args.game)
if asset_id is not None:
items = list(pkg_editor.find_pkgs(asset_id))
else:
Expand Down Expand Up @@ -205,7 +205,7 @@ async def compare_all_files_in_path(args):
input_path: Path = args.input_path
file_format: str = args.format
game: Game = args.game
limit: Optional[int] = args.limit
limit: int | None = args.limit

def apply_limit(it):
if limit is None:
Expand Down Expand Up @@ -252,7 +252,7 @@ def extract_files(args: argparse.Namespace) -> None:
root: Path = args.root
output_root: Path = args.output

pkg_editor = FileTreeEditor(root, args.game)
pkg_editor = FileTreeEditor(ExtractedRomFs(root), args.game)

output_root.mkdir(parents=True, exist_ok=True)
for file_name in pkg_editor.all_asset_names():
Expand All @@ -268,7 +268,7 @@ def replace_files(args: argparse.Namespace) -> None:
new_files: Path = args.new_files
output: Path = args.output

pkg_editor = FileTreeEditor(root, args.game)
pkg_editor = FileTreeEditor(ExtractedRomFs(root), args.game)

for file_name in new_files.rglob("*"):
if file_name.is_file():
Expand Down
2 changes: 1 addition & 1 deletion src/mercury_engine_data_structures/common_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ def _emitbuild(code):
return result


def make_enum(values: typing.Union[typing.List[str], typing.Dict[str, int]], *, add_invalid: bool = True):
def make_enum(values: list[str] | dict[str, int], *, add_invalid: bool = True):
if isinstance(values, dict):
mapping = copy.copy(values)
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import enum
import typing

import construct


class StrictEnum(construct.Adapter):
def __init__(self, enum_class: typing.Type[enum.IntEnum]):
def __init__(self, enum_class: type[enum.IntEnum]):
super().__init__(construct.Int32ul)
self.enum_class = enum_class

def _decode(self, obj: int, context, path):
return self.enum_class(obj)

def _encode(self, obj: typing.Union[str, enum.IntEnum, int], context, path) -> int:
def _encode(self, obj: str | enum.IntEnum | int, context, path) -> int:
if isinstance(obj, str):
obj = getattr(self.enum_class, obj)

Expand All @@ -39,7 +38,7 @@ def _encode_enum_{i}(obj, io, this):
return f"_encode_enum_{i}(obj, io, this)"


def BitMaskEnum(enum_type: typing.Type[enum.IntEnum]):
def BitMaskEnum(enum_type: type[enum.IntEnum]):
flags = {}
for enumentry in enum_type:
flags[enumentry.name] = 2**enumentry.value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import re
from typing import Dict, Optional, Type, Union

import construct


def _resolve_id(type_class: Union[construct.Construct, Type[construct.Construct]]) -> int:
def _resolve_id(type_class: construct.Construct | type[construct.Construct]) -> int:
if isinstance(type_class, construct.Renamed):
return _resolve_id(type_class.subcon)
return id(type_class)
Expand All @@ -16,8 +15,8 @@ def _resolve_id(type_class: Union[construct.Construct, Type[construct.Construct]

def emit_switch_cases_parse(
code: construct.CodeGen,
fields: Dict[Union[str, int], Union[construct.Construct, Type[construct.Construct]]],
custom_table_name: Optional[str] = None,
fields: dict[str | int, construct.Construct | type[construct.Construct]],
custom_table_name: str | None = None,
) -> str:
"""Construct codegen helper for handling the switch cases dict in _emitparse."""
table_name = custom_table_name
Expand Down Expand Up @@ -48,8 +47,8 @@ def {code_name}(io, this):

def emit_switch_cases_build(
code: construct.CodeGen,
fields: Dict[Union[str, int], Union[construct.Construct, Type[construct.Construct]]],
custom_table_name: Optional[str] = None,
fields: dict[str | int, construct.Construct | type[construct.Construct]],
custom_table_name: str | None = None,
) -> str:
"""Construct codegen helper for handling the switch cases dict in _emitbuild."""
table_name = custom_table_name
Expand Down
8 changes: 3 additions & 5 deletions src/mercury_engine_data_structures/crc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
Module for calculating CRC hashes with the algorithm and data used by Mercury Engine.
"""

import typing

_crc32_constants = [
0x00000000,
0x77073096,
Expand Down Expand Up @@ -523,7 +521,7 @@
]


def _algorithm(data: typing.Union[bytes, str], constants: typing.List[int], checksum: int) -> int:
def _algorithm(data: bytes | str, constants: list[int], checksum: int) -> int:
if isinstance(data, str):
data = data.encode("utf-8")

Expand All @@ -533,15 +531,15 @@ def _algorithm(data: typing.Union[bytes, str], constants: typing.List[int], chec
return checksum


def crc32(data: typing.Union[bytes, str]) -> int:
def crc32(data: bytes | str) -> int:
return _algorithm(
data,
_crc32_constants,
0xFFFFFFFF,
)


def crc64(data: typing.Union[bytes, str]) -> int:
def crc64(data: bytes | str) -> int:
return _algorithm(
data,
_crc64_constants,
Expand Down
15 changes: 7 additions & 8 deletions src/mercury_engine_data_structures/dread_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@
import json
import typing
from pathlib import Path
from typing import Dict, Optional

from mercury_engine_data_structures._dread_data_construct import KnownHashes

_root = Path(__file__).parent


@functools.lru_cache
def get_raw_types() -> Dict[str, typing.Any]:
def get_raw_types() -> dict[str, typing.Any]:
path = _root.joinpath("dread_types.json")
with path.open() as f:
return json.load(f)


@functools.lru_cache
def all_name_to_asset_id() -> Dict[str, int]:
def all_name_to_asset_id() -> dict[str, int]:
bin_path = _root.joinpath("dread_resource_names.bin")
if bin_path.exists():
return dict(KnownHashes.parse_file(bin_path))
Expand All @@ -28,16 +27,16 @@ def all_name_to_asset_id() -> Dict[str, int]:


@functools.lru_cache
def all_asset_id_to_name() -> Dict[int, str]:
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 name_for_asset_id(asset_id: int) -> Optional[str]:
def name_for_asset_id(asset_id: int) -> str | None:
return all_asset_id_to_name().get(asset_id)


@functools.lru_cache
def all_name_to_property_id() -> Dict[str, int]:
def all_name_to_property_id() -> dict[str, int]:
bin_path = _root.joinpath("dread_property_names.bin")
if bin_path.exists():
return dict(KnownHashes.parse_file(bin_path))
Expand All @@ -48,13 +47,13 @@ def all_name_to_property_id() -> Dict[str, int]:


@functools.lru_cache
def all_property_id_to_name() -> Dict[int, str]:
def all_property_id_to_name() -> dict[int, str]:
names = all_name_to_property_id()

return {asset_id: name for name, asset_id in names.items()}


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

Expand Down
Loading

0 comments on commit c51f244

Please sign in to comment.