refactor : 重构资源清单反序列化逻辑

This commit is contained in:
何冠峰
2025-09-30 15:35:32 +08:00
parent 5602addaca
commit f3ebda0c04
3 changed files with 137 additions and 185 deletions

View File

@@ -36,6 +36,9 @@ namespace YooAsset
{ {
get get
{ {
if (_watch == null)
return false;
// NOTE : 单次调用开销约1微秒 // NOTE : 单次调用开销约1微秒
return _watch.ElapsedMilliseconds - _frameTime >= MaxTimeSlice; return _watch.ElapsedMilliseconds - _frameTime >= MaxTimeSlice;
} }

View File

@@ -119,86 +119,13 @@ namespace YooAsset
/// </summary> /// </summary>
public static PackageManifest DeserializeFromJson(string jsonContent) public static PackageManifest DeserializeFromJson(string jsonContent)
{ {
return JsonUtility.FromJson<PackageManifest>(jsonContent); var manifest = JsonUtility.FromJson<PackageManifest>(jsonContent);
}
/// <summary> // 初始化资源包
/// 反序列化(二进制文件) for (int i = 0; i < manifest.BundleList.Count; i++)
/// </summary>
public static PackageManifest DeserializeFromBinary(byte[] binaryData, IManifestRestoreServices services)
{
// 创建缓存器
BufferReader buffer;
if (services != null)
{ {
var resultBytes = services.RestoreManifest(binaryData); var packageBundle = manifest.BundleList[i];
buffer = new BufferReader(resultBytes); packageBundle.InitBundle(manifest);
}
else
{
buffer = new BufferReader(binaryData);
}
// 读取文件标记
uint fileSign = buffer.ReadUInt32();
if (fileSign != ManifestDefine.FileSign)
throw new Exception("Invalid manifest file !");
// 读取文件版本
string fileVersion = buffer.ReadUTF8();
if (fileVersion != ManifestDefine.FileVersion)
throw new Exception($"The manifest file version are not compatible : {fileVersion} != {ManifestDefine.FileVersion}");
PackageManifest manifest = new PackageManifest();
{
// 读取文件头信息
manifest.FileVersion = fileVersion;
manifest.EnableAddressable = buffer.ReadBool();
manifest.SupportExtensionless = buffer.ReadBool();
manifest.LocationToLower = buffer.ReadBool();
manifest.IncludeAssetGUID = buffer.ReadBool();
manifest.OutputNameStyle = buffer.ReadInt32();
manifest.BuildBundleType = buffer.ReadInt32();
manifest.BuildPipeline = buffer.ReadUTF8();
manifest.PackageName = buffer.ReadUTF8();
manifest.PackageVersion = buffer.ReadUTF8();
manifest.PackageNote = buffer.ReadUTF8();
// 检测配置
if (manifest.EnableAddressable && manifest.LocationToLower)
throw new Exception("Addressable not support location to lower !");
// 读取资源列表
int packageAssetCount = buffer.ReadInt32();
CreateAssetCollection(manifest, packageAssetCount);
for (int i = 0; i < packageAssetCount; i++)
{
var packageAsset = new PackageAsset();
packageAsset.Address = buffer.ReadUTF8();
packageAsset.AssetPath = buffer.ReadUTF8();
packageAsset.AssetGUID = buffer.ReadUTF8();
packageAsset.AssetTags = buffer.ReadUTF8Array();
packageAsset.BundleID = buffer.ReadInt32();
packageAsset.DependBundleIDs = buffer.ReadInt32Array();
FillAssetCollection(manifest, packageAsset);
}
// 读取资源包列表
int packageBundleCount = buffer.ReadInt32();
CreateBundleCollection(manifest, packageBundleCount);
for (int i = 0; i < packageBundleCount; i++)
{
var packageBundle = new PackageBundle();
packageBundle.BundleName = buffer.ReadUTF8();
packageBundle.UnityCRC = buffer.ReadUInt32();
packageBundle.FileHash = buffer.ReadUTF8();
packageBundle.FileCRC = buffer.ReadUInt32();
packageBundle.FileSize = buffer.ReadInt64();
packageBundle.Encrypted = buffer.ReadBool();
packageBundle.Tags = buffer.ReadUTF8Array();
packageBundle.DependBundleIDs = buffer.ReadInt32Array();
FillBundleCollection(manifest, packageBundle);
}
} }
// 初始化资源清单 // 初始化资源清单
@@ -206,8 +133,20 @@ namespace YooAsset
return manifest; return manifest;
} }
/// <summary>
/// 反序列化(二进制文件)
/// </summary>
public static PackageManifest DeserializeFromBinary(byte[] binaryData, IManifestRestoreServices services)
{
DeserializeManifestOperation operation = new DeserializeManifestOperation(services, binaryData);
operation.StartOperation();
operation.WaitForAsyncComplete();
return operation.Manifest;
}
#region /// <summary>
/// 初始化资源清单
/// </summary>
public static void InitManifest(PackageManifest manifest) public static void InitManifest(PackageManifest manifest)
{ {
// 填充资源包内包含的主资源列表 // 填充资源包内包含的主资源列表
@@ -237,108 +176,6 @@ namespace YooAsset
} }
} }
public static void CreateAssetCollection(PackageManifest manifest, int assetCount)
{
manifest.AssetList = new List<PackageAsset>(assetCount);
manifest.AssetDic = new Dictionary<string, PackageAsset>(assetCount);
if (manifest.EnableAddressable)
{
manifest.AssetPathMapping1 = new Dictionary<string, string>(assetCount * 3);
}
else
{
if (manifest.LocationToLower)
manifest.AssetPathMapping1 = new Dictionary<string, string>(assetCount * 2, StringComparer.OrdinalIgnoreCase);
else
manifest.AssetPathMapping1 = new Dictionary<string, string>(assetCount * 2);
}
if (manifest.IncludeAssetGUID)
manifest.AssetPathMapping2 = new Dictionary<string, string>(assetCount);
else
manifest.AssetPathMapping2 = new Dictionary<string, string>();
}
public static void FillAssetCollection(PackageManifest manifest, PackageAsset packageAsset)
{
// 添加到列表集合
manifest.AssetList.Add(packageAsset);
// 注意:我们不允许原始路径存在重名
string assetPath = packageAsset.AssetPath;
if (manifest.AssetDic.ContainsKey(assetPath))
throw new System.Exception($"AssetPath have existed : {assetPath}");
else
manifest.AssetDic.Add(assetPath, packageAsset);
// 填充AssetPathMapping1
{
string location = packageAsset.AssetPath;
// 添加原生路径的映射
if (manifest.AssetPathMapping1.ContainsKey(location))
throw new System.Exception($"Location have existed : {location}");
else
manifest.AssetPathMapping1.Add(location, packageAsset.AssetPath);
// 添加无后缀名路径的映射
if (manifest.SupportExtensionless)
{
string locationWithoutExtension = Path.ChangeExtension(location, null);
if (ReferenceEquals(location, locationWithoutExtension) == false)
{
if (manifest.AssetPathMapping1.ContainsKey(locationWithoutExtension))
YooLogger.Warning($"Location have existed : {locationWithoutExtension}");
else
manifest.AssetPathMapping1.Add(locationWithoutExtension, packageAsset.AssetPath);
}
}
}
// 添加可寻址地址
if (manifest.EnableAddressable)
{
string location = packageAsset.Address;
if (string.IsNullOrEmpty(location) == false)
{
if (manifest.AssetPathMapping1.ContainsKey(location))
throw new System.Exception($"Location have existed : {location}");
else
manifest.AssetPathMapping1.Add(location, packageAsset.AssetPath);
}
}
// 填充AssetPathMapping2
if (manifest.IncludeAssetGUID)
{
if (manifest.AssetPathMapping2.ContainsKey(packageAsset.AssetGUID))
throw new System.Exception($"AssetGUID have existed : {packageAsset.AssetGUID}");
else
manifest.AssetPathMapping2.Add(packageAsset.AssetGUID, packageAsset.AssetPath);
}
}
public static void CreateBundleCollection(PackageManifest manifest, int bundleCount)
{
manifest.BundleList = new List<PackageBundle>(bundleCount);
manifest.BundleDic1 = new Dictionary<string, PackageBundle>(bundleCount);
manifest.BundleDic2 = new Dictionary<string, PackageBundle>(bundleCount);
manifest.BundleDic3 = new Dictionary<string, PackageBundle>(bundleCount);
}
public static void FillBundleCollection(PackageManifest manifest, PackageBundle packageBundle)
{
// 初始化资源包
packageBundle.InitBundle(manifest);
// 添加到列表集合
manifest.BundleList.Add(packageBundle);
manifest.BundleDic1.Add(packageBundle.BundleName, packageBundle);
manifest.BundleDic2.Add(packageBundle.FileName, packageBundle);
manifest.BundleDic3.Add(packageBundle.BundleGUID, packageBundle);
}
#endregion
/// <summary> /// <summary>
/// 获取资源文件的后缀名 /// 获取资源文件的后缀名
/// </summary> /// </summary>

View File

@@ -117,7 +117,7 @@ namespace YooAsset
{ {
_packageAssetCount = _buffer.ReadInt32(); _packageAssetCount = _buffer.ReadInt32();
_progressTotalValue = _packageAssetCount; _progressTotalValue = _packageAssetCount;
ManifestTools.CreateAssetCollection(Manifest, _packageAssetCount); CreateAssetCollection(Manifest, _packageAssetCount);
_steps = ESteps.DeserializeAssetList; _steps = ESteps.DeserializeAssetList;
} }
if (_steps == ESteps.DeserializeAssetList) if (_steps == ESteps.DeserializeAssetList)
@@ -131,7 +131,7 @@ namespace YooAsset
packageAsset.AssetTags = _buffer.ReadUTF8Array(); packageAsset.AssetTags = _buffer.ReadUTF8Array();
packageAsset.BundleID = _buffer.ReadInt32(); packageAsset.BundleID = _buffer.ReadInt32();
packageAsset.DependBundleIDs = _buffer.ReadInt32Array(); packageAsset.DependBundleIDs = _buffer.ReadInt32Array();
ManifestTools.FillAssetCollection(Manifest, packageAsset); FillAssetCollection(Manifest, packageAsset);
_packageAssetCount--; _packageAssetCount--;
Progress = 1f - _packageAssetCount / _progressTotalValue; Progress = 1f - _packageAssetCount / _progressTotalValue;
@@ -149,7 +149,7 @@ namespace YooAsset
{ {
_packageBundleCount = _buffer.ReadInt32(); _packageBundleCount = _buffer.ReadInt32();
_progressTotalValue = _packageBundleCount; _progressTotalValue = _packageBundleCount;
ManifestTools.CreateBundleCollection(Manifest, _packageBundleCount); CreateBundleCollection(Manifest, _packageBundleCount);
_steps = ESteps.DeserializeBundleList; _steps = ESteps.DeserializeBundleList;
} }
if (_steps == ESteps.DeserializeBundleList) if (_steps == ESteps.DeserializeBundleList)
@@ -165,7 +165,7 @@ namespace YooAsset
packageBundle.Encrypted = _buffer.ReadBool(); packageBundle.Encrypted = _buffer.ReadBool();
packageBundle.Tags = _buffer.ReadUTF8Array(); packageBundle.Tags = _buffer.ReadUTF8Array();
packageBundle.DependBundleIDs = _buffer.ReadInt32Array(); packageBundle.DependBundleIDs = _buffer.ReadInt32Array();
ManifestTools.FillBundleCollection(Manifest, packageBundle); FillBundleCollection(Manifest, packageBundle);
_packageBundleCount--; _packageBundleCount--;
Progress = 1f - _packageBundleCount / _progressTotalValue; Progress = 1f - _packageBundleCount / _progressTotalValue;
@@ -194,5 +194,117 @@ namespace YooAsset
Error = e.Message; Error = e.Message;
} }
} }
internal override void InternalWaitForAsyncComplete()
{
while (true)
{
if (ExecuteWhileDone())
{
_steps = ESteps.Done;
break;
}
}
}
private void CreateAssetCollection(PackageManifest manifest, int assetCount)
{
manifest.AssetList = new List<PackageAsset>(assetCount);
manifest.AssetDic = new Dictionary<string, PackageAsset>(assetCount);
if (manifest.EnableAddressable)
{
manifest.AssetPathMapping1 = new Dictionary<string, string>(assetCount * 3);
}
else
{
if (manifest.LocationToLower)
manifest.AssetPathMapping1 = new Dictionary<string, string>(assetCount * 2, StringComparer.OrdinalIgnoreCase);
else
manifest.AssetPathMapping1 = new Dictionary<string, string>(assetCount * 2);
}
if (manifest.IncludeAssetGUID)
manifest.AssetPathMapping2 = new Dictionary<string, string>(assetCount);
else
manifest.AssetPathMapping2 = new Dictionary<string, string>();
}
private void FillAssetCollection(PackageManifest manifest, PackageAsset packageAsset)
{
// 添加到列表集合
manifest.AssetList.Add(packageAsset);
// 注意:我们不允许原始路径存在重名
string assetPath = packageAsset.AssetPath;
if (manifest.AssetDic.ContainsKey(assetPath))
throw new System.Exception($"AssetPath have existed : {assetPath}");
else
manifest.AssetDic.Add(assetPath, packageAsset);
// 填充AssetPathMapping1
{
string location = packageAsset.AssetPath;
// 添加原生路径的映射
if (manifest.AssetPathMapping1.ContainsKey(location))
throw new System.Exception($"Location have existed : {location}");
else
manifest.AssetPathMapping1.Add(location, packageAsset.AssetPath);
// 添加无后缀名路径的映射
if (manifest.SupportExtensionless)
{
string locationWithoutExtension = Path.ChangeExtension(location, null);
if (ReferenceEquals(location, locationWithoutExtension) == false)
{
if (manifest.AssetPathMapping1.ContainsKey(locationWithoutExtension))
YooLogger.Warning($"Location have existed : {locationWithoutExtension}");
else
manifest.AssetPathMapping1.Add(locationWithoutExtension, packageAsset.AssetPath);
}
}
}
// 填充AssetPathMapping2
if (manifest.IncludeAssetGUID)
{
if (manifest.AssetPathMapping2.ContainsKey(packageAsset.AssetGUID))
throw new System.Exception($"AssetGUID have existed : {packageAsset.AssetGUID}");
else
manifest.AssetPathMapping2.Add(packageAsset.AssetGUID, packageAsset.AssetPath);
}
// 添加可寻址地址
if (manifest.EnableAddressable)
{
string location = packageAsset.Address;
if (string.IsNullOrEmpty(location) == false)
{
if (manifest.AssetPathMapping1.ContainsKey(location))
throw new System.Exception($"Location have existed : {location}");
else
manifest.AssetPathMapping1.Add(location, packageAsset.AssetPath);
}
}
}
private void CreateBundleCollection(PackageManifest manifest, int bundleCount)
{
manifest.BundleList = new List<PackageBundle>(bundleCount);
manifest.BundleDic1 = new Dictionary<string, PackageBundle>(bundleCount);
manifest.BundleDic2 = new Dictionary<string, PackageBundle>(bundleCount);
manifest.BundleDic3 = new Dictionary<string, PackageBundle>(bundleCount);
}
private void FillBundleCollection(PackageManifest manifest, PackageBundle packageBundle)
{
// 初始化资源包
packageBundle.InitBundle(manifest);
// 添加到列表集合
manifest.BundleList.Add(packageBundle);
manifest.BundleDic1.Add(packageBundle.BundleName, packageBundle);
manifest.BundleDic2.Add(packageBundle.FileName, packageBundle);
manifest.BundleDic3.Add(packageBundle.BundleGUID, packageBundle);
}
} }
} }