feat : support share pack rule

支持共享资源打包规则
pull/229/head
hevinci 2024-01-03 15:13:28 +08:00
parent 58f9aea979
commit 1fb78185ff
11 changed files with 102 additions and 12 deletions

View File

@ -70,13 +70,14 @@ namespace YooAsset.Editor
} }
/// <summary> /// <summary>
/// 设置为统一的着色器包名 /// 设置资源包名称
/// </summary> /// </summary>
public void SetShaderBundleName(string packageName, bool uniqueBundleName) public void SetBundleName(string bundleName)
{ {
// 获取着色器打包规则结果 if (HasBundleName())
PackRuleResult shaderPackRuleResult = DefaultPackRule.CreateShadersPackRuleResult(); throw new System.Exception("Should never get here !");
BundleName = shaderPackRuleResult.GetBundleName(packageName, uniqueBundleName);
BundleName = bundleName;
} }
/// <summary> /// <summary>

View File

@ -47,6 +47,11 @@ namespace YooAsset.Editor
public string PackageVersion; public string PackageVersion;
/// <summary>
/// 是否启用共享资源打包
/// </summary>
public bool EnableSharePackRule = false;
/// <summary> /// <summary>
/// 验证构建结果 /// 验证构建结果
/// </summary> /// </summary>

View File

@ -31,16 +31,18 @@ namespace YooAsset.Editor
buildReport.Summary.BuildMode = buildParameters.BuildMode; buildReport.Summary.BuildMode = buildParameters.BuildMode;
buildReport.Summary.BuildPackageName = buildParameters.PackageName; buildReport.Summary.BuildPackageName = buildParameters.PackageName;
buildReport.Summary.BuildPackageVersion = buildParameters.PackageVersion; buildReport.Summary.BuildPackageVersion = buildParameters.PackageVersion;
// 收集器配置
buildReport.Summary.UniqueBundleName = buildMapContext.Command.UniqueBundleName; buildReport.Summary.UniqueBundleName = buildMapContext.Command.UniqueBundleName;
buildReport.Summary.EnableAddressable = buildMapContext.Command.EnableAddressable; buildReport.Summary.EnableAddressable = buildMapContext.Command.EnableAddressable;
buildReport.Summary.LocationToLower = buildMapContext.Command.LocationToLower; buildReport.Summary.LocationToLower = buildMapContext.Command.LocationToLower;
buildReport.Summary.IncludeAssetGUID = buildMapContext.Command.IncludeAssetGUID; buildReport.Summary.IncludeAssetGUID = buildMapContext.Command.IncludeAssetGUID;
buildReport.Summary.IgnoreDefaultType = buildMapContext.Command.IgnoreDefaultType; buildReport.Summary.IgnoreDefaultType = buildMapContext.Command.IgnoreDefaultType;
buildReport.Summary.AutoCollectShaders = buildMapContext.Command.AutoCollectShaders; 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)) if (buildParameters.BuildPipeline == nameof(BuiltinBuildPipeline))
{ {
var builtinBuildParameters = buildParameters as BuiltinBuildParameters; var builtinBuildParameters = buildParameters as BuiltinBuildParameters;

View File

@ -86,23 +86,45 @@ namespace YooAsset.Editor
// 6. 自动收集所有依赖的着色器 // 6. 自动收集所有依赖的着色器
if (collectResult.Command.AutoCollectShaders) if (collectResult.Command.AutoCollectShaders)
{ {
// 获取着色器打包规则结果
PackRuleResult shaderPackRuleResult = DefaultPackRule.CreateShadersPackRuleResult();
string shaderBundleName = shaderPackRuleResult.GetBundleName(collectResult.Command.PackageName, collectResult.Command.UniqueBundleName);
foreach (var buildAssetInfo in allBuildAssetInfos.Values) foreach (var buildAssetInfo in allBuildAssetInfos.Values)
{ {
if (buildAssetInfo.CollectorType == ECollectorType.None) if (buildAssetInfo.CollectorType == ECollectorType.None)
{ {
if (buildAssetInfo.AssetInfo.IsShaderAsset()) 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.AssetFileCount = allBuildAssetInfos.Count;
context.Command = collectResult.Command; context.Command = collectResult.Command;
// 8. 移除不参与构建的资源 // 9. 移除不参与构建的资源
List<BuildAssetInfo> removeBuildList = new List<BuildAssetInfo>(); List<BuildAssetInfo> removeBuildList = new List<BuildAssetInfo>();
foreach (var buildAssetInfo in allBuildAssetInfos.Values) foreach (var buildAssetInfo in allBuildAssetInfos.Values)
{ {
@ -114,7 +136,7 @@ namespace YooAsset.Editor
allBuildAssetInfos.Remove(removeValue.AssetInfo.AssetPath); allBuildAssetInfos.Remove(removeValue.AssetInfo.AssetPath);
} }
// 9. 构建资源列表 // 10. 构建资源列表
var allPackAssets = allBuildAssetInfos.Values.ToList(); var allPackAssets = allBuildAssetInfos.Values.ToList();
if (allPackAssets.Count == 0) if (allPackAssets.Count == 0)
{ {
@ -177,5 +199,31 @@ namespace YooAsset.Editor
allCollectAssets.Remove(removeValue); allCollectAssets.Remove(removeValue);
} }
} }
#region 共享资源打包规则
/// <summary>
/// 共享资源打包前置处理
/// </summary>
protected virtual void PreProcessPackShareBundle(BuildParameters buildParameters, CollectCommand command, Dictionary<string, BuildAssetInfo> allBuildAssetInfos)
{
}
/// <summary>
/// 共享资源打包后置处理
/// </summary>
protected virtual void PostProcessPackShareBundle()
{
}
/// <summary>
/// 获取共享资源包名称
/// </summary>
protected virtual PackRuleResult GetShareBundleName(BuildAssetInfo buildAssetInfo)
{
string bundleName = Path.GetDirectoryName(buildAssetInfo.AssetInfo.AssetPath);
PackRuleResult result = new PackRuleResult(bundleName, DefaultPackRule.AssetBundleFileExtension);
return result;
}
#endregion
} }
} }

View File

@ -16,6 +16,13 @@ namespace YooAsset.Editor
// 检测基础构建参数 // 检测基础构建参数
buildParametersContext.CheckBuildParameters(); 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) if (buildParameters.BuildMode == EBuildMode.DryRunBuild)
{ {

View File

@ -14,6 +14,7 @@ namespace YooAsset.Editor
PackageOutputDirectoryExists = 115, PackageOutputDirectoryExists = 115,
RecommendScriptBuildPipeline = 130, RecommendScriptBuildPipeline = 130,
BuildPipelineNotSupportBuildMode = 140, BuildPipelineNotSupportBuildMode = 140,
BuildPipelineNotSupportSharePackRule = 141,
// TaskGetBuildMap // TaskGetBuildMap
RemoveInvalidTags = 200, RemoveInvalidTags = 200,

View File

@ -36,6 +36,7 @@ namespace YooAsset.Editor
buildParameters.BuildMode = buildMode; buildParameters.BuildMode = buildMode;
buildParameters.PackageName = PackageName; buildParameters.PackageName = PackageName;
buildParameters.PackageVersion = GetPackageVersion(); buildParameters.PackageVersion = GetPackageVersion();
buildParameters.EnableSharePackRule = true;
buildParameters.VerifyBuildingResult = true; buildParameters.VerifyBuildingResult = true;
buildParameters.FileNameStyle = fileNameStyle; buildParameters.FileNameStyle = fileNameStyle;
buildParameters.BuildinFileCopyOption = buildinFileCopyOption; buildParameters.BuildinFileCopyOption = buildinFileCopyOption;

View File

@ -36,6 +36,7 @@ namespace YooAsset.Editor
buildParameters.BuildMode = buildMode; buildParameters.BuildMode = buildMode;
buildParameters.PackageName = PackageName; buildParameters.PackageName = PackageName;
buildParameters.PackageVersion = GetPackageVersion(); buildParameters.PackageVersion = GetPackageVersion();
buildParameters.EnableSharePackRule = true;
buildParameters.VerifyBuildingResult = true; buildParameters.VerifyBuildingResult = true;
buildParameters.FileNameStyle = fileNameStyle; buildParameters.FileNameStyle = fileNameStyle;
buildParameters.BuildinFileCopyOption = buildinFileCopyOption; buildParameters.BuildinFileCopyOption = buildinFileCopyOption;

View File

@ -28,6 +28,14 @@ namespace YooAsset.Editor
_bundleExtension = bundleExtension; _bundleExtension = bundleExtension;
} }
/// <summary>
/// 结果是否有效
/// </summary>
public bool IsValid()
{
return string.IsNullOrEmpty(_bundleName) == false && string.IsNullOrEmpty(_bundleExtension) == false;
}
/// <summary> /// <summary>
/// 获取资源包全名称 /// 获取资源包全名称
/// </summary> /// </summary>
@ -41,6 +49,20 @@ namespace YooAsset.Editor
fullName = $"{bundleName}.{_bundleExtension}"; fullName = $"{bundleName}.{_bundleExtension}";
return fullName.ToLower(); return fullName.ToLower();
} }
/// <summary>
/// 获取共享资源包全名称
/// </summary>
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();
}
} }
/// <summary> /// <summary>

View File

@ -62,6 +62,7 @@ namespace YooAsset.Editor
public bool AutoCollectShaders; public bool AutoCollectShaders;
// 构建参数 // 构建参数
public bool EnableSharePackRule;
public string EncryptionClassName; public string EncryptionClassName;
public EFileNameStyle FileNameStyle; public EFileNameStyle FileNameStyle;
public ECompressOption CompressOption; public ECompressOption CompressOption;

View File

@ -67,7 +67,7 @@ namespace YooAsset.Editor
_items.Add(new ItemWrapper("Package Version", buildReport.Summary.BuildPackageVersion)); _items.Add(new ItemWrapper("Package Version", buildReport.Summary.BuildPackageVersion));
_items.Add(new ItemWrapper(string.Empty, string.Empty)); _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("Unique Bundle Name", $"{buildReport.Summary.UniqueBundleName}"));
_items.Add(new ItemWrapper("Enable Addressable", $"{buildReport.Summary.EnableAddressable}")); _items.Add(new ItemWrapper("Enable Addressable", $"{buildReport.Summary.EnableAddressable}"));
_items.Add(new ItemWrapper("Location To Lower", $"{buildReport.Summary.LocationToLower}")); _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(string.Empty, string.Empty));
_items.Add(new ItemWrapper("Build Params", 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("Encryption Class Name", buildReport.Summary.EncryptionClassName));
_items.Add(new ItemWrapper("FileNameStyle", $"{buildReport.Summary.FileNameStyle}")); _items.Add(new ItemWrapper("FileNameStyle", $"{buildReport.Summary.FileNameStyle}"));
_items.Add(new ItemWrapper("CompressOption", $"{buildReport.Summary.CompressOption}")); _items.Add(new ItemWrapper("CompressOption", $"{buildReport.Summary.CompressOption}"));