update parallel update bundle info only in UNITY_EDITOR

pull/539/head
yangpei1010110 2025-04-22 23:06:08 +08:00
parent 51c9943cf2
commit 76da89538d
2 changed files with 79 additions and 10 deletions

View File

@ -3,20 +3,60 @@ using System.IO;
using System.Text; using System.Text;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using UnityEditor; using UnityEditor;
namespace YooAsset.Editor namespace YooAsset.Editor {
{ public abstract class TaskUpdateBundleInfo {
public abstract class TaskUpdateBundleInfo public void UpdateBundleInfo(BuildContext context) {
{
public void UpdateBundleInfo(BuildContext context)
{
var buildParametersContext = context.GetContextObject<BuildParametersContext>(); var buildParametersContext = context.GetContextObject<BuildParametersContext>();
var buildMapContext = context.GetContextObject<BuildMapContext>(); var buildMapContext = context.GetContextObject<BuildMapContext>();
string pipelineOutputDirectory = buildParametersContext.GetPipelineOutputDirectory(); string pipelineOutputDirectory = buildParametersContext.GetPipelineOutputDirectory();
string packageOutputDirectory = buildParametersContext.GetPackageOutputDirectory(); string packageOutputDirectory = buildParametersContext.GetPackageOutputDirectory();
int outputNameStyle = (int)buildParametersContext.Parameters.FileNameStyle; int outputNameStyle = (int)buildParametersContext.Parameters.FileNameStyle;
// 仅在编辑器下并行构建
#if UNITY_EDITOR
Parallel.ForEach(buildMapContext.Collection, bundleInfo => {
// 1.检测文件名长度
{
// NOTE检测文件名长度不要超过260字符。
string fileName = bundleInfo.BundleName;
if (fileName.Length >= 260) {
string message = BuildLogger.GetErrorMessage(ErrorCode.CharactersOverTheLimit, $"Bundle file name character count exceeds limit : {fileName}");
throw new Exception(message);
}
}
// 2.更新构建输出的文件路径
{
bundleInfo.BuildOutputFilePath = $"{pipelineOutputDirectory}/{bundleInfo.BundleName}";
if (bundleInfo.Encrypted)
bundleInfo.PackageSourceFilePath = bundleInfo.EncryptedFilePath;
else
bundleInfo.PackageSourceFilePath = bundleInfo.BuildOutputFilePath;
}
// 3.更新文件其它信息
{
bundleInfo.PackageUnityHash = GetUnityHash(bundleInfo, context);
bundleInfo.PackageUnityCRC = GetUnityCRC(bundleInfo, context);
bundleInfo.PackageFileHash = GetBundleFileHash(bundleInfo, buildParametersContext);
bundleInfo.PackageFileCRC = GetBundleFileCRC(bundleInfo, buildParametersContext);
bundleInfo.PackageFileSize = GetBundleFileSize(bundleInfo, buildParametersContext);
}
// 4.更新补丁包输出的文件路径
{
string bundleName = bundleInfo.BundleName;
string fileHash = bundleInfo.PackageFileHash;
string fileExtension = ManifestTools.GetRemoteBundleFileExtension(bundleName);
string fileName = ManifestTools.GetRemoteBundleFileName(outputNameStyle, bundleName, fileExtension, fileHash);
bundleInfo.PackageDestFilePath = $"{packageOutputDirectory}/{fileName}";
}
});
#else
// 1.检测文件名长度 // 1.检测文件名长度
foreach (var bundleInfo in buildMapContext.Collection) foreach (var bundleInfo in buildMapContext.Collection)
{ {
@ -58,12 +98,13 @@ namespace YooAsset.Editor
string fileName = ManifestTools.GetRemoteBundleFileName(outputNameStyle, bundleName, fileExtension, fileHash); string fileName = ManifestTools.GetRemoteBundleFileName(outputNameStyle, bundleName, fileExtension, fileHash);
bundleInfo.PackageDestFilePath = $"{packageOutputDirectory}/{fileName}"; bundleInfo.PackageDestFilePath = $"{packageOutputDirectory}/{fileName}";
} }
#endif
} }
protected abstract string GetUnityHash(BuildBundleInfo bundleInfo, BuildContext context); protected abstract string GetUnityHash(BuildBundleInfo bundleInfo, BuildContext context);
protected abstract uint GetUnityCRC(BuildBundleInfo bundleInfo, BuildContext context); protected abstract uint GetUnityCRC(BuildBundleInfo bundleInfo, BuildContext context);
protected abstract string GetBundleFileHash(BuildBundleInfo bundleInfo, BuildParametersContext buildParametersContext); protected abstract string GetBundleFileHash(BuildBundleInfo bundleInfo, BuildParametersContext buildParametersContext);
protected abstract string GetBundleFileCRC(BuildBundleInfo bundleInfo, BuildParametersContext buildParametersContext); protected abstract string GetBundleFileCRC(BuildBundleInfo bundleInfo, BuildParametersContext buildParametersContext);
protected abstract long GetBundleFileSize(BuildBundleInfo bundleInfo, BuildParametersContext buildParametersContext); protected abstract long GetBundleFileSize(BuildBundleInfo bundleInfo, BuildParametersContext buildParametersContext);
} }
} }

View File

@ -328,6 +328,33 @@ namespace YooAsset
/// </summary> /// </summary>
public static string GetRemoteBundleFileName(int nameStyle, string bundleName, string fileExtension, string fileHash) public static string GetRemoteBundleFileName(int nameStyle, string bundleName, string fileExtension, string fileHash)
{ {
// 编辑器下的 ESBP 会并行调用
#if UNITY_EDITOR
if (nameStyle == (int)EFileNameStyle.HashName)
{
return $"{fileHash}{fileExtension}";
}
else if (nameStyle == (int)EFileNameStyle.BundleName)
{
return bundleName;
}
else if (nameStyle == (int)EFileNameStyle.BundleName_HashName)
{
if (string.IsNullOrEmpty(fileExtension))
{
return $"{bundleName}_{fileHash}";
}
else
{
string fileName = bundleName.Remove(bundleName.LastIndexOf('.'));
return $"{fileName}_{fileHash}{fileExtension}";
}
}
else
{
throw new NotImplementedException($"Invalid name style : {nameStyle}");
}
#else
if (nameStyle == (int)EFileNameStyle.HashName) if (nameStyle == (int)EFileNameStyle.HashName)
{ {
return StringUtility.Format("{0}{1}", fileHash, fileExtension); return StringUtility.Format("{0}{1}", fileHash, fileExtension);
@ -352,6 +379,7 @@ namespace YooAsset
{ {
throw new NotImplementedException($"Invalid name style : {nameStyle}"); throw new NotImplementedException($"Invalid name style : {nameStyle}");
} }
#endif
} }
} }
} }