This repository was archived by the owner on Feb 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__main__.py
More file actions
658 lines (610 loc) · 22.7 KB
/
__main__.py
File metadata and controls
658 lines (610 loc) · 22.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
"""Simple codegen for typetree classes
- Supports nested types
- Supports inheritance
- Automatically resolves import order and dependencies
- Generated classes support nested types, and will be initialized with the correct types even with nested dicts
NOTE:
- Cannot resolve namespace conflicts if the same class name is defined in multiple namespaces
- Missing type definitions are marked with # XXX: Fallback of {org_type} and typedefed as object
- Circular inheritance is checked and raises RecursionError
- The output order (imports, classes) are deterministic and lexicographically sorted
- The output is emitted in lieu of the Namespace structure of the TypeTree dump, presented as Python modules in directories
USAGE:
python typetree_codegen.py <typetree_dump.json> <output_dir>
"""
# From https://github.com/K0lb3/UnityPy/blob/master/generators/ClassesGenerator.py
BASE_TYPE_MAP = {
"char": "str",
"short": "int",
"int": "int",
"long long": "int",
"unsigned short": "int",
"unsigned int": "int",
"unsigned long long": "int",
"UInt8": "int",
"UInt16": "int",
"UInt32": "int",
"UInt64": "int",
"SInt8": "int",
"SInt16": "int",
"SInt32": "int",
"SInt64": "int",
"Type*": "int",
"FileSize": "int",
"float": "float",
"double": "float",
"bool": "bool",
"string": "str",
"TypelessData": "bytes",
# -- Extra
"Byte[]": "bytes",
"Byte": "int",
"String": "str",
"Int32": "int",
"Single": "float",
"Color": "ColorRGBA",
"Vector2": "Vector2f",
"Vector3": "Vector3f",
"Vector4": "Vector4f",
"Quaternion": "Quaternionf",
"Object": "object",
"Type": "type",
"MethodInfo": "object",
"PropertyInfo": "object",
}
Typetree_MonoBehaviour = [
{
"m_Type": "MonoBehaviour",
"m_Name": "Base",
"m_MetaFlag": 0,
"m_Level": 0
},
{
"m_Type": "PPtr<GameObject>",
"m_Name": "m_GameObject",
"m_MetaFlag": 0,
"m_Level": 1
},
{
"m_Type": "int",
"m_Name": "m_FileID",
"m_MetaFlag": 0,
"m_Level": 2
},
{
"m_Type": "SInt64",
"m_Name": "m_PathID",
"m_MetaFlag": 0,
"m_Level": 2
},
{
"m_Type": "UInt8",
"m_Name": "m_Enabled",
"m_MetaFlag": 16384,
"m_Level": 1
},
{
"m_Type": "PPtr<MonoScript>",
"m_Name": "m_Script",
"m_MetaFlag": 0,
"m_Level": 1
},
{
"m_Type": "int",
"m_Name": "m_FileID",
"m_MetaFlag": 0,
"m_Level": 2
},
{
"m_Type": "SInt64",
"m_Name": "m_PathID",
"m_MetaFlag": 0,
"m_Level": 2
},
{
"m_Type": "string",
"m_Name": "m_Name",
"m_MetaFlag": 0,
"m_Level": 1
},
{
"m_Type": "Array",
"m_Name": "Array",
"m_MetaFlag": 16384,
"m_Level": 2
},
{
"m_Type": "int",
"m_Name": "size",
"m_MetaFlag": 0,
"m_Level": 3
},
{
"m_Type": "char",
"m_Name": "data",
"m_MetaFlag": 0,
"m_Level": 3
}
]
# XXX: Can't use attrs here since subclassing MonoBehavior and such - though defined by the typetree dump
# seem to be only valid if the class isn't a property of another class
# In which case the MonoBehavior attributes are inherited by the parent class and does not
# initialize the property class
# XXX: Need some boilerplate to handle this
HEADER = "\n".join(
[
"# fmt: off",
"# Auto-generated by https://github.com/mos9527/UnityPyTypetreeCodegen",
"" "from typing import List, Union, Optional, TypeVar, Type",
"from UnityPy.files.ObjectReader import ObjectReader",
"from UnityPy.classes import *",
"from UnityPy.classes.math import (ColorRGBA, Matrix3x4f, Matrix4x4f, Quaternionf, Vector2f, Vector3f, Vector4f, float3, float4,)",
'''
UTTCG_Classes = dict()
def UTTCGen(fullname: str, typetree: List[dict]):
"""dataclass-like decorator for typetree classess with nested type support
limitations:
- the behavior is similar to slotted dataclasses where shared attributes are inherited
but allows ommiting init of the parent if kwargs are not sufficient
- generally supports nested types, however untested and could be slow
- and ofc, zero type checking and safeguards :/
"""
REFERENCED_ARGS = {'object_reader'}
def __inner(clazz: T) -> T:
# Allow these to be propogated to the props
def __init__(self, **d):
def reduce_init(clazz, **d):
types : dict = clazz.__annotations__
for k, sub in types.items():
if type(sub) == str:
sub = eval(sub) # attrs turns these into strings...why?
while sub.__name__ == "Optional":
sub = sub.__args__[0] # Reduce Optional[T] -> T
reduce_arg = getattr(sub, "__args__", [None])[0]
if k in REFERENCED_ARGS: # Directly refcounted
reduce_arg = sub = lambda x: x
if reduce_arg is not None and isinstance(d[k], list):
if hasattr(reduce_arg, "__annotations__") or hasattr(reduce_arg, "__args__"):
setattr(self, k, [reduce_arg(**x) for x in d[k]])
else:
setattr(self, k, [reduce_arg(x) for x in d[k]])
elif reduce_arg is not None and isinstance(d[k], dict) and hasattr(sub, "__annotations__"):
setattr(self, k, sub(**d[k]))
else:
# Reduce typings to respective types
if hasattr(sub, "__origin__") and sub.__origin__ is not None:
sub = sub.__origin__
if isinstance(d[k], dict):
# Special cases
# Color with `rgba` field
if sub == ColorRGBA and 'rgba' in d[k]:
rgba = d[k]['rgba']
r = ((rgba >> 24) & 0xFF) / 255
g = ((rgba >> 16) & 0xFF) / 255
b = ((rgba >> 8) & 0xFF) / 255
a = (rgba & 0xFF) / 255
setattr(self, k, sub(r, g, b, a))
else:
setattr(self, k, sub(**d[k]))
else:
setattr(self, k, sub(d[k]))
def reduce_base(clazz, **d):
for __base__ in clazz.__bases__:
if hasattr(__base__, "__annotations__"):
types : dict = __base__.__annotations__
args = {k:d[k] for k in types if k in d}
if len(args) == len(types):
super(clazz, self).__init__(**args)
reduce_init(__base__, **d)
reduce_base(__base__, **d)
reduce_base(clazz, **d)
reduce_init(clazz, **d)
def __repr__(self) -> str:
return f"{clazz.__name__}({', '.join([f'{k}={getattr(self, k)!r}' for k in self.__annotations__])})"
def __save(self):
self.object_reader.save_typetree(self, self.__typetree__)
clazz.__init__ = __init__
clazz.__repr__ = __repr__
clazz.__typetree__ = typetree
clazz.__fullname__ = fullname
clazz.save = __save
UTTCG_Classes[fullname] = clazz
return clazz
return __inner
# Helper functions
def UTTCGen_GetClass(src: MonoBehaviour | str) -> Type:
"""Get the class definition from MonoBehaviour or a full type name."""
if isinstance(src, MonoBehaviour):
script = src.m_Script.read()
src = script.m_ClassName
if script.m_Namespace:
src = f"{script.m_Namespace}.{src}"
return UTTCG_Classes.get(src, None)
T = TypeVar("T")
def UTTCGen_AsInstance(cls : Type[T], src: MonoBehaviour | ObjectReader, check_read : bool = True) -> T:
"""Instantiate a class from the typetree definition and the raw data.
In most cases, this is the function you want to use.
It will read the typetree data from the MonoBehaviour instance and instantiate the class with the data.
Args:
cls: The class to instantiate. This should be a class that has been decorated with the UTTCGen decorator.
src (MonoBehaviour | ObjectReader): The MonoBehaviour instance or ObjectReader to read from.
check_read (bool): Whether to check if all fields are read. Defaults to True.
Returns:
An instance of the class defined by the typetree.
"""
if isinstance(src, MonoBehaviour):
src = src.object_reader
raw_def = src.read_typetree(cls.__typetree__, check_read=check_read)
instance = cls(object_reader=src, **raw_def)
return instance
''',
]
)
from collections import defaultdict
import argparse, json
def translate_name(m_Name: str, **kwargs):
"""A la https://github.com/K0lb3/UnityPy/blob/b811a2942297b5d8107e9a10249df80a87492282/UnityPy/helpers/TypeTreeNode.py#L361.
With extra handling for templated/generic types and reserved keywords.
"""
NG = "<>|`="
m_Name = m_Name.replace("<>", "__generic_") # Generic templates
m_Name = m_Name.replace("<", "_").replace(">", "_") # Templated
for c in NG:
m_Name = m_Name.replace(c, "_")
RESERVED_NAMES = {
"class",
"def",
"return",
"if",
"else",
"elif",
"for",
"while",
"in",
"is",
"not",
"and",
"or",
"from",
"import",
"as",
"with",
"try",
"except",
"finally",
"raise",
"assert",
"break",
"continue",
"pass",
"yield",
"True",
"False",
}
if m_Name in RESERVED_NAMES:
m_Name = "_" + m_Name
return m_Name
from UnityPy import classes as UnityBuiltin
from TypeTreeGeneratorAPI import TypeTreeNode
from logging import getLogger
import os, shutil
from typing import Dict, List
logger = getLogger("codegen")
def translate_type(
m_Type: str, strip=False, fallback=True, typenames: dict = dict(), feild_nexts : list = [], parent_name = None
):
if m_Type == parent_name:
return 'object' # XXX Recusrive. Python doesn't like those
if m_Type in BASE_TYPE_MAP:
return BASE_TYPE_MAP[m_Type]
if getattr(UnityBuiltin, m_Type, None):
return m_Type
if m_Type in typenames:
return m_Type
if m_Type.endswith("[]") or m_Type == 'List`1':
if feild_nexts:
m_Type = translate_type(feild_nexts[3].m_Type, strip, fallback, typenames, feild_nexts, parent_name)
elif m_Type.endswith("[]") :
m_Type = m_Type[:-2]
else:
m_Type = None
if not strip:
if m_Type:
return f"List[{m_Type}]"
else:
logger.warning(f"Unknown list type of element {m_Type}, using fallback")
return 'list'
else:
return m_Type
if m_Type.startswith("PPtr<"):
m_Type = translate_type(m_Type[5:-1], strip, fallback, typenames, feild_nexts, parent_name)
if not strip:
return f"PPtr[{m_Type}]"
else:
return m_Type
if fallback:
logger.warning(f"Unknown type {m_Type}, using fallback")
return "object"
else:
return m_Type
def declare_field(name: str, type: str, org_type: str = None):
name = translate_name(name)
if type not in {"object", "List[object]", "PPtr[object]"}:
return f"{name} : {type}"
else:
# We'd skip parsing these further if we don't know the type
if type == "object":
type = 'dict'
if type == "List[object]":
type = 'List[dict]'
return f"{name} : {type} # XXX: Fallback of {org_type}"
from io import TextIOWrapper
def topsort(graph: dict):
# Sort the keys in topological order
# We don't assume the guarantee otherwise
graph = {k: list(sorted((i for i in v if i != k))) for k, v in graph.items()}
vis = defaultdict(lambda: 0)
topo = list()
def dfs(u):
vis[u] = 1
for v in graph.get(u, []):
if vis[v] == 1:
return False
if vis[v] == 0 and not dfs(v):
return False
vis[u] = 2
topo.append(u)
return True
flag = 1
for clazz in graph:
if not vis[clazz]:
flag &= dfs(clazz)
# XXX: Shouldn't happen. Need to figure out how this is possible
# assert flag, "graph contains cycle"
return topo
def process_namespace(
f: TextIOWrapper,
classname_nodes: Dict[str, List[TypeTreeNode]],
namespace: str = "",
import_root: str = "",
import_defs: dict = dict(),
):
def emit_line(*lines: str):
for line in lines:
f.write(line)
f.write("\n")
if not lines:
f.write("\n")
logger.info(
f"Subpass 1: Generating class dependency graph for {namespace or "<default namespace>"}"
)
emit_line("# fmt: off")
emit_line("# Auto-generated by https://github.com/mos9527/UnityPyTypetreeCodegen")
emit_line(f"# Python definition for {namespace or "<default namespace>"}", "")
if import_root:
emit_line(f"from {import_root} import *")
for clazz, parent in import_defs.items():
emit_line(f"from {import_root}{parent or ''} import {clazz}")
emit_line()
# Emit by topo order
graph = {
clazz: {
translate_type(field.m_Type, strip=True, fallback=False, feild_nexts=fields[i:]) for (i,field) in enumerate(fields)
}
for clazz, fields in classname_nodes.items()
}
topo = topsort(graph)
clazzes = list()
logger.info(f"Subpass 2: Generating code for {namespace}")
dp = defaultdict(lambda: -1)
for clazz in topo:
fullname = f"{namespace}.{clazz}" if namespace else clazz
fields = classname_nodes.get(clazz, None)
if not fields:
logger.debug(
f"Class {clazz} has no fields defined in TypeTree dump, skipped"
)
continue
lvl0 = list(filter(lambda field: field.m_Level == 0, fields))
lvl1 = list(filter(lambda field: field.m_Level == 1, fields))
clazz = translate_name(clazz)
clazzes.append(clazz)
clazz_fields = list()
def __encoder(obj):
if isinstance(obj, TypeTreeNode):
return obj.__dict__
return obj
clazz_typetree = json.dumps(fields, default=__encoder)
emit_line(f"@UTTCGen('{fullname}', {clazz_typetree})")
# Heuristic: If there is a lvl1 and a lvl0 field, it's a subclass
if lvl1 and lvl0:
parent = translate_type(fields[0].m_Type, strip=True, fallback=False, feild_nexts=fields[0:], parent_name=clazz)
emit_line(f"class {translate_name(clazz)}({translate_name(parent)}):")
if dp[parent] == -1:
# Reuse parent's fields with best possible effort
if pa_dep1 := getattr(UnityBuiltin, parent, None):
dp[parent] = len(pa_dep1.__annotations__)
else:
raise ValueError # XXX: Should NEVER happen
pa_dep1 = dp[parent]
cur_dep1 = pa_dep1
for dep, (i, field) in enumerate(
filter(lambda field: field[1].m_Level == 1, enumerate(fields))
):
if field.m_Level > 1:
continue # Nested
if dep < pa_dep1:
# Skip parent fields at lvl1
continue
name, type = field.m_Name, translate_type(
field.m_Type, typenames=classname_nodes | import_defs, feild_nexts=fields[i:], parent_name=clazz
)
emit_line(f"\t{declare_field(name, type, field.m_Type)}")
clazz_fields.append((name, type, field.m_Type))
cur_dep1 += 1
dp[clazz] = cur_dep1
else:
# No inheritance
emit_line(f"class {clazz}:")
for i, field in enumerate(fields):
if field.m_Level > 1:
continue # Nested
name, type = field.m_Name, translate_type(
field.m_Type, typenames=classname_nodes | import_defs, feild_nexts=fields[i:], parent_name=clazz
)
emit_line(f"\t{declare_field(name, type, field.m_Type)}")
clazz_fields.append((name, type))
dp[clazz] = len(fields)
if not clazz_fields:
# Empty class. Consider MRO
emit_line("\tpass")
def process_typetree(fullname_nodes: Dict[str, List[TypeTreeNode]], outdir: str):
handles: Dict[str, TextIOWrapper] = dict()
def __open(fname: str):
fname = os.path.join(outdir, fname)
if fname not in handles:
os.makedirs(os.path.dirname(fname), exist_ok=True)
handles[fname] = open(fname, "w")
return handles[fname]
namespaces = defaultdict(dict)
namespacesT = defaultdict(None)
logger.info("Pass 1: Building namespace")
for key in fullname_nodes:
fullkey = key.split(".")
if len(fullkey) == 1:
namespace, clazz = None, fullkey[0]
else:
namespace, clazz = fullkey[:-1], fullkey[-1]
namespace = ".".join(namespace)
namespaces[namespace][clazz] = fullname_nodes[key]
if clazz not in namespacesT:
namespacesT[clazz] = namespace
else:
logger.error(
f"Class {clazz} already defined in {namespacesT[clazz]} but found again in {namespace}"
)
logger.error(
f"Need manual intervention to resolve the conflict. Using first definition for now."
)
logger.info("Pass 2: Generating import graph")
# Build import graph
namespaceDeps = defaultdict(set)
for namespace, classname_nodes in namespaces.items():
for clazz, fields in classname_nodes.items():
for i, field in enumerate(fields):
if type(field) != TypeTreeNode:
field = fields[i] = TypeTreeNode(**field)
ftype = translate_type(field.m_Type, strip=True, fallback=False)
if ftype in namespacesT and namespacesT[ftype] != namespace:
namespaceDeps[namespace].add(ftype)
logger.info("Pass 3: Emitting namespace as Python modules")
__open("__init__.py").write(HEADER)
# XXX: This part can be trivally parallelized
for namespace, classname_nodes in sorted(
namespaces.items(), key=lambda x: x[0].count(".") if x[0] else 0
):
# CubismTaskHandler -> generated/__init__.py
# Live2D.Cubism.Core.CubismMoc -> generated/Live2D/Cubism/Core/__init__.py
if namespace:
ndots = namespace.count(".") + 2
dotss = "." * ndots
f = __open(os.path.join(*namespace.split("."), "__init__.py"))
deps = {k: namespacesT[k] for k in namespaceDeps[namespace]}
deps = dict(sorted(deps.items()))
process_namespace(f, classname_nodes, namespace, dotss, deps)
else:
f = __open("__init__.py")
process_namespace(f, classname_nodes, namespace)
import re, logging
from TypeTreeGeneratorAPI import TypeTreeGenerator
def __main__():
parser = argparse.ArgumentParser()
parser.add_argument(
"--unity-version",
help="Unity version to use for typetree generation",
default="2022.3.21f1",
)
parser.add_argument(
"--filter",
help="Filter classnames by regex",
default=".*",
)
parser.add_argument(
"--json",
help="[JSON] Load tree dump in json format {str[fullname]: List[TypeTreeNode]},...",
)
parser.add_argument(
"--asm-dir",
help="[Asm] Load typetree dump from game assembly DLL folder",
type=str
)
parser.add_argument(
"--il2cpp",
help="[IL2CPP] Load typetree dump from IL2CPP binaries",
type=str
)
parser.add_argument(
"--metadata",
help="[IL2CPP] Load typetree dump from metadata files",
type=str
)
parser.add_argument(
"--log-level",
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
default="WARNING",
)
parser.add_argument(
"--outdir",
help="Output directory for generated code",
default="generated",
)
args = parser.parse_args()
logging.basicConfig(level=args.log_level)
shutil.rmtree(args.outdir, ignore_errors=True)
os.makedirs(args.outdir, exist_ok=True)
typetree = dict()
gen = TypeTreeGenerator(args.unity_version, "AssetStudio")
def populate_gen():
# https://github.com/UnityPy-Org/TypeTreeGeneratorAPI/pull/1
for asm,clz in gen.get_class_definitions():
try:
node = gen.get_nodes_as_json(asm, clz)
node = json.loads(node)
# https://github.com/UnityPy-Org/TypeTreeGeneratorAPI/pull/4
if node and node[0]["m_Level"] != 0:
node = Typetree_MonoBehaviour + node
typetree[clz] = node
except Exception as e:
logger.warning(f"Skipping nodes for {asm}.{clz}: {e}")
if args.asm_dir:
print("Loading .NET Assemblies", args.asm_dir)
for dll in os.listdir(args.asm_dir):
if not dll.lower().endswith(".dll"):
continue
try:
print(f"Loading {dll}")
gen.load_dll(open(os.path.join(args.asm_dir, dll), "rb").read())
except Exception as e:
logger.warning(f"Skipping {dll}: {e}")
populate_gen()
elif args.il2cpp and args.metadata:
print("Loading IL2CPP", args.il2cpp, args.metadata)
gen.load_il2cpp(open(args.il2cpp, "rb").read(), open(args.metadata, "rb").read())
populate_gen()
elif args.json:
print("Loading JSON", args.json)
with open(args.json, "r") as f:
typetree = json.load(f)
else:
raise ValueError("No valid input source specified.")
if typetree:
with open(".typetree.json", "w") as f:
json.dump(typetree, f, indent=4)
regex = re.compile(args.filter)
typetree = {k: v for k, v in typetree.items() if regex.match(k)}
process_typetree(typetree, args.outdir)
return 0
return -1
import sys
if __name__ == "__main__":
sys.exit(__main__())