Skip to content

Commit 114da60

Browse files
committed
TypeTreeHelper - remove now unnecessary extra field handling
1 parent 7baccf7 commit 114da60

File tree

2 files changed

+4
-108
lines changed

2 files changed

+4
-108
lines changed

UnityPy/helpers/TypeTreeHelper.py

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -235,24 +235,11 @@ def read_value(
235235
else:
236236
clz = getattr(classes, node.m_Type, UnknownObject)
237237
try:
238+
if clz is UnknownObject:
239+
raise TypeError
238240
value = clz(**value)
239241
except TypeError:
240-
keys = set(value.keys())
241-
annotation_keys = set(clz.__annotations__)
242-
missing_keys = annotation_keys - keys
243-
if clz is UnknownObject or missing_keys:
244-
value = UnknownObject(node, **value)
245-
else:
246-
extra_keys = keys - annotation_keys
247-
if extra_keys:
248-
instance = clz(
249-
**{key: value[key] for key in annotation_keys}
250-
)
251-
for key in extra_keys:
252-
setattr(instance, key, value[key])
253-
value = instance
254-
else:
255-
value = UnknownObject(**value)
242+
value = UnknownObject(node, **value)
256243

257244
if align:
258245
reader.align_stream()
@@ -343,7 +330,6 @@ def read_value_array(
343330
keys = set(child._clean_name for child in node.m_Children)
344331
annotation_keys = set(clz.__annotations__)
345332
missing_keys = annotation_keys - keys
346-
extra_keys = keys - annotation_keys
347333
if missing_keys or clz is UnknownObject:
348334
value = [
349335
UnknownObject(
@@ -355,23 +341,6 @@ def read_value_array(
355341
)
356342
for _ in range(size)
357343
]
358-
elif extra_keys:
359-
value = [None] * size
360-
for i in range(size):
361-
value_i_d = {
362-
child._clean_name: read_value(child, reader, config)
363-
for child in node.m_Children
364-
}
365-
value_i = clz(
366-
**{
367-
key: value
368-
for key, value in value_i_d.items()
369-
if key in annotation_keys
370-
}
371-
)
372-
for key in extra_keys:
373-
setattr(value_i, key, value_i_d[key])
374-
value[i] = value_i
375344
else:
376345
value = [
377346
clz(

UnityPyBoost/TypeTreeHelper.cpp

Lines changed: 1 addition & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -454,37 +454,11 @@ inline PyObject *read_class(ReaderT *reader, TypeTreeNodeObject *node, TypeTreeR
454454
return value;
455455
}
456456

457-
#if PY_VERSION_HEX >= 0x030e0000
458-
PyObject *get_annotations = nullptr;
459-
#endif
460-
461-
inline PyObject *get_annotations(PyObject *clz)
462-
{
463-
#if PY_VERSION_HEX >= 0x030e0000
464-
if (get_annotations == nullptr)
465-
{
466-
// from annotationlib import get_annotations
467-
PyObject *annotationlib = PyImport_ImportModule("annotationlib");
468-
get_annotations = PyObject_GetAttrString(annotationlib, "get_annotations");
469-
Py_INCREF(get_annotations);
470-
Py_DECREF(annotationlib);
471-
}
472-
return PyObject_CallFunctionObjArgs(get_annotations, clz, NULL);
473-
#else
474-
return PyObject_GetAttrString(clz, "__annotations__");
475-
#endif
476-
}
477-
478457
inline PyObject *parse_class(PyObject *kwargs, TypeTreeNodeObject *node, TypeTreeReaderConfigT *config)
479458
{
480459
PyObject *instance = nullptr;
481460
PyObject *clz = nullptr;
482461
PyObject *args = PyTuple_New(0);
483-
PyObject *annotations = nullptr;
484-
PyObject *extras = nullptr;
485-
// dict iterator values
486-
PyObject *key, *value = nullptr;
487-
Py_ssize_t pos;
488462

489463
if (node->_data_type == NodeDataType::PPtr)
490464
{
@@ -518,63 +492,16 @@ inline PyObject *parse_class(PyObject *kwargs, TypeTreeNodeObject *node, TypeTre
518492
}
519493
PyErr_Clear();
520494

521-
// possibly extra fields
522-
annotations = get_annotations(clz);
523-
if (annotations == nullptr)
524-
{
525-
PyErr_SetString(PyExc_ValueError, "Failed to get annotations");
526-
goto PARSE_CLASS_CLEANUP;
527-
}
528-
extras = PyDict_New();
529-
for (int i = 0; i < PyList_GET_SIZE(node->m_Children); i++)
530-
{
531-
TypeTreeNodeObject *child = (TypeTreeNodeObject *)PyList_GET_ITEM(node->m_Children, i); // - borrowed ref +/- 0
532-
if (PyDict_Contains(annotations, child->_clean_name) == 1)
533-
{
534-
continue;
535-
}
536-
PyObject *extra_value = PyDict_GetItem(kwargs, child->_clean_name); // - borrowed ref +/- 0
537-
PyDict_SetItem(extras, child->_clean_name, extra_value); // +1
538-
PyDict_DelItem(kwargs, child->_clean_name); // -1
539-
}
540-
541-
if (PyDict_Size(extras) == 0)
542-
{
543-
Py_DECREF(clz); // 1->0
544-
clz = PyObject_GetAttrString(config->classes, "UnknownObject"); // 0->1
545-
PyDict_SetItemString(kwargs, "__node__", (PyObject *)node);
546-
}
547-
548-
instance = PyObject_Call(clz, args, kwargs);
549-
if (instance != nullptr)
550-
{
551-
pos = 0;
552-
while (PyDict_Next(extras, &pos, &key, &value))
553-
{
554-
PyObject_GenericSetAttr(instance, key, value);
555-
}
556-
goto PARSE_CLASS_CLEANUP;
557-
}
558-
PyErr_Clear();
559-
560-
// if we still failed to create an instance, fallback to UnknownObject
495+
// fallback to UnknownObject
561496
Py_DECREF(clz);
562497
clz = PyObject_GetAttrString(config->classes, "UnknownObject");
563498
PyDict_SetItemString(kwargs, "__node__", (PyObject *)node);
564-
// merge extras back into kwargs
565-
pos = 0;
566-
while (PyDict_Next(extras, &pos, &key, &value))
567-
{
568-
PyDict_SetItem(kwargs, key, value);
569-
}
570499
instance = PyObject_Call(clz, args, kwargs);
571500

572501
PARSE_CLASS_CLEANUP:
573502
Py_DECREF(args);
574503
Py_DECREF(kwargs);
575504
Py_XDECREF(clz);
576-
Py_XDECREF(annotations);
577-
Py_XDECREF(extras);
578505
return instance;
579506
}
580507

0 commit comments

Comments
 (0)