44import platform
55from UnityPy .streams import EndianBinaryWriter
66
7- # dll init
8- ROOT = os .path .dirname (os .path .dirname (os .path .realpath (__file__ )))
9- LIB_PATH = os .path .join (ROOT , "lib" , "FMOD" , platform .system (), platform .architecture ()[0 ])
10- if platform .system () == 'Windows' :
11- _dll = WinDLL (os .path .join (LIB_PATH , "fmod.dll" ))
12- elif platform .system () == "Linux" :
13- _dll = CDLL (os .path .join (LIB_PATH , "libfmod.so" ))
14- elif platform .system () == "Darwin" :
15- _dll = CDLL (os .path .join (LIB_PATH , "libfmod.dylib" ))
16- else :
17- print (
18- "UnityPy/export/AudioClipConverter - Warning: unknown system\n If you want to export AudioClips, you have to set UnityPy.export.AudioClipConverter._dll yourself." )
7+ _dll = None
8+
9+
10+ def load_fmod_library ():
11+ global _dll
12+ if _dll is not None :
13+ return
14+
15+ ROOT = os .path .dirname (os .path .dirname (os .path .realpath (__file__ )))
16+
17+ # determine system - Windows, Darwin, Linux, Android
18+ system = platform .system ()
19+ if system == "Linux" and "ANDROID_BOOTLOGO" in os .environ :
20+ system = "Android"
21+ # determine architecture
22+ machine = platform .machine ()
23+ arch = platform .architecture ()[0 ]
24+
25+ if system in ["Windows" , "Darwin" ]:
26+ if arch == "32bit" :
27+ arch = "x86"
28+ elif arch == "64bit" :
29+ arch = "x64"
30+ elif system == "Linux" :
31+ # Raspberry Pi and Linux on arm projects
32+ if "arm" in machine :
33+ if arch == "32bit" :
34+ arch = "armhf" if machine .endswith ("l" ) else "arm"
35+ elif arch == "64bit" :
36+ # Raise an exception for now; Once it gets supported by FMOD we can just modify the code here
37+ _dll = False
38+ raise NotImplementedError ("ARM64 not supported by FMOD.\n Use a 32bit python version." )
39+ elif arch == "32bit" :
40+ arch = "x86"
41+ elif arch == "64bit" :
42+ arch = "x86_64"
43+ else :
44+ _dll = False
45+ raise NotImplementedError ("Couldn't find a correct FMOD library for your system ({system} - {arch})." )
46+ try :
47+ # build path and load library
48+ LIB_PATH = os .path .join (ROOT , "lib" , "FMOD" , system , arch )
49+ if system == 'Windows' :
50+ _dll = WinDLL (os .path .join (LIB_PATH , "fmod.dll" ))
51+ elif system in ["Linux" , "Android" ]:
52+ _dll = CDLL (os .path .join (LIB_PATH , "libfmod.so" ))
53+ elif system == "Darwin" :
54+ _dll = CDLL (os .path .join (LIB_PATH , "libfmod.dylib" ))
55+ except Exception as e :
56+ raise ImportError (
57+ f"Failed to import the fmod library - Exception: { e } .\
58+ If you want to export AudioClips, you have to set UnityPy.export.AudioClipConverter._dll yourself."
59+ )
1960
2061
2162def extract_audioclip_samples (audio ) -> dict :
@@ -25,11 +66,10 @@ def extract_audioclip_samples(audio) -> dict:
2566 :return: {filename : sample(bytes)}
2667 :rtype: dict
2768 """
28-
2969 if not audio .m_AudioData :
3070 # eg. StreamedResource not available
3171 return {}
32-
72+
3373 magic = memoryview (audio .m_AudioData )[:4 ]
3474 if magic == b'OggS' :
3575 return {'%s.ogg' % audio .name : audio .m_AudioData }
@@ -39,13 +79,17 @@ def extract_audioclip_samples(audio) -> dict:
3979
4080
4181def dump_samples (clip ):
82+ if _dll is None :
83+ load_fmod_library ()
84+ if not _dll :
85+ return {}
4286 # init system
4387 # system = pyfmodex.System()
4488 sys_ptr = c_void_p ()
4589 ckresult (_dll .FMOD_System_Create (byref (sys_ptr )))
4690 # system.init(1, INIT_FLAGS.NORMAL, None)
4791 ckresult (_dll .FMOD_System_Init (sys_ptr , clip .m_Channels , None , None ))
48-
92+
4993 # get sound
5094 exinfo = byref (CREATESOUNDEXINFO (length = clip .m_Size ))
5195 # sound = system.create_sound(bytes(clip.m_AudioData),mode=MODE.OPENMEMORY,exinfo=exinfo)
@@ -63,7 +107,7 @@ def dump_samples(clip):
63107 subsound = sound .get_subsound (i )
64108 samples [filename ] = subsound_to_wav (subsound )
65109 subsound .release ()
66-
110+
67111 sound .release ()
68112 # system.release()
69113 ckresult (_dll .FMOD_System_Release (sys_ptr ))
@@ -76,7 +120,7 @@ def subsound_to_wav(subsound):
76120 channels = subsound .format .channels
77121 bits = subsound .format .bits
78122 sample_rate = int (subsound .default_frequency )
79-
123+
80124 # write to buffer
81125 w = EndianBinaryWriter (endian = "<" )
82126 # riff chucnk
@@ -111,7 +155,7 @@ def subsound_to_wav(subsound):
111155
112156class CREATESOUNDEXINFO (Structure ):
113157 _fields_ = [("cbsize" , c_int ), ("length" , c_uint )]
114-
158+
115159 def __init__ (self , * args , ** kwargs ):
116160 Structure .__init__ (self , * args , ** kwargs )
117161 self .cbsize = sizeof (self )
@@ -128,27 +172,27 @@ def __init__(self, ptr):
128172 :param ptr: The pointer representing this object.
129173 """
130174 self ._ptr = ptr
131-
175+
132176 def _call_fmod (self , funcname , * args ):
133177 result = getattr (_dll , funcname )(self ._ptr , * args )
134178 ckresult (result )
135-
179+
136180 @property
137181 def num_subsounds (self ):
138182 num = c_int ()
139183 self ._call_fmod ("FMOD_Sound_GetNumSubSounds" , byref (num ))
140184 return num .value
141-
185+
142186 def get_subsound (self , index ):
143187 sh_ptr = c_void_p ()
144188 self ._call_fmod ("FMOD_Sound_GetSubSound" , index , byref (sh_ptr ))
145189 return Sound (sh_ptr )
146-
190+
147191 def get_length (self , ltype ):
148192 len = c_uint ()
149193 self ._call_fmod ("FMOD_Sound_GetLength" , byref (len ), int (ltype ))
150194 return len .value
151-
195+
152196 @property
153197 def format (self ):
154198 type = c_int ()
@@ -158,14 +202,14 @@ def format(self):
158202 self ._call_fmod ("FMOD_Sound_GetFormat" , byref (type ),
159203 byref (format ), byref (channels ), byref (bits ))
160204 return so (type = type .value , format = format .value , channels = channels .value , bits = bits .value )
161-
205+
162206 @property
163207 def default_frequency (self ):
164208 freq = c_float ()
165209 pri = c_int ()
166210 self ._call_fmod ("FMOD_Sound_GetDefaults" , byref (freq ), byref (pri ))
167211 return freq .value
168-
212+
169213 def lock (self , offset , length ):
170214 ptr1 = c_void_p ()
171215 len1 = c_uint ()
@@ -174,10 +218,10 @@ def lock(self, offset, length):
174218 ckresult (_dll .FMOD_Sound_Lock (self ._ptr , offset , length ,
175219 byref (ptr1 ), byref (ptr2 ), byref (len1 ), byref (len2 )))
176220 return (ptr1 , len1 ), (ptr2 , len2 )
177-
221+
178222 def release (self ):
179223 self ._call_fmod ("FMOD_Sound_Release" )
180-
224+
181225 def unlock (self , i1 , i2 ):
182226 """I1 and I2 are tuples of form (ptr, len)."""
183227 ckresult (_dll .FMOD_Sound_Unlock (self ._ptr , i1 [0 ], i2 [0 ], i1 [1 ], i2 [1 ]))
@@ -193,7 +237,7 @@ class FmodError(Exception):
193237 def __init__ (self , result ):
194238 self .result = result
195239 self .message = result .name .replace ("_" , " " )
196-
240+
197241 def __str__ (self ):
198242 return self .message
199243
0 commit comments