Skip to content

Commit

Permalink
(#107 & #15) fixing some .as_bytes() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
snake-biscuits committed Jun 13, 2023
1 parent 235b9ee commit d4a42d6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
8 changes: 6 additions & 2 deletions bsp_tool/branches/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ def as_cpp(self, inline_as: str = None, one_liner_limit: int = 80) -> str:
else:
return "\n".join(["struct {", inner, "} " + f"{inline_as};"])

def as_tuple(self) -> list:
def as_tuple(self) -> tuple:
"""recreates the array this instance was generated from"""
_tuple = list()
for attr in self._mapping:
Expand All @@ -461,11 +461,15 @@ def as_tuple(self) -> list:
_tuple.extend(value.as_tuple()) # recursive call
elif isinstance(value, BitField): # BitField is Iterable!
_tuple.append(value.as_int())
elif isinstance(value, str):
_tuple.append(value.encode("ascii", errors="ignore"))
elif isinstance(value, bytes):
_tuple.append(value)
elif isinstance(value, Iterable): # includes _classes
_tuple.extend(value)
else:
_tuple.append(value)
return [int(x) if f in "bBhHiI" else x for x, f in zip(_tuple, split_format(self._format))]
return tuple(_tuple)


class BitField:
Expand Down
11 changes: 5 additions & 6 deletions bsp_tool/branches/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,18 @@ def search(self, **search: Dict[str, str]) -> List[Dict[str, str]]:
# TODO: search_any_regex

def as_bytes(self) -> bytes:
entities = []
entities = list()
for entity_dict in self: # Dict[str, Union[str, List[str]]]
entity = ["{"]
entity = list()
for key, value in entity_dict.items():
if isinstance(value, str):
entity.append(f'"{key}" "{value}"')
elif isinstance(value, list): # multiple entries
entity.extend([f'"{key}" "{v}"' for v in value])
else:
raise RuntimeError("Entity values must be")
entity.append("}")
entities.append("\n".join(entity))
return b"\n".join(map(lambda e: e.encode("ascii"), entities)) + b"\n\x00"
raise RuntimeError("Entity values must be either a string or list of strings")
entities.append("\n".join(["{", *entity, "}"]).encode("ascii", errors="ignore"))
return b"\n".join(entities) + b"\n\x00"

@classmethod
def from_bytes(cls, raw_lump: bytes):
Expand Down

0 comments on commit d4a42d6

Please sign in to comment.