From a91f1bd02015d9aa858699c71af69ba6aa3c7fe8 Mon Sep 17 00:00:00 2001 From: hevinci Date: Wed, 16 Mar 2022 17:35:21 +0800 Subject: [PATCH] Update AssetBundleBuilder --- .../BuildReport/BuildAssetInfo.cs | 43 +++----- .../BuildReport/BuildBundleInfo.cs | 28 +---- .../BuildReport/BuildReport.cs | 38 ++++++- .../BuildTasks/TaskBuilding.cs | 6 +- .../BuildTasks/TaskCreatePatchManifest.cs | 24 ++--- .../BuildTasks/TaskCreateReadme.cs | 8 +- .../BuildTasks/TaskEncryption.cs | 4 +- .../BuildTasks/TaskGetBuildMap.cs | 102 +++++------------- 8 files changed, 100 insertions(+), 153 deletions(-) diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildReport/BuildAssetInfo.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildReport/BuildAssetInfo.cs index e3b0306..601fad4 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildReport/BuildAssetInfo.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildReport/BuildAssetInfo.cs @@ -13,17 +13,12 @@ namespace YooAsset.Editor /// /// 资源路径 /// - public string AssetPath { private set; get; } + public string AssetPath; /// - /// 资源包标签 + /// 资源包完整名称 /// - public string BundleLabel { private set; get; } - - /// - /// 资源包文件格式 - /// - public string BundleVariant { private set; get; } + public string BundleName; /// /// 是否为原生资源 @@ -46,10 +41,10 @@ namespace YooAsset.Editor public int DependCount = 0; /// - /// 依赖的所有资源信息 - /// 注意:包括零依赖资源(零依赖资源的资源包名无效) + /// 依赖的所有资源 + /// 注意:包括零依赖资源和冗余资源(资源包名无效) /// - public List AllDependAssetInfos { private set; get; } = null; + public List AllDependAssetInfos; public BuildAssetInfo(string assetPath) @@ -73,11 +68,10 @@ namespace YooAsset.Editor /// public void SetBundleLabelAndVariant(string bundleLabel, string bundleVariant) { - if (string.IsNullOrEmpty(BundleLabel) == false || string.IsNullOrEmpty(BundleVariant) == false) + if (string.IsNullOrEmpty(BundleName) == false) throw new System.Exception("Should never get here !"); - BundleLabel = bundleLabel; - BundleVariant = bundleVariant; + BundleName = AssetBundleBuilderHelper.MakeBundleName(bundleLabel, bundleVariant); } /// @@ -95,25 +89,14 @@ namespace YooAsset.Editor } /// - /// 获取资源包的完整名称 + /// 资源包名是否有效 /// - public string GetBundleName() + public bool BundleNameIsValid() { - if (string.IsNullOrEmpty(BundleLabel) || string.IsNullOrEmpty(BundleVariant)) - throw new System.ArgumentNullException(); - - return AssetBundleBuilderHelper.MakeBundleName(BundleLabel, BundleVariant); - } - - /// - /// 检测资源包名是否有效 - /// - public bool CheckBundleNameValid() - { - if (string.IsNullOrEmpty(BundleLabel) == false && string.IsNullOrEmpty(BundleVariant) == false) - return true; - else + if (string.IsNullOrEmpty(BundleName)) return false; + else + return true; } } } \ No newline at end of file diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildReport/BuildBundleInfo.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildReport/BuildBundleInfo.cs index 0422173..7d39a39 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildReport/BuildBundleInfo.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildReport/BuildBundleInfo.cs @@ -15,22 +15,12 @@ namespace YooAsset.Editor /// /// 资源包完整名称 /// - public string BundleName { private set; get; } - - /// - /// 资源包标签名 - /// - public string BundleLabel { private set; get; } - - /// - /// 资源包文件格式 - /// - public string BundleVariant { private set; get; } + public string BundleName; /// /// 包含的资源列表 /// - public readonly List Assets = new List(); + public List Assets = new List(); /// /// 是否为原生文件 @@ -49,11 +39,9 @@ namespace YooAsset.Editor } - public BuildBundleInfo(string bundleLabel, string bundleVariant) + public BuildBundleInfo(string bundleName) { - BundleLabel = bundleLabel; - BundleVariant = bundleVariant; - BundleName = AssetBundleBuilderHelper.MakeBundleName(bundleLabel, bundleVariant); + BundleName = bundleName; } /// @@ -110,14 +98,6 @@ namespace YooAsset.Editor return result.ToArray(); } - /// - /// 获取主动收集的资源路径列表 - /// - public string[] GetCollectAssetPaths() - { - return Assets.Where(t => t.IsCollectAsset).Select(t => t.AssetPath).ToArray(); - } - /// /// 获取构建的资源路径列表 /// diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildReport/BuildReport.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildReport/BuildReport.cs index 3fd7a33..309621a 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildReport/BuildReport.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildReport/BuildReport.cs @@ -1,10 +1,8 @@ using System; -using System.IO; using System.Collections; using System.Collections.Generic; -using UnityEngine; -namespace YooAsset +namespace YooAsset.Editor { /// /// 构建报告 @@ -12,6 +10,40 @@ namespace YooAsset [Serializable] public class BuildReport { + /// + /// 资源包列表 + /// + public readonly List BundleInfos = new List(1000); + /// + /// 冗余的资源列表 + /// + public readonly List RedundancyList = new List(1000); + + + /// + /// 检测是否包含BundleName + /// + public bool IsContainsBundle(string bundleFullName) + { + return TryGetBundleInfo(bundleFullName, out BuildBundleInfo bundleInfo); + } + + /// + /// 尝试获取资源包类,如果没有返回空 + /// + public bool TryGetBundleInfo(string bundleFullName, out BuildBundleInfo result) + { + foreach (var bundleInfo in BundleInfos) + { + if (bundleInfo.BundleName == bundleFullName) + { + result = bundleInfo; + return true; + } + } + result = null; + return false; + } } } \ No newline at end of file diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskBuilding.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskBuilding.cs index 78c945a..f8ba7e4 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskBuilding.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskBuilding.cs @@ -31,7 +31,7 @@ namespace YooAsset.Editor context.SetContextObject(unityManifestContext); // 拷贝原生文件 - foreach (var bundleInfo in buildMapContext.BundleInfos) + foreach (var bundleInfo in buildMapContext.Report.BundleInfos) { if (bundleInfo.IsRawFile) { @@ -62,7 +62,7 @@ namespace YooAsset.Editor // 1. 过滤掉原生Bundle List expectBundles = new List(buildedBundles.Length); - foreach(var bundleInfo in buildMapContext.BundleInfos) + foreach(var bundleInfo in buildMapContext.Report.BundleInfos) { if (bundleInfo.IsRawFile == false) expectBundles.Add(bundleInfo); @@ -77,7 +77,7 @@ namespace YooAsset.Editor // 3. 正向验证Bundle foreach (var bundleName in buildedBundles) { - if (buildMapContext.IsContainsBundle(bundleName) == false) + if (buildMapContext.Report.IsContainsBundle(bundleName) == false) { throw new Exception($"Should never get here !"); } diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskCreatePatchManifest.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskCreatePatchManifest.cs index a393d35..46c0bb5 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskCreatePatchManifest.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskCreatePatchManifest.cs @@ -29,7 +29,7 @@ namespace YooAsset.Editor patchManifest.ResourceVersion = buildParameters.Parameters.BuildVersion; patchManifest.BuildinTags = buildParameters.Parameters.BuildinTags; patchManifest.BundleList = GetAllPatchBundle(buildParameters, buildMapContext, encryptionContext); - patchManifest.AssetList = GetAllPatchAsset(buildMapContext, patchManifest.BundleList); + patchManifest.AssetList = GetAllPatchAsset(buildMapContext, patchManifest); // 创建补丁清单文件 string manifestFilePath = $"{buildParameters.PipelineOutputDirectory}/{ResourceSettingData.Setting.PatchManifestFileName}"; @@ -61,7 +61,7 @@ namespace YooAsset.Editor oldPatchManifest = AssetBundleBuilderHelper.LoadPatchManifestFile(buildParameters.PipelineOutputDirectory); } - foreach (var bundleInfo in buildMapContext.BundleInfos) + foreach (var bundleInfo in buildMapContext.Report.BundleInfos) { var bundleName = bundleInfo.BundleName; string filePath = $"{buildParameters.PipelineOutputDirectory}/{bundleName}"; @@ -111,41 +111,41 @@ namespace YooAsset.Editor /// /// 获取资源列表 /// - private List GetAllPatchAsset(TaskGetBuildMap.BuildMapContext buildMapContext, List bundleList) + private List GetAllPatchAsset(TaskGetBuildMap.BuildMapContext buildMapContext, PatchManifest patchManifest) { List result = new List(1000); - foreach (var bundleInfo in buildMapContext.BundleInfos) + foreach (var bundleInfo in buildMapContext.Report.BundleInfos) { var assetInfos = bundleInfo.GetCollectAssetInfos(); foreach (var assetInfo in assetInfos) { PatchAsset patchAsset = new PatchAsset(); patchAsset.AssetPath = assetInfo.AssetPath; - patchAsset.BundleID = GetAssetBundleID(assetInfo.GetBundleName(), bundleList); - patchAsset.DependIDs = GetAssetBundleDependIDs(assetInfo, bundleList); + patchAsset.BundleID = GetAssetBundleID(assetInfo.BundleName, patchManifest); + patchAsset.DependIDs = GetAssetBundleDependIDs(assetInfo, patchManifest); result.Add(patchAsset); } } return result; } - private int[] GetAssetBundleDependIDs(BuildAssetInfo assetInfo, List bundleList) + private int[] GetAssetBundleDependIDs(BuildAssetInfo assetInfo, PatchManifest patchManifest) { List result = new List(); foreach (var dependAssetInfo in assetInfo.AllDependAssetInfos) { - if (dependAssetInfo.CheckBundleNameValid() == false) + if (dependAssetInfo.BundleNameIsValid() == false) continue; - int bundleID = GetAssetBundleID(dependAssetInfo.GetBundleName(), bundleList); + int bundleID = GetAssetBundleID(dependAssetInfo.BundleName, patchManifest); if (result.Contains(bundleID) == false) result.Add(bundleID); } return result.ToArray(); } - private int GetAssetBundleID(string bundleName, List bundleList) + private int GetAssetBundleID(string bundleName, PatchManifest patchManifest) { - for (int index = 0; index < bundleList.Count; index++) + for (int index = 0; index < patchManifest.BundleList.Count; index++) { - if (bundleList[index].BundleName == bundleName) + if (patchManifest.BundleList[index].BundleName == bundleName) return index; } throw new Exception($"Not found bundle name : {bundleName}"); diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskCreateReadme.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskCreateReadme.cs index 14e4108..cbdbc9c 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskCreateReadme.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskCreateReadme.cs @@ -77,17 +77,17 @@ namespace YooAsset.Editor AppendData(content, ""); AppendData(content, $"--冗余列表--"); - for (int i = 0; i < buildMapContext.RedundancyList.Count; i++) + for (int i = 0; i < buildMapContext.Report.RedundancyList.Count; i++) { - string redundancyAssetPath = buildMapContext.RedundancyList[i]; + string redundancyAssetPath = buildMapContext.Report.RedundancyList[i]; AppendData(content, redundancyAssetPath); } AppendData(content, ""); AppendData(content, $"--构建列表--"); - for (int i = 0; i < buildMapContext.BundleInfos.Count; i++) + for (int i = 0; i < buildMapContext.Report.BundleInfos.Count; i++) { - string bundleName = buildMapContext.BundleInfos[i].BundleName; + string bundleName = buildMapContext.Report.BundleInfos[i].BundleName; AppendData(content, bundleName); } diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskEncryption.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskEncryption.cs index d40dc1e..5e92ed9 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskEncryption.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskEncryption.cs @@ -64,7 +64,7 @@ namespace YooAsset.Editor UnityEngine.Debug.Log($"开始加密资源文件"); int progressValue = 0; - foreach (var bundleInfo in buildMapContext.BundleInfos) + foreach (var bundleInfo in buildMapContext.Report.BundleInfos) { var bundleName = bundleInfo.BundleName; string filePath = $"{buildParameters.PipelineOutputDirectory}/{bundleName}"; @@ -83,7 +83,7 @@ namespace YooAsset.Editor } // 进度条 - EditorTools.DisplayProgressBar("加密资源包", ++progressValue, buildMapContext.BundleInfos.Count); + EditorTools.DisplayProgressBar("加密资源包", ++progressValue, buildMapContext.Report.BundleInfos.Count); } EditorTools.ClearProgressBar(); diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskGetBuildMap.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskGetBuildMap.cs index 2bf4c27..ca8cba6 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskGetBuildMap.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskGetBuildMap.cs @@ -11,31 +11,22 @@ namespace YooAsset.Editor { public class BuildMapContext : IContextObject { - /// - /// 资源包列表 - /// - public readonly List BundleInfos = new List(1000); - - /// - /// 冗余的资源列表 - /// - public readonly List RedundancyList = new List(1000); - + public BuildReport Report = new BuildReport(); /// /// 添加一个打包资源 /// public void PackAsset(BuildAssetInfo assetInfo) { - if (TryGetBundleInfo(assetInfo.GetBundleName(), out BuildBundleInfo bundleInfo)) + if (Report.TryGetBundleInfo(assetInfo.BundleName, out BuildBundleInfo bundleInfo)) { bundleInfo.PackAsset(assetInfo); } else { - BuildBundleInfo newBundleInfo = new BuildBundleInfo(assetInfo.BundleLabel, assetInfo.BundleVariant); + BuildBundleInfo newBundleInfo = new BuildBundleInfo(assetInfo.BundleName); newBundleInfo.PackAsset(assetInfo); - BundleInfos.Add(newBundleInfo); + Report.BundleInfos.Add(newBundleInfo); } } @@ -44,8 +35,8 @@ namespace YooAsset.Editor /// public List GetAllAssets() { - List result = new List(BundleInfos.Count); - foreach (var bundleInfo in BundleInfos) + List result = new List(Report.BundleInfos.Count); + foreach (var bundleInfo in Report.BundleInfos) { result.AddRange(bundleInfo.Assets); } @@ -57,31 +48,19 @@ namespace YooAsset.Editor /// public string[] GetAssetTags(string bundleFullName) { - if (TryGetBundleInfo(bundleFullName, out BuildBundleInfo bundleInfo)) + if (Report.TryGetBundleInfo(bundleFullName, out BuildBundleInfo bundleInfo)) { return bundleInfo.GetAssetTags(); } throw new Exception($"Not found {nameof(BuildBundleInfo)} : {bundleFullName}"); } - /// - /// 获取AssetBundle内收集的资源路径列表 - /// - public string[] GetCollectAssetPaths(string bundleFullName) - { - if (TryGetBundleInfo(bundleFullName, out BuildBundleInfo bundleInfo)) - { - return bundleInfo.GetCollectAssetPaths(); - } - throw new Exception($"Not found {nameof(BuildBundleInfo)} : {bundleFullName}"); - } - /// /// 获取AssetBundle内构建的资源路径列表 /// public string[] GetBuildinAssetPaths(string bundleFullName) { - if (TryGetBundleInfo(bundleFullName, out BuildBundleInfo bundleInfo)) + if (Report.TryGetBundleInfo(bundleFullName, out BuildBundleInfo bundleInfo)) { return bundleInfo.GetBuildinAssetPaths(); } @@ -93,36 +72,14 @@ namespace YooAsset.Editor /// public UnityEditor.AssetBundleBuild[] GetPipelineBuilds() { - List builds = new List(BundleInfos.Count); - foreach (var bundleInfo in BundleInfos) + List builds = new List(Report.BundleInfos.Count); + foreach (var bundleInfo in Report.BundleInfos) { if (bundleInfo.IsRawFile == false) builds.Add(bundleInfo.CreatePipelineBuild()); } return builds.ToArray(); } - - /// - /// 检测是否包含BundleName - /// - public bool IsContainsBundle(string bundleFullName) - { - return TryGetBundleInfo(bundleFullName, out BuildBundleInfo bundleInfo); - } - - private bool TryGetBundleInfo(string bundleFullName, out BuildBundleInfo result) - { - foreach (var bundleInfo in BundleInfos) - { - if (bundleInfo.BundleName == bundleFullName) - { - result = bundleInfo; - return true; - } - } - result = null; - return false; - } } @@ -142,7 +99,7 @@ namespace YooAsset.Editor /// private void SetupBuildMap(BuildMapContext buildMapContext, AssetBundleBuilder.BuildParametersContext buildParameters) { - Dictionary buildAssets = new Dictionary(); + Dictionary buildAssetDic = new Dictionary(); // 1. 获取主动收集的资源 List allCollectInfos = AssetBundleCollectorSettingData.GetAllCollectAssets(); @@ -160,23 +117,19 @@ namespace YooAsset.Editor string assetPath = depends[i].AssetPath; // 如果已经存在,则增加该资源的依赖计数 - if (buildAssets.ContainsKey(assetPath)) - { - buildAssets[assetPath].DependCount++; - } + if (buildAssetDic.ContainsKey(assetPath)) + buildAssetDic[assetPath].DependCount++; else - { - buildAssets.Add(assetPath, depends[i]); - } + buildAssetDic.Add(assetPath, depends[i]); // 添加资源标记 - buildAssets[assetPath].AddAssetTags(collectInfo.AssetTags); + buildAssetDic[assetPath].AddAssetTags(collectInfo.AssetTags); // 注意:检测是否为主动收集资源 if (assetPath == mainAssetPath) { - buildAssets[mainAssetPath].IsCollectAsset = true; - buildAssets[mainAssetPath].IsRawAsset = collectInfo.IsRawAsset; + buildAssetDic[mainAssetPath].IsCollectAsset = true; + buildAssetDic[mainAssetPath].IsRawAsset = collectInfo.IsRawAsset; } } @@ -187,9 +140,9 @@ namespace YooAsset.Editor { string assetPath = depends[i].AssetPath; if (assetPath != mainAssetPath) - allDependAssetInfos.Add(buildAssets[assetPath]); + allDependAssetInfos.Add(buildAssetDic[assetPath]); } - buildAssets[mainAssetPath].SetAllDependAssetInfos(allDependAssetInfos); + buildAssetDic[mainAssetPath].SetAllDependAssetInfos(allDependAssetInfos); EditorTools.DisplayProgressBar("依赖文件分析", ++progressValue, allCollectInfos.Count); } @@ -198,7 +151,7 @@ namespace YooAsset.Editor // 3. 移除零依赖的资源 var redundancy = CreateAssetRedundancy(); List undependentAssets = new List(); - foreach (KeyValuePair pair in buildAssets) + foreach (KeyValuePair pair in buildAssetDic) { var buildAssetInfo = pair.Value; if (buildAssetInfo.IsCollectAsset) @@ -216,7 +169,7 @@ namespace YooAsset.Editor if(redundancy.Check(buildAssetInfo.AssetPath)) { undependentAssets.Add(buildAssetInfo); - buildMapContext.RedundancyList.Add(buildAssetInfo.AssetPath); + buildMapContext.Report.RedundancyList.Add(buildAssetInfo.AssetPath); continue; } } @@ -227,18 +180,18 @@ namespace YooAsset.Editor if (AssetBundleCollectorSettingData.HasCollector(buildAssetInfo.AssetPath) == false) { undependentAssets.Add(buildAssetInfo); - buildMapContext.RedundancyList.Add(buildAssetInfo.AssetPath); + buildMapContext.Report.RedundancyList.Add(buildAssetInfo.AssetPath); } } } foreach (var assetInfo in undependentAssets) { - buildAssets.Remove(assetInfo.AssetPath); + buildAssetDic.Remove(assetInfo.AssetPath); } // 4. 设置资源包名 progressValue = 0; - foreach (KeyValuePair pair in buildAssets) + foreach (KeyValuePair pair in buildAssetDic) { var assetInfo = pair.Value; var bundleLabel = AssetBundleCollectorSettingData.GetBundleLabel(assetInfo.AssetPath); @@ -246,15 +199,14 @@ namespace YooAsset.Editor assetInfo.SetBundleLabelAndVariant(bundleLabel, ResourceSettingData.Setting.RawFileVariant); else assetInfo.SetBundleLabelAndVariant(bundleLabel, ResourceSettingData.Setting.AssetBundleFileVariant); - EditorTools.DisplayProgressBar("设置资源包名", ++progressValue, buildAssets.Count); + EditorTools.DisplayProgressBar("设置资源包名", ++progressValue, buildAssetDic.Count); } EditorTools.ClearProgressBar(); // 4. 构建资源包 - var allAssets = buildAssets.Values.ToList(); + var allAssets = buildAssetDic.Values.ToList(); if (allAssets.Count == 0) throw new Exception("构建的资源列表不能为空"); - UnityEngine.Debug.Log($"构建的资源列表里总共有{allAssets.Count}个资源"); foreach (var assetInfo in allAssets) { buildMapContext.PackAsset(assetInfo); @@ -285,7 +237,7 @@ namespace YooAsset.Editor /// private void CheckBuildMapContent(BuildMapContext buildMapContext) { - foreach (var bundleInfo in buildMapContext.BundleInfos) + foreach (var bundleInfo in buildMapContext.Report.BundleInfos) { // 注意:原生文件资源包只能包含一个原生文件 bool isRawFile = bundleInfo.IsRawFile;