update resource manager

重构运行时核心代码
This commit is contained in:
何冠峰
2024-12-24 18:45:37 +08:00
parent 6254d00bb5
commit 4fc0d06908
56 changed files with 610 additions and 1715 deletions
@@ -3,19 +3,14 @@ namespace YooAsset
{
internal class BundleInfo
{
private readonly string _importFilePath;
private readonly IFileSystem _fileSystem;
private readonly string _importFilePath;
/// <summary>
/// 资源包对象
/// </summary>
public readonly PackageBundle Bundle;
/// <summary>
/// 注意:该字段只用于帮助编辑器下的模拟模式。
/// </summary>
public string[] IncludeAssetsInEditor;
public BundleInfo(IFileSystem fileSystem, PackageBundle bundle)
{
@@ -31,21 +26,13 @@ namespace YooAsset
}
/// <summary>
/// 加载资源文件
/// 加载资源包
/// </summary>
public FSLoadBundleOperation LoadBundleFile()
{
return _fileSystem.LoadBundleFile(Bundle);
}
/// <summary>
/// 卸载资源文件
/// </summary>
public void UnloadBundleFile(object result)
{
_fileSystem.UnloadBundleFile(Bundle, result);
}
/// <summary>
/// 创建下载器
/// </summary>
@@ -0,0 +1,26 @@
namespace YooAsset
{
internal enum EBuildBundleType
{
/// <summary>
/// 未知类型
/// </summary>
Unknown = 0,
/// <summary>
/// 虚拟资源包
/// </summary>
VirtualBundle = 1,
/// <summary>
/// AssetBundle
/// </summary>
AssetBundle = 2,
/// <summary>
/// 原生文件
/// </summary>
RawBundle = 3,
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2ea43593e14f4fb469b0e9abe9a8434d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -39,6 +39,7 @@ namespace YooAsset
buffer.WriteBool(manifest.LocationToLower);
buffer.WriteBool(manifest.IncludeAssetGUID);
buffer.WriteInt32(manifest.OutputNameStyle);
buffer.WriteInt32(manifest.BuildBundleType);
buffer.WriteUTF8(manifest.BuildPipeline);
buffer.WriteUTF8(manifest.PackageName);
buffer.WriteUTF8(manifest.PackageVersion);
@@ -111,6 +112,7 @@ namespace YooAsset
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();
@@ -122,7 +124,7 @@ namespace YooAsset
// 读取资源列表
int packageAssetCount = buffer.ReadInt32();
manifest.AssetList = new List<PackageAsset>(packageAssetCount);
CreateAssetCollection(manifest, packageAssetCount);
for (int i = 0; i < packageAssetCount; i++)
{
var packageAsset = new PackageAsset();
@@ -131,12 +133,12 @@ namespace YooAsset
packageAsset.AssetGUID = buffer.ReadUTF8();
packageAsset.AssetTags = buffer.ReadUTF8Array();
packageAsset.BundleID = buffer.ReadInt32();
manifest.AssetList.Add(packageAsset);
FillAssetCollection(manifest, packageAsset);
}
// 读取资源包列表
int packageBundleCount = buffer.ReadInt32();
manifest.BundleList = new List<PackageBundle>(packageBundleCount);
CreateBundleCollection(manifest, packageBundleCount);
for (int i = 0; i < packageBundleCount; i++)
{
var packageBundle = new PackageBundle();
@@ -148,36 +150,129 @@ namespace YooAsset
packageBundle.Encrypted = buffer.ReadBool();
packageBundle.Tags = buffer.ReadUTF8Array();
packageBundle.DependIDs = buffer.ReadInt32Array();
manifest.BundleList.Add(packageBundle);
FillBundleCollection(manifest, packageBundle);
}
}
// 填充BundleDic
manifest.BundleDic1 = new Dictionary<string, PackageBundle>(manifest.BundleList.Count);
manifest.BundleDic2 = new Dictionary<string, PackageBundle>(manifest.BundleList.Count);
foreach (var packageBundle in manifest.BundleList)
{
packageBundle.ParseBundle(manifest);
manifest.BundleDic1.Add(packageBundle.BundleName, packageBundle);
manifest.BundleDic2.Add(packageBundle.FileName, packageBundle);
}
// 填充AssetDic
manifest.AssetDic = new Dictionary<string, PackageAsset>(manifest.AssetList.Count);
foreach (var packageAsset in manifest.AssetList)
{
// 注意:我们不允许原始路径存在重名
string assetPath = packageAsset.AssetPath;
if (manifest.AssetDic.ContainsKey(assetPath))
throw new Exception($"AssetPath have existed : {assetPath}");
else
manifest.AssetDic.Add(assetPath, packageAsset);
}
// 初始化资源清单
InitManifest(manifest);
return manifest;
}
#endif
#region 解析资源清单辅助方法
public static void InitManifest(PackageManifest manifest)
{
// 填充资源包内包含的主资源列表
foreach (var packageAsset in manifest.AssetList)
{
int bundleID = packageAsset.BundleID;
if (bundleID >= 0 && bundleID < manifest.BundleList.Count)
{
var packageBundle = manifest.BundleList[bundleID];
packageBundle.IncludeMainAssets.Add(packageAsset);
}
else
{
throw new Exception($"Invalid bundle id : {bundleID} Asset path : {packageAsset.AssetPath}");
}
}
}
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
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.LocationToLower)
location = location.ToLower();
// 添加原生路径的映射
if (manifest.AssetPathMapping1.ContainsKey(location))
throw new System.Exception($"Location have existed : {location}");
else
manifest.AssetPathMapping1.Add(location, packageAsset.AssetPath);
// 添加无后缀名路径的映射
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>
@@ -1,6 +1,7 @@
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System;
namespace YooAsset
{
@@ -14,6 +15,7 @@ namespace YooAsset
DeserializeAssetList,
PrepareBundleList,
DeserializeBundleList,
InitManifest,
Done,
}
@@ -80,6 +82,7 @@ namespace YooAsset
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();
@@ -95,20 +98,8 @@ namespace YooAsset
if (_steps == ESteps.PrepareAssetList)
{
_packageAssetCount = _buffer.ReadInt32();
Manifest.AssetList = new List<PackageAsset>(_packageAssetCount);
Manifest.AssetDic = new Dictionary<string, PackageAsset>(_packageAssetCount);
if (Manifest.EnableAddressable)
Manifest.AssetPathMapping1 = new Dictionary<string, string>(_packageAssetCount * 3);
else
Manifest.AssetPathMapping1 = new Dictionary<string, string>(_packageAssetCount * 2);
if (Manifest.IncludeAssetGUID)
Manifest.AssetPathMapping2 = new Dictionary<string, string>(_packageAssetCount);
else
Manifest.AssetPathMapping2 = new Dictionary<string, string>();
_progressTotalValue = _packageAssetCount;
ManifestTools.CreateAssetCollection(Manifest, _packageAssetCount);
_steps = ESteps.DeserializeAssetList;
}
if (_steps == ESteps.DeserializeAssetList)
@@ -121,57 +112,7 @@ namespace YooAsset
packageAsset.AssetGUID = _buffer.ReadUTF8();
packageAsset.AssetTags = _buffer.ReadUTF8Array();
packageAsset.BundleID = _buffer.ReadInt32();
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.LocationToLower)
location = location.ToLower();
// 添加原生路径的映射
if (Manifest.AssetPathMapping1.ContainsKey(location))
throw new System.Exception($"Location have existed : {location}");
else
Manifest.AssetPathMapping1.Add(location, packageAsset.AssetPath);
// 添加无后缀名路径的映射
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);
}
ManifestTools.FillAssetCollection(Manifest, packageAsset);
_packageAssetCount--;
Progress = 1f - _packageAssetCount / _progressTotalValue;
@@ -188,11 +129,8 @@ namespace YooAsset
if (_steps == ESteps.PrepareBundleList)
{
_packageBundleCount = _buffer.ReadInt32();
Manifest.BundleList = new List<PackageBundle>(_packageBundleCount);
Manifest.BundleDic1 = new Dictionary<string, PackageBundle>(_packageBundleCount);
Manifest.BundleDic2 = new Dictionary<string, PackageBundle>(_packageBundleCount);
Manifest.BundleDic3 = new Dictionary<string, PackageBundle>(_packageBundleCount);
_progressTotalValue = _packageBundleCount;
ManifestTools.CreateBundleCollection(Manifest, _packageBundleCount);
_steps = ESteps.DeserializeBundleList;
}
if (_steps == ESteps.DeserializeBundleList)
@@ -208,14 +146,7 @@ namespace YooAsset
packageBundle.Encrypted = _buffer.ReadBool();
packageBundle.Tags = _buffer.ReadUTF8Array();
packageBundle.DependIDs = _buffer.ReadInt32Array();
packageBundle.ParseBundle(Manifest);
Manifest.BundleList.Add(packageBundle);
Manifest.BundleDic1.Add(packageBundle.BundleName, packageBundle);
Manifest.BundleDic2.Add(packageBundle.FileName, packageBundle);
// 注意:原始文件可能存在相同的BundleGUID
if (Manifest.BundleDic3.ContainsKey(packageBundle.BundleGUID) == false)
Manifest.BundleDic3.Add(packageBundle.BundleGUID, packageBundle);
ManifestTools.FillBundleCollection(Manifest, packageBundle);
_packageBundleCount--;
Progress = 1f - _packageBundleCount / _progressTotalValue;
@@ -225,10 +156,16 @@ namespace YooAsset
if (_packageBundleCount <= 0)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
_steps = ESteps.InitManifest;
}
}
if (_steps == ESteps.InitManifest)
{
ManifestTools.InitManifest(Manifest);
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
}
catch (System.Exception e)
{
@@ -31,6 +31,12 @@ namespace YooAsset
/// </summary>
public int BundleID;
/// <summary>
/// 所属资源包名称(仅编辑器有效)
/// </summary>
[NonSerialized]
public string BundleNameInEditor;
/// <summary>
/// 是否包含Tag
/// </summary>
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Collections.Generic;
namespace YooAsset
{
@@ -46,17 +47,6 @@ namespace YooAsset
/// </summary>
public int[] DependIDs;
/// <summary>
/// 所属的包裹名称
/// </summary>
public string PackageName { private set; get; }
/// <summary>
/// 所属的构建管线
/// </summary>
public string BuildPipeline { private set; get; }
/// <summary>
/// 资源包GUID
/// </summary>
@@ -65,6 +55,18 @@ namespace YooAsset
get { return FileHash; }
}
/// <summary>
/// 资源包类型
/// </summary>
private int _bundleType;
public int BundleType
{
get
{
return _bundleType;
}
}
/// <summary>
/// 文件名称
/// </summary>
@@ -93,18 +95,23 @@ namespace YooAsset
}
}
/// <summary>
/// 包含的主资源集合
/// </summary>
[NonSerialized]
public readonly List<PackageAsset> IncludeMainAssets = new List<PackageAsset>(10);
public PackageBundle()
{
}
/// <summary>
/// 解析资源包
/// 初始化资源包
/// </summary>
public void ParseBundle(PackageManifest manifest)
public void InitBundle(PackageManifest manifest)
{
PackageName = manifest.PackageName;
BuildPipeline = manifest.BuildPipeline;
_bundleType = manifest.BuildBundleType;
_fileExtension = ManifestTools.GetRemoteBundleFileExtension(BundleName);
_fileName = ManifestTools.GetRemoteBundleFileName(manifest.OutputNameStyle, BundleName, _fileExtension, FileHash);
}
@@ -28,6 +28,11 @@ namespace YooAsset
/// </summary>
public int OutputNameStyle;
/// <summary>
/// 构建资源包类型
/// </summary>
public int BuildBundleType;
/// <summary>
/// 构建管线名称
/// </summary>
@@ -37,6 +37,11 @@ namespace YooAsset
/// </summary>
public int OutputNameStyle;
/// <summary>
/// 构建资源包类型
/// </summary>
public int BuildBundleType;
/// <summary>
/// 构建管线名称
/// </summary>
@@ -67,6 +72,23 @@ namespace YooAsset
/// </summary>
public List<PackageBundle> BundleList = new List<PackageBundle>();
/// <summary>
/// 资源映射集合(提供AssetPath获取PackageAsset)
/// </summary>
[NonSerialized]
public Dictionary<string, PackageAsset> AssetDic;
/// <summary>
/// 资源路径映射集合(提供Location获取AssetPath)
/// </summary>
[NonSerialized]
public Dictionary<string, string> AssetPathMapping1;
/// <summary>
/// 资源路径映射集合(提供AssetGUID获取AssetPath)
/// </summary>
[NonSerialized]
public Dictionary<string, string> AssetPathMapping2;
/// <summary>
/// 资源包集合(提供BundleName获取PackageBundle)
@@ -86,24 +108,6 @@ namespace YooAsset
[NonSerialized]
public Dictionary<string, PackageBundle> BundleDic3;
/// <summary>
/// 资源映射集合(提供AssetPath获取PackageAsset)
/// </summary>
[NonSerialized]
public Dictionary<string, PackageAsset> AssetDic;
/// <summary>
/// 资源路径映射集合(提供Location获取AssetPath)
/// </summary>
[NonSerialized]
public Dictionary<string, string> AssetPathMapping1;
/// <summary>
/// 资源路径映射集合(提供AssetGUID获取AssetPath)
/// </summary>
[NonSerialized]
public Dictionary<string, string> AssetPathMapping2;
/// <summary>
/// 获取包裹的详细信息
@@ -116,6 +120,7 @@ namespace YooAsset
details.LocationToLower = LocationToLower;
details.IncludeAssetGUID = IncludeAssetGUID;
details.OutputNameStyle = OutputNameStyle;
details.BuildBundleType = BuildBundleType;
details.BuildPipeline = BuildPipeline;
details.PackageName = PackageName;
details.PackageVersion = PackageVersion;
@@ -357,25 +362,6 @@ namespace YooAsset
}
}
/// <summary>
/// 获取资源包内的主资源列表
/// </summary>
public string[] GetBundleIncludeAssets(string assetPath)
{
List<string> assetList = new List<string>();
if (TryGetPackageAsset(assetPath, out PackageAsset result))
{
foreach (var packageAsset in AssetList)
{
if (packageAsset.BundleID == result.BundleID)
{
assetList.Add(packageAsset.AssetPath);
}
}
}
return assetList.ToArray();
}
#region 调试方法
[Conditional("DEBUG")]
private void DebugCheckLocation(string location)
@@ -3,6 +3,14 @@ namespace YooAsset
{
public class EditorSimulateBuildParam
{
/// <summary>
/// 包裹名称
/// </summary>
public string PackageName;
/// <summary>
/// 模拟构建管线名称
/// </summary>
public string BuildPipelineName = "EditorSimulateBuildPipeline";
}
}
@@ -106,7 +106,6 @@ namespace YooAsset
if (EditorFileSystem.Belong(packageBundle))
{
BundleInfo bundleInfo = new BundleInfo(EditorFileSystem, packageBundle);
bundleInfo.IncludeAssetsInEditor = ActiveManifest.GetBundleIncludeAssets(assetInfo.AssetPath);
return bundleInfo;
}