TaskVerifyBuildResult.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Linq;
  3. using System.IO;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEditor;
  7. using UnityEngine;
  8. namespace YooAsset.Editor
  9. {
  10. [TaskAttribute("验证构建结果")]
  11. public class TaskVerifyBuildResult : IBuildTask
  12. {
  13. void IBuildTask.Run(BuildContext context)
  14. {
  15. var buildParametersContext = context.GetContextObject<BuildParametersContext>();
  16. // 模拟构建模式下跳过验证
  17. if (buildParametersContext.Parameters.BuildMode == EBuildMode.SimulateBuild)
  18. return;
  19. // 验证构建结果
  20. if (buildParametersContext.Parameters.VerifyBuildingResult)
  21. {
  22. var buildResultContext = context.GetContextObject<TaskBuilding.BuildResultContext>();
  23. VerifyingBuildingResult(context, buildResultContext.UnityManifest);
  24. }
  25. }
  26. /// <summary>
  27. /// 验证构建结果
  28. /// </summary>
  29. private void VerifyingBuildingResult(BuildContext context, AssetBundleManifest unityManifest)
  30. {
  31. var buildParameters = context.GetContextObject<BuildParametersContext>();
  32. var buildMapContext = context.GetContextObject<BuildMapContext>();
  33. string[] buildedBundles = unityManifest.GetAllAssetBundles();
  34. // 1. 过滤掉原生Bundle
  35. string[] mapBundles = buildMapContext.BundleInfos.Where(t => t.IsRawFile == false).Select(t => t.BundleName).ToArray();
  36. // 2. 验证Bundle
  37. List<string> exceptBundleList1 = buildedBundles.Except(mapBundles).ToList();
  38. if (exceptBundleList1.Count > 0)
  39. {
  40. foreach (var exceptBundle in exceptBundleList1)
  41. {
  42. Debug.LogWarning($"差异资源包: {exceptBundle}");
  43. }
  44. throw new System.Exception("存在差异资源包!请查看警告信息!");
  45. }
  46. // 3. 验证Bundle
  47. List<string> exceptBundleList2 = mapBundles.Except(buildedBundles).ToList();
  48. if (exceptBundleList2.Count > 0)
  49. {
  50. foreach (var exceptBundle in exceptBundleList2)
  51. {
  52. Debug.LogWarning($"差异资源包: {exceptBundle}");
  53. }
  54. throw new System.Exception("存在差异资源包!请查看警告信息!");
  55. }
  56. // 4. 验证Asset
  57. bool isPass = true;
  58. var buildMode = buildParameters.Parameters.BuildMode;
  59. if (buildMode == EBuildMode.ForceRebuild || buildMode == EBuildMode.IncrementalBuild)
  60. {
  61. int progressValue = 0;
  62. foreach (var buildedBundle in buildedBundles)
  63. {
  64. string filePath = $"{buildParameters.PipelineOutputDirectory}/{buildedBundle}";
  65. string[] buildedAssetPaths = GetAssetBundleAllAssets(filePath);
  66. string[] mapAssetPaths = buildMapContext.GetBuildinAssetPaths(buildedBundle);
  67. if (mapAssetPaths.Length != buildedAssetPaths.Length)
  68. {
  69. Debug.LogWarning($"构建的Bundle文件内的资源对象数量和预期不匹配 : {buildedBundle}");
  70. var exceptAssetList1 = mapAssetPaths.Except(buildedAssetPaths).ToList();
  71. foreach (var excpetAsset in exceptAssetList1)
  72. {
  73. Debug.LogWarning($"构建失败的资源对象路径为 : {excpetAsset}");
  74. }
  75. var exceptAssetList2 = buildedAssetPaths.Except(mapAssetPaths).ToList();
  76. foreach (var excpetAsset in exceptAssetList2)
  77. {
  78. Debug.LogWarning($"构建失败的资源对象路径为 : {excpetAsset}");
  79. }
  80. isPass = false;
  81. continue;
  82. }
  83. EditorTools.DisplayProgressBar("验证构建结果", ++progressValue, buildedBundles.Length);
  84. }
  85. EditorTools.ClearProgressBar();
  86. if (isPass == false)
  87. {
  88. throw new Exception("构建结果验证没有通过,请参考警告日志!");
  89. }
  90. }
  91. BuildRunner.Log("构建结果验证成功!");
  92. }
  93. /// <summary>
  94. /// 解析.manifest文件并获取资源列表
  95. /// </summary>
  96. private string[] GetAssetBundleAllAssets(string filePath)
  97. {
  98. string manifestFilePath = $"{filePath}.manifest";
  99. List<string> assetLines = new List<string>();
  100. using (StreamReader reader = File.OpenText(manifestFilePath))
  101. {
  102. string content;
  103. bool findTarget = false;
  104. while (null != (content = reader.ReadLine()))
  105. {
  106. if (content.StartsWith("Dependencies:"))
  107. break;
  108. if (findTarget == false && content.StartsWith("Assets:"))
  109. findTarget = true;
  110. if (findTarget)
  111. {
  112. if (content.StartsWith("- "))
  113. {
  114. string assetPath = content.TrimStart("- ".ToCharArray());
  115. assetLines.Add(assetPath);
  116. }
  117. }
  118. }
  119. }
  120. return assetLines.ToArray();
  121. }
  122. }
  123. }