UMPSettings.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. namespace UMP
  6. {
  7. [CreateAssetMenu(fileName = "UMPSettings", menuName = "UMP/UMPSettings")]
  8. [Serializable]
  9. public class UMPSettings : ScriptableObject
  10. {
  11. public enum Platforms
  12. {
  13. None = 1,
  14. Win = 2,
  15. Mac = 4,
  16. Linux = 8,
  17. WebGL = 16,
  18. Android = 32,
  19. iOS = 64
  20. }
  21. public enum BitModes
  22. {
  23. x86,
  24. x86_64
  25. }
  26. private const string MAC_APPS_FOLDER_NAME = "/Applications";
  27. private const string MAC_VLC_PACKAGE_NAME = "vlc.app";
  28. private const string MAC_LIBVLC_PACKAGE_NAME = "libvlc.bundle";
  29. private const string MAC_PACKAGE_LIB_PATH = @"Contents/MacOS/lib";
  30. private const string WIN_REG_KEY_X86 = @"SOFTWARE\WOW6432Node\VideoLAN\VLC";
  31. private const string WIN_REG_KEY_X86_64 = @"SOFTWARE\VideoLAN\VLC";
  32. private static string[] LIN_APPS_FOLDERS_PATHS = new string[] { "/usr/lib",
  33. "/usr/lib64",
  34. "/usr/lib/x86_64-linux-gnu/" };
  35. public const string SETTINGS_FILE_NAME = "UMPSettings";
  36. public const string ASSET_NAME = "UniversalMediaPlayer";
  37. public const string LIB_VLC_NAME = "libvlc";
  38. public const string LIB_VLC_CORE_NAME = "libvlccore";
  39. public const string DESKTOP_CATEGORY_NAME = "Desktop";
  40. public const string PLUGINS_FOLDER_NAME = "Plugins";
  41. private const string ASSETS_FOLDER_NAME = "Assets";
  42. #region Instance
  43. private static UMPSettings _instance;
  44. public static UMPSettings Instance
  45. {
  46. get
  47. {
  48. if (_instance == null)
  49. {
  50. _instance = Resources.Load<UMPSettings>(SETTINGS_FILE_NAME);
  51. if (_instance == null)
  52. Debug.LogError(string.Format("[UMPSetting] Could not find settings file '{0}' in UMP 'Resources' folder. " +
  53. "Try to correctly import UMP asset to your project or create the new settings file by click with right mouse on UMP 'Resources' folder and choose: 'Create'->'UMP'->'UMPSettings'.", SETTINGS_FILE_NAME));
  54. if (Application.isEditor)
  55. {
  56. if (!_instance.IsValidAssetPath)
  57. Debug.LogError("[UMPSetting] Asset path is not correct, please check the settings file in UMP 'Resources' folder.");
  58. }
  59. if ((RuntimePlatform & Desktop) == RuntimePlatform)
  60. {
  61. if (!ContainsLibVLC(_instance.LibrariesPath))
  62. Debug.LogError("[UMPSetting] Can't find LibVLC libraries, try to check the settings file in UMP 'Resources' folder.");
  63. }
  64. }
  65. return _instance;
  66. }
  67. }
  68. #endregion
  69. #region Asset Path
  70. [SerializeField]
  71. private string _assetPath = Path.Combine(ASSETS_FOLDER_NAME, ASSET_NAME).Replace(@"\", "/");
  72. /// <summary>
  73. /// Get/Set path to main asset folder.
  74. /// </summary>
  75. public string AssetPath
  76. {
  77. get
  78. {
  79. return _assetPath;
  80. }
  81. set
  82. {
  83. _assetPath = value;
  84. }
  85. }
  86. /// <summary>
  87. /// Check if main asset folder is valid.
  88. /// </summary>
  89. public bool IsValidAssetPath
  90. {
  91. get
  92. {
  93. return Directory.Exists(_assetPath) && Directory.GetFiles(_assetPath).Length > 0;
  94. }
  95. }
  96. #endregion
  97. #region Audio Source
  98. [SerializeField]
  99. private bool _useAudioSource = false;
  100. /// <summary>
  101. /// Check if needed to use Unity 'AudioSource' component (only for Desktop platforms).
  102. /// </summary>
  103. public bool UseAudioSource
  104. {
  105. get { return _useAudioSource; }
  106. }
  107. #endregion
  108. #region External Libraries
  109. [SerializeField]
  110. private bool _useExternalLibraries = false;
  111. /// <summary>
  112. /// Check if needed to use external libraries path (only for Desktop platforms).
  113. /// </summary>
  114. public bool UseExternalLibraries
  115. {
  116. get { return _useExternalLibraries; }
  117. }
  118. #endregion
  119. #region Libraries Path
  120. [SerializeField]
  121. private string _librariesPath = string.Empty;
  122. /// <summary>
  123. /// Get path to the liraries that will be used for media player (only for Desktop platforms).
  124. /// </summary>
  125. public string LibrariesPath
  126. {
  127. get
  128. {
  129. var path = GetLibrariesPath(RuntimePlatform, _useExternalLibraries);
  130. if (ContainsLibVLC(path))
  131. _librariesPath = path;
  132. else if (!ContainsLibVLC(_librariesPath))
  133. _librariesPath = string.Empty;
  134. return _librariesPath;
  135. }
  136. }
  137. #endregion
  138. #region Players Android
  139. [SerializeField]
  140. private PlayerOptionsAndroid.PlayerTypes _playersAndroid = PlayerOptionsAndroid.PlayerTypes.Native | PlayerOptionsAndroid.PlayerTypes.LibVLC;
  141. public PlayerOptionsAndroid.PlayerTypes PlayersAndroid
  142. {
  143. get { return _playersAndroid; }
  144. set
  145. {
  146. _playersAndroid = value;
  147. }
  148. }
  149. #endregion
  150. #region Players IPhone
  151. [SerializeField]
  152. private PlayerOptionsIPhone.PlayerTypes _playersIPhone = PlayerOptionsIPhone.PlayerTypes.Native | PlayerOptionsIPhone.PlayerTypes.FFmpeg;
  153. public PlayerOptionsIPhone.PlayerTypes PlayersIPhone
  154. {
  155. get { return _playersIPhone; }
  156. set
  157. {
  158. _playersIPhone = value;
  159. }
  160. }
  161. #endregion
  162. #region Exported Paths
  163. [SerializeField]
  164. private string[] _androidExportedPaths = new string[0];
  165. public string[] AndroidExportedPaths
  166. {
  167. get { return _androidExportedPaths; }
  168. set
  169. {
  170. _androidExportedPaths = value;
  171. }
  172. }
  173. #endregion
  174. #region Youtube Function Pattern
  175. [SerializeField]
  176. private string _youtubeDecryptFunction = @"\bc\s*&&\s*d\.set\([^,]+\s*,[^(]*\(([a-zA-Z0-9$]+)\(";
  177. public string YoutubeDecryptFunction
  178. {
  179. get { return _youtubeDecryptFunction; }
  180. }
  181. #endregion
  182. /// <summary>
  183. /// Returns the libraries path for specific platform.
  184. /// </summary>
  185. /// <param name="platform">Runtime platform</param>
  186. /// <param name="externalSpace">Use external space (for libraries that previously installed on your system)</param>
  187. /// <returns></returns>
  188. public string GetLibrariesPath(Platforms platform, bool externalSpace)
  189. {
  190. string librariesPath = string.Empty;
  191. if (platform != Platforms.None)
  192. {
  193. if (!externalSpace)
  194. {
  195. if (Application.isEditor)
  196. {
  197. librariesPath = Path.Combine(_assetPath, PLUGINS_FOLDER_NAME);
  198. librariesPath = Path.Combine(librariesPath, PlatformFolderName(platform));
  199. if (platform == Platforms.Win || platform == Platforms.Mac || platform == Platforms.Linux)
  200. librariesPath = Path.Combine(librariesPath, EditorBitModeFolderName);
  201. }
  202. else
  203. {
  204. librariesPath = Path.Combine(Application.dataPath, PLUGINS_FOLDER_NAME);
  205. if (platform == Platforms.Linux)
  206. librariesPath = Path.Combine(librariesPath, EditorBitModeFolderName);
  207. }
  208. if (platform == Platforms.Mac)
  209. librariesPath = Path.Combine(librariesPath, Path.Combine(MAC_LIBVLC_PACKAGE_NAME, MAC_PACKAGE_LIB_PATH));
  210. if (!Directory.Exists(librariesPath))
  211. librariesPath = string.Empty;
  212. }
  213. else
  214. {
  215. if (platform == Platforms.Win)
  216. {
  217. librariesPath = NativeInterop.ReadLocalRegKey(EditorBitMode == BitModes.x86 ? WIN_REG_KEY_X86 : WIN_REG_KEY_X86_64, "InstallDir");
  218. }
  219. if (platform == Platforms.Mac)
  220. {
  221. var appsFolderInfo = new DirectoryInfo(MAC_APPS_FOLDER_NAME);
  222. var packages = appsFolderInfo.GetDirectories();
  223. foreach (var package in packages)
  224. {
  225. if (package.FullName.ToLower().Contains(MAC_VLC_PACKAGE_NAME))
  226. librariesPath = Path.Combine(package.FullName, MAC_PACKAGE_LIB_PATH);
  227. }
  228. }
  229. if (platform == Platforms.Linux)
  230. {
  231. DirectoryInfo appsFolderInfo = null;
  232. foreach (var appFolder in LIN_APPS_FOLDERS_PATHS)
  233. {
  234. if (Directory.Exists(appFolder))
  235. appsFolderInfo = new DirectoryInfo(appFolder);
  236. if (appsFolderInfo != null)
  237. {
  238. var appsLibs = appsFolderInfo.GetFiles();
  239. foreach (var lib in appsLibs)
  240. {
  241. if (lib.FullName.ToLower().Contains(LIB_VLC_NAME))
  242. librariesPath = appFolder;
  243. }
  244. }
  245. }
  246. }
  247. }
  248. if (!librariesPath.Equals(string.Empty))
  249. librariesPath = Path.GetFullPath(librariesPath + Path.AltDirectorySeparatorChar);
  250. }
  251. return librariesPath;
  252. }
  253. /// <summary>
  254. /// Returns installed platforms.
  255. /// </summary>
  256. /// <param name="platform">Platforms group (mobile/desktop)</param>
  257. /// <returns></returns>
  258. public string[] GetInstalledPlatforms(Platforms category)
  259. {
  260. var installedPlatforms = new List<string>();
  261. foreach (Platforms platform in Enum.GetValues(typeof(Platforms)))
  262. {
  263. var librariesPath = GetLibrariesPath(platform, false);
  264. if (!string.IsNullOrEmpty(librariesPath))
  265. {
  266. foreach (var file in Directory.GetFiles(librariesPath))
  267. {
  268. if (Path.GetFileName(file).Contains(ASSET_NAME))
  269. {
  270. if ((category & Desktop) == Desktop &&
  271. (platform == Platforms.Win || platform == Platforms.Mac || platform == Platforms.Linux) &&
  272. !installedPlatforms.Contains(DESKTOP_CATEGORY_NAME))
  273. {
  274. installedPlatforms.Add(DESKTOP_CATEGORY_NAME);
  275. }
  276. if ((category & Mobile) == Mobile &&
  277. platform == Platforms.Android &&
  278. !installedPlatforms.Contains(Platforms.Android.ToString()))
  279. {
  280. installedPlatforms.Add(Platforms.Android.ToString());
  281. }
  282. if ((category & Mobile) == Mobile &&
  283. platform == Platforms.iOS &&
  284. !installedPlatforms.Contains(Platforms.iOS.ToString()))
  285. {
  286. installedPlatforms.Add(Platforms.iOS.ToString());
  287. }
  288. if ((category & Desktop) == Desktop &&
  289. platform == Platforms.WebGL &&
  290. !installedPlatforms.Contains(Platforms.WebGL.ToString()))
  291. {
  292. installedPlatforms.Add(Platforms.WebGL.ToString());
  293. }
  294. break;
  295. }
  296. }
  297. }
  298. }
  299. return installedPlatforms.ToArray();
  300. }
  301. #region Static Methods
  302. /// <summary>
  303. /// Returns the desktop platforms (Read Only).
  304. /// </summary>
  305. public static Platforms Desktop
  306. {
  307. get { return Platforms.Win | Platforms.Mac | Platforms.Linux; }
  308. }
  309. /// <summary>
  310. /// Returns the mobile platforms (Read Only).
  311. /// </summary>
  312. public static Platforms Mobile
  313. {
  314. get { return Platforms.Android | Platforms.iOS; }
  315. }
  316. /// <summary>
  317. /// Returns the Unity Editor bit mode (Read Only).
  318. /// </summary>
  319. public static BitModes EditorBitMode
  320. {
  321. get { return IntPtr.Size == 4 ? BitModes.x86 : BitModes.x86_64; }
  322. }
  323. /// <summary>
  324. /// Returns the folder name for current Unity Editor bit mode (Read Only).
  325. /// </summary>
  326. public static string EditorBitModeFolderName
  327. {
  328. get { return Enum.GetName(typeof(BitModes), EditorBitMode); }
  329. }
  330. /// <summary>
  331. /// Returns the current running platform that supported by UMP asset (Read Only).
  332. /// </summary>
  333. public static Platforms RuntimePlatform
  334. {
  335. get
  336. {
  337. var runtimePlatform = Platforms.None;
  338. var platform = Application.platform;
  339. if (platform == UnityEngine.RuntimePlatform.WindowsEditor ||
  340. Application.platform == UnityEngine.RuntimePlatform.WindowsPlayer)
  341. runtimePlatform = Platforms.Win;
  342. if (platform == UnityEngine.RuntimePlatform.OSXEditor ||
  343. Application.platform == UnityEngine.RuntimePlatform.OSXPlayer)
  344. runtimePlatform = Platforms.Mac;
  345. if (platform == UnityEngine.RuntimePlatform.LinuxPlayer ||
  346. (int)Application.platform == 16)
  347. runtimePlatform = Platforms.Linux;
  348. if (platform == UnityEngine.RuntimePlatform.WebGLPlayer)
  349. runtimePlatform = Platforms.WebGL;
  350. if (platform == UnityEngine.RuntimePlatform.Android)
  351. runtimePlatform = Platforms.Android;
  352. if (platform == UnityEngine.RuntimePlatform.IPhonePlayer)
  353. runtimePlatform = Platforms.iOS;
  354. return runtimePlatform;
  355. }
  356. }
  357. /// <summary>
  358. /// Returns the platform folder name for specific platform.
  359. /// </summary>
  360. /// <param name="platform">Runtime platform</param>
  361. /// <returns></returns>
  362. public static string PlatformFolderName(Platforms platform)
  363. {
  364. if (platform != Platforms.None)
  365. return platform.ToString();
  366. return string.Empty;
  367. }
  368. /// <summary>
  369. /// Returns the folder name for current platform the game is running on (Read Only).
  370. /// </summary>
  371. public static string RuntimePlatformFolderName
  372. {
  373. get
  374. {
  375. return PlatformFolderName(RuntimePlatform);
  376. }
  377. }
  378. /// <summary>
  379. /// Checking if libVLC exists by the current file path (only for Desktop platforms).
  380. /// </summary>
  381. public static bool ContainsLibVLC(string path)
  382. {
  383. var result = false;
  384. if (!string.IsNullOrEmpty(path) && Directory.Exists(path))
  385. {
  386. var files = Directory.GetFiles(path);
  387. var includes = 0;
  388. var libExt = string.Empty;
  389. switch (RuntimePlatform)
  390. {
  391. case Platforms.Win:
  392. libExt = "dll";
  393. break;
  394. case Platforms.Mac:
  395. libExt = "dylib";
  396. break;
  397. case Platforms.Linux:
  398. libExt = "so";
  399. break;
  400. }
  401. foreach (var file in files)
  402. {
  403. if (file.EndsWith(string.Format("{0}.{1}", LIB_VLC_NAME, libExt)) ||
  404. file.EndsWith(string.Format("{0}.{1}", LIB_VLC_CORE_NAME, libExt)))
  405. includes++;
  406. }
  407. if (includes >= 2)
  408. result = true;
  409. }
  410. return result;
  411. }
  412. #endregion
  413. }
  414. }