Skip to content

Commit

Permalink
if this doesnt work....
Browse files Browse the repository at this point in the history
  • Loading branch information
steven11sjf committed Oct 14, 2024
1 parent 2e8e549 commit c3e3321
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
8 changes: 3 additions & 5 deletions src/mercury_engine_data_structures/_dread_data_construct.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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])

Check warning on line 82 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#L77-L82

Added lines #L77 - L82 were not covered by tests
else:
a["versions"] = all_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#L84

Added line #L84 was not covered by tests

Expand Down
49 changes: 25 additions & 24 deletions tools/create_class_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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]
)


Expand Down

0 comments on commit c3e3321

Please sign in to comment.