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

Make it black #75

Merged
merged 2 commits into from
Jul 30, 2023
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
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ repos:
hooks:
- id: ruff
args: [ --fix, --exit-non-zero-on-fix ]

- repo: https://github.com/psf/black
rev: 23.7.0
hooks:
- id: black
338 changes: 180 additions & 158 deletions parse_pwe_templates.py

Large diffs are not rendered by default.

7 changes: 2 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@


def generate_property_templates():
subprocess.run([
sys.executable,
os.fspath(Path(__file__).parent.joinpath("parse_pwe_templates.py"))
], check=True)
subprocess.run([sys.executable, os.fspath(Path(__file__).parent.joinpath("parse_pwe_templates.py"))], check=True)


class GenerateTemplateCommand(egg_info):
Expand All @@ -29,6 +26,6 @@ def run(self):

setup(
cmdclass={
'egg_info': GenerateTemplateCommand,
"egg_info": GenerateTemplateCommand,
},
)
30 changes: 12 additions & 18 deletions src/retro_data_structures/asset_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from retro_data_structures.game_check import Game

if typing.TYPE_CHECKING:
import uuid
from collections.abc import Iterator
from pathlib import Path

Expand Down Expand Up @@ -116,6 +115,7 @@ class AssetManager:
_ensured_asset_ids: mapping of pak name to assets we'll copy into it when saving
_modified_resources: mapping of asset id to raw resources. When saving, these asset ids are replaced
"""

headers: dict[str, construct.Container]
_paks_for_asset_id: dict[AssetId, set[str]]
_types_for_asset_id: dict[AssetId, AssetType]
Expand Down Expand Up @@ -165,9 +165,7 @@ def _update_headers(self):

self._custom_asset_ids.update(dict(json.loads(custom_names_text).items()))

self.all_paks = list(
self.provider.rglob("*.pak")
)
self.all_paks = list(self.provider.rglob("*.pak"))

for name in self.all_paks:
with self.provider.open_binary(name) as f:
Expand Down Expand Up @@ -252,17 +250,15 @@ def get_asset_format(self, asset_id: NameOrAssetId) -> type[BaseResource]:
asset_type = self.get_asset_type(asset_id)
return formats.resource_type_for(asset_type)

def get_parsed_asset(self, asset_id: NameOrAssetId, *,
type_hint: type[T] = BaseResource) -> T:
def get_parsed_asset(self, asset_id: NameOrAssetId, *, type_hint: type[T] = BaseResource) -> T:
"""
Gets the resource with the given name and decodes it based on the extension.
"""
format_class = self.get_asset_format(asset_id)
if type_hint is not BaseResource and type_hint != format_class:
raise ValueError(f"type_hint was {type_hint}, pak listed {format_class}")

return format_class.parse(self.get_raw_asset(asset_id).data, target_game=self.target_game,
asset_manager=self)
return format_class.parse(self.get_raw_asset(asset_id).data, target_game=self.target_game, asset_manager=self)

def get_file(self, path: NameOrAssetId, type_hint: type[T] = BaseResource) -> T:
"""
Expand Down Expand Up @@ -290,8 +286,7 @@ def register_custom_asset_name(self, name: str, asset_id: AssetId):
def get_custom_asset(self, name: str) -> AssetId | None:
return self._custom_asset_ids.get(name)

def add_new_asset(self, name: str, new_data: Resource,
in_paks: typing.Iterable[str] = ()) -> AssetId:
def add_new_asset(self, name: str, new_data: Resource, in_paks: typing.Iterable[str] = ()) -> AssetId:
"""
Adds an asset that doesn't already exist.
:return: Asset id of the new asset.
Expand Down Expand Up @@ -398,8 +393,11 @@ def get_pak(self, pak_name: str) -> Pak:

return self._in_memory_paks[pak_name]

def _get_dependencies_for_asset(self, asset_id: NameOrAssetId, must_exist: bool,
) -> Iterator[Dependency]:
def _get_dependencies_for_asset(
self,
asset_id: NameOrAssetId,
must_exist: bool,
) -> Iterator[Dependency]:
if not self.target_game.is_valid_asset_id(asset_id):
return

Expand Down Expand Up @@ -500,15 +498,11 @@ def get_audio_group_dependency(self, sound_id: int) -> Iterator[Dependency]:

if self._audio_group_dependency is None:
self._audio_group_dependency = tuple(
self.get_file(asset, Dgrp)
for asset in self.target_game.audio_group_dependencies()
self.get_file(asset, Dgrp) for asset in self.target_game.audio_group_dependencies()
)

dep = Dependency("AGSC", agsc, False)
if any(
(dep in deps.direct_dependencies)
for deps in self._audio_group_dependency
):
if any((dep in deps.direct_dependencies) for deps in self._audio_group_dependency):
return
else:
yield dep
Expand Down
7 changes: 3 additions & 4 deletions src/retro_data_structures/base_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ def resource_type(cls) -> AssetType:
raise NotImplementedError

@classmethod
def parse(cls, data: bytes, target_game: Game,
asset_manager: AssetManager | None = None) -> Self:
return cls(cls.construct_class(target_game).parse(data, target_game=target_game),
target_game, asset_manager)
def parse(cls, data: bytes, target_game: Game, asset_manager: AssetManager | None = None) -> Self:
return cls(cls.construct_class(target_game).parse(data, target_game=target_game), target_game, asset_manager)

def build(self) -> bytes:
return self.construct_class(self.target_game).build(self._raw, target_game=self.target_game)
Expand Down Expand Up @@ -115,4 +113,5 @@ class RawResource(typing.NamedTuple):
data: bytes
compressed: bool = False


Resource = RawResource | BaseResource
9 changes: 2 additions & 7 deletions src/retro_data_structures/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ def do_decode_from_pak(args):
print(asset_manager.get_parsed_asset(asset_id).raw)



def do_find_in_paks(args):
game: Game = args.game
asset_id: int = args.asset_id
Expand All @@ -199,9 +198,7 @@ def do_extract(args):
target_asset = asset_manager.get_raw_asset(asset_id)

destination.mkdir(parents=True, exist_ok=True)
destination.joinpath(f"{asset_id}.{target_asset.type.lower()}").write_bytes(
target_asset.data
)
destination.joinpath(f"{asset_id}.{target_asset.type.lower()}").write_bytes(target_asset.data)


def list_dependencies(args):
Expand Down Expand Up @@ -264,9 +261,7 @@ def id_generator(asset_type):
print(f"* Dependency: {dependency[1]:08x} ({dependency[0]})")

print("==================\n>> All converted assets")
reverse_converted_ids: dict[AssetId, tuple[Game, AssetId]] = {
v: k for k, v in converter.converted_ids.items()
}
reverse_converted_ids: dict[AssetId, tuple[Game, AssetId]] = {v: k for k, v in converter.converted_ids.items()}

for converted_asset in converter.converted_assets.values():
print(
Expand Down
13 changes: 7 additions & 6 deletions src/retro_data_structures/compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,15 @@ def _decode(self, segments, context, path):
return b"".join(segments)

def _encode(self, uncompressed, context, path):

decompressed_size = construct.evaluate(self.decompressed_size, context)
if decompressed_size != len(uncompressed):
raise ValueError("Decompressed size {} doesn't match size of data to compress ({}) at {}".format(
decompressed_size,
len(uncompressed),
path,
))
raise ValueError(
"Decompressed size {} doesn't match size of data to compress ({}) at {}".format(
decompressed_size,
len(uncompressed),
path,
)
)

segment_size = self.segment_size
return [
Expand Down
8 changes: 6 additions & 2 deletions src/retro_data_structures/construct_extensions/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ def _emitseq(ksy, bitwise):
return [
{"id": "countfield", "type": countfield._compileprimitivetype(ksy, bitwise)},
{"id": "extra", "type": extrafield._compileprimitivetype(ksy, bitwise)},
{"id": "data", "type": subcon._compileprimitivetype(ksy, bitwise),
"repeat": "expr", "repeat_expr": "countfield"},
{
"id": "data",
"type": subcon._compileprimitivetype(ksy, bitwise),
"repeat": "expr",
"repeat_expr": "countfield",
},
]

macro._emitseq = _emitseq
Expand Down
6 changes: 4 additions & 2 deletions src/retro_data_structures/conversion/anim.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ def convert_from_prime(data: Resource, details: AssetDetails, converter: AssetCo


def convert_from_echoes( # noqa: PLR0912 Too many branches
data: Resource, details: AssetDetails, converter: AssetConverter,
):
data: Resource,
details: AssetDetails,
converter: AssetConverter,
):
if converter.target_game != Game.PRIME:
raise UnsupportedTargetGame(Game.ECHOES, converter.target_game)

Expand Down
12 changes: 6 additions & 6 deletions src/retro_data_structures/conversion/asset_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ class AssetConverter:
converted_assets: dict[AssetId, ConvertedAsset]

def __init__(
self,
target_game: Game,
asset_providers: dict[Game, AssetManager],
id_generator: IdGenerator,
converters: Callable[[AssetDetails], ResourceConverter],
self,
target_game: Game,
asset_providers: dict[Game, AssetManager],
id_generator: IdGenerator,
converters: Callable[[AssetDetails], ResourceConverter],
):
self.target_game = target_game
self.asset_providers = asset_providers
Expand All @@ -63,7 +63,7 @@ def __init__(
self._being_converted = set()

def convert_id(
self, asset_id: AssetId | None, source_game: Game, *, missing_assets_as_invalid: bool = True
self, asset_id: AssetId | None, source_game: Game, *, missing_assets_as_invalid: bool = True
) -> AssetId:
if asset_id is not None and source_game.is_valid_asset_id(asset_id):
try:
Expand Down
41 changes: 13 additions & 28 deletions src/retro_data_structures/conversion/part.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ def upgrade(data, converter: AssetConverter, source_game: Game):


def _downgrade_color_mdao(element):
if (
element["body"]["body"]["a"]["type"] == "KEYE"
and element["body"]["body"]["b"]["type"] == "KEYP"
):
if element["body"]["body"]["a"]["type"] == "KEYE" and element["body"]["body"]["b"]["type"] == "KEYP":
org_colr_mado_a_keye = element["body"]["body"]["a"]["body"]["keys"]
new_colr_cnst_a_keyp_a = copy.deepcopy(element["body"]["body"]["a"])
new_colr_cnst_a_keyp_b = copy.deepcopy(element["body"]["body"]["a"])
Expand Down Expand Up @@ -54,10 +51,7 @@ def _downgrade_color_mdao(element):


def _downgrade_color_mult(element): # noqa: PLR0915 Too many statements
if (
element["body"]["body"]["a"]["type"] == "PULS"
and element["body"]["body"]["b"]["type"] == "KEYP"
):
if element["body"]["body"]["a"]["type"] == "PULS" and element["body"]["body"]["b"]["type"] == "KEYP":
org_colr_mult_b_keyp = element["body"]["body"]["b"]["body"]["keys"]
new_colr_a_c_mult_b_keyp_a = copy.deepcopy(element["body"]["body"]["b"])
new_colr_a_c_mult_b_keyp_b = copy.deepcopy(element["body"]["body"]["b"])
Expand All @@ -75,8 +69,8 @@ def _downgrade_color_mult(element): # noqa: PLR0915 Too many statements
new_colr_a_c_mult_b_keyp_d["body"]["keys"][i] = key[3]

if (
element["body"]["body"]["a"]["body"]["c"]["type"] == "KEYP"
and element["body"]["body"]["a"]["body"]["d"]["type"] == "KEYP"
element["body"]["body"]["a"]["body"]["c"]["type"] == "KEYP"
and element["body"]["body"]["a"]["body"]["d"]["type"] == "KEYP"
):
org_colr_mult_a_c_keyp = element["body"]["body"]["a"]["body"]["c"]["body"]["keys"]
new_colr_a_c_mult_a_keyp_c_a = copy.deepcopy(element["body"]["body"]["a"]["body"]["c"])
Expand Down Expand Up @@ -294,8 +288,8 @@ def downgrade(data, converter: AssetConverter, source_game: Game): # noqa: PLR0
if element["type"] == "EMTR":
if element["body"]["type"] == "SEMR":
if (
element["body"]["body"]["a"]["type"] == "RNDV"
and element["body"]["body"]["b"]["type"] == "RNDV"
element["body"]["body"]["a"]["type"] == "RNDV"
and element["body"]["body"]["b"]["type"] == "RNDV"
):
element["body"]["type"] = "SPHE"
element["body"]["body"] = {
Expand All @@ -319,29 +313,20 @@ def downgrade(data, converter: AssetConverter, source_game: Game): # noqa: PLR0
},
}
if (
element["body"]["body"]["a"]["type"] == "RNDV"
and element["body"]["body"]["b"]["type"] == "CNST"
element["body"]["body"]["a"]["type"] == "RNDV"
and element["body"]["body"]["b"]["type"] == "CNST"
):
element["body"]["type"] = "SPHE"
element["body"]["body"] = {
"a": {
"type": "RTOV",
"body": {
"type": "CNST",
"body": 0
}
},
"a": {"type": "RTOV", "body": {"type": "CNST", "body": 0}},
"b": element["body"]["body"]["a"]["body"],
"c": {
"type": "RAND",
"body": {
"a": {
"type": "CNST",
"body": 0
},
"b": element["body"]["body"]["b"]["body"]["a"]
}
}
"a": {"type": "CNST", "body": 0},
"b": element["body"]["body"]["b"]["body"]["a"],
},
},
}
if element["body"]["type"] == "ELPS":
element["body"]["type"] = "SPHE"
Expand Down
Loading