|
2 | 2 |
|
3 | 3 | import re
|
4 | 4 | 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 |
6 | 6 |
|
7 | 7 | from attrs import define, field
|
8 | 8 |
|
@@ -139,16 +139,32 @@ def read_string(reader: EndianBinaryReader, value: int) -> str:
|
139 | 139 | return fake_root.m_Children[0]
|
140 | 140 |
|
141 | 141 | @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: |
143 | 145 | fake_root: TypeTreeNode = cls(-1, "", "", 0, 0, [])
|
144 | 146 | stack: List[TypeTreeNode] = [fake_root]
|
145 | 147 | parent = fake_root
|
146 | 148 | prev = fake_root
|
147 | 149 |
|
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] |
151 | 166 |
|
| 167 | + for node in nodes: |
152 | 168 | if node.m_Level > prev.m_Level:
|
153 | 169 | stack.append(parent)
|
154 | 170 | parent = prev
|
|
0 commit comments