ContextBuilder.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using UnityEditor;
  7. namespace XGame.Editor.Build
  8. {
  9. internal class ContextBuilder
  10. {
  11. private BuildContext _context;
  12. private BuildConfig _config;
  13. private bool _collectCmd;
  14. public ContextBuilder(BuildConfig config, bool collectCmd)
  15. {
  16. _config = config;
  17. _collectCmd = collectCmd;
  18. }
  19. public BuildContext GetProduct()
  20. {
  21. return _context;
  22. }
  23. public bool Build()
  24. {
  25. //创建文件夹
  26. if (!Directory.Exists(_config.project.outputPath))
  27. {
  28. try
  29. {
  30. Directory.CreateDirectory(_config.project.outputPath);
  31. }
  32. catch (Exception ex)
  33. {
  34. BuildLog.Exception(ex);
  35. BuildLog.Error($"打包输出目录错误,无法创建文件夹。{_config.project.outputPath}");
  36. OnFailed(BuildErrorCode.OutputPathInvalid);
  37. return false;
  38. }
  39. }
  40. //转换Version
  41. var versionName = _config.project.bundleVersion;
  42. if (!Version.TryParse(versionName, out var version))
  43. {
  44. var idx = versionName.LastIndexOf('.');
  45. if (idx > 0)
  46. {
  47. //第一次解析失败,去掉最后一个"."后面的字符,再重试一次
  48. versionName = versionName.Substring(0, idx);
  49. try
  50. {
  51. version = Version.Parse(versionName);
  52. }
  53. catch (Exception ex)
  54. {
  55. BuildLog.Exception(ex);
  56. BuildLog.Error($"Bundle Version failure. Version:{_config.project.bundleVersion}");
  57. OnFailed(BuildErrorCode.BundleVersionFailure);
  58. return false;
  59. }
  60. }
  61. else
  62. {
  63. BuildLog.Error($"Bundle Version Can't Find Separator. Version:{_config.project.bundleVersion}");
  64. OnFailed(BuildErrorCode.BundleVersionFailure);
  65. return false;
  66. }
  67. }
  68. //string callback = null;
  69. //if (_config.callback != null)
  70. //{
  71. // //回调转成字符串
  72. // callback = $"<callback type:{_config.callback.Method.ReflectedType.AssemblyQualifiedName} method:{_config.callback.Method.Name} /callback>";
  73. // //清空原回调
  74. // _config.callback = null;
  75. //}
  76. //app输出路径
  77. var output = GetBuildAppPath(_config, version);
  78. _context = new BuildContext()
  79. {
  80. config = _config,
  81. appOutputPath = output,
  82. version = version,
  83. //complatedEvent = callback,
  84. };
  85. if (_collectCmd)
  86. {
  87. _context.cmdTypeNames = CollectCmdTypeNames();
  88. }
  89. return true;
  90. }
  91. private void OnFailed(BuildErrorCode errorCode)
  92. {
  93. BuildUtils.BuildCompleted(_config.callback, errorCode);
  94. if (_config.editor.autoExit)
  95. {
  96. BuildUtils.ExitApplication(errorCode);
  97. }
  98. }
  99. private string GetBuildAppPath(BuildConfig config, Version version)
  100. {
  101. var output = Path.Combine(config.project.outputPath, $"{config.project.productName}_v{version}");
  102. string ext;
  103. switch (config.target)
  104. {
  105. case BuildTarget.Android:
  106. ext = config.android.exportAsGoogleAndroidProject ? string.Empty : ".apk";
  107. break;
  108. case BuildTarget.iOS:
  109. ext = string.Empty;
  110. break;
  111. case BuildTarget.StandaloneWindows:
  112. case BuildTarget.StandaloneWindows64:
  113. if (Directory.Exists(output))
  114. {
  115. UnityEditor.FileUtil.DeleteFileOrDirectory(output);
  116. }
  117. Directory.CreateDirectory(output);
  118. ext = $"{config.project.productName}.exe";
  119. break;
  120. case BuildTarget.StandaloneOSX:
  121. if (Directory.Exists(output))
  122. {
  123. UnityEditor.FileUtil.DeleteFileOrDirectory(output);
  124. }
  125. Directory.CreateDirectory(output);
  126. ext = $"{config.project.productName}";
  127. break;
  128. default:
  129. ext = string.Empty;
  130. break;
  131. }
  132. if (!string.IsNullOrEmpty(ext))
  133. {
  134. output = Path.Combine(output, ext);
  135. }
  136. return output;
  137. }
  138. private List<string> CollectCmdTypeNames()
  139. {
  140. var cmdType = typeof(IBuildCommand);
  141. var assembly = cmdType.Assembly;
  142. var types = assembly.GetTypes();
  143. var cmdTypesMap = new Dictionary<uint, Type>();
  144. foreach (var type in types)
  145. {
  146. if (type.IsClass && cmdType.IsAssignableFrom(type))
  147. {
  148. if (type.IsAbstract) continue;
  149. var attribute = type.GetCustomAttribute<BuildCommandAttribute>();
  150. if (attribute != null)
  151. {
  152. var priority = attribute.Priority;
  153. if (cmdTypesMap.ContainsKey(priority))
  154. {
  155. BuildLog.Error($"BuildCommand Priority Repeated. Last:{cmdTypesMap[priority]} Next:{type}");
  156. }
  157. else
  158. {
  159. cmdTypesMap.Add(attribute.Priority, type);
  160. }
  161. }
  162. }
  163. }
  164. var prioritys = cmdTypesMap.Keys.ToList();
  165. prioritys.Sort((a, b) => a.CompareTo(b));
  166. var typeNames = new List<string>();
  167. foreach (var priority in prioritys)
  168. {
  169. typeNames.Add(cmdTypesMap[priority].AssemblyQualifiedName);
  170. }
  171. return typeNames;
  172. }
  173. }
  174. }