PackRun.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace Pack
  8. {
  9. public static class PackRun
  10. {
  11. private static PackPlatforms s_Configs = null;
  12. public static PackPlatforms GetConfigs(bool forceRefresh = false)
  13. {
  14. if (s_Configs == null || forceRefresh)
  15. {
  16. s_Configs = null;
  17. try
  18. {
  19. string path = Path.GetFullPath(Application.dataPath + PackConstant.PackPlatformConfigPath);
  20. if (File.Exists(path))
  21. {
  22. UTF8Encoding uTF8Encoding = new UTF8Encoding(false);
  23. string configStr = File.ReadAllText(path, uTF8Encoding);
  24. s_Configs = JsonUtility.FromJson<PackPlatforms>(configStr);
  25. }
  26. }
  27. catch (Exception e)
  28. {
  29. s_Configs = null;
  30. Debug.LogException(e);
  31. }
  32. }
  33. return s_Configs;
  34. }
  35. public static void SaveConfigs(PackPlatforms packPlatforms)
  36. {
  37. if (packPlatforms == null) return;
  38. try
  39. {
  40. string path = Path.GetFullPath(Application.dataPath + PackConstant.PackPlatformConfigPath);
  41. if (!File.Exists(path))
  42. {
  43. if (!Directory.Exists(Path.GetDirectoryName(path)))
  44. {
  45. Directory.CreateDirectory(Path.GetDirectoryName(path));
  46. }
  47. var fs = File.Create(path);
  48. fs.Dispose();
  49. }
  50. s_Configs = packPlatforms;
  51. string configStr = JsonUtility.ToJson(packPlatforms, true);
  52. UTF8Encoding uTF8Encoding = new UTF8Encoding(false);
  53. File.WriteAllText(path, configStr, uTF8Encoding);
  54. }
  55. catch (Exception e)
  56. {
  57. Debug.LogException(e);
  58. }
  59. }
  60. public static void ChangePlatform(string channelUniqueId)
  61. {
  62. PackPlatforms packPlatforms = GetConfigs(true);
  63. foreach (PackPlatformBase packPlatformBase in packPlatforms.packPlatforms)
  64. {
  65. if (channelUniqueId == packPlatformBase.channelUniqueId)
  66. {
  67. ChangePlatform(packPlatformBase);
  68. return;
  69. }
  70. }
  71. throw new PackException("目标打包平台是错误的");
  72. }
  73. public static void ChangePlatform(PackPlatformBase packPlatformBase)
  74. {
  75. if (packPlatformBase == null)
  76. {
  77. throw new PackException("目标打包平台是错误的");
  78. }
  79. if (!packPlatformBase.CanChangePlatform())
  80. {
  81. packPlatformBase.ChangePlatform();
  82. return;
  83. }
  84. if (EditorApplication.isCompiling)
  85. {
  86. throw new PackException("编辑器正在编译脚本文件,不能开始切换平台");
  87. }
  88. ClearCurEnv();
  89. RecordCurEnv(packPlatformBase);
  90. packPlatformBase.ChangePlatform();
  91. }
  92. public static void BuildAB(string channelUniqueId)
  93. {
  94. PackPlatforms packPlatforms = GetConfigs(true);
  95. foreach (PackPlatformBase packPlatformBase in packPlatforms.packPlatforms)
  96. {
  97. if (channelUniqueId == packPlatformBase.channelUniqueId)
  98. {
  99. BuildAB(packPlatformBase);
  100. return;
  101. }
  102. }
  103. throw new PackException("目标打包平台是错误的");
  104. }
  105. public static void BuildAB(PackPlatformBase packPlatformBase)
  106. {
  107. if (packPlatformBase == null)
  108. {
  109. throw new PackException("目标打包平台是错误的");
  110. }
  111. if (!packPlatformBase.CanBuildAssetBundles())
  112. {
  113. packPlatformBase.BuildAssetBundles();
  114. return;
  115. }
  116. if (EditorApplication.isCompiling)
  117. {
  118. throw new PackException("编辑器正在编译脚本文件,不能开始编译AB");
  119. }
  120. ClearCurEnv();
  121. RecordCurEnv(packPlatformBase);
  122. packPlatformBase.BuildAssetBundles();
  123. }
  124. public static void BuildApp(string channelUniqueId)
  125. {
  126. PackPlatforms packPlatforms = GetConfigs(true);
  127. foreach (PackPlatformBase packPlatformBase in packPlatforms.packPlatforms)
  128. {
  129. if (channelUniqueId == packPlatformBase.channelUniqueId)
  130. {
  131. BuildApp(packPlatformBase);
  132. return;
  133. }
  134. }
  135. throw new PackException("目标打包平台是错误的");
  136. }
  137. public static void BuildApp(PackPlatformBase packPlatformBase)
  138. {
  139. if (packPlatformBase == null)
  140. {
  141. throw new PackException("目标打包平台是错误的");
  142. }
  143. if (!packPlatformBase.CanBuildApp())
  144. {
  145. packPlatformBase.BuildApp();
  146. return;
  147. }
  148. if (EditorApplication.isCompiling)
  149. {
  150. throw new PackException("编辑器正在编译脚本文件,不能开始编译App");
  151. }
  152. ClearCurEnv();
  153. RecordCurEnv(packPlatformBase);
  154. packPlatformBase.BuildApp();
  155. }
  156. public static string GetCurChannelUniqueId()
  157. {
  158. if (packToolsSettings != null)
  159. {
  160. return packToolsSettings.channelUniqueId;
  161. }
  162. return "";
  163. }
  164. public static void ClearBuildAndResetDefault()
  165. {
  166. if (EditorApplication.isCompiling)
  167. {
  168. throw new PackException("编辑器正在编译脚本文件,不能开始编译App");
  169. }
  170. ClearCurEnv();
  171. new PackPlatformDefault().ChangePlatform();
  172. }
  173. private static void ClearCurEnv()
  174. {
  175. PackPlatforms packPlatforms = GetConfigs();
  176. string channelUniqueId = string.Empty;
  177. string buildTargetStr = string.Empty;
  178. if (packToolsSettings != null)
  179. {
  180. channelUniqueId = packToolsSettings.channelUniqueId;
  181. buildTargetStr = packToolsSettings.buildClassName;
  182. }
  183. PackPlatformBase curPackPlatformBase = null;
  184. if (!string.IsNullOrEmpty(channelUniqueId))
  185. {
  186. curPackPlatformBase = packPlatforms.packPlatforms.FindFirst((x) => x.channelUniqueId == channelUniqueId);
  187. }
  188. if (curPackPlatformBase != null)
  189. {
  190. curPackPlatformBase.BuildClear();
  191. }
  192. else
  193. {
  194. curPackPlatformBase = packPlatforms.packPlatforms.FindFirst((x) => x.GetType().FullName == buildTargetStr);
  195. if (curPackPlatformBase != null)
  196. {
  197. curPackPlatformBase.BuildClear();
  198. }
  199. }
  200. RecordCurEnv(null);
  201. }
  202. private static void RecordCurEnv(PackPlatformBase packPlatformBase)
  203. {
  204. if (packPlatformBase == null)
  205. {
  206. if (packToolsSettings != null)
  207. {
  208. packToolsSettings.channelUniqueId = string.Empty;
  209. packToolsSettings.buildClassName = string.Empty;
  210. }
  211. }
  212. else
  213. {
  214. if (packToolsSettings != null)
  215. {
  216. packToolsSettings.channelUniqueId = packPlatformBase.channelUniqueId;
  217. packToolsSettings.buildClassName = packPlatformBase.GetType().FullName;
  218. }
  219. }
  220. SavePackToolsSettings();
  221. }
  222. private static void CommandLineBuildApp()
  223. {
  224. if (EditorApplication.isCompiling)
  225. {
  226. throw new PackException("编辑器正在编译脚本文件,不能开始执行命令行");
  227. }
  228. Dictionary<string, string> args = GetValidCommandLineArgs(Environment.GetCommandLineArgs());
  229. if (args.Count <= 0)
  230. {
  231. throw new PackException("参数为空,无法打包");
  232. }
  233. if (args.ContainsKey("-h"))
  234. {
  235. string content = GetCanPackPlatformStr();
  236. throw new PackException(content);
  237. }
  238. if (args.ContainsKey("-buildAB"))
  239. {
  240. if (string.IsNullOrEmpty(args["-buildAB"]))
  241. throw new PackException("目标平台不能为空,请使用-h来查看可编译平台");
  242. else
  243. {
  244. BuildAB(args["-buildAB"]);
  245. return;
  246. }
  247. }
  248. if (args.ContainsKey("-buildApp"))
  249. {
  250. if (string.IsNullOrEmpty(args["-buildApp"]))
  251. throw new PackException("目标平台不能为空,请使用-h来查看可编译平台");
  252. else
  253. {
  254. BuildApp(args["-buildApp"]);
  255. return;
  256. }
  257. }
  258. throw new PackException("无可执行的命令");
  259. }
  260. private static void CommandLineLog(string message)
  261. {
  262. Debug.Log(PackConstant.TAG_START + message + PackConstant.TAG_END);
  263. }
  264. private static List<string> s_ValidCommandLineArgNames = new List<string>() {
  265. "-h",
  266. "-buildAB",
  267. "-buildApp",
  268. "-androidEnv"
  269. };
  270. private static Dictionary<string, string> GetValidCommandLineArgs(string[] args)
  271. {
  272. Dictionary<string, string> maps = new Dictionary<string, string>();
  273. for (int i = 0, iMax = args.Length; i < iMax; i++)
  274. {
  275. if (s_ValidCommandLineArgNames.Contains(args[i]))
  276. {
  277. if (i + 1 >= iMax)
  278. maps.Add(args[i], string.Empty);
  279. else
  280. maps.Add(args[i], args[i + 1]);
  281. i++;
  282. }
  283. }
  284. return maps;
  285. }
  286. private static string GetCanPackPlatformStr()
  287. {
  288. PackPlatforms packPlatforms = GetConfigs(true);
  289. if (packPlatforms == null || packPlatforms.packPlatforms == null ||
  290. packPlatforms.packPlatforms.Length <= 0)
  291. {
  292. return "无可打包的平台,请检查";
  293. }
  294. StringBuilder sb = new StringBuilder();
  295. sb.Append("ID");
  296. sb.Append("\t\t\t\t");
  297. sb.Append("Name");
  298. sb.Append("\n");
  299. for (int i = 0, iMax = packPlatforms.packPlatforms.Length; i < iMax; i++)
  300. {
  301. PackPlatformBase packPlatformBase = packPlatforms.packPlatforms[i];
  302. sb.Append(packPlatformBase.channelUniqueId);
  303. sb.Append("\t\t\t");
  304. sb.Append(packPlatformBase.GetChannelUniqueName());
  305. sb.Append("\n");
  306. }
  307. string content = sb.ToString();
  308. if (string.IsNullOrEmpty(content))
  309. {
  310. return "无可打包的平台,请检查";
  311. }
  312. return content;
  313. }
  314. private static void BuildPlayerHandler(BuildPlayerOptions options)
  315. {
  316. PackWindow.OpenPackWindow();
  317. EditorUtility.DisplayDialog("提示", "请使用打包工具来打包", "好的");
  318. }
  319. private static void OnQuitting()
  320. {
  321. SavePackToolsSettings();
  322. }
  323. [InitializeOnLoadMethod]
  324. private static void RegisterBuildPlayerHandler()
  325. {
  326. BuildPlayerWindow.RegisterBuildPlayerHandler(BuildPlayerHandler);
  327. EditorApplication.quitting += OnQuitting;
  328. }
  329. private static PackToolsSettings s_PackToolsSettings = null;
  330. public static PackToolsSettings packToolsSettings
  331. {
  332. get
  333. {
  334. if (s_PackToolsSettings == null)
  335. {
  336. if (File.Exists(Application.dataPath + PackConstant.CurPackEnvPath))
  337. {
  338. string content = File.ReadAllText(Application.dataPath + PackConstant.CurPackEnvPath);
  339. s_PackToolsSettings = JsonUtility.FromJson<PackToolsSettings>(content);
  340. }
  341. if (s_PackToolsSettings == null)
  342. s_PackToolsSettings = new PackToolsSettings();
  343. }
  344. return s_PackToolsSettings;
  345. }
  346. }
  347. public static void SavePackToolsSettings()
  348. {
  349. if (packToolsSettings != null && packToolsSettings.IsDirty())
  350. {
  351. File.WriteAllText(Application.dataPath + PackConstant.CurPackEnvPath, JsonUtility.ToJson(packToolsSettings));
  352. }
  353. }
  354. [Serializable]
  355. public class PackToolsSettings : ISerializationCallbackReceiver
  356. {
  357. [SerializeField]
  358. private string m_ChannelUniqueId = "";
  359. [SerializeField]
  360. private string m_BuildClassName = "";
  361. public string channelUniqueId
  362. {
  363. get; set;
  364. }
  365. public string buildClassName
  366. {
  367. get; set;
  368. }
  369. public PackToolsSettings()
  370. {
  371. OnAfterDeserialize();
  372. }
  373. public bool IsDirty()
  374. {
  375. if (m_ChannelUniqueId != channelUniqueId)
  376. return true;
  377. if (m_BuildClassName != buildClassName)
  378. return true;
  379. return false;
  380. }
  381. public void OnAfterDeserialize()
  382. {
  383. channelUniqueId = m_ChannelUniqueId;
  384. buildClassName = m_BuildClassName;
  385. }
  386. public void OnBeforeSerialize()
  387. {
  388. m_ChannelUniqueId = channelUniqueId;
  389. m_BuildClassName = buildClassName;
  390. }
  391. }
  392. }
  393. }