Skip to content

Commit

Permalink
ObjectReader - add parse_as functions, PPtr - fix parse_as functions,…
Browse files Browse the repository at this point in the history
… add read_typetree backward comp
  • Loading branch information
K0lb3 committed Nov 16, 2024
1 parent d2d4c16 commit 72611c7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
12 changes: 7 additions & 5 deletions UnityPy/classes/PPtr.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ def path_id(self) -> int:
def type(self) -> ClassIDType:
return self.deref().type

# backwards compatibility - to be removed in UnityPy 2
def read(self):
# backwards compatibility
return self.deref_parse_as_object()

# backwards compatibility - to be removed in UnityPy 2
def read_typetree(self):
return self.deref_parse_as_dict()

def deref(self, assetsfile: Optional[SerializedFile] = None) -> ObjectReader[T]:
assetsfile = assetsfile or self.assetsfile
if assetsfile is None:
Expand Down Expand Up @@ -85,14 +89,12 @@ def deref(self, assetsfile: Optional[SerializedFile] = None) -> ObjectReader[T]:
return cast("ObjectReader[T]", assetsfile.objects[self.m_PathID])

def deref_parse_as_object(self, assetsfile: Optional[SerializedFile] = None) -> T:
return self.deref(assetsfile).read()
return self.deref(assetsfile).parse_as_object()

def deref_parse_as_dict(
self, assetsfile: Optional[SerializedFile] = None
) -> dict[str, Any]:
ret = self.deref(assetsfile).parse_as_dict()
assert isinstance(ret, dict)
return ret
return self.deref(assetsfile).parse_as_dict()

def __bool__(self):
return self.m_PathID != 0
Expand Down
7 changes: 7 additions & 0 deletions UnityPy/files/ObjectReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,10 @@ def _get_typetree_node(
if not node:
raise TypeTreeError("There are no TypeTree nodes for this object.")
return node

# UnityPy 2 syntax early implementation
def parse_as_object(self) -> T:
return self.read()

def parse_as_dict(self) -> dict:
return self.read_typetree()

0 comments on commit 72611c7

Please sign in to comment.