From c3e3321e17ccb53642ca505025365eca823307bd Mon Sep 17 00:00:00 2001 From: Steven Franklin Date: Mon, 14 Oct 2024 00:53:36 -0500 Subject: [PATCH] if this doesnt work.... --- .../_dread_data_construct.py | 8 ++- tools/create_class_definitions.py | 49 ++++++++++--------- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/src/mercury_engine_data_structures/_dread_data_construct.py b/src/mercury_engine_data_structures/_dread_data_construct.py index 948b298..2d14ed8 100644 --- a/src/mercury_engine_data_structures/_dread_data_construct.py +++ b/src/mercury_engine_data_structures/_dread_data_construct.py @@ -2,8 +2,6 @@ import construct -from mercury_engine_data_structures.game_check import GameVersion - class CompressedZSTD(construct.Tunnel): def __init__(self, subcon, level: int = 3): @@ -76,12 +74,12 @@ def _parse(self, stream, context, path) -> dict[str, int]: return result def _build(self, obj: dict[str, dict], stream, context, path): - ver_to_val = GameVersion.versions_for_game(context.target_game) - all_vers = sum([v.bitmask for v in ver_to_val.values()]) + 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].bitmask for v in vers]) + a["versions"] = sum([ver_to_val[v] for v in vers]) else: a["versions"] = all_vers diff --git a/tools/create_class_definitions.py b/tools/create_class_definitions.py index 5fa4771..9559483 100644 --- a/tools/create_class_definitions.py +++ b/tools/create_class_definitions.py @@ -2,18 +2,29 @@ import collections import copy import json -import typing from pathlib import Path +from typing import Any import construct meds_root = Path(__file__).parents[1].joinpath("src", "mercury_engine_data_structures") dread_data_construct_path = meds_root.joinpath("_dread_data_construct.py") -game_check_path = meds_root.joinpath("game_check.py") data_construct = construct.Container() exec(compile(dread_data_construct_path.read_text(), dread_data_construct_path, "exec"), data_construct) -exec(compile(game_check_path.read_text(), game_check_path, "exec"), data_construct) + +# This must be updated along with src/mercury_engine_data_structures/game_check.py! +GAME_VERSION_BUILD_DATA = { + "sr": { + "1.0.0": 1, + }, + "dread": { + "1.0.0": 1, + "1.0.1": 2, + "2.0.0": 4, + "2.1.0": 8, + }, +} def _type_name_to_python_identifier(type_name: str): @@ -28,7 +39,7 @@ def _type_name_to_python_identifier(type_name: str): class TypeExporter: - def __init__(self, all_types: dict[str, typing.Any], primitive_to_construct, type_lib): + def __init__(self, all_types: dict[str, Any], primitive_to_construct, type_lib): self.all_types = all_types self._exported_types = {} self._types_with_pointer = set() @@ -216,30 +227,20 @@ def export_code(self): return code -def game_argument_type(s: str): - try: - return data_construct.Game(int(s)) - except ValueError: - # not a number, look by name - for g in data_construct.Game: - g = typing.cast(data_construct.Game, g) - if g.name.lower() == s.lower(): - return g +def game_argument_type(s: str) -> str: + if s not in ["dread", "sr"]: raise ValueError(f"No enum named {s} found") + return s def main(): parser = argparse.ArgumentParser() - - choices = [] - for g in data_construct.Game: - g = typing.cast(data_construct.Game, g) - choices.append(g.value) - choices.append(g.name) - - parser.add_argument("game", help="The game of the file", type=game_argument_type, choices=list(data_construct.Game)) + parser.add_argument( + "game", help="The game to create the class definitions for", type=game_argument_type, choices=["dread", "sr"] + ) args = parser.parse_args() - if args.game == data_construct.Game.DREAD: + + if args.game == "dread": types_path = meds_root.joinpath("dread_types.json") output_name = "dread_types.py" resource_name = "dread_resource_names" @@ -274,7 +275,7 @@ def main(): output_path = meds_root.joinpath("formats", output_name) # Skip it for sr as it has some errors in its types json - if args.game != data_construct.Game.SAMUS_RETURNS: + if args.game != "sr": all_types: dict[str, type_lib.BaseType] = copy.copy(type_lib.TypeLib(game_types, 11).all_types()) type_exporter = TypeExporter(all_types, primitive_to_construct, type_lib) @@ -292,7 +293,7 @@ def main(): resource_data: dict[str, dict] = json.load(f) data_construct.VersionedHashes.build_file( - resource_data, meds_root.joinpath(f"{resource_name}.bin"), target_game=data_construct.Game(args.game) + resource_data, meds_root.joinpath(f"{resource_name}.bin"), versions=GAME_VERSION_BUILD_DATA[args.game] )