Optimized asset system and patch system framework

优化了资源定位不正确导致的错误报告方式。
YooAssets.ProcessOperation()重命名为YooAssets.StartOperation()
YooAssets.GetBundleInfo()已经移除方法。
YooAssets.IsNeedDownloadFromRemote()新增加方法。
This commit is contained in:
hevinci
2022-05-11 16:48:17 +08:00
parent 67eeae31c7
commit 4cad587609
34 changed files with 726 additions and 573 deletions
@@ -1,7 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
@@ -12,16 +11,8 @@ namespace YooAsset
private static readonly List<AssetBundleLoaderBase> _loaders = new List<AssetBundleLoaderBase>(1000);
private static readonly List<ProviderBase> _providers = new List<ProviderBase>(1000);
private static readonly Dictionary<string, SceneOperationHandle> _sceneHandles = new Dictionary<string, SceneOperationHandle>(100);
/// <summary>
/// 在编辑器下模拟运行
/// </summary>
public static bool SimulationOnEditor { private set; get; }
/// <summary>
/// 运行时的最大加载个数
/// </summary>
public static int AssetLoadingMaxNumber { private set; get; }
private static bool _simulationOnEditor;
private static int _loadingMaxNumber;
public static IDecryptionServices DecryptionServices { private set; get; }
public static IBundleServices BundleServices { private set; get; }
@@ -31,10 +22,10 @@ namespace YooAsset
/// 初始化资源系统
/// 注意:在使用AssetSystem之前需要初始化
/// </summary>
public static void Initialize(bool simulationOnEditor, int assetLoadingMaxNumber, IDecryptionServices decryptionServices, IBundleServices bundleServices)
public static void Initialize(bool simulationOnEditor, int loadingMaxNumber, IDecryptionServices decryptionServices, IBundleServices bundleServices)
{
SimulationOnEditor = simulationOnEditor;
AssetLoadingMaxNumber = assetLoadingMaxNumber;
_simulationOnEditor = simulationOnEditor;
_loadingMaxNumber = loadingMaxNumber;
DecryptionServices = decryptionServices;
BundleServices = bundleServices;
}
@@ -63,7 +54,7 @@ namespace YooAsset
}
else
{
if (loadingCount < AssetLoadingMaxNumber)
if (loadingCount < _loadingMaxNumber)
provider.Update();
if (provider.IsDone == false)
@@ -77,7 +68,7 @@ namespace YooAsset
/// </summary>
public static void UnloadUnusedAssets()
{
if (SimulationOnEditor)
if (_simulationOnEditor)
{
for (int i = _providers.Count - 1; i >= 0; i--)
{
@@ -128,15 +119,21 @@ namespace YooAsset
Resources.UnloadUnusedAssets();
}
/// <summary>
/// 加载场景
/// </summary>
public static SceneOperationHandle LoadSceneAsync(string scenePath, LoadSceneMode sceneMode, bool activateOnLoad, int priority)
public static SceneOperationHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode, bool activateOnLoad, int priority)
{
if (assetInfo.IsInvalid)
{
CompletedProvider completedProvider = new CompletedProvider(assetInfo);
return completedProvider.CreateHandle<SceneOperationHandle>();
}
// 注意:场景句柄永远保持唯一
if (_sceneHandles.ContainsKey(scenePath))
return _sceneHandles[scenePath];
string providerGUID = assetInfo.ProviderGUID;
if (_sceneHandles.ContainsKey(providerGUID))
return _sceneHandles[providerGUID];
// 如果加载的是主场景,则卸载所有缓存的场景
if (sceneMode == LoadSceneMode.Single)
@@ -144,68 +141,79 @@ namespace YooAsset
UnloadAllScene();
}
ProviderBase provider = TryGetProvider(scenePath);
ProviderBase provider = TryGetProvider(providerGUID);
if (provider == null)
{
if (SimulationOnEditor)
provider = new DatabaseSceneProvider(scenePath, sceneMode, activateOnLoad, priority);
if (_simulationOnEditor)
provider = new DatabaseSceneProvider(assetInfo, sceneMode, activateOnLoad, priority);
else
provider = new BundledSceneProvider(scenePath, sceneMode, activateOnLoad, priority);
provider = new BundledSceneProvider(assetInfo, sceneMode, activateOnLoad, priority);
provider.InitSpawnDebugInfo();
_providers.Add(provider);
}
var handle = provider.CreateHandle() as SceneOperationHandle;
_sceneHandles.Add(scenePath, handle);
var handle = provider.CreateHandle<SceneOperationHandle>();
_sceneHandles.Add(providerGUID, handle);
return handle;
}
/// <summary>
/// 加载资源对象
/// </summary>
public static AssetOperationHandle LoadAssetAsync(string assetPath, System.Type assetType)
public static AssetOperationHandle LoadAssetAsync(AssetInfo assetInfo)
{
ProviderBase provider = TryGetProvider(assetPath);
if (assetInfo.IsInvalid)
{
CompletedProvider completedProvider = new CompletedProvider(assetInfo);
return completedProvider.CreateHandle<AssetOperationHandle>();
}
ProviderBase provider = TryGetProvider(assetInfo.ProviderGUID);
if (provider == null)
{
if (SimulationOnEditor)
provider = new DatabaseAssetProvider(assetPath, assetType);
if (_simulationOnEditor)
provider = new DatabaseAssetProvider(assetInfo);
else
provider = new BundledAssetProvider(assetPath, assetType);
provider = new BundledAssetProvider(assetInfo);
provider.InitSpawnDebugInfo();
_providers.Add(provider);
}
return provider.CreateHandle() as AssetOperationHandle;
return provider.CreateHandle<AssetOperationHandle>();
}
/// <summary>
/// 加载子资源对象
/// </summary>
public static SubAssetsOperationHandle LoadSubAssetsAsync(string assetPath, System.Type assetType)
public static SubAssetsOperationHandle LoadSubAssetsAsync(AssetInfo assetInfo)
{
ProviderBase provider = TryGetProvider(assetPath);
if (assetInfo.IsInvalid)
{
CompletedProvider completedProvider = new CompletedProvider(assetInfo);
return completedProvider.CreateHandle<SubAssetsOperationHandle>();
}
ProviderBase provider = TryGetProvider(assetInfo.ProviderGUID);
if (provider == null)
{
if (SimulationOnEditor)
provider = new DatabaseSubAssetsProvider(assetPath, assetType);
if (_simulationOnEditor)
provider = new DatabaseSubAssetsProvider(assetInfo);
else
provider = new BundledSubAssetsProvider(assetPath, assetType);
provider = new BundledSubAssetsProvider(assetInfo);
provider.InitSpawnDebugInfo();
_providers.Add(provider);
}
return provider.CreateHandle() as SubAssetsOperationHandle;
return provider.CreateHandle<SubAssetsOperationHandle>();
}
internal static void UnloadSubScene(ProviderBase provider)
{
string scenePath = provider.AssetPath;
if (_sceneHandles.ContainsKey(scenePath) == false)
string providerGUID = provider.MainAssetInfo.ProviderGUID;
if (_sceneHandles.ContainsKey(providerGUID) == false)
throw new Exception("Should never get here !");
// 释放子场景句柄
_sceneHandles[scenePath].ReleaseInternal();
_sceneHandles.Remove(scenePath);
_sceneHandles[providerGUID].ReleaseInternal();
_sceneHandles.Remove(providerGUID);
// 卸载未被使用的资源(包括场景)
AssetSystem.UnloadUnusedAssets();
@@ -239,24 +247,19 @@ namespace YooAsset
}
}
internal static AssetBundleLoaderBase CreateOwnerAssetBundleLoader(string assetPath)
internal static AssetBundleLoaderBase CreateOwnerAssetBundleLoader(AssetInfo assetInfo)
{
string bundleName = BundleServices.GetBundleName(assetPath);
BundleInfo bundleInfo = BundleServices.GetBundleInfo(bundleName);
BundleInfo bundleInfo = BundleServices.GetBundleInfo(assetInfo);
return CreateAssetBundleLoaderInternal(bundleInfo);
}
internal static List<AssetBundleLoaderBase> CreateDependAssetBundleLoaders(string assetPath)
internal static List<AssetBundleLoaderBase> CreateDependAssetBundleLoaders(AssetInfo assetInfo)
{
List<AssetBundleLoaderBase> result = new List<AssetBundleLoaderBase>();
string[] depends = BundleServices.GetAllDependencies(assetPath);
if (depends != null)
BundleInfo[] depends = BundleServices.GetAllDependBundleInfos(assetInfo);
List<AssetBundleLoaderBase> result = new List<AssetBundleLoaderBase>(depends.Length);
foreach (var bundleInfo in depends)
{
foreach (var dependBundleName in depends)
{
BundleInfo dependBundleInfo = BundleServices.GetBundleInfo(dependBundleName);
AssetBundleLoaderBase dependLoader = CreateAssetBundleLoaderInternal(dependBundleInfo);
result.Add(dependLoader);
}
AssetBundleLoaderBase dependLoader = CreateAssetBundleLoaderInternal(bundleInfo);
result.Add(dependLoader);
}
return result;
}
@@ -291,7 +294,7 @@ namespace YooAsset
for (int i = 0; i < _loaders.Count; i++)
{
AssetBundleLoaderBase temp = _loaders[i];
if (temp.BundleFileInfo.BundleName.Equals(bundleName))
if (temp.MainBundleInfo.BundleName.Equals(bundleName))
{
loader = temp;
break;
@@ -299,13 +302,13 @@ namespace YooAsset
}
return loader;
}
private static ProviderBase TryGetProvider(string assetPath)
private static ProviderBase TryGetProvider(string providerGUID)
{
ProviderBase provider = null;
for (int i = 0; i < _providers.Count; i++)
{
ProviderBase temp = _providers[i];
if (temp.AssetPath.Equals(assetPath))
if (temp.MainAssetInfo.ProviderGUID.Equals(providerGUID))
{
provider = temp;
break;
@@ -314,7 +317,6 @@ namespace YooAsset
return provider;
}
#region 调试专属方法
internal static void GetDebugReport(DebugReport report)
{
@@ -325,7 +327,7 @@ namespace YooAsset
foreach (var provider in _providers)
{
DebugProviderInfo providerInfo = new DebugProviderInfo();
providerInfo.AssetPath = provider.AssetPath;
providerInfo.AssetPath = provider.MainAssetInfo.AssetPath;
providerInfo.SpawnScene = provider.SpawnScene;
providerInfo.SpawnTime = provider.SpawnTime;
providerInfo.RefCount = provider.RefCount;
@@ -135,7 +135,7 @@ namespace YooAsset
private InstantiateOperation InstantiateAsyncInternal(Vector3 position, Quaternion rotation, Transform parent, bool setPositionRotation)
{
InstantiateOperation operation = new InstantiateOperation(this, position, rotation, parent, setPositionRotation);
OperationSystem.ProcessOperaiton(operation);
OperationSystem.StartOperaiton(operation);
return operation;
}
}
@@ -4,13 +4,13 @@ namespace YooAsset
{
public abstract class OperationHandleBase : IEnumerator
{
private readonly string _cachedAssetPath;
private readonly AssetInfo _assetInfo;
internal ProviderBase Provider { private set; get; }
internal OperationHandleBase(ProviderBase provider)
{
Provider = provider;
_cachedAssetPath = provider.AssetPath;
_assetInfo = provider.MainAssetInfo;
}
internal abstract void InvokeCallback();
@@ -85,9 +85,9 @@ namespace YooAsset
else
{
if (Provider == null)
YooLogger.Warning($"Operation handle is released : {_cachedAssetPath}");
YooLogger.Warning($"Operation handle is released : {_assetInfo.AssetPath}");
else if (Provider.IsDestroyed)
YooLogger.Warning($"Provider is destroyed : {_cachedAssetPath}");
YooLogger.Warning($"Provider is destroyed : {_assetInfo.AssetPath}");
return false;
}
}
@@ -102,7 +102,7 @@ namespace YooAsset
{
string error = $"{nameof(SceneOperationHandle)} is invalid.";
var operation = new UnloadSceneOperation(error);
OperationSystem.ProcessOperaiton(operation);
OperationSystem.StartOperaiton(operation);
return operation;
}
@@ -112,7 +112,7 @@ namespace YooAsset
string error = $"Cannot unload main scene. Use {nameof(YooAssets.LoadSceneAsync)} method to change the main scene !";
YooLogger.Error(error);
var operation = new UnloadSceneOperation(error);
OperationSystem.ProcessOperaiton(operation);
OperationSystem.StartOperaiton(operation);
return operation;
}
@@ -121,7 +121,7 @@ namespace YooAsset
AssetSystem.UnloadSubScene(Provider);
{
var operation = new UnloadSceneOperation(sceneObject);
OperationSystem.ProcessOperaiton(operation);
OperationSystem.StartOperaiton(operation);
return operation;
}
}
@@ -39,31 +39,33 @@ namespace YooAsset
if (_steps == ESteps.None)
{
if (BundleFileInfo.LoadMode == BundleInfo.ELoadMode.None)
if(MainBundleInfo.IsInvalid)
{
_steps = ESteps.Done;
Status = EStatus.Failed;
LastError = $"Invalid load mode : {BundleFileInfo.BundleName}";
LastError = $"The bundle info is invalid : {MainBundleInfo.BundleName}";
YooLogger.Error(LastError);
return;
}
else if (BundleFileInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote)
if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote)
{
_steps = ESteps.Download;
_fileLoadPath = BundleFileInfo.GetCacheLoadPath();
_fileLoadPath = MainBundleInfo.GetCacheLoadPath();
}
else if (BundleFileInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
{
_steps = ESteps.LoadFile;
_fileLoadPath = BundleFileInfo.GetStreamingLoadPath();
_fileLoadPath = MainBundleInfo.GetStreamingLoadPath();
}
else if (BundleFileInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache)
else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache)
{
_steps = ESteps.LoadFile;
_fileLoadPath = BundleFileInfo.GetCacheLoadPath();
_fileLoadPath = MainBundleInfo.GetCacheLoadPath();
}
else
{
throw new System.NotImplementedException(BundleFileInfo.LoadMode.ToString());
throw new System.NotImplementedException(MainBundleInfo.LoadMode.ToString());
}
}
@@ -71,7 +73,7 @@ namespace YooAsset
if (_steps == ESteps.Download)
{
int failedTryAgain = int.MaxValue;
_downloader = DownloadSystem.BeginDownload(BundleFileInfo, failedTryAgain);
_downloader = DownloadSystem.BeginDownload(MainBundleInfo, failedTryAgain);
_steps = ESteps.CheckDownload;
}
@@ -109,12 +111,12 @@ namespace YooAsset
#endif
// Load assetBundle file
if (BundleFileInfo.IsEncrypted)
if (MainBundleInfo.IsEncrypted)
{
if (AssetSystem.DecryptionServices == null)
throw new Exception($"{nameof(AssetBundleFileLoader)} need {nameof(IDecryptionServices)} : {BundleFileInfo.BundleName}");
throw new Exception($"{nameof(AssetBundleFileLoader)} need {nameof(IDecryptionServices)} : {MainBundleInfo.BundleName}");
ulong offset = AssetSystem.DecryptionServices.GetFileOffset(BundleFileInfo);
ulong offset = AssetSystem.DecryptionServices.GetFileOffset();
if (_isWaitForAsyncComplete)
CacheBundle = AssetBundle.LoadFromFile(_fileLoadPath, 0, offset);
else
@@ -154,7 +156,7 @@ namespace YooAsset
{
_steps = ESteps.Done;
Status = EStatus.Failed;
LastError = $"Failed to load assetBundle : {BundleFileInfo.BundleName}";
LastError = $"Failed to load assetBundle : {MainBundleInfo.BundleName}";
YooLogger.Error(LastError);
}
else
@@ -183,7 +185,7 @@ namespace YooAsset
if (_isShowWaitForAsyncError == false)
{
_isShowWaitForAsyncError = true;
YooLogger.Error($"WaitForAsyncComplete failed ! BundleName : {BundleFileInfo.BundleName} States : {Status}");
YooLogger.Error($"WaitForAsyncComplete failed ! BundleName : {MainBundleInfo.BundleName} States : {Status}");
}
break;
}
@@ -17,7 +17,7 @@ namespace YooAsset
/// <summary>
/// 资源包文件信息
/// </summary>
public BundleInfo BundleFileInfo { private set; get; }
public BundleInfo MainBundleInfo { private set; get; }
/// <summary>
/// 引用计数
@@ -45,7 +45,7 @@ namespace YooAsset
public AssetBundleLoaderBase(BundleInfo bundleInfo)
{
BundleFileInfo = bundleInfo;
MainBundleInfo = bundleInfo;
RefCount = 0;
Status = EStatus.None;
}
@@ -91,9 +91,9 @@ namespace YooAsset
if (forceDestroy == false)
{
if (RefCount > 0)
throw new Exception($"Bundle file loader ref is not zero : {BundleFileInfo.BundleName}");
throw new Exception($"Bundle file loader ref is not zero : {MainBundleInfo.BundleName}");
if (IsDone() == false)
throw new Exception($"Bundle file loader is not done : {BundleFileInfo.BundleName}");
throw new Exception($"Bundle file loader is not done : {MainBundleInfo.BundleName}");
}
if (CacheBundle != null)
@@ -37,28 +37,30 @@ namespace YooAsset
if (_steps == ESteps.None)
{
if (BundleFileInfo.LoadMode == BundleInfo.ELoadMode.None)
if (MainBundleInfo.IsInvalid)
{
_steps = ESteps.Done;
Status = EStatus.Failed;
LastError = $"Invalid load mode : {BundleFileInfo.BundleName}";
LastError = $"The bundle info is invalid : {MainBundleInfo.BundleName}";
YooLogger.Error(LastError);
return;
}
else if (BundleFileInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
{
_steps = ESteps.LoadFile;
_webURL = BundleFileInfo.GetStreamingLoadPath();
_webURL = MainBundleInfo.GetStreamingLoadPath();
}
else
{
throw new System.NotImplementedException(BundleFileInfo.LoadMode.ToString());
throw new System.NotImplementedException(MainBundleInfo.LoadMode.ToString());
}
}
// 1. 从服务器或缓存中获取AssetBundle文件
if (_steps == ESteps.LoadFile)
{
string hash = StringUtility.RemoveExtension(BundleFileInfo.Hash);
string hash = StringUtility.RemoveExtension(MainBundleInfo.Hash);
_webRequest = UnityWebRequestAssetBundle.GetAssetBundle(_webURL, Hash128.Parse(hash));
_webRequest.SendWebRequest();
_steps = ESteps.CheckFile;
@@ -87,7 +89,7 @@ namespace YooAsset
{
_steps = ESteps.Done;
Status = EStatus.Failed;
LastError = $"AssetBundle file is invalid : {BundleFileInfo.BundleName}";
LastError = $"AssetBundle file is invalid : {MainBundleInfo.BundleName}";
YooLogger.Error(LastError);
}
else
@@ -12,9 +12,9 @@ namespace YooAsset
private readonly List<AssetBundleLoaderBase> _dependBundles;
public DependAssetBundleGroup(string assetPath)
public DependAssetBundleGroup(List<AssetBundleLoaderBase> dpendBundles)
{
_dependBundles = AssetSystem.CreateDependAssetBundleLoaders(assetPath);
_dependBundles = dpendBundles;
}
/// <summary>
@@ -102,7 +102,7 @@ namespace YooAsset
foreach (var loader in _dependBundles)
{
var bundleInfo = new DebugBundleInfo();
bundleInfo.BundleName = loader.BundleFileInfo.BundleName;
bundleInfo.BundleName = loader.MainBundleInfo.BundleName;
bundleInfo.RefCount = loader.RefCount;
bundleInfo.Status = loader.Status;
output.Add(bundleInfo);
@@ -7,7 +7,7 @@ namespace YooAsset
/// </summary>
public abstract class RawFileOperation : AsyncOperationBase
{
protected readonly BundleInfo _bundleInfo;
internal readonly BundleInfo _bundleInfo;
/// <summary>
/// 原生文件的拷贝路径
@@ -49,6 +49,34 @@ namespace YooAsset
}
}
/// <summary>
/// 发生错误的原生文件操作
/// </summary>
internal sealed class CompletedRawFileOperation : RawFileOperation
{
private readonly string _error;
internal CompletedRawFileOperation(string error, string copyPath) : base(null, copyPath)
{
_error = error;
}
internal override void Start()
{
Status = EOperationStatus.Failed;
Error = _error;
}
internal override void Update()
{
}
/// <summary>
/// 原生文件的缓存路径
/// </summary>
public override string GetCachePath()
{
return string.Empty;
}
}
/// <summary>
/// 编辑器下模拟运行的原生文件操作
/// </summary>
@@ -17,8 +17,7 @@ namespace YooAsset
}
}
public BundledAssetProvider(string assetPath, System.Type assetType)
: base(assetPath, assetType)
public BundledAssetProvider(AssetInfo assetInfo) : base(assetInfo)
{
}
public override void Update()
@@ -36,19 +35,19 @@ namespace YooAsset
{
if (IsWaitForAsyncComplete)
{
DependBundles.WaitForAsyncComplete();
DependBundleGroup.WaitForAsyncComplete();
OwnerBundle.WaitForAsyncComplete();
}
if (DependBundles.IsDone() == false)
if (DependBundleGroup.IsDone() == false)
return;
if (OwnerBundle.IsDone() == false)
return;
if (DependBundles.IsSucceed() == false)
if (DependBundleGroup.IsSucceed() == false)
{
Status = EStatus.Fail;
LastError = DependBundles.GetLastError();
LastError = DependBundleGroup.GetLastError();
InvokeCompletion();
return;
}
@@ -69,17 +68,17 @@ namespace YooAsset
{
if (IsWaitForAsyncComplete)
{
if (AssetType == null)
AssetObject = OwnerBundle.CacheBundle.LoadAsset(AssetName);
if (MainAssetInfo.AssetType == null)
AssetObject = OwnerBundle.CacheBundle.LoadAsset(MainAssetInfo.AssetName);
else
AssetObject = OwnerBundle.CacheBundle.LoadAsset(AssetName, AssetType);
AssetObject = OwnerBundle.CacheBundle.LoadAsset(MainAssetInfo.AssetName, MainAssetInfo.AssetType);
}
else
{
if (AssetType == null)
_cacheRequest = OwnerBundle.CacheBundle.LoadAssetAsync(AssetName);
if (MainAssetInfo.AssetType == null)
_cacheRequest = OwnerBundle.CacheBundle.LoadAssetAsync(MainAssetInfo.AssetName);
else
_cacheRequest = OwnerBundle.CacheBundle.LoadAssetAsync(AssetName, AssetType);
_cacheRequest = OwnerBundle.CacheBundle.LoadAssetAsync(MainAssetInfo.AssetName, MainAssetInfo.AssetType);
}
Status = EStatus.Checking;
}
@@ -106,10 +105,10 @@ namespace YooAsset
Status = AssetObject == null ? EStatus.Fail : EStatus.Success;
if (Status == EStatus.Fail)
{
if(AssetType == null)
LastError = $"Failed to load asset : {AssetName} AssetType : null AssetBundle : {OwnerBundle.BundleFileInfo.BundleName}";
if(MainAssetInfo.AssetType == null)
LastError = $"Failed to load asset : {MainAssetInfo.AssetName} AssetType : null AssetBundle : {OwnerBundle.MainBundleInfo.BundleName}";
else
LastError = $"Failed to load asset : {AssetName} AssetType : {AssetType} AssetBundle : {OwnerBundle.BundleFileInfo.BundleName}";
LastError = $"Failed to load asset : {MainAssetInfo.AssetName} AssetType : {MainAssetInfo.AssetType} AssetBundle : {OwnerBundle.MainBundleInfo.BundleName}";
YooLogger.Error(LastError);
}
InvokeCompletion();
@@ -6,15 +6,17 @@ namespace YooAsset
internal abstract class BundledProvider : ProviderBase
{
protected AssetBundleLoaderBase OwnerBundle { private set; get; }
protected DependAssetBundleGroup DependBundles { private set; get; }
protected DependAssetBundleGroup DependBundleGroup { private set; get; }
public BundledProvider(string assetPath, System.Type assetType) : base(assetPath, assetType)
public BundledProvider(AssetInfo assetInfo) : base(assetInfo)
{
OwnerBundle = AssetSystem.CreateOwnerAssetBundleLoader(assetPath);
OwnerBundle = AssetSystem.CreateOwnerAssetBundleLoader(assetInfo);
OwnerBundle.Reference();
OwnerBundle.AddProvider(this);
DependBundles = new DependAssetBundleGroup(assetPath);
DependBundles.Reference();
var dependBundles = AssetSystem.CreateDependAssetBundleLoaders(assetInfo);
DependBundleGroup = new DependAssetBundleGroup(dependBundles);
DependBundleGroup.Reference();
}
public override void Destroy()
{
@@ -26,10 +28,10 @@ namespace YooAsset
OwnerBundle.Release();
OwnerBundle = null;
}
if (DependBundles != null)
if (DependBundleGroup != null)
{
DependBundles.Release();
DependBundles = null;
DependBundleGroup.Release();
DependBundleGroup = null;
}
}
@@ -39,12 +41,12 @@ namespace YooAsset
internal void GetBundleDebugInfos(List<DebugBundleInfo> output)
{
var bundleInfo = new DebugBundleInfo();
bundleInfo.BundleName = OwnerBundle.BundleFileInfo.BundleName;
bundleInfo.BundleName = OwnerBundle.MainBundleInfo.BundleName;
bundleInfo.RefCount = OwnerBundle.RefCount;
bundleInfo.Status = OwnerBundle.Status;
output.Add(bundleInfo);
DependBundles.GetBundleDebugInfos(output);
DependBundleGroup.GetBundleDebugInfos(output);
}
}
}
@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
@@ -22,11 +23,10 @@ namespace YooAsset
}
}
public BundledSceneProvider(string scenePath, LoadSceneMode sceneMode, bool activateOnLoad, int priority)
: base(scenePath, null)
public BundledSceneProvider(AssetInfo assetInfo, LoadSceneMode sceneMode, bool activateOnLoad, int priority) : base(assetInfo)
{
SceneMode = sceneMode;
_sceneName = System.IO.Path.GetFileNameWithoutExtension(scenePath);
_sceneName = Path.GetFileNameWithoutExtension(assetInfo.AssetPath);
_activateOnLoad = activateOnLoad;
_priority = priority;
}
@@ -43,15 +43,15 @@ namespace YooAsset
// 1. 检测资源包
if (Status == EStatus.CheckBundle)
{
if (DependBundles.IsDone() == false)
if (DependBundleGroup.IsDone() == false)
return;
if (OwnerBundle.IsDone() == false)
return;
if (DependBundles.IsSucceed() == false)
if (DependBundleGroup.IsSucceed() == false)
{
Status = EStatus.Fail;
LastError = DependBundles.GetLastError();
LastError = DependBundleGroup.GetLastError();
InvokeCompletion();
return;
}
@@ -98,7 +98,7 @@ namespace YooAsset
Status = SceneObject.IsValid() ? EStatus.Success : EStatus.Fail;
if(Status == EStatus.Fail)
{
LastError = $"The load scene is invalid : {AssetPath}";
LastError = $"The load scene is invalid : {MainAssetInfo.AssetPath}";
YooLogger.Error(LastError);
}
InvokeCompletion();
@@ -17,8 +17,7 @@ namespace YooAsset
}
}
public BundledSubAssetsProvider(string assetPath, System.Type assetType)
: base(assetPath, assetType)
public BundledSubAssetsProvider(AssetInfo assetInfo) : base(assetInfo)
{
}
public override void Update()
@@ -36,19 +35,19 @@ namespace YooAsset
{
if (IsWaitForAsyncComplete)
{
DependBundles.WaitForAsyncComplete();
DependBundleGroup.WaitForAsyncComplete();
OwnerBundle.WaitForAsyncComplete();
}
if (DependBundles.IsDone() == false)
if (DependBundleGroup.IsDone() == false)
return;
if (OwnerBundle.IsDone() == false)
return;
if (DependBundles.IsSucceed() == false)
if (DependBundleGroup.IsSucceed() == false)
{
Status = EStatus.Fail;
LastError = DependBundles.GetLastError();
LastError = DependBundleGroup.GetLastError();
InvokeCompletion();
return;
}
@@ -69,17 +68,17 @@ namespace YooAsset
{
if (IsWaitForAsyncComplete)
{
if (AssetType == null)
AllAssetObjects = OwnerBundle.CacheBundle.LoadAssetWithSubAssets(AssetName);
if (MainAssetInfo.AssetType == null)
AllAssetObjects = OwnerBundle.CacheBundle.LoadAssetWithSubAssets(MainAssetInfo.AssetName);
else
AllAssetObjects = OwnerBundle.CacheBundle.LoadAssetWithSubAssets(AssetName, AssetType);
AllAssetObjects = OwnerBundle.CacheBundle.LoadAssetWithSubAssets(MainAssetInfo.AssetName, MainAssetInfo.AssetType);
}
else
{
if (AssetType == null)
_cacheRequest = OwnerBundle.CacheBundle.LoadAssetWithSubAssetsAsync(AssetName);
if (MainAssetInfo.AssetType == null)
_cacheRequest = OwnerBundle.CacheBundle.LoadAssetWithSubAssetsAsync(MainAssetInfo.AssetName);
else
_cacheRequest = OwnerBundle.CacheBundle.LoadAssetWithSubAssetsAsync(AssetName, AssetType);
_cacheRequest = OwnerBundle.CacheBundle.LoadAssetWithSubAssetsAsync(MainAssetInfo.AssetName, MainAssetInfo.AssetType);
}
Status = EStatus.Checking;
}
@@ -106,10 +105,10 @@ namespace YooAsset
Status = AllAssetObjects == null ? EStatus.Fail : EStatus.Success;
if (Status == EStatus.Fail)
{
if (AssetType == null)
LastError = $"Failed to load sub assets : {AssetName} AssetType : null AssetBundle : {OwnerBundle.BundleFileInfo.BundleName}";
if (MainAssetInfo.AssetType == null)
LastError = $"Failed to load sub assets : {MainAssetInfo.AssetName} AssetType : null AssetBundle : {OwnerBundle.MainBundleInfo.BundleName}";
else
LastError = $"Failed to load sub assets : {AssetName} AssetType : {AssetType} AssetBundle : {OwnerBundle.BundleFileInfo.BundleName}";
LastError = $"Failed to load sub assets : {MainAssetInfo.AssetName} AssetType : {MainAssetInfo.AssetType} AssetBundle : {OwnerBundle.MainBundleInfo.BundleName}";
YooLogger.Error(LastError);
}
InvokeCompletion();
@@ -0,0 +1,33 @@
namespace YooAsset
{
internal sealed class CompletedProvider : ProviderBase
{
public override float Progress
{
get
{
if (IsDone)
return 1f;
else
return 0;
}
}
public CompletedProvider(AssetInfo assetInfo) : base(assetInfo)
{
}
public override void Update()
{
if (IsDone)
return;
if (Status == EStatus.None)
{
Status = EStatus.Fail;
LastError = MainAssetInfo.Error;
InvokeCompletion();
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8e64fd2ee771f7d498838ebf375a9531
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -17,8 +17,7 @@ namespace YooAsset
}
}
public DatabaseAssetProvider(string assetPath, System.Type assetType)
: base(assetPath, assetType)
public DatabaseAssetProvider(AssetInfo assetInfo) : base(assetInfo)
{
}
public override void Update()
@@ -30,11 +29,11 @@ namespace YooAsset
if (Status == EStatus.None)
{
// 检测资源文件是否存在
string guid = UnityEditor.AssetDatabase.AssetPathToGUID(AssetPath);
string guid = UnityEditor.AssetDatabase.AssetPathToGUID(MainAssetInfo.AssetPath);
if (string.IsNullOrEmpty(guid))
{
Status = EStatus.Fail;
LastError = $"Not found asset : {AssetPath}";
LastError = $"Not found asset : {MainAssetInfo.AssetPath}";
YooLogger.Error(LastError);
InvokeCompletion();
return;
@@ -50,10 +49,10 @@ namespace YooAsset
// 1. 加载资源对象
if (Status == EStatus.Loading)
{
if (AssetType == null)
AssetObject = UnityEditor.AssetDatabase.LoadMainAssetAtPath(AssetPath);
if (MainAssetInfo.AssetType == null)
AssetObject = UnityEditor.AssetDatabase.LoadMainAssetAtPath(MainAssetInfo.AssetPath);
else
AssetObject = UnityEditor.AssetDatabase.LoadAssetAtPath(AssetPath, AssetType);
AssetObject = UnityEditor.AssetDatabase.LoadAssetAtPath(MainAssetInfo.AssetPath, MainAssetInfo.AssetType);
Status = EStatus.Checking;
}
@@ -66,7 +65,7 @@ namespace YooAsset
{
AssetObject = null;
Status = EStatus.Fail;
LastError = $"The loaded asset object is not main asset : {AssetPath} AssetType : {AssetType}";
LastError = $"The loaded asset object is not main asset : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}";
YooLogger.Error(LastError);
InvokeCompletion();
return;
@@ -76,12 +75,12 @@ namespace YooAsset
Status = AssetObject == null ? EStatus.Fail : EStatus.Success;
if (Status == EStatus.Fail)
{
if (AssetType == null)
LastError = $"Failed to load asset object : {AssetPath} AssetType : null";
if (MainAssetInfo.AssetType == null)
LastError = $"Failed to load asset object : {MainAssetInfo.AssetPath} AssetType : null";
else
LastError = $"Failed to load asset object : {AssetPath} AssetType : {AssetType}";
LastError = $"Failed to load asset object : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}";
YooLogger.Error(LastError);
}
}
InvokeCompletion();
}
#endif
@@ -19,8 +19,7 @@ namespace YooAsset
}
}
public DatabaseSceneProvider(string scenePath, LoadSceneMode sceneMode, bool activateOnLoad, int priority)
: base(scenePath, null)
public DatabaseSceneProvider(AssetInfo assetInfo, LoadSceneMode sceneMode, bool activateOnLoad, int priority) : base(assetInfo)
{
SceneMode = sceneMode;
_activateOnLoad = activateOnLoad;
@@ -42,7 +41,7 @@ namespace YooAsset
{
LoadSceneParameters loadSceneParameters = new LoadSceneParameters();
loadSceneParameters.loadSceneMode = SceneMode;
_asyncOp = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(AssetPath, loadSceneParameters);
_asyncOp = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(MainAssetInfo.AssetPath, loadSceneParameters);
if (_asyncOp != null)
{
_asyncOp.allowSceneActivation = true;
@@ -52,7 +51,7 @@ namespace YooAsset
else
{
Status = EStatus.Fail;
LastError = $"Failed to load scene : {AssetPath}";
LastError = $"Failed to load scene : {MainAssetInfo.AssetPath}";
YooLogger.Error(LastError);
InvokeCompletion();
}
@@ -70,7 +69,7 @@ namespace YooAsset
Status = SceneObject.IsValid() ? EStatus.Success : EStatus.Fail;
if (Status == EStatus.Fail)
{
LastError = $"The loaded scene is invalid : {AssetPath}";
LastError = $"The loaded scene is invalid : {MainAssetInfo.AssetPath}";
YooLogger.Error(LastError);
}
InvokeCompletion();
@@ -17,8 +17,7 @@ namespace YooAsset
}
}
public DatabaseSubAssetsProvider(string assetPath, System.Type assetType)
: base(assetPath, assetType)
public DatabaseSubAssetsProvider(AssetInfo assetInfo) : base(assetInfo)
{
}
public override void Update()
@@ -30,11 +29,11 @@ namespace YooAsset
if (Status == EStatus.None)
{
// 检测资源文件是否存在
string guid = UnityEditor.AssetDatabase.AssetPathToGUID(AssetPath);
string guid = UnityEditor.AssetDatabase.AssetPathToGUID(MainAssetInfo.AssetPath);
if (string.IsNullOrEmpty(guid))
{
Status = EStatus.Fail;
LastError = $"Not found asset : {AssetPath}";
LastError = $"Not found asset : {MainAssetInfo.AssetPath}";
YooLogger.Error(LastError);
InvokeCompletion();
return;
@@ -50,17 +49,17 @@ namespace YooAsset
// 1. 加载资源对象
if (Status == EStatus.Loading)
{
if (AssetType == null)
if (MainAssetInfo.AssetType == null)
{
AllAssetObjects = UnityEditor.AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetPath);
AllAssetObjects = UnityEditor.AssetDatabase.LoadAllAssetRepresentationsAtPath(MainAssetInfo.AssetPath);
}
else
{
UnityEngine.Object[] findAssets = UnityEditor.AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetPath);
UnityEngine.Object[] findAssets = UnityEditor.AssetDatabase.LoadAllAssetRepresentationsAtPath(MainAssetInfo.AssetPath);
List<UnityEngine.Object> result = new List<Object>(findAssets.Length);
foreach (var findAsset in findAssets)
{
if (AssetType.IsAssignableFrom(findAsset.GetType()))
if (MainAssetInfo.AssetType.IsAssignableFrom(findAsset.GetType()))
result.Add(findAsset);
}
AllAssetObjects = result.ToArray();
@@ -74,10 +73,10 @@ namespace YooAsset
Status = AllAssetObjects == null ? EStatus.Fail : EStatus.Success;
if (Status == EStatus.Fail)
{
if (AssetType == null)
LastError = $"Failed to load sub assets : {AssetPath} AssetType : null";
if (MainAssetInfo.AssetType == null)
LastError = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : null";
else
LastError = $"Failed to load sub assets : {AssetPath} AssetType : {AssetType}";
LastError = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}";
YooLogger.Error(LastError);
}
InvokeCompletion();
@@ -18,19 +18,9 @@ namespace YooAsset
}
/// <summary>
/// 资源路径
/// 资源信息
/// </summary>
public string AssetPath { private set; get; }
/// <summary>
/// 资源对象的名称
/// </summary>
public string AssetName { private set; get; }
/// <summary>
/// 资源对象的类型
/// </summary>
public System.Type AssetType { private set; get; }
public AssetInfo MainAssetInfo { private set; get; }
/// <summary>
/// 获取的资源对象
@@ -95,11 +85,9 @@ namespace YooAsset
private readonly List<OperationHandleBase> _handles = new List<OperationHandleBase>();
public ProviderBase(string assetPath, System.Type assetType)
public ProviderBase(AssetInfo assetInfo)
{
AssetPath = assetPath;
AssetName = System.IO.Path.GetFileName(assetPath);
AssetType = assetType;
MainAssetInfo = assetInfo;
}
/// <summary>
@@ -126,25 +114,38 @@ namespace YooAsset
return RefCount <= 0;
}
/// <summary>
/// 是否为场景提供者
/// </summary>
public bool IsSceneProvider()
{
if (this is BundledSceneProvider || this is DatabaseSceneProvider)
return true;
else
return false;
}
/// <summary>
/// 创建操作句柄
/// </summary>
/// <returns></returns>
public OperationHandleBase CreateHandle()
public T CreateHandle<T>() where T : OperationHandleBase
{
// 引用计数增加
RefCount++;
OperationHandleBase handle;
if (IsSceneProvider())
if (typeof(T) == typeof(AssetOperationHandle))
handle = new AssetOperationHandle(this);
else if (typeof(T) == typeof(SceneOperationHandle))
handle = new SceneOperationHandle(this);
else if (IsSubAssetsProvider())
else if (typeof(T) == typeof(SubAssetsOperationHandle))
handle = new SubAssetsOperationHandle(this);
else
handle = new AssetOperationHandle(this);
throw new System.NotImplementedException();
_handles.Add(handle);
return handle;
return handle as T;
}
/// <summary>
@@ -175,7 +176,7 @@ namespace YooAsset
// 验证结果
if (IsDone == false)
{
YooLogger.Warning($"WaitForAsyncComplete failed to loading : {AssetPath}");
YooLogger.Warning($"WaitForAsyncComplete failed to loading : {MainAssetInfo.AssetPath}");
}
}
@@ -196,21 +197,6 @@ namespace YooAsset
}
}
public bool IsSceneProvider()
{
if (this is BundledSceneProvider || this is DatabaseSceneProvider)
return true;
else
return false;
}
public bool IsSubAssetsProvider()
{
if (this is BundledSubAssetsProvider || this is DatabaseSubAssetsProvider)
return true;
else
return false;
}
#region 异步编程相关
private TaskCompletionSource<object> _taskCompletionSource;
protected void InvokeCompletion()