22import os
33from io import BytesIO
44from pathlib import Path
5- from typing import Callable , Dict , List , Union
5+ from typing import Callable , Dict , List , Optional , Tuple , Union
66
77import UnityPy
88from UnityPy .classes import (
1919 Texture2D ,
2020)
2121from UnityPy .enums .ClassIDType import ClassIDType
22+ from UnityPy .files import SerializedFile
2223
2324
2425def export_obj (
@@ -27,8 +28,8 @@ def export_obj(
2728 append_name : bool = False ,
2829 append_path_id : bool = False ,
2930 export_unknown_as_typetree : bool = False ,
30- asset_filter : Callable [[Object ], bool ] = None ,
31- ) -> List [int ]:
31+ asset_filter : Optional [ Callable [[Object ], bool ] ] = None ,
32+ ) -> List [Tuple [ SerializedFile , int ] ]:
3233 """Exports the given object to the given filepath.
3334
3435 Args:
@@ -76,8 +77,8 @@ def extract_assets(
7677 ignore_first_container_dirs : int = 0 ,
7778 append_path_id : bool = False ,
7879 export_unknown_as_typetree : bool = False ,
79- asset_filter : Callable [[Object ], bool ] = None ,
80- ) -> List [int ]:
80+ asset_filter : Optional [ Callable [[Object ], bool ] ] = None ,
81+ ) -> List [Tuple [ SerializedFile , int ] ]:
8182 """Extracts some or all assets from the given source.
8283
8384 Args:
@@ -90,7 +91,7 @@ def extract_assets(
9091 asset_filter (func(object)->bool, optional): Determines whether to export an object. Defaults to all objects.
9192
9293 Returns:
93- List[int]: [description]
94+ List[Tuple[SerializedFile, int] ]: [description]
9495 """
9596 # load source
9697 env = UnityPy .load (src )
@@ -153,15 +154,15 @@ def defaulted_export_index(type: ClassIDType):
153154###############################################################################
154155
155156
156- def exportTextAsset (obj : TextAsset , fp : str , extension : str = ".txt" ) -> List [int ]:
157+ def exportTextAsset (obj : TextAsset , fp : str , extension : str = ".txt" ) -> List [Tuple [ SerializedFile , int ] ]:
157158 if not extension :
158159 extension = ".txt"
159160 with open (f"{ fp } { extension } " , "wb" ) as f :
160161 f .write (obj .m_Script .encode ("utf-8" , "surrogateescape" ))
161162 return [(obj .assets_file , obj .object_reader .path_id )]
162163
163164
164- def exportFont (obj : Font , fp : str , extension : str = "" ) -> List [int ]:
165+ def exportFont (obj : Font , fp : str , extension : str = "" ) -> List [Tuple [ SerializedFile , int ] ]:
165166 # TODO - export glyphs
166167 if obj .m_FontData :
167168 extension = ".ttf"
@@ -172,15 +173,15 @@ def exportFont(obj: Font, fp: str, extension: str = "") -> List[int]:
172173 return [(obj .assets_file , obj .object_reader .path_id )]
173174
174175
175- def exportMesh (obj : Mesh , fp : str , extension = ".obj" ) -> List [int ]:
176+ def exportMesh (obj : Mesh , fp : str , extension = ".obj" ) -> List [Tuple [ SerializedFile , int ] ]:
176177 if not extension :
177178 extension = ".obj"
178179 with open (f"{ fp } { extension } " , "wt" , encoding = "utf8" , newline = "" ) as f :
179180 f .write (obj .export ())
180181 return [(obj .assets_file , obj .object_reader .path_id )]
181182
182183
183- def exportShader (obj : Shader , fp : str , extension = ".txt" ) -> List [int ]:
184+ def exportShader (obj : Shader , fp : str , extension = ".txt" ) -> List [Tuple [ SerializedFile , int ] ]:
184185 if not extension :
185186 extension = ".txt"
186187 with open (f"{ fp } { extension } " , "wt" , encoding = "utf8" , newline = "" ) as f :
@@ -190,7 +191,7 @@ def exportShader(obj: Shader, fp: str, extension=".txt") -> List[int]:
190191
191192def exportMonoBehaviour (
192193 obj : Union [MonoBehaviour , Object ], fp : str , extension : str = ""
193- ) -> List [int ]:
194+ ) -> List [Tuple [ SerializedFile , int ] ]:
194195 export = None
195196
196197 if obj .object_reader .serialized_type .node :
@@ -224,7 +225,7 @@ def exportMonoBehaviour(
224225 return [(obj .assets_file , obj .object_reader .path_id )]
225226
226227
227- def exportAudioClip (obj : AudioClip , fp : str , extension : str = "" ) -> List [int ]:
228+ def exportAudioClip (obj : AudioClip , fp : str , extension : str = "" ) -> List [Tuple [ SerializedFile , int ] ]:
228229 samples = obj .samples
229230 if len (samples ) == 0 :
230231 pass
@@ -239,7 +240,7 @@ def exportAudioClip(obj: AudioClip, fp: str, extension: str = "") -> List[int]:
239240 return [(obj .assets_file , obj .object_reader .path_id )]
240241
241242
242- def exportSprite (obj : Sprite , fp : str , extension : str = ".png" ) -> List [int ]:
243+ def exportSprite (obj : Sprite , fp : str , extension : str = ".png" ) -> List [Tuple [ SerializedFile , int ] ]:
243244 if not extension :
244245 extension = ".png"
245246 obj .image .save (f"{ fp } { extension } " )
@@ -254,16 +255,15 @@ def exportSprite(obj: Sprite, fp: str, extension: str = ".png") -> List[int]:
254255 return exported
255256
256257
257- def exportTexture2D (obj : Texture2D , fp : str , extension : str = ".png" ) -> List [int ]:
258+ def exportTexture2D (obj : Texture2D , fp : str , extension : str = ".png" ) -> List [Tuple [ SerializedFile , int ] ]:
258259 if not extension :
259260 extension = ".png"
260261 if obj .m_Width :
261262 # textures can be empty
262263 obj .image .save (f"{ fp } { extension } " )
263264 return [(obj .assets_file , obj .path_id )]
264265
265-
266- def exportGameObject (obj : GameObject , fp : str , extension : str = "" ) -> List [int ]:
266+ def exportGameObject (obj : GameObject , fp : str , extension : str = "" ) -> List [Tuple [SerializedFile , int ]]:
267267 exported = [(obj .assets_file , obj .path_id )]
268268 refs = crawl_obj (obj )
269269 if refs :
@@ -299,7 +299,7 @@ def exportGameObject(obj: GameObject, fp: str, extension: str = "") -> List[int]
299299MONOBEHAVIOUR_TYPETREES : Dict ["Assembly-Name.dll" , Dict ["Class-Name" , List [Dict ]]] = {}
300300
301301
302- def crawl_obj (obj : Object , ret : dict = None ) -> Dict [int , Union [Object , PPtr ]]:
302+ def crawl_obj (obj : Object , ret : Optional [ dict ] = None ) -> Dict [int , Union [Object , PPtr ]]:
303303 """Crawls through the data struture of the object and returns a list of all the components."""
304304 if not ret :
305305 ret = {}
0 commit comments