UMPPostBuilds.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. using System.IO;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using UnityEditor.Callbacks;
  5. using UMP;
  6. using System;
  7. #if UNITY_IPHONE
  8. using UnityEditor.iOS.Xcode;
  9. #endif
  10. public class UMPPostBuilds : MonoBehaviour
  11. {
  12. [PostProcessBuild]
  13. [Obsolete]
  14. public static void OnPostprocessBuild(BuildTarget buildTarget, string path)
  15. {
  16. var settings = UMPSettings.Instance;
  17. switch (buildTarget)
  18. {
  19. case BuildTarget.StandaloneWindows:
  20. BuildWindowsPlayer32(path, settings);
  21. break;
  22. case BuildTarget.StandaloneWindows64:
  23. BuildWindowsPlayer64(path, settings);
  24. break;
  25. case BuildTarget.StandaloneLinux:
  26. BuildLinuxPlayer32(path, settings);
  27. break;
  28. case BuildTarget.StandaloneLinux64:
  29. BuildLinuxPlayer64(path, settings);
  30. break;
  31. case BuildTarget.StandaloneLinuxUniversal:
  32. BuildLinuxPlayerUniversal(path, settings);
  33. break;
  34. case BuildTarget.iOS:
  35. BuildForiOS(path, settings);
  36. break;
  37. }
  38. if (buildTarget == (BuildTarget)2 || buildTarget == (BuildTarget)27)
  39. BuildMacPlayer64(path, buildTarget, settings);
  40. }
  41. private static void BuildForiOS(string path, UMPSettings settings)
  42. {
  43. #if UNITY_IPHONE
  44. var projPath = Path.Combine(path, "Unity-iPhone.xcodeproj/project.pbxproj");
  45. var pbxProject = new PBXProject();
  46. pbxProject.ReadFromString(File.ReadAllText(projPath));
  47. var target = pbxProject.TargetGuidByName("Unity-iPhone");
  48. // Activate Background Mode for Audio
  49. string plistPath = path + "/Info.plist";
  50. PlistDocument plist = new PlistDocument();
  51. plist.ReadFromString(File.ReadAllText(plistPath));
  52. PlistElementDict rootDict = plist.root;
  53. var buildKey = "UIBackgroundModes";
  54. rootDict.CreateArray(buildKey).AddString("audio");
  55. File.WriteAllText(plistPath, plist.WriteToString());
  56. var fileGuid = pbxProject.AddFile("usr/lib/" + "libc++.dylib", "Frameworks/" + "libc++.dylib", PBXSourceTree.Sdk);
  57. pbxProject.AddFileToBuild(target, fileGuid);
  58. fileGuid = pbxProject.AddFile("usr/lib/" + "libz.dylib", "Frameworks/" + "libz.dylib", PBXSourceTree.Sdk);
  59. pbxProject.AddFileToBuild(target, fileGuid);
  60. fileGuid = pbxProject.AddFile("usr/lib/" + "libz.tbd", "Frameworks/" + "libz.tbd", PBXSourceTree.Sdk);
  61. pbxProject.AddFileToBuild(target, fileGuid);
  62. fileGuid = pbxProject.AddFile("usr/lib/" + "libbz2.dylib", "Frameworks/" + "libbz2.dylib", PBXSourceTree.Sdk);
  63. pbxProject.AddFileToBuild(target, fileGuid);
  64. fileGuid = pbxProject.AddFile("usr/lib/" + "libbz2.tbd", "Frameworks/" + "libbz2.tbd", PBXSourceTree.Sdk);
  65. pbxProject.AddFileToBuild(target, fileGuid);
  66. if ((settings.PlayersIPhone & PlayerOptionsIPhone.PlayerTypes.Native) == PlayerOptionsIPhone.PlayerTypes.Native)
  67. pbxProject.AddFrameworkToProject(target, "CoreImage.framework", true);
  68. File.WriteAllText(projPath, pbxProject.WriteToString());
  69. #endif
  70. }
  71. public static void BuildWindowsPlayer32(string path, UMPSettings settings)
  72. {
  73. string buildPath = Path.GetDirectoryName(path);
  74. string dataPath = buildPath + "/" + Path.GetFileNameWithoutExtension(path) + "_Data";
  75. if (!string.IsNullOrEmpty(buildPath))
  76. {
  77. if (!settings.UseExternalLibraries)
  78. {
  79. CopyPlugins(settings.AssetPath + "/Plugins/Win/x86/plugins/", dataPath + "/Plugins/plugins/");
  80. }
  81. else
  82. {
  83. if (File.Exists(dataPath + "/Plugins/" + UMPSettings.LIB_VLC_NAME + ".dll"))
  84. File.Delete(dataPath + "/Plugins/" + UMPSettings.LIB_VLC_NAME + ".dll");
  85. if (File.Exists(dataPath + "/Plugins/" + UMPSettings.LIB_VLC_CORE_NAME + ".dll"))
  86. File.Delete(dataPath + "/Plugins/" + UMPSettings.LIB_VLC_CORE_NAME + ".dll");
  87. }
  88. }
  89. Debug.Log("Standalone Windows (x86) build is completed: " + path);
  90. }
  91. public static void BuildWindowsPlayer64(string path, UMPSettings settings)
  92. {
  93. string buildPath = Path.GetDirectoryName(path);
  94. string dataPath = buildPath + "/" + Path.GetFileNameWithoutExtension(path) + "_Data";
  95. if (!string.IsNullOrEmpty(buildPath))
  96. {
  97. if (!settings.UseExternalLibraries)
  98. {
  99. CopyPlugins(settings.AssetPath + "/Plugins/Win/x86_64/plugins/", dataPath + "/Plugins/plugins/");
  100. }
  101. else
  102. {
  103. if (File.Exists(dataPath + "/Plugins/" + UMPSettings.LIB_VLC_NAME + ".dll"))
  104. File.Delete(dataPath + "/Plugins/" + UMPSettings.LIB_VLC_NAME + ".dll");
  105. if (File.Exists(dataPath + "/Plugins/" + UMPSettings.LIB_VLC_CORE_NAME + ".dll"))
  106. File.Delete(dataPath + "/Plugins/" + UMPSettings.LIB_VLC_CORE_NAME + ".dll");
  107. }
  108. }
  109. Debug.Log("Standalone Windows (x86_x64) build is completed: " + path);
  110. }
  111. public static void BuildMacPlayer64(string path, BuildTarget target, UMPSettings settings)
  112. {
  113. string buildPath = Path.GetDirectoryName(path);
  114. string dataPath = buildPath + "/" + Path.GetFileName(path) + "/Contents";
  115. if (target == (BuildTarget)2)
  116. {
  117. if (Directory.Exists(dataPath + "/Plugins/x86_64/" + UMPSettings.LIB_VLC_NAME + ".bundle"))
  118. Directory.Move(dataPath + "/Plugins/x86_64/" + UMPSettings.LIB_VLC_NAME + ".bundle", dataPath + "/Plugins/" + UMPSettings.LIB_VLC_NAME + ".bundle");
  119. if (Directory.Exists(dataPath + "/Plugins/x86_64/" + UMPSettings.ASSET_NAME + ".bundle"))
  120. Directory.Move(dataPath + "/Plugins/x86_64/" + UMPSettings.ASSET_NAME + ".bundle", dataPath + "/Plugins/" + UMPSettings.ASSET_NAME + ".bundle");
  121. }
  122. if (!string.IsNullOrEmpty(buildPath) && settings.UseExternalLibraries)
  123. {
  124. if (Directory.Exists(dataPath + "/Plugins/" + UMPSettings.LIB_VLC_NAME + ".bundle"))
  125. Directory.Delete(dataPath + "/Plugins/" + UMPSettings.LIB_VLC_NAME + ".bundle", true);
  126. }
  127. Debug.Log("Standalone Mac (x86_x64) build is completed: " + dataPath);
  128. }
  129. public static void BuildLinuxPlayer32(string path, UMPSettings settings)
  130. {
  131. string buildPath = Path.GetDirectoryName(path);
  132. string dataPath = buildPath + "/" + Path.GetFileNameWithoutExtension(path) + "_Data";
  133. string umpLauncherPath = settings.AssetPath + "/Plugins/Linux/UMPInstaller.sh";
  134. string umpRemoverPath = settings.AssetPath + "/Plugins/Linux/UMPRemover.sh";
  135. if (!string.IsNullOrEmpty(buildPath))
  136. {
  137. if (!settings.UseExternalLibraries)
  138. {
  139. string vlcFolderPath32 = dataPath + "/Plugins/x86/vlc";
  140. if (!Directory.Exists(vlcFolderPath32))
  141. Directory.CreateDirectory(vlcFolderPath32);
  142. CopyPlugins(settings.AssetPath + "/Plugins/Linux/x86/plugins/", vlcFolderPath32 + "/plugins/");
  143. }
  144. else
  145. {
  146. if (File.Exists(dataPath + "/Plugins/x86/" + UMPSettings.LIB_VLC_NAME + ".so"))
  147. File.Delete(dataPath + "/Plugins/x86/" + UMPSettings.LIB_VLC_NAME + ".so");
  148. if (File.Exists(dataPath + "/Plugins/x86/" + UMPSettings.LIB_VLC_CORE_NAME + ".so"))
  149. File.Delete(dataPath + "/Plugins/x86/" + UMPSettings.LIB_VLC_CORE_NAME + ".so");
  150. }
  151. CopyShellScript(umpLauncherPath, buildPath);
  152. CopyShellScript(umpRemoverPath, buildPath);
  153. }
  154. Debug.Log("Standalone Linux (x86) build is completed: " + path);
  155. }
  156. public static void BuildLinuxPlayer64(string path, UMPSettings settings)
  157. {
  158. string buildPath = Path.GetDirectoryName(path);
  159. string dataPath = buildPath + "/" + Path.GetFileNameWithoutExtension(path) + "_Data";
  160. string umpLauncherPath = settings.AssetPath + "/Plugins/Linux/UMPInstaller.sh";
  161. string umpRemoverPath = settings.AssetPath + "/Plugins/Linux/UMPRemover.sh";
  162. if (!string.IsNullOrEmpty(buildPath))
  163. {
  164. if (!settings.UseExternalLibraries)
  165. {
  166. string vlcFolderPath64 = dataPath + "/Plugins/x86_64/vlc";
  167. if (!Directory.Exists(vlcFolderPath64))
  168. Directory.CreateDirectory(vlcFolderPath64);
  169. CopyPlugins(settings.AssetPath + "/Plugins/Linux/x86_64/plugins/", vlcFolderPath64 + "/plugins/");
  170. }
  171. else
  172. {
  173. if (File.Exists(dataPath + "/Plugins/x86_64/" + UMPSettings.LIB_VLC_NAME + ".so"))
  174. File.Delete(dataPath + "/Plugins/x86_64/" + UMPSettings.LIB_VLC_NAME + ".so");
  175. if (File.Exists(dataPath + "/Plugins/x86_64/" + UMPSettings.LIB_VLC_CORE_NAME + ".so"))
  176. File.Delete(dataPath + "/Plugins/x86_64/" + UMPSettings.LIB_VLC_CORE_NAME + ".so");
  177. }
  178. CopyShellScript(umpLauncherPath, buildPath);
  179. CopyShellScript(umpRemoverPath, buildPath);
  180. }
  181. Debug.Log("Standalone Linux (x86_x64) build is completed: " + path);
  182. }
  183. public static void BuildLinuxPlayerUniversal(string path, UMPSettings settings)
  184. {
  185. string buildPath = Path.GetDirectoryName(path);
  186. string dataPath = buildPath + "/" + Path.GetFileNameWithoutExtension(path) + "_Data";
  187. string umpLauncherPath = settings.AssetPath + "/Plugins/Linux/UMPInstaller.sh";
  188. string umpRemoverPath = settings.AssetPath + "/Plugins/Linux/UMPRemover.sh";
  189. if (!string.IsNullOrEmpty(buildPath))
  190. {
  191. if (!settings.UseExternalLibraries)
  192. {
  193. string vlcFolderPath32 = dataPath + "/Plugins/x86/vlc";
  194. if (!Directory.Exists(vlcFolderPath32))
  195. Directory.CreateDirectory(vlcFolderPath32);
  196. string vlcFolderPath64 = dataPath + "/Plugins/x86_64/vlc";
  197. if (!Directory.Exists(vlcFolderPath64))
  198. Directory.CreateDirectory(vlcFolderPath64);
  199. CopyPlugins(settings.AssetPath + "/Plugins/Linux/x86/plugins/", vlcFolderPath32 + "/plugins/");
  200. CopyPlugins(settings.AssetPath + "/Plugins/Linux/x86_64/plugins/", vlcFolderPath64 + "/plugins/");
  201. }
  202. else
  203. {
  204. if (File.Exists(dataPath + "/Plugins/x86/" + UMPSettings.LIB_VLC_NAME + ".so"))
  205. File.Delete(dataPath + "/Plugins/x86/" + UMPSettings.LIB_VLC_NAME + ".so");
  206. if (File.Exists(dataPath + "/Plugins/x86/" + UMPSettings.LIB_VLC_CORE_NAME + ".so"))
  207. File.Delete(dataPath + "/Plugins/x86/" + UMPSettings.LIB_VLC_CORE_NAME + ".so");
  208. if (File.Exists(dataPath + "/Plugins/x86_64/" + UMPSettings.LIB_VLC_NAME + ".so"))
  209. File.Delete(dataPath + "/Plugins/x86_64/" + UMPSettings.LIB_VLC_NAME + ".so");
  210. if (File.Exists(dataPath + "/Plugins/x86_64/" + UMPSettings.LIB_VLC_CORE_NAME + ".so"))
  211. File.Delete(dataPath + "/Plugins/x86_64/" + UMPSettings.LIB_VLC_CORE_NAME + ".so");
  212. }
  213. CopyShellScript(umpLauncherPath, buildPath);
  214. CopyShellScript(umpRemoverPath, buildPath);
  215. }
  216. Debug.Log("Standalone Linux (Universal) build is completed: " + path);
  217. }
  218. private static void CopyPlugins(string sourcePath, string targetPath)
  219. {
  220. string fileName = string.Empty;
  221. string destFile = targetPath;
  222. if (!Directory.Exists(targetPath))
  223. Directory.CreateDirectory(targetPath);
  224. string[] directories = Directory.GetDirectories(sourcePath);
  225. foreach (var d in directories)
  226. {
  227. string[] files = Directory.GetFiles(d);
  228. if (files.Length > 0)
  229. {
  230. destFile = Path.Combine(targetPath, Path.GetFileName(d));
  231. Directory.CreateDirectory(destFile);
  232. }
  233. foreach (var s in files)
  234. {
  235. if (Path.GetExtension(s).Equals(".meta"))
  236. continue;
  237. fileName = Path.GetFileName(s);
  238. File.Copy(s, Path.Combine(destFile, fileName), false);
  239. }
  240. }
  241. }
  242. private static void CopyShellScript(string scriptPath, string targetPath)
  243. {
  244. string scriptName = Path.GetFileName(scriptPath);
  245. File.Copy(scriptPath, Path.Combine(targetPath, scriptName), false);
  246. }
  247. }