From 4ea52a92875e14041bc4d003b5a6986d949bcf14 Mon Sep 17 00:00:00 2001 From: hevinci Date: Wed, 3 Aug 2022 20:47:43 +0800 Subject: [PATCH] Update AssetBundleBuilder --- .../AssetBundleBuilder/BuildBundleInfo.cs | 20 +++++++ .../BuildReport/ReportBundleInfo.cs | 13 +++-- .../BuildTasks/TaskBuilding.cs | 40 +++++++++++--- .../BuildTasks/TaskBuilding_SBP.cs | 32 ++++++++++- .../BuildTasks/TaskCreatePatchManifest.cs | 55 ++++++------------- .../BuildTasks/TaskCreateReport.cs | 17 +++--- .../BuildTasks/TaskVerifyBuildResult.cs | 4 +- .../BuildTasks/TaskVerifyBuildResult_SBP.cs | 2 +- 8 files changed, 121 insertions(+), 62 deletions(-) diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildBundleInfo.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildBundleInfo.cs index b5b1b70..9308d2c 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildBundleInfo.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildBundleInfo.cs @@ -35,6 +35,26 @@ namespace YooAsset.Editor } } + /// + /// 构建内容哈希值 + /// + public string ContentHash { set; get; } = "00000000000000000000000000000000"; //32位 + + /// + /// 文件哈希值 + /// + public string FileHash { set; get; } = "00000000000000000000000000000000"; //32位 + + /// + /// 文件CRC32 + /// + public string FileCRC { set; get; } = "00000000"; //8位 + + /// + /// 文件大小 + /// + public long FileSize { set; get; } = 0; + public BuildBundleInfo(string bundleName) { diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildReport/ReportBundleInfo.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildReport/ReportBundleInfo.cs index 9f5cda7..96ee204 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildReport/ReportBundleInfo.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildReport/ReportBundleInfo.cs @@ -34,19 +34,24 @@ namespace YooAsset.Editor public string FileName; /// - /// 哈希值 + /// 内容哈希值 /// - public string Hash; + public string ContentHash; + + /// + /// 文件哈希值 + /// + public string FileHash; /// /// 文件校验码 /// - public string CRC; + public string FileCRC; /// /// 文件大小(字节数) /// - public long SizeBytes; + public long FileSize; /// /// Tags diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskBuilding.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskBuilding.cs index f5c150f..4f04d51 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskBuilding.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskBuilding.cs @@ -10,7 +10,7 @@ namespace YooAsset.Editor [TaskAttribute("资源构建内容打包")] public class TaskBuilding : IBuildTask { - public class UnityManifestContext : IContextObject + public class BuildResultContext : IContextObject { public AssetBundleManifest UnityManifest; } @@ -26,19 +26,19 @@ namespace YooAsset.Editor return; BuildAssetBundleOptions opt = buildParametersContext.GetPipelineBuildOptions(); - AssetBundleManifest unityManifest = BuildPipeline.BuildAssetBundles(buildParametersContext.PipelineOutputDirectory, buildMapContext.GetPipelineBuilds(), opt, buildParametersContext.Parameters.BuildTarget); - if (unityManifest == null) + AssetBundleManifest buildResults = BuildPipeline.BuildAssetBundles(buildParametersContext.PipelineOutputDirectory, buildMapContext.GetPipelineBuilds(), opt, buildParametersContext.Parameters.BuildTarget); + if (buildResults == null) throw new Exception("构建过程中发生错误!"); BuildRunner.Log("Unity引擎打包成功!"); - UnityManifestContext unityManifestContext = new UnityManifestContext(); - unityManifestContext.UnityManifest = unityManifest; - context.SetContextObject(unityManifestContext); + BuildResultContext buildResultContext = new BuildResultContext(); + buildResultContext.UnityManifest = buildResults; + context.SetContextObject(buildResultContext); - // 拷贝原生文件 if (buildMode == EBuildMode.ForceRebuild || buildMode == EBuildMode.IncrementalBuild) { CopyRawBundle(buildMapContext, buildParametersContext); + UpdateBuildBundleInfo(buildMapContext, buildParametersContext, buildResultContext); } } @@ -60,5 +60,31 @@ namespace YooAsset.Editor } } } + + /// + /// 更新构建结果 + /// + private void UpdateBuildBundleInfo(BuildMapContext buildMapContext, BuildParametersContext buildParametersContext, BuildResultContext buildResult) + { + foreach (var bundleInfo in buildMapContext.BundleInfos) + { + string filePath = $"{buildParametersContext.PipelineOutputDirectory}/{bundleInfo.BundleName}"; + bundleInfo.FileHash = HashUtility.FileMD5(filePath); + bundleInfo.FileCRC = HashUtility.FileCRC32(filePath); + bundleInfo.FileSize = FileUtility.GetFileSize(filePath); + if (bundleInfo.IsRawFile) + { + bundleInfo.ContentHash = bundleInfo.FileHash; + } + else + { + var hash = buildResult.UnityManifest.GetAssetBundleHash(bundleInfo.BundleName); + if (hash.isValid) + bundleInfo.ContentHash = hash.ToString(); + else + throw new Exception($"Not found bundle in build result : {bundleInfo.BundleName}"); + } + } + } } } \ No newline at end of file diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskBuilding_SBP.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskBuilding_SBP.cs index d274a0d..7cda86e 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskBuilding_SBP.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskBuilding_SBP.cs @@ -12,7 +12,7 @@ namespace YooAsset.Editor [TaskAttribute("资源构建内容打包")] public class TaskBuilding_SBP : IBuildTask { - public class SBPBuildResultContext : IContextObject + public class BuildResultContext : IContextObject { public IBundleBuildResults Results; } @@ -42,14 +42,14 @@ namespace YooAsset.Editor } BuildRunner.Log("Unity引擎打包成功!"); - SBPBuildResultContext buildResultContext = new SBPBuildResultContext(); + BuildResultContext buildResultContext = new BuildResultContext(); buildResultContext.Results = buildResults; context.SetContextObject(buildResultContext); - // 拷贝原生文件 if (buildMode == EBuildMode.ForceRebuild || buildMode == EBuildMode.IncrementalBuild) { CopyRawBundle(buildMapContext, buildParametersContext); + UpdateBuildBundleInfo(buildMapContext, buildParametersContext, buildResultContext); } } @@ -71,5 +71,31 @@ namespace YooAsset.Editor } } } + + /// + /// 更新构建结果 + /// + private void UpdateBuildBundleInfo(BuildMapContext buildMapContext, BuildParametersContext buildParametersContext, BuildResultContext buildResult) + { + foreach (var bundleInfo in buildMapContext.BundleInfos) + { + string filePath = $"{buildParametersContext.PipelineOutputDirectory}/{bundleInfo.BundleName}"; + bundleInfo.FileHash = HashUtility.FileMD5(filePath); + bundleInfo.FileCRC = HashUtility.FileCRC32(filePath); + bundleInfo.FileSize = FileUtility.GetFileSize(filePath); + if (bundleInfo.IsRawFile) + { + bundleInfo.ContentHash = bundleInfo.FileHash; + } + else + { + // 注意:当资源包的依赖列表发生变化的时候,ContentHash也会发生变化! + if (buildResult.Results.BundleInfos.TryGetValue(bundleInfo.BundleName, out var value)) + bundleInfo.ContentHash = value.Hash.ToString(); + else + throw new Exception($"Not found bundle in build result : {bundleInfo.BundleName}"); + } + } + } } } \ No newline at end of file diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskCreatePatchManifest.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskCreatePatchManifest.cs index aae167e..3d3ab13 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskCreatePatchManifest.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskCreatePatchManifest.cs @@ -22,8 +22,6 @@ namespace YooAsset.Editor private void CreatePatchManifestFile(BuildContext context) { var buildParameters = context.GetContextObject(); - var buildMapContext = context.GetContextObject(); - var encryptionContext = context.GetContextObject(); int resourceVersion = buildParameters.Parameters.BuildVersion; // 创建新补丁清单 @@ -33,13 +31,13 @@ namespace YooAsset.Editor patchManifest.EnableAddressable = buildParameters.Parameters.EnableAddressable; patchManifest.OutputNameStyle = (int)buildParameters.Parameters.OutputNameStyle; patchManifest.BuildinTags = buildParameters.Parameters.BuildinTags; - patchManifest.BundleList = GetAllPatchBundle(buildParameters, buildMapContext, encryptionContext); - patchManifest.AssetList = GetAllPatchAsset(buildParameters, buildMapContext, patchManifest); + patchManifest.BundleList = GetAllPatchBundle(context); + patchManifest.AssetList = GetAllPatchAsset(context, patchManifest); // 更新Unity内置资源包的引用关系 if (buildParameters.Parameters.BuildPipeline == EBuildPipeline.ScriptableBuildPipeline) { - var buildResultContext = context.GetContextObject(); + var buildResultContext = context.GetContextObject(); UpdateBuiltInBundleReference(patchManifest, buildResultContext.Results); } @@ -64,27 +62,28 @@ namespace YooAsset.Editor /// /// 获取资源包列表 /// - private List GetAllPatchBundle(BuildParametersContext buildParameters, BuildMapContext buildMapContext, - TaskEncryption.EncryptionContext encryptionContext) + private List GetAllPatchBundle(BuildContext context) { + var buildParameters = context.GetContextObject(); + var buildMapContext = context.GetContextObject(); + var encryptionContext = context.GetContextObject(); + List result = new List(1000); List buildinTags = buildParameters.Parameters.GetBuildinTags(); - var buildMode = buildParameters.Parameters.BuildMode; - bool standardBuild = buildMode == EBuildMode.ForceRebuild || buildMode == EBuildMode.IncrementalBuild; foreach (var bundleInfo in buildMapContext.BundleInfos) { var bundleName = bundleInfo.BundleName; - string filePath = $"{buildParameters.PipelineOutputDirectory}/{bundleName}"; - string hash = GetFileHash(filePath, standardBuild); - string crc32 = GetFileCRC(filePath, standardBuild); - long size = GetFileSize(filePath, standardBuild); + string contentHash = bundleInfo.ContentHash; + string fileHash = bundleInfo.FileHash; + string fileCRC = bundleInfo.FileCRC; + long fileSize = bundleInfo.FileSize; string[] tags = buildMapContext.GetBundleTags(bundleName); bool isEncrypted = encryptionContext.IsEncryptFile(bundleName); bool isBuildin = IsBuildinBundle(tags, buildinTags); bool isRawFile = bundleInfo.IsRawFile; - PatchBundle patchBundle = new PatchBundle(bundleName, hash, crc32, size, tags); + PatchBundle patchBundle = new PatchBundle(bundleName, contentHash, fileHash, fileCRC, fileSize, tags); patchBundle.SetFlagsValue(isEncrypted, isBuildin, isRawFile); result.Add(patchBundle); } @@ -104,33 +103,15 @@ namespace YooAsset.Editor } return false; } - private string GetFileHash(string filePath, bool standardBuild) - { - if (standardBuild) - return HashUtility.FileMD5(filePath); - else - return "00000000000000000000000000000000"; //32位 - } - private string GetFileCRC(string filePath, bool standardBuild) - { - if (standardBuild) - return HashUtility.FileCRC32(filePath); - else - return "00000000"; //8位 - } - private long GetFileSize(string filePath, bool standardBuild) - { - if (standardBuild) - return FileUtility.GetFileSize(filePath); - else - return 0; - } /// /// 获取资源列表 /// - private List GetAllPatchAsset(BuildParametersContext buildParameters, BuildMapContext buildMapContext, PatchManifest patchManifest) + private List GetAllPatchAsset(BuildContext context, PatchManifest patchManifest) { + var buildParameters = context.GetContextObject(); + var buildMapContext = context.GetContextObject(); + List result = new List(1000); foreach (var bundleInfo in buildMapContext.BundleInfos) { @@ -206,7 +187,7 @@ namespace YooAsset.Editor if (conflictAssetPathList.Count > 0) { List newDependIDs = new List(patchAsset.DependIDs); - if(newDependIDs.Contains(shaderBundleId) == false) + if (newDependIDs.Contains(shaderBundleId) == false) newDependIDs.Add(shaderBundleId); patchAsset.DependIDs = newDependIDs.ToArray(); } diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskCreateReport.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskCreateReport.cs index dc68407..56bfb88 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskCreateReport.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskCreateReport.cs @@ -81,7 +81,7 @@ namespace YooAsset.Editor reportAssetInfo.AssetTags = patchAsset.AssetTags; reportAssetInfo.AssetGUID = AssetDatabase.AssetPathToGUID(patchAsset.AssetPath); reportAssetInfo.MainBundleName = mainBundle.BundleName; - reportAssetInfo.MainBundleSize = mainBundle.SizeBytes; + reportAssetInfo.MainBundleSize = mainBundle.FileSize; reportAssetInfo.DependBundles = GetDependBundles(patchManifest, patchAsset); reportAssetInfo.DependAssets = GetDependAssets(buildMapContext, mainBundle.BundleName, patchAsset.AssetPath); buildReport.AssetInfos.Add(reportAssetInfo); @@ -94,9 +94,10 @@ namespace YooAsset.Editor ReportBundleInfo reportBundleInfo = new ReportBundleInfo(); reportBundleInfo.BundleName = patchBundle.BundleName; reportBundleInfo.FileName = patchBundle.FileName; - reportBundleInfo.Hash = patchBundle.Hash; - reportBundleInfo.CRC = patchBundle.CRC; - reportBundleInfo.SizeBytes = patchBundle.SizeBytes; + reportBundleInfo.ContentHash = patchBundle.ContentHash; + reportBundleInfo.FileHash = patchBundle.FileHash; + reportBundleInfo.FileCRC = patchBundle.FileCRC; + reportBundleInfo.FileSize = patchBundle.FileSize; reportBundleInfo.Tags = patchBundle.Tags; reportBundleInfo.Flags = patchBundle.Flags; buildReport.BundleInfos.Add(reportBundleInfo); @@ -172,7 +173,7 @@ namespace YooAsset.Editor long fileBytes = 0; foreach (var patchBundle in patchManifest.BundleList) { - fileBytes += patchBundle.SizeBytes; + fileBytes += patchBundle.FileSize; } return fileBytes; } @@ -192,7 +193,7 @@ namespace YooAsset.Editor foreach (var patchBundle in patchManifest.BundleList) { if (patchBundle.IsBuildin) - fileBytes += patchBundle.SizeBytes; + fileBytes += patchBundle.FileSize; } return fileBytes; } @@ -212,7 +213,7 @@ namespace YooAsset.Editor foreach (var patchBundle in patchManifest.BundleList) { if (patchBundle.IsEncrypted) - fileBytes += patchBundle.SizeBytes; + fileBytes += patchBundle.FileSize; } return fileBytes; } @@ -232,7 +233,7 @@ namespace YooAsset.Editor foreach (var patchBundle in patchManifest.BundleList) { if (patchBundle.IsRawFile) - fileBytes += patchBundle.SizeBytes; + fileBytes += patchBundle.FileSize; } return fileBytes; } diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskVerifyBuildResult.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskVerifyBuildResult.cs index 2950e3e..8816f0c 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskVerifyBuildResult.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskVerifyBuildResult.cs @@ -22,8 +22,8 @@ namespace YooAsset.Editor // 验证构建结果 if (buildParametersContext.Parameters.VerifyBuildingResult) { - var unityManifestContext = context.GetContextObject(); - VerifyingBuildingResult(context, unityManifestContext.UnityManifest); + var buildResultContext = context.GetContextObject(); + VerifyingBuildingResult(context, buildResultContext.UnityManifest); } } diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskVerifyBuildResult_SBP.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskVerifyBuildResult_SBP.cs index a1b2078..9b52d8e 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskVerifyBuildResult_SBP.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskVerifyBuildResult_SBP.cs @@ -23,7 +23,7 @@ namespace YooAsset.Editor // 验证构建结果 if (buildParametersContext.Parameters.VerifyBuildingResult) { - var buildResultContext = context.GetContextObject(); + var buildResultContext = context.GetContextObject(); VerifyingBuildingResult(context, buildResultContext.Results); } }