TaskCreateReport.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEditor;
  5. namespace YooAsset.Editor
  6. {
  7. [TaskAttribute("创建构建报告文件")]
  8. public class TaskCreateReport : IBuildTask
  9. {
  10. void IBuildTask.Run(BuildContext context)
  11. {
  12. var buildParameters = context.GetContextObject<BuildParametersContext>();
  13. var buildMapContext = context.GetContextObject<BuildMapContext>();
  14. buildParameters.StopWatch();
  15. var buildMode = buildParameters.Parameters.BuildMode;
  16. if (buildMode != EBuildMode.SimulateBuild)
  17. {
  18. CreateReportFile(buildParameters, buildMapContext);
  19. }
  20. float buildSeconds = buildParameters.GetBuildingSeconds();
  21. BuildRunner.Info($"Build time consuming {buildSeconds} seconds.");
  22. }
  23. private void CreateReportFile(BuildParametersContext buildParameters, BuildMapContext buildMapContext)
  24. {
  25. PatchManifest patchManifest = AssetBundleBuilderHelper.LoadPatchManifestFile(buildParameters.PipelineOutputDirectory, buildParameters.Parameters.BuildVersion);
  26. BuildReport buildReport = new BuildReport();
  27. // 概述信息
  28. {
  29. #if UNITY_2019_4_OR_NEWER
  30. UnityEditor.PackageManager.PackageInfo packageInfo = UnityEditor.PackageManager.PackageInfo.FindForAssembly(typeof(BuildReport).Assembly);
  31. if (packageInfo != null)
  32. buildReport.Summary.YooVersion = packageInfo.version;
  33. #endif
  34. buildReport.Summary.UnityVersion = UnityEngine.Application.unityVersion;
  35. buildReport.Summary.BuildDate = DateTime.Now.ToString();
  36. buildReport.Summary.BuildSeconds = (int)buildParameters.GetBuildingSeconds();
  37. buildReport.Summary.BuildTarget = buildParameters.Parameters.BuildTarget;
  38. buildReport.Summary.BuildPipeline = buildParameters.Parameters.BuildPipeline;
  39. buildReport.Summary.BuildMode = buildParameters.Parameters.BuildMode;
  40. buildReport.Summary.BuildVersion = buildParameters.Parameters.BuildVersion;
  41. buildReport.Summary.BuildinTags = buildParameters.Parameters.BuildinTags;
  42. buildReport.Summary.EnableAddressable = buildParameters.Parameters.EnableAddressable;
  43. buildReport.Summary.CopyBuildinTagFiles = buildParameters.Parameters.CopyBuildinTagFiles;
  44. buildReport.Summary.EncryptionServicesClassName = buildParameters.Parameters.EncryptionServices == null ?
  45. "null" : buildParameters.Parameters.EncryptionServices.GetType().FullName;
  46. // 构建参数
  47. buildReport.Summary.OutputNameStyle = buildParameters.Parameters.OutputNameStyle;
  48. buildReport.Summary.CompressOption = buildParameters.Parameters.CompressOption;
  49. buildReport.Summary.DisableWriteTypeTree = buildParameters.Parameters.DisableWriteTypeTree;
  50. buildReport.Summary.IgnoreTypeTreeChanges = buildParameters.Parameters.IgnoreTypeTreeChanges;
  51. // 构建结果
  52. buildReport.Summary.AssetFileTotalCount = buildMapContext.AssetFileCount;
  53. buildReport.Summary.MainAssetTotalCount = GetMainAssetCount(patchManifest);
  54. buildReport.Summary.AllBundleTotalCount = GetAllBundleCount(patchManifest);
  55. buildReport.Summary.AllBundleTotalSize = GetAllBundleSize(patchManifest);
  56. buildReport.Summary.BuildinBundleTotalCount = GetBuildinBundleCount(patchManifest);
  57. buildReport.Summary.BuildinBundleTotalSize = GetBuildinBundleSize(patchManifest);
  58. buildReport.Summary.EncryptedBundleTotalCount = GetEncryptedBundleCount(patchManifest);
  59. buildReport.Summary.EncryptedBundleTotalSize = GetEncryptedBundleSize(patchManifest);
  60. buildReport.Summary.RawBundleTotalCount = GetRawBundleCount(patchManifest);
  61. buildReport.Summary.RawBundleTotalSize = GetRawBundleSize(patchManifest);
  62. }
  63. // 资源对象列表
  64. buildReport.AssetInfos = new List<ReportAssetInfo>(patchManifest.AssetList.Count);
  65. foreach (var patchAsset in patchManifest.AssetList)
  66. {
  67. var mainBundle = patchManifest.BundleList[patchAsset.BundleID];
  68. ReportAssetInfo reportAssetInfo = new ReportAssetInfo();
  69. reportAssetInfo.Address = patchAsset.Address;
  70. reportAssetInfo.AssetPath = patchAsset.AssetPath;
  71. reportAssetInfo.AssetTags = patchAsset.AssetTags;
  72. reportAssetInfo.AssetGUID = AssetDatabase.AssetPathToGUID(patchAsset.AssetPath);
  73. reportAssetInfo.MainBundleName = mainBundle.BundleName;
  74. reportAssetInfo.MainBundleSize = mainBundle.FileSize;
  75. reportAssetInfo.DependBundles = GetDependBundles(patchManifest, patchAsset);
  76. reportAssetInfo.DependAssets = GetDependAssets(buildMapContext, mainBundle.BundleName, patchAsset.AssetPath);
  77. buildReport.AssetInfos.Add(reportAssetInfo);
  78. }
  79. // 资源包列表
  80. buildReport.BundleInfos = new List<ReportBundleInfo>(patchManifest.BundleList.Count);
  81. foreach (var patchBundle in patchManifest.BundleList)
  82. {
  83. ReportBundleInfo reportBundleInfo = new ReportBundleInfo();
  84. reportBundleInfo.BundleName = patchBundle.BundleName;
  85. reportBundleInfo.FileName = patchBundle.FileName;
  86. reportBundleInfo.FileHash = patchBundle.FileHash;
  87. reportBundleInfo.FileCRC = patchBundle.FileCRC;
  88. reportBundleInfo.FileSize = patchBundle.FileSize;
  89. reportBundleInfo.Tags = patchBundle.Tags;
  90. reportBundleInfo.Flags = patchBundle.Flags;
  91. buildReport.BundleInfos.Add(reportBundleInfo);
  92. }
  93. // 删除旧文件
  94. string filePath = $"{buildParameters.PipelineOutputDirectory}/{YooAssetSettingsData.GetReportFileName(buildParameters.Parameters.BuildVersion)}";
  95. if (File.Exists(filePath))
  96. File.Delete(filePath);
  97. // 序列化文件
  98. BuildReport.Serialize(filePath, buildReport);
  99. BuildRunner.Log($"资源构建报告文件创建完成:{filePath}");
  100. }
  101. /// <summary>
  102. /// 获取资源对象依赖的所有资源包
  103. /// </summary>
  104. private List<string> GetDependBundles(PatchManifest patchManifest, PatchAsset patchAsset)
  105. {
  106. List<string> dependBundles = new List<string>(patchAsset.DependIDs.Length);
  107. foreach (int index in patchAsset.DependIDs)
  108. {
  109. string dependBundleName = patchManifest.BundleList[index].BundleName;
  110. dependBundles.Add(dependBundleName);
  111. }
  112. return dependBundles;
  113. }
  114. /// <summary>
  115. /// 获取资源对象依赖的其它所有资源
  116. /// </summary>
  117. private List<string> GetDependAssets(BuildMapContext buildMapContext, string bundleName, string assetPath)
  118. {
  119. List<string> result = new List<string>();
  120. if (buildMapContext.TryGetBundleInfo(bundleName, out BuildBundleInfo bundleInfo))
  121. {
  122. BuildAssetInfo findAssetInfo = null;
  123. foreach (var buildinAsset in bundleInfo.BuildinAssets)
  124. {
  125. if (buildinAsset.AssetPath == assetPath)
  126. {
  127. findAssetInfo = buildinAsset;
  128. break;
  129. }
  130. }
  131. if (findAssetInfo == null)
  132. {
  133. throw new Exception($"Not found asset {assetPath} in bunlde {bundleName}");
  134. }
  135. foreach (var dependAssetInfo in findAssetInfo.AllDependAssetInfos)
  136. {
  137. result.Add(dependAssetInfo.AssetPath);
  138. }
  139. }
  140. else
  141. {
  142. throw new Exception($"Not found bundle : {bundleName}");
  143. }
  144. return result;
  145. }
  146. private int GetMainAssetCount(PatchManifest patchManifest)
  147. {
  148. return patchManifest.AssetList.Count;
  149. }
  150. private int GetAllBundleCount(PatchManifest patchManifest)
  151. {
  152. return patchManifest.BundleList.Count;
  153. }
  154. private long GetAllBundleSize(PatchManifest patchManifest)
  155. {
  156. long fileBytes = 0;
  157. foreach (var patchBundle in patchManifest.BundleList)
  158. {
  159. fileBytes += patchBundle.FileSize;
  160. }
  161. return fileBytes;
  162. }
  163. private int GetBuildinBundleCount(PatchManifest patchManifest)
  164. {
  165. int fileCount = 0;
  166. foreach (var patchBundle in patchManifest.BundleList)
  167. {
  168. if (patchBundle.IsBuildin)
  169. fileCount++;
  170. }
  171. return fileCount;
  172. }
  173. private long GetBuildinBundleSize(PatchManifest patchManifest)
  174. {
  175. long fileBytes = 0;
  176. foreach (var patchBundle in patchManifest.BundleList)
  177. {
  178. if (patchBundle.IsBuildin)
  179. fileBytes += patchBundle.FileSize;
  180. }
  181. return fileBytes;
  182. }
  183. private int GetEncryptedBundleCount(PatchManifest patchManifest)
  184. {
  185. int fileCount = 0;
  186. foreach (var patchBundle in patchManifest.BundleList)
  187. {
  188. if (patchBundle.IsEncrypted)
  189. fileCount++;
  190. }
  191. return fileCount;
  192. }
  193. private long GetEncryptedBundleSize(PatchManifest patchManifest)
  194. {
  195. long fileBytes = 0;
  196. foreach (var patchBundle in patchManifest.BundleList)
  197. {
  198. if (patchBundle.IsEncrypted)
  199. fileBytes += patchBundle.FileSize;
  200. }
  201. return fileBytes;
  202. }
  203. private int GetRawBundleCount(PatchManifest patchManifest)
  204. {
  205. int fileCount = 0;
  206. foreach (var patchBundle in patchManifest.BundleList)
  207. {
  208. if (patchBundle.IsRawFile)
  209. fileCount++;
  210. }
  211. return fileCount;
  212. }
  213. private long GetRawBundleSize(PatchManifest patchManifest)
  214. {
  215. long fileBytes = 0;
  216. foreach (var patchBundle in patchManifest.BundleList)
  217. {
  218. if (patchBundle.IsRawFile)
  219. fileBytes += patchBundle.FileSize;
  220. }
  221. return fileBytes;
  222. }
  223. }
  224. }