|
62 | 62 | [ |
63 | 63 | "# fmt: off", |
64 | 64 | "# 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", |
66 | 66 | "from UnityPy.files.ObjectReader import ObjectReader", |
67 | 67 | "from UnityPy.classes import *", |
68 | 68 | "from UnityPy.classes.math import (ColorRGBA, Matrix3x4f, Matrix4x4f, Quaternionf, Vector2f, Vector3f, Vector4f, float3, float4,)", |
69 | | - '''T = TypeVar("T") |
| 69 | + ''' |
70 | 70 | UTTCG_Classes = dict() |
71 | 71 | def UTTCGen(fullname: str, typetree: dict): |
72 | 72 | """dataclass-like decorator for typetree classess with nested type support |
@@ -121,37 +121,40 @@ def __save(self): |
121 | 121 | clazz.__init__ = __init__ |
122 | 122 | clazz.__repr__ = __repr__ |
123 | 123 | clazz.__typetree__ = typetree |
| 124 | + clazz.__fullname__ = fullname |
124 | 125 | clazz.save = __save |
125 | 126 | UTTCG_Classes[fullname] = clazz |
126 | 127 | return clazz |
127 | 128 | return __inner |
128 | 129 |
|
129 | 130 | # 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) |
130 | 139 |
|
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: |
132 | 142 | """Instantiate a class from the typetree definition and the raw data. |
133 | 143 |
|
134 | 144 | In most cases, this is the function you want to use. |
135 | 145 | It will read the typetree data from the MonoBehaviour instance and instantiate the class with the data. |
136 | 146 |
|
137 | 147 | 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. |
140 | 150 |
|
141 | 151 | Returns: |
142 | | - T: An instance of the class defined by the typetree. |
| 152 | + An instance of the class defined by the typetree. |
143 | 153 | """ |
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" |
151 | 154 | if isinstance(src, MonoBehaviour): |
152 | 155 | 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) |
155 | 158 | return instance |
156 | 159 | ''', |
157 | 160 | ] |
|
0 commit comments