From 72611c78b909cefe811a0bf4b8994269ba1f0edf Mon Sep 17 00:00:00 2001 From: Rudolf Kolbe Date: Sat, 16 Nov 2024 20:39:43 +0100 Subject: [PATCH] ObjectReader - add parse_as functions, PPtr - fix parse_as functions, add read_typetree backward comp --- UnityPy/classes/PPtr.py | 12 +++++++----- UnityPy/files/ObjectReader.py | 7 +++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/UnityPy/classes/PPtr.py b/UnityPy/classes/PPtr.py index 8b1294be..1459583f 100644 --- a/UnityPy/classes/PPtr.py +++ b/UnityPy/classes/PPtr.py @@ -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: @@ -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 diff --git a/UnityPy/files/ObjectReader.py b/UnityPy/files/ObjectReader.py index d1cdc834..4b9f7d62 100644 --- a/UnityPy/files/ObjectReader.py +++ b/UnityPy/files/ObjectReader.py @@ -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()