Skip to content

Commit f0b3315

Browse files
committed
Better type hints
1 parent cecd1ff commit f0b3315

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ Used in
1313
Example usage in `rla_transport_defines`
1414
```python
1515
env = UnityPy.load(...)
16-
from generated.Sekai.Streaming import TransportDefine
17-
from generated import UTTCGen_AsInstance
16+
from generated import UTTCGen_AsInstance, UTTCGen_GetClass
1817

1918
for reader in filter(lambda x: x.type == ClassIDType.MonoBehaviour, env.objects):
2019
name = reader.peek_name()
21-
if name.startswith("TransportDefine"):
22-
instance = UTTCGen_AsInstance(reader, "Sekai.Streaming.TransportDefine")
23-
# ...or `instance = UTTCGen_AsInstance(reader)` is `m_Script` field is accessible
24-
instance : TransportDefine
20+
if name.startswith("TransportDefine"):
21+
from generated.Sekai.Streaming import TransportDefine
22+
# ...or TransportDefine = UTTCGen_GetClass("Sekai.Streaming.TransportDefine")
23+
# ...or TransportDefine = UTTCGen_GetClass(reader.read(check_read=False))
24+
instance = UTTCGen_AsInstance(TransportDefine, reader)
2525
print(f'"{name}":({instance.validBones}, {instance.validBlendShapes}),')
2626
# Possibly modify on the instance itself and saving it is also possible
2727
instance.validBones[0] = "BadBone"

UnityPyTypetreeCodegen/__main__.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@
6262
[
6363
"# fmt: off",
6464
"# Auto-generated by https://github.com/mos9527/UnityPyTypetreeCodegen",
65-
"" "from typing import List, Union, Optional, TypeVar",
65+
"" "from typing import List, Union, Optional, TypeVar, Type",
6666
"from UnityPy.files.ObjectReader import ObjectReader",
6767
"from UnityPy.classes import *",
6868
"from UnityPy.classes.math import (ColorRGBA, Matrix3x4f, Matrix4x4f, Quaternionf, Vector2f, Vector3f, Vector4f, float3, float4,)",
69-
'''T = TypeVar("T")
69+
'''
7070
UTTCG_Classes = dict()
7171
def UTTCGen(fullname: str, typetree: dict):
7272
"""dataclass-like decorator for typetree classess with nested type support
@@ -121,37 +121,40 @@ def __save(self):
121121
clazz.__init__ = __init__
122122
clazz.__repr__ = __repr__
123123
clazz.__typetree__ = typetree
124+
clazz.__fullname__ = fullname
124125
clazz.save = __save
125126
UTTCG_Classes[fullname] = clazz
126127
return clazz
127128
return __inner
128129
129130
# Helper functions
131+
def UTTCGen_GetClasss(src: MonoBehaviour | str) -> Type:
132+
"""Get the class definition from MonoBehaviour or a full type name."""
133+
if isinstance(src, MonoBehaviour):
134+
script = src.m_Script.read()
135+
src = script.m_ClassName
136+
if script.m_Namespace:
137+
src = f"{script.m_Namespace}.{src}"
138+
return UTTCG_Classes.get(src, None)
130139
131-
def UTTCGen_AsInstance(src: MonoBehaviour | ObjectReader, fullname: str = None) -> T:
140+
T = TypeVar("T")
141+
def UTTCGen_AsInstance(cls : Type[T], src: MonoBehaviour | ObjectReader) -> T:
132142
"""Instantiate a class from the typetree definition and the raw data.
133143
134144
In most cases, this is the function you want to use.
135145
It will read the typetree data from the MonoBehaviour instance and instantiate the class with the data.
136146
137147
Args:
138-
src (MonoBehaviour | ObjectReader): The MonoBehaviour instance or ObjectReader to read from.
139-
fullname (str): The full name of the class to read. If None, it will be read from the MonoBehaviour instance's `m_Script` property.
148+
cls: The class to instantiate. This should be a class that has been decorated with the UTTCGen decorator.
149+
src (MonoBehaviour | ObjectReader): The MonoBehaviour instance or ObjectReader to read from.
140150
141151
Returns:
142-
T: An instance of the class defined by the typetree.
152+
An instance of the class defined by the typetree.
143153
"""
144-
if not fullname and isinstance(src, MonoBehaviour):
145-
script = src.m_Script.read()
146-
fullname = script.m_ClassName
147-
if script.m_Namespace:
148-
fullname = f"{script.m_Namespace}.{fullname}"
149-
clazz = UTTCG_Classes.get(fullname, None)
150-
assert clazz is not None, f"Class definition for {fullname} not found"
151154
if isinstance(src, MonoBehaviour):
152155
src = src.object_reader
153-
raw_def = src.read_typetree(clazz.__typetree__, check_read=False)
154-
instance = clazz(object_reader=src, **raw_def)
156+
raw_def = src.read_typetree(cls.__typetree__, check_read=False)
157+
instance = cls(object_reader=src, **raw_def)
155158
return instance
156159
''',
157160
]

0 commit comments

Comments
 (0)