Skip to content

Commit 32589ac

Browse files
committed
Version 0.0.14
TMP-led improvments
1 parent e9058c0 commit 32589ac

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
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, 13)
1+
__version__ = (0, 0, 14)

UnityPyTypetreeCodegen/__main__.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"Object": "object",
5656
"Type": "type",
5757
"MethodInfo": "object",
58-
"PropertyInfo": "object",
58+
"PropertyInfo": "object",
5959
}
6060
Typetree_MonoBehaviour = [
6161
{
@@ -312,22 +312,33 @@ def translate_name(m_Name: str, **kwargs):
312312

313313

314314
def translate_type(
315-
m_Type: str, strip=False, fallback=True, typenames: dict = dict(), **kwargs
315+
m_Type: str, strip=False, fallback=True, typenames: dict = dict(), feild_nexts : list = [], parent_name = None
316316
):
317+
if m_Type == parent_name:
318+
return 'object' # XXX Recusrive. Python doesn't like those
317319
if m_Type in BASE_TYPE_MAP:
318320
return BASE_TYPE_MAP[m_Type]
319321
if getattr(UnityBuiltin, m_Type, None):
320322
return m_Type
321323
if m_Type in typenames:
322324
return m_Type
323-
if m_Type.endswith("[]"):
324-
m_Type = translate_type(m_Type[:-2], strip, fallback, typenames)
325+
if m_Type.endswith("[]") or m_Type == 'List`1':
326+
if feild_nexts:
327+
m_Type = translate_type(feild_nexts[3].m_Type, strip, fallback, typenames, feild_nexts, parent_name)
328+
elif m_Type.endswith("[]") :
329+
m_Type = m_Type[:-2]
330+
else:
331+
m_Type = None
325332
if not strip:
326-
return f"List[{m_Type}]"
333+
if m_Type:
334+
return f"List[{m_Type}]"
335+
else:
336+
logger.warning(f"Unknown list type of element {m_Type}, using fallback")
337+
return 'list'
327338
else:
328339
return m_Type
329340
if m_Type.startswith("PPtr<"):
330-
m_Type = translate_type(m_Type[5:-1], strip, fallback, typenames)
341+
m_Type = translate_type(m_Type[5:-1], strip, fallback, typenames, feild_nexts, parent_name)
331342
if not strip:
332343
return f"PPtr[{m_Type}]"
333344
else:
@@ -411,7 +422,7 @@ def emit_line(*lines: str):
411422
# Emit by topo order
412423
graph = {
413424
clazz: {
414-
translate_type(field.m_Type, strip=True, fallback=False) for field in fields
425+
translate_type(field.m_Type, strip=True, fallback=False, feild_nexts=fields[i:]) for (i,field) in enumerate(fields)
415426
}
416427
for clazz, fields in classname_nodes.items()
417428
}
@@ -420,7 +431,7 @@ def emit_line(*lines: str):
420431

421432
logger.info(f"Subpass 2: Generating code for {namespace}")
422433
dp = defaultdict(lambda: -1)
423-
for clazz in topo:
434+
for clazz in topo:
424435
fullname = f"{namespace}.{clazz}" if namespace else clazz
425436
fields = classname_nodes.get(clazz, None)
426437
if not fields:
@@ -443,7 +454,7 @@ def __encoder(obj):
443454
emit_line(f"@UTTCGen('{fullname}', {clazz_typetree})")
444455
# Heuristic: If there is a lvl1 and a lvl0 field, it's a subclass
445456
if lvl1 and lvl0:
446-
parent = translate_type(fields[0].m_Type, strip=True, fallback=False)
457+
parent = translate_type(fields[0].m_Type, strip=True, fallback=False, feild_nexts=fields[0:], parent_name=clazz)
447458
emit_line(f"class {translate_name(clazz)}({translate_name(parent)}):")
448459
if dp[parent] == -1:
449460
# Reuse parent's fields with best possible effort
@@ -462,7 +473,7 @@ def __encoder(obj):
462473
# Skip parent fields at lvl1
463474
continue
464475
name, type = field.m_Name, translate_type(
465-
field.m_Type, typenames=classname_nodes | import_defs
476+
field.m_Type, typenames=classname_nodes | import_defs, feild_nexts=fields[i:], parent_name=clazz
466477
)
467478
emit_line(f"\t{declare_field(name, type, field.m_Type)}")
468479
clazz_fields.append((name, type, field.m_Type))
@@ -471,11 +482,11 @@ def __encoder(obj):
471482
else:
472483
# No inheritance
473484
emit_line(f"class {clazz}:")
474-
for field in fields:
485+
for i, field in enumerate(fields):
475486
if field.m_Level > 1:
476487
continue # Nested
477488
name, type = field.m_Name, translate_type(
478-
field.m_Type, typenames=classname_nodes | import_defs
489+
field.m_Type, typenames=classname_nodes | import_defs, feild_nexts=fields[i:], parent_name=clazz
479490
)
480491
emit_line(f"\t{declare_field(name, type, field.m_Type)}")
481492
clazz_fields.append((name, type))

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"License :: OSI Approved :: Apache Software License",
2222
"Operating System :: OS Independent",
2323
],
24-
install_requires=["TypeTreeGeneratorAPIEx", "UnityPy>=1.21"],
24+
install_requires=["TypeTreeGeneratorAPIEx>=0.0.9", "UnityPy>=1.21"],
2525
entry_points={
2626
"console_scripts": [
2727
"UnityPyTypetreeCodegen = UnityPyTypetreeCodegen.__main__:__main__"

0 commit comments

Comments
 (0)