123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using System;
- using System.Linq;
- using System.IO;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEditor;
- using UnityEngine;
- namespace YooAsset.Editor
- {
- [TaskAttribute("验证构建结果")]
- public class TaskVerifyBuildResult : IBuildTask
- {
- void IBuildTask.Run(BuildContext context)
- {
- var buildParametersContext = context.GetContextObject<BuildParametersContext>();
-
- if (buildParametersContext.Parameters.BuildMode == EBuildMode.SimulateBuild)
- return;
-
- if (buildParametersContext.Parameters.VerifyBuildingResult)
- {
- var buildResultContext = context.GetContextObject<TaskBuilding.BuildResultContext>();
- VerifyingBuildingResult(context, buildResultContext.UnityManifest);
- }
- }
-
-
-
- private void VerifyingBuildingResult(BuildContext context, AssetBundleManifest unityManifest)
- {
- var buildParameters = context.GetContextObject<BuildParametersContext>();
- var buildMapContext = context.GetContextObject<BuildMapContext>();
- string[] buildedBundles = unityManifest.GetAllAssetBundles();
-
- string[] mapBundles = buildMapContext.BundleInfos.Where(t => t.IsRawFile == false).Select(t => t.BundleName).ToArray();
-
- List<string> exceptBundleList1 = buildedBundles.Except(mapBundles).ToList();
- if (exceptBundleList1.Count > 0)
- {
- foreach (var exceptBundle in exceptBundleList1)
- {
- Debug.LogWarning($"差异资源包: {exceptBundle}");
- }
- throw new System.Exception("存在差异资源包!请查看警告信息!");
- }
-
- List<string> exceptBundleList2 = mapBundles.Except(buildedBundles).ToList();
- if (exceptBundleList2.Count > 0)
- {
- foreach (var exceptBundle in exceptBundleList2)
- {
- Debug.LogWarning($"差异资源包: {exceptBundle}");
- }
- throw new System.Exception("存在差异资源包!请查看警告信息!");
- }
-
- bool isPass = true;
- var buildMode = buildParameters.Parameters.BuildMode;
- if (buildMode == EBuildMode.ForceRebuild || buildMode == EBuildMode.IncrementalBuild)
- {
- int progressValue = 0;
- foreach (var buildedBundle in buildedBundles)
- {
- string filePath = $"{buildParameters.PipelineOutputDirectory}/{buildedBundle}";
- string[] buildedAssetPaths = GetAssetBundleAllAssets(filePath);
- string[] mapAssetPaths = buildMapContext.GetBuildinAssetPaths(buildedBundle);
- if (mapAssetPaths.Length != buildedAssetPaths.Length)
- {
- Debug.LogWarning($"构建的Bundle文件内的资源对象数量和预期不匹配 : {buildedBundle}");
- var exceptAssetList1 = mapAssetPaths.Except(buildedAssetPaths).ToList();
- foreach (var excpetAsset in exceptAssetList1)
- {
- Debug.LogWarning($"构建失败的资源对象路径为 : {excpetAsset}");
- }
- var exceptAssetList2 = buildedAssetPaths.Except(mapAssetPaths).ToList();
- foreach (var excpetAsset in exceptAssetList2)
- {
- Debug.LogWarning($"构建失败的资源对象路径为 : {excpetAsset}");
- }
- isPass = false;
- continue;
- }
- EditorTools.DisplayProgressBar("验证构建结果", ++progressValue, buildedBundles.Length);
- }
- EditorTools.ClearProgressBar();
- if (isPass == false)
- {
- throw new Exception("构建结果验证没有通过,请参考警告日志!");
- }
- }
- BuildRunner.Log("构建结果验证成功!");
- }
-
-
-
- private string[] GetAssetBundleAllAssets(string filePath)
- {
- string manifestFilePath = $"{filePath}.manifest";
- List<string> assetLines = new List<string>();
- using (StreamReader reader = File.OpenText(manifestFilePath))
- {
- string content;
- bool findTarget = false;
- while (null != (content = reader.ReadLine()))
- {
- if (content.StartsWith("Dependencies:"))
- break;
- if (findTarget == false && content.StartsWith("Assets:"))
- findTarget = true;
- if (findTarget)
- {
- if (content.StartsWith("- "))
- {
- string assetPath = content.TrimStart("- ".ToCharArray());
- assetLines.Add(assetPath);
- }
- }
- }
- }
- return assetLines.ToArray();
- }
- }
- }
|