mirror of https://github.com/tuyoogame/YooAsset
Compare commits
No commits in common. "07086b68c8c49e3b8784e875bf8eb6e2e06bf133" and "ce023cedb88dae9e255a68855a02daa7f44d3c2f" have entirely different histories.
07086b68c8
...
ce023cedb8
|
@ -3,88 +3,38 @@ using UnityEngine;
|
||||||
|
|
||||||
namespace YooAsset
|
namespace YooAsset
|
||||||
{
|
{
|
||||||
public sealed class UnloadAllAssetsOptions
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 释放所有资源句柄,防止卸载过程中触发完成回调!
|
|
||||||
/// </summary>
|
|
||||||
public bool ReleaseAllHandles = true;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 卸载过程中锁定加载操作,防止新的任务请求!
|
|
||||||
/// </summary>
|
|
||||||
public bool LockLoadOperation = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public sealed class UnloadAllAssetsOperation : AsyncOperationBase
|
public sealed class UnloadAllAssetsOperation : AsyncOperationBase
|
||||||
{
|
{
|
||||||
private enum ESteps
|
private enum ESteps
|
||||||
{
|
{
|
||||||
None,
|
None,
|
||||||
CheckOptions,
|
|
||||||
ReleaseAll,
|
|
||||||
AbortDownload,
|
AbortDownload,
|
||||||
CheckLoading,
|
CheckLoading,
|
||||||
DestroyAll,
|
UnloadAll,
|
||||||
Done,
|
Done,
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly ResourceManager _resManager;
|
private readonly ResourceManager _resManager;
|
||||||
private readonly UnloadAllAssetsOptions _options;
|
|
||||||
private ESteps _steps = ESteps.None;
|
private ESteps _steps = ESteps.None;
|
||||||
|
|
||||||
internal UnloadAllAssetsOperation(ResourceManager resourceManager, UnloadAllAssetsOptions options)
|
internal UnloadAllAssetsOperation(ResourceManager resourceManager)
|
||||||
{
|
{
|
||||||
_resManager = resourceManager;
|
_resManager = resourceManager;
|
||||||
_options = options;
|
|
||||||
}
|
}
|
||||||
internal override void InternalOnStart()
|
internal override void InternalOnStart()
|
||||||
{
|
{
|
||||||
_steps = ESteps.CheckOptions;
|
_steps = ESteps.AbortDownload;
|
||||||
}
|
}
|
||||||
internal override void InternalOnUpdate()
|
internal override void InternalOnUpdate()
|
||||||
{
|
{
|
||||||
if (_steps == ESteps.None || _steps == ESteps.Done)
|
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (_steps == ESteps.CheckOptions)
|
|
||||||
{
|
|
||||||
if (_options == null)
|
|
||||||
{
|
|
||||||
_steps = ESteps.Done;
|
|
||||||
Status = EOperationStatus.Failed;
|
|
||||||
Error = $"{nameof(UnloadAllAssetsOptions)} is null.";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 设置锁定状态
|
|
||||||
if (_options.LockLoadOperation)
|
|
||||||
_resManager.LockLoadOperation = true;
|
|
||||||
|
|
||||||
_steps = ESteps.ReleaseAll;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_steps == ESteps.ReleaseAll)
|
|
||||||
{
|
|
||||||
// 清空所有场景句柄
|
|
||||||
_resManager.SceneHandles.Clear();
|
|
||||||
|
|
||||||
// 释放所有资源句柄
|
|
||||||
if (_options.ReleaseAllHandles)
|
|
||||||
{
|
|
||||||
foreach (var provider in _resManager.ProviderDic.Values)
|
|
||||||
{
|
|
||||||
provider.ReleaseAllHandles();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_steps = ESteps.AbortDownload;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_steps == ESteps.AbortDownload)
|
if (_steps == ESteps.AbortDownload)
|
||||||
{
|
{
|
||||||
// 注意:终止所有下载任务
|
// 注意:终止所有下载任务
|
||||||
foreach (var loader in _resManager.LoaderDic.Values)
|
var loaderDic = _resManager._loaderDic;
|
||||||
|
foreach (var loader in loaderDic.Values)
|
||||||
{
|
{
|
||||||
loader.AbortDownloadOperation();
|
loader.AbortDownloadOperation();
|
||||||
}
|
}
|
||||||
|
@ -94,32 +44,44 @@ namespace YooAsset
|
||||||
if (_steps == ESteps.CheckLoading)
|
if (_steps == ESteps.CheckLoading)
|
||||||
{
|
{
|
||||||
// 注意:等待所有任务完成
|
// 注意:等待所有任务完成
|
||||||
foreach (var provider in _resManager.ProviderDic.Values)
|
var providerDic = _resManager._providerDic;
|
||||||
|
foreach (var provider in providerDic.Values)
|
||||||
{
|
{
|
||||||
if (provider.IsDone == false)
|
if (provider.IsDone == false)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_steps = ESteps.DestroyAll;
|
_steps = ESteps.UnloadAll;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_steps == ESteps.DestroyAll)
|
if (_steps == ESteps.UnloadAll)
|
||||||
{
|
{
|
||||||
|
var loaderDic = _resManager._loaderDic;
|
||||||
|
var providerDic = _resManager._providerDic;
|
||||||
|
|
||||||
|
// 清空所有场景句柄
|
||||||
|
_resManager._sceneHandles.Clear();
|
||||||
|
|
||||||
|
// 释放所有资源句柄
|
||||||
|
foreach (var provider in providerDic.Values)
|
||||||
|
{
|
||||||
|
provider.ReleaseAllHandles();
|
||||||
|
}
|
||||||
|
|
||||||
// 强制销毁资源提供者
|
// 强制销毁资源提供者
|
||||||
foreach (var provider in _resManager.ProviderDic.Values)
|
foreach (var provider in providerDic.Values)
|
||||||
{
|
{
|
||||||
provider.DestroyProvider();
|
provider.DestroyProvider();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 强制销毁文件加载器
|
// 强制销毁文件加载器
|
||||||
foreach (var loader in _resManager.LoaderDic.Values)
|
foreach (var loader in loaderDic.Values)
|
||||||
{
|
{
|
||||||
loader.DestroyLoader();
|
loader.DestroyLoader();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 清空数据
|
// 清空数据
|
||||||
_resManager.ProviderDic.Clear();
|
providerDic.Clear();
|
||||||
_resManager.LoaderDic.Clear();
|
loaderDic.Clear();
|
||||||
_resManager.LockLoadOperation = false;
|
|
||||||
|
|
||||||
// 注意:调用底层接口释放所有资源
|
// 注意:调用底层接口释放所有资源
|
||||||
Resources.UnloadUnusedAssets();
|
Resources.UnloadUnusedAssets();
|
||||||
|
|
|
@ -31,16 +31,17 @@ namespace YooAsset
|
||||||
|
|
||||||
if (_steps == ESteps.UnloadUnused)
|
if (_steps == ESteps.UnloadUnused)
|
||||||
{
|
{
|
||||||
var removeList = new List<LoadBundleFileOperation>(_resManager.LoaderDic.Count);
|
var loaderDic = _resManager._loaderDic;
|
||||||
|
var removeList = new List<LoadBundleFileOperation>(loaderDic.Count);
|
||||||
|
|
||||||
// 注意:优先销毁资源提供者
|
// 注意:优先销毁资源提供者
|
||||||
foreach (var loader in _resManager.LoaderDic.Values)
|
foreach (var loader in loaderDic.Values)
|
||||||
{
|
{
|
||||||
loader.TryDestroyProviders();
|
loader.TryDestroyProviders();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取销毁列表
|
// 获取销毁列表
|
||||||
foreach (var loader in _resManager.LoaderDic.Values)
|
foreach (var loader in loaderDic.Values)
|
||||||
{
|
{
|
||||||
if (loader.CanDestroyLoader())
|
if (loader.CanDestroyLoader())
|
||||||
{
|
{
|
||||||
|
@ -53,9 +54,13 @@ namespace YooAsset
|
||||||
{
|
{
|
||||||
string bundleName = loader.LoadBundleInfo.Bundle.BundleName;
|
string bundleName = loader.LoadBundleInfo.Bundle.BundleName;
|
||||||
loader.DestroyLoader();
|
loader.DestroyLoader();
|
||||||
_resManager.LoaderDic.Remove(bundleName);
|
_resManager._loaderDic.Remove(bundleName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 注意:调用底层接口释放所有资源
|
||||||
|
if (removeList.Count > 0)
|
||||||
|
Resources.UnloadUnusedAssets();
|
||||||
|
|
||||||
_steps = ESteps.Done;
|
_steps = ESteps.Done;
|
||||||
Status = EOperationStatus.Succeed;
|
Status = EOperationStatus.Succeed;
|
||||||
}
|
}
|
||||||
|
|
|
@ -86,7 +86,7 @@ namespace YooAsset
|
||||||
|
|
||||||
// 依赖资源包加载器集合
|
// 依赖资源包加载器集合
|
||||||
var dependLoaders = manager.CreateDependBundleFileLoaders(assetInfo);
|
var dependLoaders = manager.CreateDependBundleFileLoaders(assetInfo);
|
||||||
if (dependLoaders.Count > 0)
|
if(dependLoaders.Count > 0)
|
||||||
_bundleLoaders.AddRange(dependLoaders);
|
_bundleLoaders.AddRange(dependLoaders);
|
||||||
|
|
||||||
// 增加引用计数
|
// 增加引用计数
|
||||||
|
|
|
@ -9,9 +9,9 @@ namespace YooAsset
|
||||||
{
|
{
|
||||||
internal class ResourceManager
|
internal class ResourceManager
|
||||||
{
|
{
|
||||||
internal readonly Dictionary<string, ProviderOperation> ProviderDic = new Dictionary<string, ProviderOperation>(5000);
|
internal readonly Dictionary<string, ProviderOperation> _providerDic = new Dictionary<string, ProviderOperation>(5000);
|
||||||
internal readonly Dictionary<string, LoadBundleFileOperation> LoaderDic = new Dictionary<string, LoadBundleFileOperation>(5000);
|
internal readonly Dictionary<string, LoadBundleFileOperation> _loaderDic = new Dictionary<string, LoadBundleFileOperation>(5000);
|
||||||
internal readonly List<SceneHandle> SceneHandles = new List<SceneHandle>(100);
|
internal readonly List<SceneHandle> _sceneHandles = new List<SceneHandle>(100);
|
||||||
private long _sceneCreateIndex = 0;
|
private long _sceneCreateIndex = 0;
|
||||||
private IBundleQuery _bundleQuery;
|
private IBundleQuery _bundleQuery;
|
||||||
|
|
||||||
|
@ -20,11 +20,6 @@ namespace YooAsset
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public readonly string PackageName;
|
public readonly string PackageName;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 锁定加载操作
|
|
||||||
/// </summary>
|
|
||||||
public bool LockLoadOperation = false;
|
|
||||||
|
|
||||||
|
|
||||||
public ResourceManager(string packageName)
|
public ResourceManager(string packageName)
|
||||||
{
|
{
|
||||||
|
@ -69,7 +64,7 @@ namespace YooAsset
|
||||||
{
|
{
|
||||||
string bundleName = mainLoader.LoadBundleInfo.Bundle.BundleName;
|
string bundleName = mainLoader.LoadBundleInfo.Bundle.BundleName;
|
||||||
mainLoader.DestroyLoader();
|
mainLoader.DestroyLoader();
|
||||||
LoaderDic.Remove(bundleName);
|
_loaderDic.Remove(bundleName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,7 +79,7 @@ namespace YooAsset
|
||||||
{
|
{
|
||||||
string bundleName = dependLoader.LoadBundleInfo.Bundle.BundleName;
|
string bundleName = dependLoader.LoadBundleInfo.Bundle.BundleName;
|
||||||
dependLoader.DestroyLoader();
|
dependLoader.DestroyLoader();
|
||||||
LoaderDic.Remove(bundleName);
|
_loaderDic.Remove(bundleName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -97,15 +92,6 @@ namespace YooAsset
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public SceneHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneParameters loadSceneParams, bool suspendLoad, uint priority)
|
public SceneHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneParameters loadSceneParams, bool suspendLoad, uint priority)
|
||||||
{
|
{
|
||||||
if (LockLoadOperation)
|
|
||||||
{
|
|
||||||
string error = $"The load operation locked !";
|
|
||||||
YooLogger.Error(error);
|
|
||||||
CompletedProvider completedProvider = new CompletedProvider(this, assetInfo);
|
|
||||||
completedProvider.SetCompletedWithError(error);
|
|
||||||
return completedProvider.CreateHandle<SceneHandle>();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (assetInfo.IsInvalid)
|
if (assetInfo.IsInvalid)
|
||||||
{
|
{
|
||||||
YooLogger.Error($"Failed to load scene ! {assetInfo.Error}");
|
YooLogger.Error($"Failed to load scene ! {assetInfo.Error}");
|
||||||
|
@ -120,14 +106,14 @@ namespace YooAsset
|
||||||
{
|
{
|
||||||
provider = new SceneProvider(this, providerGUID, assetInfo, loadSceneParams, suspendLoad);
|
provider = new SceneProvider(this, providerGUID, assetInfo, loadSceneParams, suspendLoad);
|
||||||
provider.InitSpawnDebugInfo();
|
provider.InitSpawnDebugInfo();
|
||||||
ProviderDic.Add(providerGUID, provider);
|
_providerDic.Add(providerGUID, provider);
|
||||||
OperationSystem.StartOperation(PackageName, provider);
|
OperationSystem.StartOperation(PackageName, provider);
|
||||||
}
|
}
|
||||||
|
|
||||||
provider.Priority = priority;
|
provider.Priority = priority;
|
||||||
var handle = provider.CreateHandle<SceneHandle>();
|
var handle = provider.CreateHandle<SceneHandle>();
|
||||||
handle.PackageName = PackageName;
|
handle.PackageName = PackageName;
|
||||||
SceneHandles.Add(handle);
|
_sceneHandles.Add(handle);
|
||||||
return handle;
|
return handle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,15 +122,6 @@ namespace YooAsset
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public AssetHandle LoadAssetAsync(AssetInfo assetInfo, uint priority)
|
public AssetHandle LoadAssetAsync(AssetInfo assetInfo, uint priority)
|
||||||
{
|
{
|
||||||
if (LockLoadOperation)
|
|
||||||
{
|
|
||||||
string error = $"The load operation locked !";
|
|
||||||
YooLogger.Error(error);
|
|
||||||
CompletedProvider completedProvider = new CompletedProvider(this, assetInfo);
|
|
||||||
completedProvider.SetCompletedWithError(error);
|
|
||||||
return completedProvider.CreateHandle<AssetHandle>();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (assetInfo.IsInvalid)
|
if (assetInfo.IsInvalid)
|
||||||
{
|
{
|
||||||
YooLogger.Error($"Failed to load asset ! {assetInfo.Error}");
|
YooLogger.Error($"Failed to load asset ! {assetInfo.Error}");
|
||||||
|
@ -159,7 +136,7 @@ namespace YooAsset
|
||||||
{
|
{
|
||||||
provider = new AssetProvider(this, providerGUID, assetInfo);
|
provider = new AssetProvider(this, providerGUID, assetInfo);
|
||||||
provider.InitSpawnDebugInfo();
|
provider.InitSpawnDebugInfo();
|
||||||
ProviderDic.Add(providerGUID, provider);
|
_providerDic.Add(providerGUID, provider);
|
||||||
OperationSystem.StartOperation(PackageName, provider);
|
OperationSystem.StartOperation(PackageName, provider);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -172,15 +149,6 @@ namespace YooAsset
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public SubAssetsHandle LoadSubAssetsAsync(AssetInfo assetInfo, uint priority)
|
public SubAssetsHandle LoadSubAssetsAsync(AssetInfo assetInfo, uint priority)
|
||||||
{
|
{
|
||||||
if (LockLoadOperation)
|
|
||||||
{
|
|
||||||
string error = $"The load operation locked !";
|
|
||||||
YooLogger.Error(error);
|
|
||||||
CompletedProvider completedProvider = new CompletedProvider(this, assetInfo);
|
|
||||||
completedProvider.SetCompletedWithError(error);
|
|
||||||
return completedProvider.CreateHandle<SubAssetsHandle>();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (assetInfo.IsInvalid)
|
if (assetInfo.IsInvalid)
|
||||||
{
|
{
|
||||||
YooLogger.Error($"Failed to load sub assets ! {assetInfo.Error}");
|
YooLogger.Error($"Failed to load sub assets ! {assetInfo.Error}");
|
||||||
|
@ -195,7 +163,7 @@ namespace YooAsset
|
||||||
{
|
{
|
||||||
provider = new SubAssetsProvider(this, providerGUID, assetInfo);
|
provider = new SubAssetsProvider(this, providerGUID, assetInfo);
|
||||||
provider.InitSpawnDebugInfo();
|
provider.InitSpawnDebugInfo();
|
||||||
ProviderDic.Add(providerGUID, provider);
|
_providerDic.Add(providerGUID, provider);
|
||||||
OperationSystem.StartOperation(PackageName, provider);
|
OperationSystem.StartOperation(PackageName, provider);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -208,15 +176,6 @@ namespace YooAsset
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public AllAssetsHandle LoadAllAssetsAsync(AssetInfo assetInfo, uint priority)
|
public AllAssetsHandle LoadAllAssetsAsync(AssetInfo assetInfo, uint priority)
|
||||||
{
|
{
|
||||||
if (LockLoadOperation)
|
|
||||||
{
|
|
||||||
string error = $"The load operation locked !";
|
|
||||||
YooLogger.Error(error);
|
|
||||||
CompletedProvider completedProvider = new CompletedProvider(this, assetInfo);
|
|
||||||
completedProvider.SetCompletedWithError(error);
|
|
||||||
return completedProvider.CreateHandle<AllAssetsHandle>();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (assetInfo.IsInvalid)
|
if (assetInfo.IsInvalid)
|
||||||
{
|
{
|
||||||
YooLogger.Error($"Failed to load all assets ! {assetInfo.Error}");
|
YooLogger.Error($"Failed to load all assets ! {assetInfo.Error}");
|
||||||
|
@ -231,7 +190,7 @@ namespace YooAsset
|
||||||
{
|
{
|
||||||
provider = new AllAssetsProvider(this, providerGUID, assetInfo);
|
provider = new AllAssetsProvider(this, providerGUID, assetInfo);
|
||||||
provider.InitSpawnDebugInfo();
|
provider.InitSpawnDebugInfo();
|
||||||
ProviderDic.Add(providerGUID, provider);
|
_providerDic.Add(providerGUID, provider);
|
||||||
OperationSystem.StartOperation(PackageName, provider);
|
OperationSystem.StartOperation(PackageName, provider);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -244,15 +203,6 @@ namespace YooAsset
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public RawFileHandle LoadRawFileAsync(AssetInfo assetInfo, uint priority)
|
public RawFileHandle LoadRawFileAsync(AssetInfo assetInfo, uint priority)
|
||||||
{
|
{
|
||||||
if (LockLoadOperation)
|
|
||||||
{
|
|
||||||
string error = $"The load operation locked !";
|
|
||||||
YooLogger.Error(error);
|
|
||||||
CompletedProvider completedProvider = new CompletedProvider(this, assetInfo);
|
|
||||||
completedProvider.SetCompletedWithError(error);
|
|
||||||
return completedProvider.CreateHandle<RawFileHandle>();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (assetInfo.IsInvalid)
|
if (assetInfo.IsInvalid)
|
||||||
{
|
{
|
||||||
YooLogger.Error($"Failed to load raw file ! {assetInfo.Error}");
|
YooLogger.Error($"Failed to load raw file ! {assetInfo.Error}");
|
||||||
|
@ -267,7 +217,7 @@ namespace YooAsset
|
||||||
{
|
{
|
||||||
provider = new RawFileProvider(this, providerGUID, assetInfo);
|
provider = new RawFileProvider(this, providerGUID, assetInfo);
|
||||||
provider.InitSpawnDebugInfo();
|
provider.InitSpawnDebugInfo();
|
||||||
ProviderDic.Add(providerGUID, provider);
|
_providerDic.Add(providerGUID, provider);
|
||||||
OperationSystem.StartOperation(PackageName, provider);
|
OperationSystem.StartOperation(PackageName, provider);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -295,12 +245,12 @@ namespace YooAsset
|
||||||
{
|
{
|
||||||
foreach (var provider in removeList)
|
foreach (var provider in removeList)
|
||||||
{
|
{
|
||||||
ProviderDic.Remove(provider.ProviderGUID);
|
_providerDic.Remove(provider.ProviderGUID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
internal bool HasAnyLoader()
|
internal bool HasAnyLoader()
|
||||||
{
|
{
|
||||||
return LoaderDic.Count > 0;
|
return _loaderDic.Count > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private LoadBundleFileOperation CreateBundleFileLoaderInternal(BundleInfo bundleInfo)
|
private LoadBundleFileOperation CreateBundleFileLoaderInternal(BundleInfo bundleInfo)
|
||||||
|
@ -314,19 +264,19 @@ namespace YooAsset
|
||||||
// 新增下载需求
|
// 新增下载需求
|
||||||
loaderOperation = new LoadBundleFileOperation(this, bundleInfo);
|
loaderOperation = new LoadBundleFileOperation(this, bundleInfo);
|
||||||
OperationSystem.StartOperation(PackageName, loaderOperation);
|
OperationSystem.StartOperation(PackageName, loaderOperation);
|
||||||
LoaderDic.Add(bundleName, loaderOperation);
|
_loaderDic.Add(bundleName, loaderOperation);
|
||||||
return loaderOperation;
|
return loaderOperation;
|
||||||
}
|
}
|
||||||
private LoadBundleFileOperation TryGetBundleFileLoader(string bundleName)
|
private LoadBundleFileOperation TryGetBundleFileLoader(string bundleName)
|
||||||
{
|
{
|
||||||
if (LoaderDic.TryGetValue(bundleName, out LoadBundleFileOperation value))
|
if (_loaderDic.TryGetValue(bundleName, out LoadBundleFileOperation value))
|
||||||
return value;
|
return value;
|
||||||
else
|
else
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
private ProviderOperation TryGetAssetProvider(string providerGUID)
|
private ProviderOperation TryGetAssetProvider(string providerGUID)
|
||||||
{
|
{
|
||||||
if (ProviderDic.TryGetValue(providerGUID, out ProviderOperation value))
|
if (_providerDic.TryGetValue(providerGUID, out ProviderOperation value))
|
||||||
return value;
|
return value;
|
||||||
else
|
else
|
||||||
return null;
|
return null;
|
||||||
|
@ -334,7 +284,7 @@ namespace YooAsset
|
||||||
private void OnSceneUnloaded(Scene scene)
|
private void OnSceneUnloaded(Scene scene)
|
||||||
{
|
{
|
||||||
List<SceneHandle> removeList = new List<SceneHandle>();
|
List<SceneHandle> removeList = new List<SceneHandle>();
|
||||||
foreach (var sceneHandle in SceneHandles)
|
foreach (var sceneHandle in _sceneHandles)
|
||||||
{
|
{
|
||||||
if (sceneHandle.IsValid)
|
if (sceneHandle.IsValid)
|
||||||
{
|
{
|
||||||
|
@ -347,15 +297,15 @@ namespace YooAsset
|
||||||
}
|
}
|
||||||
foreach (var sceneHandle in removeList)
|
foreach (var sceneHandle in removeList)
|
||||||
{
|
{
|
||||||
SceneHandles.Remove(sceneHandle);
|
_sceneHandles.Remove(sceneHandle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#region 调试信息
|
#region 调试信息
|
||||||
internal List<DebugProviderInfo> GetDebugReportInfos()
|
internal List<DebugProviderInfo> GetDebugReportInfos()
|
||||||
{
|
{
|
||||||
List<DebugProviderInfo> result = new List<DebugProviderInfo>(ProviderDic.Count);
|
List<DebugProviderInfo> result = new List<DebugProviderInfo>(_providerDic.Count);
|
||||||
foreach (var provider in ProviderDic.Values)
|
foreach (var provider in _providerDic.Values)
|
||||||
{
|
{
|
||||||
DebugProviderInfo providerInfo = new DebugProviderInfo();
|
DebugProviderInfo providerInfo = new DebugProviderInfo();
|
||||||
providerInfo.AssetPath = provider.MainAssetInfo.AssetPath;
|
providerInfo.AssetPath = provider.MainAssetInfo.AssetPath;
|
||||||
|
|
|
@ -318,19 +318,9 @@ namespace YooAsset
|
||||||
/// 强制回收所有资源
|
/// 强制回收所有资源
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public UnloadAllAssetsOperation UnloadAllAssetsAsync()
|
public UnloadAllAssetsOperation UnloadAllAssetsAsync()
|
||||||
{
|
|
||||||
var options = new UnloadAllAssetsOptions();
|
|
||||||
return UnloadAllAssetsAsync(options);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 强制回收所有资源
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="options">卸载选项</param>
|
|
||||||
public UnloadAllAssetsOperation UnloadAllAssetsAsync(UnloadAllAssetsOptions options)
|
|
||||||
{
|
{
|
||||||
DebugCheckInitialize();
|
DebugCheckInitialize();
|
||||||
var operation = new UnloadAllAssetsOperation(_resourceManager, options);
|
var operation = new UnloadAllAssetsOperation(_resourceManager);
|
||||||
OperationSystem.StartOperation(PackageName, operation);
|
OperationSystem.StartOperation(PackageName, operation);
|
||||||
return operation;
|
return operation;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,16 @@ using YooAsset;
|
||||||
|
|
||||||
internal partial class TTFSInitializeOperation : FSInitializeFileSystemOperation
|
internal partial class TTFSInitializeOperation : FSInitializeFileSystemOperation
|
||||||
{
|
{
|
||||||
|
private enum ESteps
|
||||||
|
{
|
||||||
|
None,
|
||||||
|
RecordCacheFiles,
|
||||||
|
Done,
|
||||||
|
}
|
||||||
|
|
||||||
private readonly TiktokFileSystem _fileSystem;
|
private readonly TiktokFileSystem _fileSystem;
|
||||||
|
private RecordTiktokCacheFilesOperation _recordTiktokCacheFilesOp;
|
||||||
|
private ESteps _steps = ESteps.None;
|
||||||
|
|
||||||
public TTFSInitializeOperation(TiktokFileSystem fileSystem)
|
public TTFSInitializeOperation(TiktokFileSystem fileSystem)
|
||||||
{
|
{
|
||||||
|
@ -11,10 +20,36 @@ internal partial class TTFSInitializeOperation : FSInitializeFileSystemOperation
|
||||||
}
|
}
|
||||||
internal override void InternalOnStart()
|
internal override void InternalOnStart()
|
||||||
{
|
{
|
||||||
Status = EOperationStatus.Succeed;
|
_steps = ESteps.RecordCacheFiles;
|
||||||
}
|
}
|
||||||
internal override void InternalOnUpdate()
|
internal override void InternalOnUpdate()
|
||||||
{
|
{
|
||||||
|
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (_steps == ESteps.RecordCacheFiles)
|
||||||
|
{
|
||||||
|
if (_recordTiktokCacheFilesOp == null)
|
||||||
|
{
|
||||||
|
_recordTiktokCacheFilesOp = new RecordTiktokCacheFilesOperation(_fileSystem);
|
||||||
|
OperationSystem.StartOperation(_fileSystem.PackageName, _recordTiktokCacheFilesOp);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_recordTiktokCacheFilesOp.IsDone == false)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (_recordTiktokCacheFilesOp.Status == EOperationStatus.Succeed)
|
||||||
|
{
|
||||||
|
_steps = ESteps.Done;
|
||||||
|
Status = EOperationStatus.Succeed;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_steps = ESteps.Done;
|
||||||
|
Status = EOperationStatus.Failed;
|
||||||
|
Error = _recordTiktokCacheFilesOp.Error;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
|
@ -0,0 +1,57 @@
|
||||||
|
#if UNITY_WEBGL && DOUYINMINIGAME
|
||||||
|
using YooAsset;
|
||||||
|
using TTSDK;
|
||||||
|
|
||||||
|
internal class RecordTiktokCacheFilesOperation : AsyncOperationBase
|
||||||
|
{
|
||||||
|
private enum ESteps
|
||||||
|
{
|
||||||
|
None,
|
||||||
|
RecordCacheFiles,
|
||||||
|
WaitResponse,
|
||||||
|
Done,
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly TiktokFileSystem _fileSystem;
|
||||||
|
private ESteps _steps = ESteps.None;
|
||||||
|
|
||||||
|
public RecordTiktokCacheFilesOperation(TiktokFileSystem fileSystem)
|
||||||
|
{
|
||||||
|
_fileSystem = fileSystem;
|
||||||
|
}
|
||||||
|
internal override void InternalOnStart()
|
||||||
|
{
|
||||||
|
_steps = ESteps.RecordCacheFiles;
|
||||||
|
}
|
||||||
|
internal override void InternalOnUpdate()
|
||||||
|
{
|
||||||
|
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (_steps == ESteps.RecordCacheFiles)
|
||||||
|
{
|
||||||
|
_steps = ESteps.WaitResponse;
|
||||||
|
|
||||||
|
var fileSystemMgr = _fileSystem.GetFileSystemMgr();
|
||||||
|
var getSavedFileListParam = new GetSavedFileListParam();
|
||||||
|
getSavedFileListParam.success = (TTGetSavedFileListResponse response) =>
|
||||||
|
{
|
||||||
|
_steps = ESteps.Done;
|
||||||
|
Status = EOperationStatus.Succeed;
|
||||||
|
foreach (var fileInfo in response.fileList)
|
||||||
|
{
|
||||||
|
//TODO 需要确认存储文件为Bundle文件
|
||||||
|
_fileSystem.RecordBundleFile(fileInfo.filePath);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
getSavedFileListParam.fail = (TTGetSavedFileListResponse response) =>
|
||||||
|
{
|
||||||
|
_steps = ESteps.Done;
|
||||||
|
Status = EOperationStatus.Failed;
|
||||||
|
Error = response.errMsg;
|
||||||
|
};
|
||||||
|
fileSystemMgr.GetSavedFileList(getSavedFileListParam);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
|
@ -0,0 +1,11 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: e4420bd73f37dec468a9b23425be68f2
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
|
@ -53,7 +53,8 @@ internal class TiktokFileSystem : IFileSystem
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly Dictionary<string, string> _cacheFilePathMapping = new Dictionary<string, string>(10000);
|
private readonly HashSet<string> _recorders = new HashSet<string>();
|
||||||
|
private readonly Dictionary<string, string> _cacheFilePaths = new Dictionary<string, string>(10000);
|
||||||
private TTFileSystemManager _fileSystemMgr;
|
private TTFileSystemManager _fileSystemMgr;
|
||||||
private string _ttCacheRoot = string.Empty;
|
private string _ttCacheRoot = string.Empty;
|
||||||
|
|
||||||
|
@ -80,7 +81,7 @@ internal class TiktokFileSystem : IFileSystem
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return 0;
|
return _recorders.Count;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -184,7 +185,8 @@ internal class TiktokFileSystem : IFileSystem
|
||||||
}
|
}
|
||||||
public virtual bool Exists(PackageBundle bundle)
|
public virtual bool Exists(PackageBundle bundle)
|
||||||
{
|
{
|
||||||
return CheckCacheFileExist(bundle);
|
string filePath = GetCacheFileLoadPath(bundle);
|
||||||
|
return _recorders.Contains(filePath);
|
||||||
}
|
}
|
||||||
public virtual bool NeedDownload(PackageBundle bundle)
|
public virtual bool NeedDownload(PackageBundle bundle)
|
||||||
{
|
{
|
||||||
|
@ -207,49 +209,77 @@ internal class TiktokFileSystem : IFileSystem
|
||||||
return GetCacheFileLoadPath(bundle);
|
return GetCacheFileLoadPath(bundle);
|
||||||
}
|
}
|
||||||
public virtual byte[] ReadBundleFileData(PackageBundle bundle)
|
public virtual byte[] ReadBundleFileData(PackageBundle bundle)
|
||||||
{
|
|
||||||
if (CheckCacheFileExist(bundle))
|
|
||||||
{
|
{
|
||||||
string filePath = GetCacheFileLoadPath(bundle);
|
string filePath = GetCacheFileLoadPath(bundle);
|
||||||
|
if (CheckCacheFileExist(filePath))
|
||||||
return _fileSystemMgr.ReadFileSync(filePath);
|
return _fileSystemMgr.ReadFileSync(filePath);
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
return Array.Empty<byte>();
|
return Array.Empty<byte>();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
public virtual string ReadBundleFileText(PackageBundle bundle)
|
public virtual string ReadBundleFileText(PackageBundle bundle)
|
||||||
{
|
|
||||||
if (CheckCacheFileExist(bundle))
|
|
||||||
{
|
{
|
||||||
string filePath = GetCacheFileLoadPath(bundle);
|
string filePath = GetCacheFileLoadPath(bundle);
|
||||||
|
if (CheckCacheFileExist(filePath))
|
||||||
return _fileSystemMgr.ReadFileSync(filePath, "utf8");
|
return _fileSystemMgr.ReadFileSync(filePath, "utf8");
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
return string.Empty;
|
return string.Empty;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#region 内部方法
|
#region 内部方法
|
||||||
public TTFileSystemManager GetFileSystemMgr()
|
public TTFileSystemManager GetFileSystemMgr()
|
||||||
{
|
{
|
||||||
return _fileSystemMgr;
|
return _fileSystemMgr;
|
||||||
}
|
}
|
||||||
public bool CheckCacheFileExist(PackageBundle bundle)
|
public bool CheckCacheFileExist(string filePath)
|
||||||
{
|
{
|
||||||
string url = RemoteServices.GetRemoteMainURL(bundle.FileName);
|
return _fileSystemMgr.AccessSync(filePath);
|
||||||
return _fileSystemMgr.IsUrlCached(url);
|
|
||||||
}
|
}
|
||||||
private string GetCacheFileLoadPath(PackageBundle bundle)
|
private string GetCacheFileLoadPath(PackageBundle bundle)
|
||||||
{
|
{
|
||||||
if (_cacheFilePathMapping.TryGetValue(bundle.BundleGUID, out string filePath) == false)
|
if (_cacheFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false)
|
||||||
{
|
{
|
||||||
filePath = _fileSystemMgr.GetLocalCachedPathForUrl(bundle.FileName);
|
filePath = _fileSystemMgr.GetLocalCachedPathForUrl(bundle.FileName);
|
||||||
_cacheFilePathMapping.Add(bundle.BundleGUID, filePath);
|
_cacheFilePaths.Add(bundle.BundleGUID, filePath);
|
||||||
}
|
}
|
||||||
return filePath;
|
return filePath;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region 本地记录
|
||||||
|
public List<string> GetAllRecords()
|
||||||
|
{
|
||||||
|
return _recorders.ToList();
|
||||||
|
}
|
||||||
|
public bool RecordBundleFile(string filePath)
|
||||||
|
{
|
||||||
|
if (_recorders.Contains(filePath))
|
||||||
|
{
|
||||||
|
YooLogger.Error($"{nameof(TiktokFileSystem)} has element : {filePath}");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
_recorders.Add(filePath);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public void TryRecordBundle(PackageBundle bundle)
|
||||||
|
{
|
||||||
|
string filePath = GetCacheFileLoadPath(bundle);
|
||||||
|
if (_recorders.Contains(filePath) == false)
|
||||||
|
{
|
||||||
|
_recorders.Add(filePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void ClearAllRecords()
|
||||||
|
{
|
||||||
|
_recorders.Clear();
|
||||||
|
}
|
||||||
|
public void ClearRecord(string filePath)
|
||||||
|
{
|
||||||
|
if (_recorders.Contains(filePath))
|
||||||
|
{
|
||||||
|
_recorders.Remove(filePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
|
@ -42,6 +42,7 @@ internal class WXFSClearAllBundleFilesOperation : FSClearCacheFilesOperation
|
||||||
YooLogger.Log("微信缓存清理成功!");
|
YooLogger.Log("微信缓存清理成功!");
|
||||||
_steps = ESteps.Done;
|
_steps = ESteps.Done;
|
||||||
Status = EOperationStatus.Succeed;
|
Status = EOperationStatus.Succeed;
|
||||||
|
_fileSystem.ClearAllRecords();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
@ -4,7 +4,6 @@ using System.IO;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using YooAsset;
|
using YooAsset;
|
||||||
using WeChatWASM;
|
using WeChatWASM;
|
||||||
using static UnityEngine.Networking.UnityWebRequest;
|
|
||||||
|
|
||||||
internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation
|
internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation
|
||||||
{
|
{
|
||||||
|
@ -12,7 +11,6 @@ internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation
|
||||||
{
|
{
|
||||||
None,
|
None,
|
||||||
GetUnusedCacheFiles,
|
GetUnusedCacheFiles,
|
||||||
WaitingSearch,
|
|
||||||
ClearUnusedCacheFiles,
|
ClearUnusedCacheFiles,
|
||||||
Done,
|
Done,
|
||||||
}
|
}
|
||||||
|
@ -39,44 +37,20 @@ internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation
|
||||||
|
|
||||||
if (_steps == ESteps.GetUnusedCacheFiles)
|
if (_steps == ESteps.GetUnusedCacheFiles)
|
||||||
{
|
{
|
||||||
_steps = ESteps.WaitingSearch;
|
_unusedCacheFiles = GetUnusedCacheFiles();
|
||||||
|
|
||||||
var fileSystemMgr = _fileSystem.GetFileSystemMgr();
|
|
||||||
var statOption = new WXStatOption();
|
|
||||||
statOption.path = _fileSystem.FileRoot;
|
|
||||||
statOption.recursive = true;
|
|
||||||
statOption.success = (WXStatResponse response) =>
|
|
||||||
{
|
|
||||||
foreach (var fileStat in response.stats)
|
|
||||||
{
|
|
||||||
// 注意:存储文件必须按照Bundle文件哈希值存储!
|
|
||||||
string bundleGUID = Path.GetFileNameWithoutExtension(fileStat.path);
|
|
||||||
if (_manifest.TryGetPackageBundleByBundleGUID(bundleGUID, out PackageBundle value) == false)
|
|
||||||
{
|
|
||||||
_unusedCacheFiles.Add(fileStat.path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_steps = ESteps.ClearUnusedCacheFiles;
|
|
||||||
_unusedFileTotalCount = _unusedCacheFiles.Count;
|
_unusedFileTotalCount = _unusedCacheFiles.Count;
|
||||||
|
_steps = ESteps.ClearUnusedCacheFiles;
|
||||||
YooLogger.Log($"Found unused cache files count : {_unusedFileTotalCount}");
|
YooLogger.Log($"Found unused cache files count : {_unusedFileTotalCount}");
|
||||||
};
|
|
||||||
statOption.fail = (WXStatResponse response) =>
|
|
||||||
{
|
|
||||||
_steps = ESteps.Done;
|
|
||||||
Status = EOperationStatus.Failed;
|
|
||||||
Error = response.errMsg;
|
|
||||||
};
|
|
||||||
fileSystemMgr.Stat(statOption);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_steps == ESteps.ClearUnusedCacheFiles)
|
if (_steps == ESteps.ClearUnusedCacheFiles)
|
||||||
{
|
{
|
||||||
for (int i = _unusedCacheFiles.Count - 1; i >= 0; i--)
|
for (int i = _unusedCacheFiles.Count - 1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
string filePath = _unusedCacheFiles[i];
|
string clearFilePath = _unusedCacheFiles[i];
|
||||||
_unusedCacheFiles.RemoveAt(i);
|
_unusedCacheFiles.RemoveAt(i);
|
||||||
WX.RemoveFile(filePath, null);
|
_fileSystem.ClearRecord(clearFilePath);
|
||||||
|
WX.RemoveFile(clearFilePath, null);
|
||||||
|
|
||||||
if (OperationSystem.IsBusy)
|
if (OperationSystem.IsBusy)
|
||||||
break;
|
break;
|
||||||
|
@ -94,5 +68,30 @@ internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<string> GetUnusedCacheFiles()
|
||||||
|
{
|
||||||
|
var allRecords = _fileSystem.GetAllRecords();
|
||||||
|
List<string> result = new List<string>(allRecords.Count);
|
||||||
|
foreach (var filePath in allRecords)
|
||||||
|
{
|
||||||
|
// 如果存储文件名是按照Bundle文件哈希值存储
|
||||||
|
string bundleGUID = Path.GetFileNameWithoutExtension(filePath);
|
||||||
|
if (_manifest.TryGetPackageBundleByBundleGUID(bundleGUID, out PackageBundle value) == false)
|
||||||
|
{
|
||||||
|
result.Add(filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果存储文件名是按照Bundle文件名称存储
|
||||||
|
/*
|
||||||
|
string bundleName = Path.GetFileNameWithoutExtension(filePath);
|
||||||
|
if (_manifest.TryGetPackageBundleByBundleName(bundleName, out PackageBundle value) == false)
|
||||||
|
{
|
||||||
|
result.Add(filePath);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
|
@ -51,6 +51,7 @@ internal class WXFSDownloadFileOperation : DefaultDownloadFileOperation
|
||||||
{
|
{
|
||||||
_steps = ESteps.Done;
|
_steps = ESteps.Done;
|
||||||
Status = EOperationStatus.Succeed;
|
Status = EOperationStatus.Succeed;
|
||||||
|
_fileSystem.TryRecordBundle(Bundle); //记录下载文件
|
||||||
|
|
||||||
//TODO 解决微信小游戏插件问题
|
//TODO 解决微信小游戏插件问题
|
||||||
// Issue : https://github.com/wechat-miniprogram/minigame-unity-webgl-transform/issues/108#
|
// Issue : https://github.com/wechat-miniprogram/minigame-unity-webgl-transform/issues/108#
|
||||||
|
|
|
@ -3,7 +3,16 @@ using YooAsset;
|
||||||
|
|
||||||
internal partial class WXFSInitializeOperation : FSInitializeFileSystemOperation
|
internal partial class WXFSInitializeOperation : FSInitializeFileSystemOperation
|
||||||
{
|
{
|
||||||
|
private enum ESteps
|
||||||
|
{
|
||||||
|
None,
|
||||||
|
RecordCacheFiles,
|
||||||
|
Done,
|
||||||
|
}
|
||||||
|
|
||||||
private readonly WechatFileSystem _fileSystem;
|
private readonly WechatFileSystem _fileSystem;
|
||||||
|
private RecordWechatCacheFilesOperation _recordWechatCacheFilesOp;
|
||||||
|
private ESteps _steps = ESteps.None;
|
||||||
|
|
||||||
public WXFSInitializeOperation(WechatFileSystem fileSystem)
|
public WXFSInitializeOperation(WechatFileSystem fileSystem)
|
||||||
{
|
{
|
||||||
|
@ -11,10 +20,36 @@ internal partial class WXFSInitializeOperation : FSInitializeFileSystemOperation
|
||||||
}
|
}
|
||||||
internal override void InternalOnStart()
|
internal override void InternalOnStart()
|
||||||
{
|
{
|
||||||
Status = EOperationStatus.Succeed;
|
_steps = ESteps.RecordCacheFiles;
|
||||||
}
|
}
|
||||||
internal override void InternalOnUpdate()
|
internal override void InternalOnUpdate()
|
||||||
{
|
{
|
||||||
|
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (_steps == ESteps.RecordCacheFiles)
|
||||||
|
{
|
||||||
|
if (_recordWechatCacheFilesOp == null)
|
||||||
|
{
|
||||||
|
_recordWechatCacheFilesOp = new RecordWechatCacheFilesOperation(_fileSystem);
|
||||||
|
OperationSystem.StartOperation(_fileSystem.PackageName, _recordWechatCacheFilesOp);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_recordWechatCacheFilesOp.IsDone == false)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (_recordWechatCacheFilesOp.Status == EOperationStatus.Succeed)
|
||||||
|
{
|
||||||
|
_steps = ESteps.Done;
|
||||||
|
Status = EOperationStatus.Succeed;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_steps = ESteps.Done;
|
||||||
|
Status = EOperationStatus.Failed;
|
||||||
|
Error = _recordWechatCacheFilesOp.Error;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
|
@ -61,6 +61,7 @@ internal class WXFSLoadBundleOperation : FSLoadBundleOperation
|
||||||
_steps = ESteps.Done;
|
_steps = ESteps.Done;
|
||||||
Result = new WXAssetBundleResult(_fileSystem, _bundle, assetBundle);
|
Result = new WXAssetBundleResult(_fileSystem, _bundle, assetBundle);
|
||||||
Status = EOperationStatus.Succeed;
|
Status = EOperationStatus.Succeed;
|
||||||
|
_fileSystem.TryRecordBundle(_bundle); //记录下载文件
|
||||||
|
|
||||||
//TODO 解决微信小游戏插件问题
|
//TODO 解决微信小游戏插件问题
|
||||||
// Issue : https://github.com/wechat-miniprogram/minigame-unity-webgl-transform/issues/108#
|
// Issue : https://github.com/wechat-miniprogram/minigame-unity-webgl-transform/issues/108#
|
||||||
|
|
|
@ -0,0 +1,60 @@
|
||||||
|
#if UNITY_WEBGL && WEIXINMINIGAME
|
||||||
|
using YooAsset;
|
||||||
|
using WeChatWASM;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
internal class RecordWechatCacheFilesOperation : AsyncOperationBase
|
||||||
|
{
|
||||||
|
private enum ESteps
|
||||||
|
{
|
||||||
|
None,
|
||||||
|
RecordCacheFiles,
|
||||||
|
WaitResponse,
|
||||||
|
Done,
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly WechatFileSystem _fileSystem;
|
||||||
|
private ESteps _steps = ESteps.None;
|
||||||
|
|
||||||
|
public RecordWechatCacheFilesOperation(WechatFileSystem fileSystem)
|
||||||
|
{
|
||||||
|
_fileSystem = fileSystem;
|
||||||
|
}
|
||||||
|
internal override void InternalOnStart()
|
||||||
|
{
|
||||||
|
_steps = ESteps.RecordCacheFiles;
|
||||||
|
}
|
||||||
|
internal override void InternalOnUpdate()
|
||||||
|
{
|
||||||
|
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (_steps == ESteps.RecordCacheFiles)
|
||||||
|
{
|
||||||
|
_steps = ESteps.WaitResponse;
|
||||||
|
|
||||||
|
var fileSystemMgr = _fileSystem.GetFileSystemMgr();
|
||||||
|
var statOption = new WXStatOption();
|
||||||
|
statOption.path = _fileSystem.FileRoot;
|
||||||
|
statOption.recursive = true;
|
||||||
|
statOption.success = (WXStatResponse response) =>
|
||||||
|
{
|
||||||
|
_steps = ESteps.Done;
|
||||||
|
Status = EOperationStatus.Succeed;
|
||||||
|
foreach (var fileStat in response.stats)
|
||||||
|
{
|
||||||
|
//TODO 需要确认存储文件为Bundle文件
|
||||||
|
_fileSystem.RecordBundleFile(_fileSystem.FileRoot + fileStat.path);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
statOption.fail = (WXStatResponse response) =>
|
||||||
|
{
|
||||||
|
_steps = ESteps.Done;
|
||||||
|
Status = EOperationStatus.Failed;
|
||||||
|
Error = response.errMsg;
|
||||||
|
};
|
||||||
|
fileSystemMgr.Stat(statOption);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
|
@ -0,0 +1,11 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 640ec7bd883b8314db53b508278aea6e
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
|
@ -53,7 +53,8 @@ internal class WechatFileSystem : IFileSystem
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly Dictionary<string, string> _cacheFilePathMapping = new Dictionary<string, string>(10000);
|
private readonly HashSet<string> _recorders = new HashSet<string>();
|
||||||
|
private readonly Dictionary<string, string> _cacheFilePaths = new Dictionary<string, string>(10000);
|
||||||
private WXFileSystemManager _fileSystemMgr;
|
private WXFileSystemManager _fileSystemMgr;
|
||||||
private string _wxCacheRoot = string.Empty;
|
private string _wxCacheRoot = string.Empty;
|
||||||
|
|
||||||
|
@ -82,7 +83,7 @@ internal class WechatFileSystem : IFileSystem
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return 0;
|
return _recorders.Count;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,10 +174,10 @@ internal class WechatFileSystem : IFileSystem
|
||||||
YooLogger.Warning($"Invalid parameter : {name}");
|
YooLogger.Warning($"Invalid parameter : {name}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public virtual void OnCreate(string packageName, string packageRoot)
|
public virtual void OnCreate(string packageName, string rootDirectory)
|
||||||
{
|
{
|
||||||
PackageName = packageName;
|
PackageName = packageName;
|
||||||
_wxCacheRoot = packageRoot;
|
_wxCacheRoot = rootDirectory;
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(_wxCacheRoot))
|
if (string.IsNullOrEmpty(_wxCacheRoot))
|
||||||
{
|
{
|
||||||
|
@ -203,7 +204,7 @@ internal class WechatFileSystem : IFileSystem
|
||||||
public virtual bool Exists(PackageBundle bundle)
|
public virtual bool Exists(PackageBundle bundle)
|
||||||
{
|
{
|
||||||
string filePath = GetCacheFileLoadPath(bundle);
|
string filePath = GetCacheFileLoadPath(bundle);
|
||||||
return CheckCacheFileExist(filePath);
|
return _recorders.Contains(filePath);
|
||||||
}
|
}
|
||||||
public virtual bool NeedDownload(PackageBundle bundle)
|
public virtual bool NeedDownload(PackageBundle bundle)
|
||||||
{
|
{
|
||||||
|
@ -249,21 +250,55 @@ internal class WechatFileSystem : IFileSystem
|
||||||
}
|
}
|
||||||
public bool CheckCacheFileExist(string filePath)
|
public bool CheckCacheFileExist(string filePath)
|
||||||
{
|
{
|
||||||
string result = WX.GetCachePath(filePath);
|
string result = _fileSystemMgr.AccessSync(filePath);
|
||||||
if (string.IsNullOrEmpty(result))
|
return result.Equals("access:ok");
|
||||||
return false;
|
|
||||||
else
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
public string GetCacheFileLoadPath(PackageBundle bundle)
|
public string GetCacheFileLoadPath(PackageBundle bundle)
|
||||||
{
|
{
|
||||||
if (_cacheFilePathMapping.TryGetValue(bundle.BundleGUID, out string filePath) == false)
|
if (_cacheFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false)
|
||||||
{
|
{
|
||||||
filePath = PathUtility.Combine(_wxCacheRoot, bundle.FileName);
|
filePath = PathUtility.Combine(_wxCacheRoot, "__GAME_FILE_CACHE", _packageRoot, bundle.FileName);
|
||||||
_cacheFilePathMapping.Add(bundle.BundleGUID, filePath);
|
_cacheFilePaths.Add(bundle.BundleGUID, filePath);
|
||||||
}
|
}
|
||||||
return filePath;
|
return filePath;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region 本地记录
|
||||||
|
public List<string> GetAllRecords()
|
||||||
|
{
|
||||||
|
return _recorders.ToList();
|
||||||
|
}
|
||||||
|
public bool RecordBundleFile(string filePath)
|
||||||
|
{
|
||||||
|
if (_recorders.Contains(filePath))
|
||||||
|
{
|
||||||
|
YooLogger.Error($"{nameof(WechatFileSystem)} has element : {filePath}");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
_recorders.Add(filePath);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public void TryRecordBundle(PackageBundle bundle)
|
||||||
|
{
|
||||||
|
string filePath = GetCacheFileLoadPath(bundle);
|
||||||
|
if (_recorders.Contains(filePath) == false)
|
||||||
|
{
|
||||||
|
_recorders.Add(filePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void ClearAllRecords()
|
||||||
|
{
|
||||||
|
_recorders.Clear();
|
||||||
|
}
|
||||||
|
public void ClearRecord(string filePath)
|
||||||
|
{
|
||||||
|
if (_recorders.Contains(filePath))
|
||||||
|
{
|
||||||
|
_recorders.Remove(filePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
Loading…
Reference in New Issue