Skip to content

Commit

Permalink
uses str enum
Browse files Browse the repository at this point in the history
- todo: just use strenum after we stop supporting 3.10
  • Loading branch information
steven11sjf committed Nov 10, 2024
1 parent 2d9c803 commit 31cfd22
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions src/mercury_engine_data_structures/formats/bmssd.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,29 @@ def crc_func(obj):
return crc32 if obj._version == "1.12.0" else crc64


class ItemType(Enum):
group_name: str
class ItemType(str, Enum):
group_index: int

SCENE_BLOCK = 0, "scene_blocks"
OBJECT = 1, "objects"
LIGHT = 2, "lights"
SCENE_BLOCK = "scene_blocks", 0
OBJECT = "objects", 1
LIGHT = "lights", 2

def __new__(cls, value: int, group_name: str):
member = object.__new__(cls)
member._value_ = value
member.group_name = group_name
def __new__(cls, name: int, index: str):
member = str.__new__(cls, name)
member._value_ = name
member.group_index = index
return member

@classmethod
def from_index(cls, val: int):
for it in cls:
if it.group_index == val:
return it
return ValueError(f"Value {val} is not a valid index of ItemType")

Check warning on line 119 in src/mercury_engine_data_structures/formats/bmssd.py

View check run for this annotation

Codecov / codecov/patch

src/mercury_engine_data_structures/formats/bmssd.py#L119

Added line #L119 was not covered by tests

def __format__(self, format_spec: str) -> str:
return self.value.__format__(format_spec)


class BmssdAdapter(Adapter):
def _decode(self, obj, context, path):
Expand All @@ -130,8 +140,7 @@ def _decode(self, obj, context, path):
res.scene_groups[sg.sg_name] = construct.Container()

for ig_value, items in sg.item_groups.items():
group_type = ItemType(ig_value)
res.scene_groups[sg.sg_name][group_type] = construct.ListContainer()
group_type = ItemType.from_index(ig_value)

# objects are indexed and not hashed
if ig_value == 1:
Expand All @@ -142,9 +151,7 @@ def _decode(self, obj, context, path):
res.scene_groups[sg.sg_name][group_type] = construct.ListContainer(
[
# use raw hash value instead of block value if it doesn't exist above
res[f"_{group_type.group_name}"][block]
if res[f"_{group_type.group_name}"].get(block, None)
else block
res[f"_{group_type}"][block] if res[f"_{group_type}"].get(block, None) else block
for block in items
]
)
Expand Down Expand Up @@ -199,9 +206,9 @@ def obj_to_tuple(o):
item_count += len(items)

if group_type == ItemType.OBJECT:
sg_cont.item_groups[group_type.value] = [object_order[obj_to_tuple(o)] for o in items]
sg_cont.item_groups[group_type.group_index] = [object_order[obj_to_tuple(o)] for o in items]
else:
sg_cont.item_groups[group_type.value] = [
sg_cont.item_groups[group_type.group_index] = [
# handle integers (unmatched crc's in decode)
o if isinstance(o, int) else crc(o["model_name"])
for o in items
Expand All @@ -223,13 +230,13 @@ def get_item(self, item_name_or_id: str | int, item_type: ItemType) -> construct
if item_type == ItemType.OBJECT:
return self.raw._objects[item_name_or_id]
else:
return self.raw[f"_{item_type.group_name}"].get(item_name_or_id, None)
return self.raw[f"_{item_type}"].get(item_name_or_id, None)

Check warning on line 233 in src/mercury_engine_data_structures/formats/bmssd.py

View check run for this annotation

Codecov / codecov/patch

src/mercury_engine_data_structures/formats/bmssd.py#L233

Added line #L233 was not covered by tests

if item_type == ItemType.OBJECT:
raise ValueError("If accessing an Object type item, must use the index!")

crc = crc_func(self.raw)
return self.raw[f"_{item_type.group_name}"].get(crc(item_name_or_id), None)
return self.raw[f"_{item_type}"].get(crc(item_name_or_id), None)

def get_scene_group(self, scene_group: str) -> construct.Container:
return self.raw.scene_groups.get(scene_group, None)
Expand All @@ -245,7 +252,7 @@ def add_item(self, item: construct.Container, item_type: ItemType, scene_groups:
self.raw._objects.append(item)
else:
crc = crc_func(self.raw)
self.raw[f"_{item_type.group_name}"][crc(item["model_name"])] = item
self.raw[f"_{item_type}"][crc(item["model_name"])] = item

for sg_name in scene_groups:
self.get_scene_group(sg_name)[item_type].append(item)
Expand All @@ -260,4 +267,4 @@ def remove_item(self, item: construct.Container, item_type: ItemType):
for sg in groups:
self.remove_item_from_group(item, item_type, sg)

self.raw[f"_{item_type.group_name}"].remove(item)
self.raw[f"_{item_type}"].remove(item)

0 comments on commit 31cfd22

Please sign in to comment.