From 1fb78185ffb42d17a7cf8d99f713a9c6692581a9 Mon Sep 17 00:00:00 2001 From: hevinci Date: Wed, 3 Jan 2024 15:13:28 +0800 Subject: [PATCH] feat : support share pack rule MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 支持共享资源打包规则 --- .../AssetBundleBuilder/BuildAssetInfo.cs | 11 ++-- .../AssetBundleBuilder/BuildParameters.cs | 5 ++ .../BaseTasks/TaskCreateReport.cs | 6 +- .../BaseTasks/TaskGetBuildMap.cs | 56 +++++++++++++++++-- .../BuildTasks/TaskPrepare_RFBP.cs | 7 +++ .../BuildSystem/ErrorCode.cs | 1 + .../BuiltinBuildPipelineViewer.cs | 1 + .../ScriptableBuildPipelineViewer.cs | 1 + .../CollectRules/IPackRule.cs | 22 ++++++++ .../AssetBundleReporter/ReportSummary.cs | 1 + .../VisualViewers/ReporterSummaryViewer.cs | 3 +- 11 files changed, 102 insertions(+), 12 deletions(-) diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildAssetInfo.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildAssetInfo.cs index 3584df1..b23c7b5 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildAssetInfo.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildAssetInfo.cs @@ -70,13 +70,14 @@ namespace YooAsset.Editor } /// - /// 设置为统一的着色器包名 + /// 设置资源包名称 /// - public void SetShaderBundleName(string packageName, bool uniqueBundleName) + public void SetBundleName(string bundleName) { - // 获取着色器打包规则结果 - PackRuleResult shaderPackRuleResult = DefaultPackRule.CreateShadersPackRuleResult(); - BundleName = shaderPackRuleResult.GetBundleName(packageName, uniqueBundleName); + if (HasBundleName()) + throw new System.Exception("Should never get here !"); + + BundleName = bundleName; } /// diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildParameters.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildParameters.cs index 4472e67..0180e73 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildParameters.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildParameters.cs @@ -47,6 +47,11 @@ namespace YooAsset.Editor public string PackageVersion; + /// + /// 是否启用共享资源打包 + /// + public bool EnableSharePackRule = false; + /// /// 验证构建结果 /// diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/BaseTasks/TaskCreateReport.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/BaseTasks/TaskCreateReport.cs index 9754a00..56051f5 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/BaseTasks/TaskCreateReport.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/BaseTasks/TaskCreateReport.cs @@ -31,16 +31,18 @@ namespace YooAsset.Editor buildReport.Summary.BuildMode = buildParameters.BuildMode; buildReport.Summary.BuildPackageName = buildParameters.PackageName; buildReport.Summary.BuildPackageVersion = buildParameters.PackageVersion; + + // 收集器配置 buildReport.Summary.UniqueBundleName = buildMapContext.Command.UniqueBundleName; buildReport.Summary.EnableAddressable = buildMapContext.Command.EnableAddressable; buildReport.Summary.LocationToLower = buildMapContext.Command.LocationToLower; buildReport.Summary.IncludeAssetGUID = buildMapContext.Command.IncludeAssetGUID; buildReport.Summary.IgnoreDefaultType = buildMapContext.Command.IgnoreDefaultType; buildReport.Summary.AutoCollectShaders = buildMapContext.Command.AutoCollectShaders; - buildReport.Summary.EncryptionClassName = buildParameters.EncryptionServices == null ? - "null" : buildParameters.EncryptionServices.GetType().FullName; // 构建参数 + buildReport.Summary.EnableSharePackRule = buildParameters.EnableSharePackRule; + buildReport.Summary.EncryptionClassName = buildParameters.EncryptionServices == null ? "null" : buildParameters.EncryptionServices.GetType().FullName; if (buildParameters.BuildPipeline == nameof(BuiltinBuildPipeline)) { var builtinBuildParameters = buildParameters as BuiltinBuildParameters; diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/BaseTasks/TaskGetBuildMap.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/BaseTasks/TaskGetBuildMap.cs index fb2502c..6e78c05 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/BaseTasks/TaskGetBuildMap.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/BaseTasks/TaskGetBuildMap.cs @@ -86,23 +86,45 @@ namespace YooAsset.Editor // 6. 自动收集所有依赖的着色器 if (collectResult.Command.AutoCollectShaders) { + // 获取着色器打包规则结果 + PackRuleResult shaderPackRuleResult = DefaultPackRule.CreateShadersPackRuleResult(); + string shaderBundleName = shaderPackRuleResult.GetBundleName(collectResult.Command.PackageName, collectResult.Command.UniqueBundleName); foreach (var buildAssetInfo in allBuildAssetInfos.Values) { if (buildAssetInfo.CollectorType == ECollectorType.None) { if (buildAssetInfo.AssetInfo.IsShaderAsset()) { - buildAssetInfo.SetShaderBundleName(collectResult.Command.PackageName, collectResult.Command.UniqueBundleName); + buildAssetInfo.SetBundleName(shaderBundleName); } } } } - // 7. 记录关键信息 + // 7. 计算共享资源的包名 + if (buildParameters.EnableSharePackRule) + { + PreProcessPackShareBundle(buildParameters, collectResult.Command, allBuildAssetInfos); + foreach (var buildAssetInfo in allBuildAssetInfos.Values) + { + if (buildAssetInfo.HasBundleName() == false) + { + PackRuleResult packRuleResult = GetShareBundleName(buildAssetInfo); + if (packRuleResult.IsValid()) + { + string shareBundleName = packRuleResult.GetShareBundleName(collectResult.Command.PackageName, collectResult.Command.UniqueBundleName); + buildAssetInfo.SetBundleName(shareBundleName); + } + } + } + PostProcessPackShareBundle(); + } + + // 8. 记录关键信息 context.AssetFileCount = allBuildAssetInfos.Count; context.Command = collectResult.Command; - // 8. 移除不参与构建的资源 + // 9. 移除不参与构建的资源 List removeBuildList = new List(); foreach (var buildAssetInfo in allBuildAssetInfos.Values) { @@ -114,7 +136,7 @@ namespace YooAsset.Editor allBuildAssetInfos.Remove(removeValue.AssetInfo.AssetPath); } - // 9. 构建资源列表 + // 10. 构建资源列表 var allPackAssets = allBuildAssetInfos.Values.ToList(); if (allPackAssets.Count == 0) { @@ -177,5 +199,31 @@ namespace YooAsset.Editor allCollectAssets.Remove(removeValue); } } + + #region 共享资源打包规则 + /// + /// 共享资源打包前置处理 + /// + protected virtual void PreProcessPackShareBundle(BuildParameters buildParameters, CollectCommand command, Dictionary allBuildAssetInfos) + { + } + + /// + /// 共享资源打包后置处理 + /// + protected virtual void PostProcessPackShareBundle() + { + } + + /// + /// 获取共享资源包名称 + /// + protected virtual PackRuleResult GetShareBundleName(BuildAssetInfo buildAssetInfo) + { + string bundleName = Path.GetDirectoryName(buildAssetInfo.AssetInfo.AssetPath); + PackRuleResult result = new PackRuleResult(bundleName, DefaultPackRule.AssetBundleFileExtension); + return result; + } + #endregion } } \ No newline at end of file diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/BuildTasks/TaskPrepare_RFBP.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/BuildTasks/TaskPrepare_RFBP.cs index 381edac..6c836a5 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/BuildTasks/TaskPrepare_RFBP.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/BuildTasks/TaskPrepare_RFBP.cs @@ -16,6 +16,13 @@ namespace YooAsset.Editor // 检测基础构建参数 buildParametersContext.CheckBuildParameters(); + // 检测不被支持的参数 + if (buildParameters.EnableSharePackRule) + { + string message = BuildLogger.GetErrorMessage(ErrorCode.BuildPipelineNotSupportSharePackRule, $"{nameof(EBuildPipeline.RawFileBuildPipeline)} not support share pack rule !"); + throw new Exception(message); + } + // 检测不被支持的构建模式 if (buildParameters.BuildMode == EBuildMode.DryRunBuild) { diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildSystem/ErrorCode.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildSystem/ErrorCode.cs index 14ec201..2fb0bdb 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildSystem/ErrorCode.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildSystem/ErrorCode.cs @@ -14,6 +14,7 @@ namespace YooAsset.Editor PackageOutputDirectoryExists = 115, RecommendScriptBuildPipeline = 130, BuildPipelineNotSupportBuildMode = 140, + BuildPipelineNotSupportSharePackRule = 141, // TaskGetBuildMap RemoveInvalidTags = 200, diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/VisualViewers/BuiltinBuildPipelineViewer.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/VisualViewers/BuiltinBuildPipelineViewer.cs index d99c462..60beade 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/VisualViewers/BuiltinBuildPipelineViewer.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/VisualViewers/BuiltinBuildPipelineViewer.cs @@ -36,6 +36,7 @@ namespace YooAsset.Editor buildParameters.BuildMode = buildMode; buildParameters.PackageName = PackageName; buildParameters.PackageVersion = GetPackageVersion(); + buildParameters.EnableSharePackRule = true; buildParameters.VerifyBuildingResult = true; buildParameters.FileNameStyle = fileNameStyle; buildParameters.BuildinFileCopyOption = buildinFileCopyOption; diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/VisualViewers/ScriptableBuildPipelineViewer.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/VisualViewers/ScriptableBuildPipelineViewer.cs index 172d72b..3f87873 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/VisualViewers/ScriptableBuildPipelineViewer.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/VisualViewers/ScriptableBuildPipelineViewer.cs @@ -36,6 +36,7 @@ namespace YooAsset.Editor buildParameters.BuildMode = buildMode; buildParameters.PackageName = PackageName; buildParameters.PackageVersion = GetPackageVersion(); + buildParameters.EnableSharePackRule = true; buildParameters.VerifyBuildingResult = true; buildParameters.FileNameStyle = fileNameStyle; buildParameters.BuildinFileCopyOption = buildinFileCopyOption; diff --git a/Assets/YooAsset/Editor/AssetBundleCollector/CollectRules/IPackRule.cs b/Assets/YooAsset/Editor/AssetBundleCollector/CollectRules/IPackRule.cs index e921e2a..8f2a8c9 100644 --- a/Assets/YooAsset/Editor/AssetBundleCollector/CollectRules/IPackRule.cs +++ b/Assets/YooAsset/Editor/AssetBundleCollector/CollectRules/IPackRule.cs @@ -28,6 +28,14 @@ namespace YooAsset.Editor _bundleExtension = bundleExtension; } + /// + /// 结果是否有效 + /// + public bool IsValid() + { + return string.IsNullOrEmpty(_bundleName) == false && string.IsNullOrEmpty(_bundleExtension) == false; + } + /// /// 获取资源包全名称 /// @@ -41,6 +49,20 @@ namespace YooAsset.Editor fullName = $"{bundleName}.{_bundleExtension}"; return fullName.ToLower(); } + + /// + /// 获取共享资源包全名称 + /// + public string GetShareBundleName(string packageName, bool uniqueBundleName) + { + string fullName; + string bundleName = EditorTools.GetRegularPath(_bundleName).Replace('/', '_').Replace('.', '_').ToLower(); + if (uniqueBundleName) + fullName = $"{packageName}_share_{bundleName}.{_bundleExtension}"; + else + fullName = $"share_{bundleName}.{_bundleExtension}"; + return fullName.ToLower(); + } } /// diff --git a/Assets/YooAsset/Editor/AssetBundleReporter/ReportSummary.cs b/Assets/YooAsset/Editor/AssetBundleReporter/ReportSummary.cs index 25cc424..af67b44 100644 --- a/Assets/YooAsset/Editor/AssetBundleReporter/ReportSummary.cs +++ b/Assets/YooAsset/Editor/AssetBundleReporter/ReportSummary.cs @@ -62,6 +62,7 @@ namespace YooAsset.Editor public bool AutoCollectShaders; // 构建参数 + public bool EnableSharePackRule; public string EncryptionClassName; public EFileNameStyle FileNameStyle; public ECompressOption CompressOption; diff --git a/Assets/YooAsset/Editor/AssetBundleReporter/VisualViewers/ReporterSummaryViewer.cs b/Assets/YooAsset/Editor/AssetBundleReporter/VisualViewers/ReporterSummaryViewer.cs index c161884..ed6ff97 100644 --- a/Assets/YooAsset/Editor/AssetBundleReporter/VisualViewers/ReporterSummaryViewer.cs +++ b/Assets/YooAsset/Editor/AssetBundleReporter/VisualViewers/ReporterSummaryViewer.cs @@ -67,7 +67,7 @@ namespace YooAsset.Editor _items.Add(new ItemWrapper("Package Version", buildReport.Summary.BuildPackageVersion)); _items.Add(new ItemWrapper(string.Empty, string.Empty)); - _items.Add(new ItemWrapper("Settings", string.Empty)); + _items.Add(new ItemWrapper("Collect Settings", string.Empty)); _items.Add(new ItemWrapper("Unique Bundle Name", $"{buildReport.Summary.UniqueBundleName}")); _items.Add(new ItemWrapper("Enable Addressable", $"{buildReport.Summary.EnableAddressable}")); _items.Add(new ItemWrapper("Location To Lower", $"{buildReport.Summary.LocationToLower}")); @@ -77,6 +77,7 @@ namespace YooAsset.Editor _items.Add(new ItemWrapper(string.Empty, string.Empty)); _items.Add(new ItemWrapper("Build Params", string.Empty)); + _items.Add(new ItemWrapper("Enable Share Pack Rule", $"{buildReport.Summary.EnableSharePackRule}")); _items.Add(new ItemWrapper("Encryption Class Name", buildReport.Summary.EncryptionClassName)); _items.Add(new ItemWrapper("FileNameStyle", $"{buildReport.Summary.FileNameStyle}")); _items.Add(new ItemWrapper("CompressOption", $"{buildReport.Summary.CompressOption}"));