Skip to content

Commit 42fd94b

Browse files
committed
Version 0.0.11
Fix recursive type handling
1 parent 8a62df5 commit 42fd94b

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

UnityPyTypetreeCodegen/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = (0, 0, 10)
1+
__version__ = (0, 0, 11)

UnityPyTypetreeCodegen/__main__.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,21 @@ def reduce_init(clazz, **d):
8787
if type(sub) == str:
8888
sub = eval(sub) # attrs turns these into strings...why?
8989
while sub.__name__ == "Optional":
90-
sub = sub.__args__[0] # Reduce Optional[T] -> T
91-
reduce_arg = getattr(sub, "__args__", [None])[0]
90+
sub = sub.__args__[0] # Reduce Optional[T] -> T
91+
reduce_arg = getattr(sub, "__args__", [None])[0]
9292
if k in REFERENCED_ARGS: # Directly refcounted
9393
reduce_arg = sub = lambda x: x
9494
if reduce_arg is not None and isinstance(d[k], list):
95-
if hasattr(reduce_arg, "__annotations__"):
95+
if hasattr(reduce_arg, "__annotations__") or hasattr(reduce_arg, "__args__"):
9696
setattr(self, k, [reduce_arg(**x) for x in d[k]])
9797
else:
9898
setattr(self, k, [reduce_arg(x) for x in d[k]])
9999
elif reduce_arg is not None and isinstance(d[k], dict) and hasattr(sub, "__annotations__"):
100100
setattr(self, k, sub(**d[k]))
101101
else:
102+
# Reduce typings to respective types
103+
if hasattr(sub, "__origin__") and sub.__origin__ is not None:
104+
sub = sub.__origin__
102105
if isinstance(d[k], dict):
103106
setattr(self, k, sub(**d[k]))
104107
else:
@@ -127,6 +130,7 @@ def __save(self):
127130
return clazz
128131
return __inner
129132
133+
130134
# Helper functions
131135
def UTTCGen_GetClass(src: MonoBehaviour | str) -> Type:
132136
"""Get the class definition from MonoBehaviour or a full type name."""
@@ -361,9 +365,7 @@ def __encoder(obj):
361365
# Skip parent fields at lvl1
362366
continue
363367
if i + 1 < len(fields) and fields[i + 1].m_Type == "Array":
364-
if field.m_Type.startswith("List"):
365-
# Rename this to Type[]
366-
field.m_Type = fields[i + 3].m_Type + "[]"
368+
field.m_Type = fields[i + 3].m_Type + "[]"
367369
name, type = field.m_Name, translate_type(
368370
field.m_Type, typenames=classname_nodes | import_defs
369371
)

0 commit comments

Comments
 (0)