Skip to content

Commit 8120c3f

Browse files
committed
ObjectReader - make peek_name more universal
1 parent 9b0aefa commit 8120c3f

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

UnityPy/files/ObjectReader.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,20 +159,23 @@ def set_raw_data(self, data):
159159
def get_class(self) -> Union[Type[T], None]:
160160
return ClassIDTypeToClassMap.get(self.type)
161161

162-
def peek_name(self) -> Union[str, None]:
162+
def peek_name(self, max_name_length: int = 256) -> Union[str, None]:
163163
"""Peeks the name of the object without reading/parsing the whole object."""
164164
# TODO: EditorExtension might be enough
165+
if self.platform == BuildTarget.NoTarget:
166+
# 2x PPtr
167+
raise NotImplementedError(
168+
"Directly fetching the name for 'NoTarget' platform is not supported"
169+
)
165170
clz = self.get_class()
166-
if clz and issubclass(clz, NamedObject):
167-
self.reset()
168-
if self.platform == BuildTarget.NoTarget:
169-
# 2x PPtr
170-
raise NotImplementedError(
171-
"Directly fetching the name for 'NoTarget' platform is not supported"
172-
)
173-
return self.reader.read_aligned_string()
174-
else:
171+
if "m_Name" not in clz.__annotations__:
172+
return None
173+
self.reset()
174+
len = self.reader.read_int()
175+
if len < 0 or len > max_name_length or len + 4 > self.byte_size:
175176
return None
177+
string_data = bytes(self.reader.read_bytes(len))
178+
return string_data.decode("utf8", "surrogateescape")
176179

177180
@property
178181
def container(self):

0 commit comments

Comments
 (0)