Skip to content

Commit 347afa8

Browse files
committed
TypeTreeNode.from_list - add helper to insert unused required ByteSize and Version
1 parent e8ca4cb commit 347afa8

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

UnityPy/helpers/TypeTreeNode.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import re
44
from struct import Struct
5-
from typing import TYPE_CHECKING, Dict, Iterator, List, Optional, Tuple
5+
from typing import TYPE_CHECKING, Dict, Iterator, List, Optional, Tuple, Any, Union
66

77
from attrs import define, field
88

@@ -139,16 +139,32 @@ def read_string(reader: EndianBinaryReader, value: int) -> str:
139139
return fake_root.m_Children[0]
140140

141141
@classmethod
142-
def from_list(cls, nodes: List[dict]) -> TypeTreeNode:
142+
def from_list(
143+
cls, nodes: Union[List[Dict[str, Union[str, int]]], List[TypeTreeNode]]
144+
) -> TypeTreeNode:
143145
fake_root: TypeTreeNode = cls(-1, "", "", 0, 0, [])
144146
stack: List[TypeTreeNode] = [fake_root]
145147
parent = fake_root
146148
prev = fake_root
147149

148-
for node in nodes:
149-
if isinstance(node, dict):
150-
node = cls(**node)
150+
# check if the nodes contain all required fields
151+
if isinstance(nodes[0], dict):
152+
if (
153+
"m_Level" not in nodes[0]
154+
or "m_Type" not in nodes[0]
155+
or "m_Name" not in nodes[0]
156+
):
157+
raise ValueError(
158+
"Nodes must contain at least m_Level, m_Type and m_Name"
159+
)
160+
patch_dict = {}
161+
if "m_ByteSize" not in nodes[0]:
162+
patch_dict["m_ByteSize"] = 0
163+
if "m_Version" not in nodes[0]:
164+
patch_dict["m_Version"] = 0
165+
nodes = [cls(**node, **patch_dict) for node in nodes]
151166

167+
for node in nodes:
152168
if node.m_Level > prev.m_Level:
153169
stack.append(parent)
154170
parent = prev

0 commit comments

Comments
 (0)