PackRun.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 BuildLuaAB(PackPlatformBase packPlatformBase)
  125. {
  126. if (packPlatformBase == null)
  127. {
  128. throw new PackException("目标打包平台是错误的");
  129. }
  130. if (!packPlatformBase.CanBuildAssetBundles())
  131. {
  132. packPlatformBase.BuildOnlyLua();
  133. return;
  134. }
  135. if (EditorApplication.isCompiling)
  136. {
  137. throw new PackException("编辑器正在编译脚本文件,不能开始编译AB");
  138. }
  139. ClearCurEnv();
  140. RecordCurEnv(packPlatformBase);
  141. packPlatformBase.BuildOnlyLua();
  142. }
  143. public static void BuildApp(string channelUniqueId)
  144. {
  145. PackPlatforms packPlatforms = GetConfigs(true);
  146. foreach (PackPlatformBase packPlatformBase in packPlatforms.packPlatforms)
  147. {
  148. if (channelUniqueId == packPlatformBase.channelUniqueId)
  149. {
  150. BuildApp(packPlatformBase);
  151. return;
  152. }
  153. }
  154. throw new PackException("目标打包平台是错误的");
  155. }
  156. public static void BuildApp(PackPlatformBase packPlatformBase)
  157. {
  158. if (packPlatformBase == null)
  159. {
  160. throw new PackException("目标打包平台是错误的");
  161. }
  162. if (!packPlatformBase.CanBuildApp())
  163. {
  164. packPlatformBase.BuildApp();
  165. return;
  166. }
  167. if (EditorApplication.isCompiling)
  168. {
  169. throw new PackException("编辑器正在编译脚本文件,不能开始编译App");
  170. }
  171. ClearCurEnv();
  172. RecordCurEnv(packPlatformBase);
  173. packPlatformBase.BuildApp();
  174. }
  175. public static string GetCurChannelUniqueId()
  176. {
  177. if (packToolsSettings != null)
  178. {
  179. return packToolsSettings.channelUniqueId;
  180. }
  181. return "";
  182. }
  183. public static void ClearBuildAndResetDefault()
  184. {
  185. if (EditorApplication.isCompiling)
  186. {
  187. throw new PackException("编辑器正在编译脚本文件,不能开始编译App");
  188. }
  189. ClearCurEnv();
  190. new PackPlatformDefault().ChangePlatform();
  191. }
  192. private static void ClearCurEnv()
  193. {
  194. PackPlatforms packPlatforms = GetConfigs();
  195. string channelUniqueId = string.Empty;
  196. string buildTargetStr = string.Empty;
  197. if (packToolsSettings != null)
  198. {
  199. channelUniqueId = packToolsSettings.channelUniqueId;
  200. buildTargetStr = packToolsSettings.buildClassName;
  201. }
  202. PackPlatformBase curPackPlatformBase = null;
  203. if (!string.IsNullOrEmpty(channelUniqueId))
  204. {
  205. curPackPlatformBase = packPlatforms.packPlatforms.FindFirst((x) => x.channelUniqueId == channelUniqueId);
  206. }
  207. if (curPackPlatformBase != null)
  208. {
  209. curPackPlatformBase.BuildClear();
  210. }
  211. else
  212. {
  213. curPackPlatformBase = packPlatforms.packPlatforms.FindFirst((x) => x.GetType().FullName == buildTargetStr);
  214. if (curPackPlatformBase != null)
  215. {
  216. curPackPlatformBase.BuildClear();
  217. }
  218. }
  219. RecordCurEnv(null);
  220. }
  221. private static void RecordCurEnv(PackPlatformBase packPlatformBase)
  222. {
  223. if (packPlatformBase == null)
  224. {
  225. if (packToolsSettings != null)
  226. {
  227. packToolsSettings.channelUniqueId = string.Empty;
  228. packToolsSettings.buildClassName = string.Empty;
  229. }
  230. }
  231. else
  232. {
  233. if (packToolsSettings != null)
  234. {
  235. packToolsSettings.channelUniqueId = packPlatformBase.channelUniqueId;
  236. packToolsSettings.buildClassName = packPlatformBase.GetType().FullName;
  237. }
  238. }
  239. SavePackToolsSettings();
  240. }
  241. private static void CommandLineBuildApp()
  242. {
  243. if (EditorApplication.isCompiling)
  244. {
  245. throw new PackException("编辑器正在编译脚本文件,不能开始执行命令行");
  246. }
  247. Dictionary<string, string> args = GetValidCommandLineArgs(Environment.GetCommandLineArgs());
  248. if (args.Count <= 0)
  249. {
  250. throw new PackException("参数为空,无法打包");
  251. }
  252. if (args.ContainsKey("-h"))
  253. {
  254. string content = GetCanPackPlatformStr();
  255. throw new PackException(content);
  256. }
  257. if (args.ContainsKey("-buildAB"))
  258. {
  259. if (string.IsNullOrEmpty(args["-buildAB"]))
  260. throw new PackException("目标平台不能为空,请使用-h来查看可编译平台");
  261. else
  262. {
  263. BuildAB(args["-buildAB"]);
  264. return;
  265. }
  266. }
  267. if (args.ContainsKey("-buildApp"))
  268. {
  269. if (string.IsNullOrEmpty(args["-buildApp"]))
  270. throw new PackException("目标平台不能为空,请使用-h来查看可编译平台");
  271. else
  272. {
  273. BuildApp(args["-buildApp"]);
  274. return;
  275. }
  276. }
  277. throw new PackException("无可执行的命令");
  278. }
  279. private static void CommandLineLog(string message)
  280. {
  281. Debug.Log(PackConstant.TAG_START + message + PackConstant.TAG_END);
  282. }
  283. private static List<string> s_ValidCommandLineArgNames = new List<string>() {
  284. "-h",
  285. "-buildAB",
  286. "-buildApp",
  287. "-androidEnv"
  288. };
  289. private static Dictionary<string, string> GetValidCommandLineArgs(string[] args)
  290. {
  291. Dictionary<string, string> maps = new Dictionary<string, string>();
  292. for (int i = 0, iMax = args.Length; i < iMax; i++)
  293. {
  294. if (s_ValidCommandLineArgNames.Contains(args[i]))
  295. {
  296. if (i + 1 >= iMax)
  297. maps.Add(args[i], string.Empty);
  298. else
  299. maps.Add(args[i], args[i + 1]);
  300. i++;
  301. }
  302. }
  303. return maps;
  304. }
  305. private static string GetCanPackPlatformStr()
  306. {
  307. PackPlatforms packPlatforms = GetConfigs(true);
  308. if (packPlatforms == null || packPlatforms.packPlatforms == null ||
  309. packPlatforms.packPlatforms.Length <= 0)
  310. {
  311. return "无可打包的平台,请检查";
  312. }
  313. StringBuilder sb = new StringBuilder();
  314. sb.Append("ID");
  315. sb.Append("\t\t\t\t");
  316. sb.Append("Name");
  317. sb.Append("\n");
  318. for (int i = 0, iMax = packPlatforms.packPlatforms.Length; i < iMax; i++)
  319. {
  320. PackPlatformBase packPlatformBase = packPlatforms.packPlatforms[i];
  321. sb.Append(packPlatformBase.channelUniqueId);
  322. sb.Append("\t\t\t");
  323. sb.Append(packPlatformBase.GetChannelUniqueName());
  324. sb.Append("\n");
  325. }
  326. string content = sb.ToString();
  327. if (string.IsNullOrEmpty(content))
  328. {
  329. return "无可打包的平台,请检查";
  330. }
  331. return content;
  332. }
  333. private static void BuildPlayerHandler(BuildPlayerOptions options)
  334. {
  335. PackWindow.OpenPackWindow();
  336. EditorUtility.DisplayDialog("提示", "请使用打包工具来打包", "好的");
  337. }
  338. private static void OnQuitting()
  339. {
  340. SavePackToolsSettings();
  341. }
  342. //[InitializeOnLoadMethod]
  343. private static void RegisterBuildPlayerHandler()
  344. {
  345. BuildPlayerWindow.RegisterBuildPlayerHandler(BuildPlayerHandler);
  346. EditorApplication.quitting += OnQuitting;
  347. }
  348. private static PackToolsSettings s_PackToolsSettings = null;
  349. public static PackToolsSettings packToolsSettings
  350. {
  351. get
  352. {
  353. if (s_PackToolsSettings == null)
  354. {
  355. if (File.Exists(Application.dataPath + PackConstant.CurPackEnvPath))
  356. {
  357. string content = File.ReadAllText(Application.dataPath + PackConstant.CurPackEnvPath);
  358. s_PackToolsSettings = JsonUtility.FromJson<PackToolsSettings>(content);
  359. }
  360. if (s_PackToolsSettings == null)
  361. s_PackToolsSettings = new PackToolsSettings();
  362. }
  363. return s_PackToolsSettings;
  364. }
  365. }
  366. public static void SavePackToolsSettings()
  367. {
  368. if (packToolsSettings != null && packToolsSettings.IsDirty())
  369. {
  370. File.WriteAllText(Application.dataPath + PackConstant.CurPackEnvPath, JsonUtility.ToJson(packToolsSettings));
  371. }
  372. }
  373. [Serializable]
  374. public class PackToolsSettings : ISerializationCallbackReceiver
  375. {
  376. [SerializeField]
  377. private string m_ChannelUniqueId = "";
  378. [SerializeField]
  379. private string m_BuildClassName = "";
  380. public string channelUniqueId
  381. {
  382. get; set;
  383. }
  384. public string buildClassName
  385. {
  386. get; set;
  387. }
  388. public PackToolsSettings()
  389. {
  390. OnAfterDeserialize();
  391. }
  392. public bool IsDirty()
  393. {
  394. if (m_ChannelUniqueId != channelUniqueId)
  395. return true;
  396. if (m_BuildClassName != buildClassName)
  397. return true;
  398. return false;
  399. }
  400. public void OnAfterDeserialize()
  401. {
  402. channelUniqueId = m_ChannelUniqueId;
  403. buildClassName = m_BuildClassName;
  404. }
  405. public void OnBeforeSerialize()
  406. {
  407. m_ChannelUniqueId = channelUniqueId;
  408. m_BuildClassName = buildClassName;
  409. }
  410. }
  411. }
  412. }