OSDN Git Service

Undid HDRP
[mindgames/Mindgames_main.git] / Mindgames / Assets / Plugins / FMOD / src / Runtime / RuntimeUtils.cs
1 using System;
2 using UnityEngine;
3 #if UNITY_EDITOR
4 using UnityEditor;
5 #endif
6
7 namespace FMODUnity
8 {
9     public class EventNotFoundException : Exception
10     {
11         public Guid Guid;
12         public string Path;
13         public EventNotFoundException(string path)
14             : base("[FMOD] Event not found '" + path + "'")
15         {
16             Path = path;
17         }
18
19         public EventNotFoundException(Guid guid)
20             : base("[FMOD] Event not found " + guid.ToString("b") + "")
21         {
22             Guid = guid;
23         }
24     }
25
26     public class BusNotFoundException : Exception
27     {
28         public string Path;
29         public BusNotFoundException(string path)
30             : base("[FMOD] Bus not found '" + path + "'")
31         {
32             Path = path;
33         }
34     }
35
36     public class VCANotFoundException : Exception
37     {
38         public string Path;
39         public VCANotFoundException(string path)
40             : base("[FMOD] VCA not found '" + path + "'")
41         {
42             Path = path;
43         }
44     }
45
46     public class BankLoadException : Exception
47     {
48         public string Path;
49         public FMOD.RESULT Result;
50
51         public BankLoadException(string path, FMOD.RESULT result)
52             : base(string.Format("[FMOD] Could not load bank '{0}' : {1} : {2}", path, result.ToString(), FMOD.Error.String(result)))
53         {
54             Path = path;
55             Result = result;
56         }
57         public BankLoadException(string path, string error)
58             : base(string.Format("[FMOD] Could not load bank '{0}' : {1}", path, error))
59         {
60             Path = path;
61             Result = FMOD.RESULT.ERR_INTERNAL;
62         }
63     }
64
65     public class SystemNotInitializedException : Exception
66     {
67         public FMOD.RESULT Result;
68         public string Location;
69
70         public SystemNotInitializedException(FMOD.RESULT result, string location)
71             : base(string.Format("[FMOD] Initialization failed : {2} : {0} : {1}", result.ToString(), FMOD.Error.String(result), location))
72         {
73             Result = result;
74             Location = location;
75         }
76
77         public SystemNotInitializedException(Exception inner)
78             : base("[FMOD] Initialization failed", inner)
79         {
80         }
81     }
82
83     public enum EmitterGameEvent : int
84     {
85         None,
86         ObjectStart,
87         ObjectDestroy,
88         TriggerEnter,
89         TriggerExit,
90         TriggerEnter2D,
91         TriggerExit2D,
92         CollisionEnter,
93         CollisionExit,
94         CollisionEnter2D,
95         CollisionExit2D,
96         ObjectEnable,
97         ObjectDisable,
98         MouseEnter,
99         MouseExit,
100         MouseDown,
101         MouseUp,
102     }
103
104     public enum LoaderGameEvent : int
105     {
106         None,
107         ObjectStart,
108         ObjectDestroy,
109         TriggerEnter,
110         TriggerExit,
111         TriggerEnter2D,
112         TriggerExit2D,
113     }
114
115     public static class RuntimeUtils
116     {
117         public static string GetCommonPlatformPath(string path)
118         {
119             if (string.IsNullOrEmpty(path))
120             {
121                 return path;
122             }
123
124             return path.Replace('\\', '/');
125         }
126
127         public static FMOD.VECTOR ToFMODVector(this Vector3 vec)
128         {
129             FMOD.VECTOR temp;
130             temp.x = vec.x;
131             temp.y = vec.y;
132             temp.z = vec.z;
133
134             return temp;
135         }
136
137         public static FMOD.ATTRIBUTES_3D To3DAttributes(this Vector3 pos)
138         {
139             FMOD.ATTRIBUTES_3D attributes = new FMOD.ATTRIBUTES_3D();
140             attributes.forward = ToFMODVector(Vector3.forward);
141             attributes.up = ToFMODVector(Vector3.up);
142             attributes.position = ToFMODVector(pos);
143
144             return attributes;
145         }
146
147         public static FMOD.ATTRIBUTES_3D To3DAttributes(this Transform transform)
148         {
149             FMOD.ATTRIBUTES_3D attributes = new FMOD.ATTRIBUTES_3D();
150             attributes.forward = transform.forward.ToFMODVector();
151             attributes.up = transform.up.ToFMODVector();
152             attributes.position = transform.position.ToFMODVector();
153
154             return attributes;
155         }
156
157         public static FMOD.ATTRIBUTES_3D To3DAttributes(Transform transform, Rigidbody rigidbody = null)
158         {
159             FMOD.ATTRIBUTES_3D attributes = transform.To3DAttributes();
160
161             if (rigidbody)
162             {
163                 attributes.velocity = rigidbody.velocity.ToFMODVector();
164             }
165
166             return attributes;
167         }
168
169         public static FMOD.ATTRIBUTES_3D To3DAttributes(GameObject go, Rigidbody rigidbody = null)
170         {
171             FMOD.ATTRIBUTES_3D attributes = go.transform.To3DAttributes();
172
173             if (rigidbody)
174             {
175                 attributes.velocity = rigidbody.velocity.ToFMODVector();
176             }
177
178             return attributes;
179         }
180
181         public static FMOD.ATTRIBUTES_3D To3DAttributes(Transform transform, Rigidbody2D rigidbody)
182         {
183             FMOD.ATTRIBUTES_3D attributes = transform.To3DAttributes();
184
185             if (rigidbody)
186             {
187                 FMOD.VECTOR vel;
188                 vel.x = rigidbody.velocity.x;
189                 vel.y = rigidbody.velocity.y;
190                 vel.z = 0;
191                 attributes.velocity = vel;
192             }
193
194             return attributes;
195         }
196
197         public static FMOD.ATTRIBUTES_3D To3DAttributes(GameObject go, Rigidbody2D rigidbody)
198         {
199             FMOD.ATTRIBUTES_3D attributes = go.transform.To3DAttributes();
200
201             if (rigidbody)
202             {
203                 FMOD.VECTOR vel;
204                 vel.x = rigidbody.velocity.x;
205                 vel.y = rigidbody.velocity.y;
206                 vel.z = 0;
207                 attributes.velocity = vel;
208             }
209
210             return attributes;
211         }
212
213         // Internal Helper Functions
214         internal static FMODPlatform GetCurrentPlatform()
215         {
216             #if UNITY_EDITOR
217             return FMODPlatform.PlayInEditor;
218             #elif UNITY_STANDALONE_WIN
219             return FMODPlatform.Windows;
220             #elif UNITY_STANDALONE_OSX
221             return FMODPlatform.Mac;
222             #elif UNITY_STANDALONE_LINUX
223             return FMODPlatform.Linux;
224             #elif UNITY_TVOS
225             return FMODPlatform.AppleTV;
226             #elif UNITY_IOS
227             FMODPlatform result;
228             switch (UnityEngine.iOS.Device.generation)
229             {
230                 case UnityEngine.iOS.DeviceGeneration.iPad1Gen:
231                 case UnityEngine.iOS.DeviceGeneration.iPad2Gen:
232                 case UnityEngine.iOS.DeviceGeneration.iPad3Gen:
233                 case UnityEngine.iOS.DeviceGeneration.iPadMini1Gen:
234                 case UnityEngine.iOS.DeviceGeneration.iPhone:
235                 case UnityEngine.iOS.DeviceGeneration.iPhone3G:
236                 case UnityEngine.iOS.DeviceGeneration.iPhone3GS:
237                 case UnityEngine.iOS.DeviceGeneration.iPhone4:
238                 case UnityEngine.iOS.DeviceGeneration.iPhone4S:
239                     result = FMODPlatform.MobileLow;
240                 break;
241             default:
242                 result = FMODPlatform.MobileHigh;
243                 break;
244             }
245
246             UnityEngine.Debug.Log(String.Format("FMOD Studio: Device {0} classed as {1}", SystemInfo.deviceModel, result.ToString()));
247             return result;
248             #elif UNITY_ANDROID
249             FMODPlatform result;
250             if (SystemInfo.processorCount <= 2)
251             {
252                 result = FMODPlatform.MobileLow;
253             }
254             else if (SystemInfo.processorCount >= 8)
255             {
256                 result = FMODPlatform.MobileHigh;
257             }
258             else
259             {
260                 // check the clock rate on quad core systems
261                 string freqinfo = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";
262                 try
263                 {
264                     using (global::System.IO.TextReader reader = new global::System.IO.StreamReader(freqinfo))
265                     {
266                         string line = reader.ReadLine();
267                         int khz = Int32.Parse(line) / 1000;
268                         if (khz >= 1600)
269                         {
270                             result = FMODPlatform.MobileHigh;
271                         }
272                         else
273                         {
274                             result = FMODPlatform.MobileLow;
275                         }
276                     }
277                 }
278                 catch
279                 {
280                     result = FMODPlatform.MobileLow;
281                 }
282             }
283             
284             UnityEngine.Debug.Log(String.Format("[FMOD] Device {0} classed as {1}", SystemInfo.deviceModel, result.ToString()));
285             return result;
286             #elif UNITY_WINRT_8_1
287             FMODPlatform result;
288             if (SystemInfo.processorCount <= 2)
289             {
290                 result = FMODPlatform.MobileLow;
291             }
292             else
293             {
294                 result = FMODPlatform.MobileHigh;
295             }
296
297             UnityEngine.Debug.Log(String.Format("[FMOD] Device {0} classed as {1}", SystemInfo.deviceModel, result.ToString()));
298             return result;
299
300             #elif UNITY_PS4
301             return FMODPlatform.PS4;
302             #elif UNITY_XBOXONE
303             return FMODPlatform.XboxOne;
304             #elif UNITY_WSA_10_0
305             return FMODPlatform.UWP;
306             #elif UNITY_SWITCH
307             return FMODPlatform.Switch;
308             #elif UNITY_WEBGL
309             return FMODPlatform.WebGL;
310             #elif UNITY_STADIA
311             return FMODPlatform.Stadia;
312             #endif
313         }
314
315         const string BankExtension = ".bank";
316         internal static string GetBankPath(string bankName)
317         {
318             #if UNITY_EDITOR
319             // For play in editor use original asset location because streaming asset folder will contain platform specific banks
320             string bankFolder = Settings.Instance.SourceBankPath;
321             if (Settings.Instance.HasPlatforms)
322             {
323                 bankFolder = global::System.IO.Path.Combine(bankFolder, Settings.Instance.GetBankPlatform(FMODPlatform.PlayInEditor));
324             } 
325             #elif UNITY_ANDROID
326             string bankFolder = null;
327             if (System.IO.Path.GetExtension(Application.dataPath) == ".apk")
328             {
329                 bankFolder = "file:///android_asset";
330             }
331             else
332             {
333                 bankFolder = String.Format("jar:file://{0}!/assets", Application.dataPath);
334             }
335             #elif UNITY_WINRT_8_1 || UNITY_WSA_10_0
336             string bankFolder = "ms-appx:///Data/StreamingAssets";
337             #else
338             string bankFolder = Application.streamingAssetsPath;
339             #endif
340
341             // Special case for Switch, remove / at start if needed.
342             #if UNITY_SWITCH
343             if (bankFolder[0] == '/')
344                 bankFolder = bankFolder.Substring(1);
345             #endif
346
347             if (System.IO.Path.GetExtension(bankName) != BankExtension)
348             {
349                 return string.Format("{0}/{1}.bank", bankFolder, bankName);
350             }
351             else
352             {
353                 return string.Format("{0}/{1}", bankFolder, bankName);
354             }
355         }
356
357         internal static string GetPluginPath(string pluginName)
358         {
359             #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_XBOXONE || UNITY_WINRT_8_1 || UNITY_WSA_10_0
360                 string pluginFileName = pluginName + ".dll";
361             #elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
362                 string pluginFileName = pluginName + ".bundle";
363             #elif UNITY_PS4
364                 string pluginFileName = pluginName + ".prx";
365             #elif UNITY_ANDROID || UNITY_STANDALONE_LINUX
366                 string pluginFileName = "lib" + pluginName + ".so";
367             #elif UNITY_WEBGL
368                 string pluginFileName = pluginName + ".bc";
369             #endif
370
371             string fmodLibPath = "/Plugins/FMOD/lib";
372             #if UNITY_EDITOR_WIN && UNITY_EDITOR_64
373                 string pluginFolder = Application.dataPath + fmodLibPath + "/win/X86_64/";
374             #elif UNITY_EDITOR_WIN
375                 string pluginFolder = Application.dataPath + fmodLibPath + "/win/X86/";
376             #elif UNITY_EDITOR_OSX
377                 string pluginFolder = Application.dataPath + fmodLibPath + "/mac/";
378             #elif UNITY_STANDALONE_WIN || UNITY_PS4 || UNITY_XBOXONE || UNITY_STANDALONE_OSX || UNITY_WEBGL
379                 string pluginFolder = Application.dataPath + "/Plugins/";
380             #elif UNITY_STANDALONE_LINUX
381                 string pluginFolder = Application.dataPath + fmodLibPath + ((IntPtr.Size == 8) ? "/linux/x86_64/" : "/linux/x86/");
382             #elif UNITY_WSA || UNITY_ANDROID
383                 string pluginFolder = "";
384             #else
385                 string pluginFileName = "";
386                 string pluginFolder = "";
387             #endif
388
389             return pluginFolder + pluginFileName;
390         }
391
392         public static void EnforceLibraryOrder()
393         {
394             #if UNITY_ANDROID && !UNITY_EDITOR
395
396             AndroidJavaClass jSystem = new AndroidJavaClass("java.lang.System");
397             jSystem.CallStatic("loadLibrary", FMOD.VERSION.dll);
398             jSystem.CallStatic("loadLibrary", FMOD.Studio.STUDIO_VERSION.dll);
399
400             #endif
401
402             // Call a function in fmod.dll to make sure it's loaded before fmodstudio.dll
403             int temp1, temp2;
404             FMOD.Memory.GetStats(out temp1, out temp2);
405
406             Guid temp3;
407             FMOD.Studio.Util.parseID("", out temp3);
408         }
409
410         #if UNITY_EDITOR
411         public static FMODPlatform GetEditorFMODPlatform()
412         {
413             switch (EditorUserBuildSettings.activeBuildTarget)
414             {
415                 case BuildTarget.Android:
416                     return FMODPlatform.Android;
417                 case BuildTarget.iOS:
418                     return FMODPlatform.iOS;
419                 case BuildTarget.PS4:
420                     return FMODPlatform.PS4;
421                 #if !UNITY_2019_2_OR_NEWER
422                 case BuildTarget.StandaloneLinux:
423                 #endif
424                 case BuildTarget.StandaloneLinuxUniversal:
425                 case BuildTarget.StandaloneLinux64:
426                     return FMODPlatform.Linux;
427                 case BuildTarget.StandaloneOSX:
428                     return FMODPlatform.Mac;
429                 case BuildTarget.StandaloneWindows:
430                 case BuildTarget.StandaloneWindows64:
431                     return FMODPlatform.Windows;
432                 case BuildTarget.XboxOne:
433                     return FMODPlatform.XboxOne;
434                 case BuildTarget.WSAPlayer:
435                     return FMODPlatform.UWP;
436                 case BuildTarget.tvOS:
437                     return FMODPlatform.AppleTV;
438                 #if UNITY_SWITCH
439                 case BuildTarget.Switch:
440                     return FMODPlatform.Switch;
441                 #endif
442                 #if UNITY_WEBGL
443                 case BuildTarget.WebGL:
444                     return FMODPlatform.WebGL;
445                 #endif
446                 #if UNITY_STADIA
447                 case BuildTarget.Stadia:
448                     return FMODPlatform.Stadia;
449                 #endif
450                 default:
451                     return FMODPlatform.None;
452             }
453         }
454         #endif
455     }
456 }