update file system

This commit is contained in:
何冠峰
2024-07-05 19:29:34 +08:00
parent b151f968d7
commit a9e5e7fdd3
21 changed files with 350 additions and 513 deletions
@@ -5,6 +5,9 @@ using UnityEngine;
namespace YooAsset
{
/// <summary>
/// Web文件系统
/// </summary>
internal class DefaultWebFileSystem : IFileSystem
{
public class FileWrapper
@@ -128,6 +131,21 @@ namespace YooAsset
{
throw new System.NotImplementedException();
}
public virtual FSLoadBundleOperation LoadBundleFile(PackageBundle bundle)
{
var operation = new DWFSLoadAssetBundleOperation(this, bundle);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
public virtual void UnloadBundleFile(PackageBundle bundle, object result)
{
AssetBundle assetBundle = result as AssetBundle;
if (assetBundle == null)
return;
if (assetBundle != null)
assetBundle.Unload(true);
}
public virtual void SetParameter(string name, object value)
{
@@ -165,74 +183,26 @@ namespace YooAsset
{
return true;
}
public virtual bool Belong(string bundleGUID)
public virtual bool Exists(PackageBundle bundle)
{
return true;
}
public virtual bool Exists(PackageBundle bundle)
public virtual bool NeedDownload(PackageBundle bundle)
{
if (Belong(bundle) == false)
return false;
return Exists(bundle) == false;
}
public virtual bool NeedUnpack(PackageBundle bundle)
{
return false;
}
public virtual bool Exists(string bundleGUID)
public virtual bool NeedImport(PackageBundle bundle)
{
return false;
}
public virtual bool CheckNeedDownload(PackageBundle bundle)
{
return false;
}
public virtual bool CheckNeedUnpack(PackageBundle bundle)
{
return false;
}
public virtual bool CheckNeedImport(PackageBundle bundle)
{
return false;
}
public virtual bool WriteFile(PackageBundle bundle, string copyPath)
{
throw new System.NotImplementedException();
}
public virtual bool DeleteFile(PackageBundle bundle)
{
throw new System.NotImplementedException();
}
public virtual bool DeleteFile(string bundleGUID)
{
throw new System.NotImplementedException();
}
public virtual EFileVerifyResult VerifyFile(PackageBundle bundle)
{
return EFileVerifyResult.Succeed;
}
public virtual byte[] ReadFileBytes(PackageBundle bundle)
{
throw new System.NotImplementedException();
}
public virtual string ReadFileText(PackageBundle bundle)
{
throw new System.NotImplementedException();
}
public virtual FSLoadBundleOperation LoadBundleFile(PackageBundle bundle)
{
var operation = new DWFSLoadAssetBundleOperation(this, bundle);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
public virtual void UnloadBundleFile(PackageBundle bundle, object result)
{
AssetBundle assetBundle = result as AssetBundle;
if (assetBundle == null)
return;
if (assetBundle != null)
assetBundle.Unload(true);
}
#region 内部方法
protected string GetDefaultWebRoot()
{
@@ -270,9 +240,9 @@ namespace YooAsset
}
/// <summary>
/// 记录缓存信息
/// 记录文件信息
/// </summary>
public bool Record(string bundleGUID, FileWrapper wrapper)
public bool RecordFile(string bundleGUID, FileWrapper wrapper)
{
if (_wrappers.ContainsKey(bundleGUID))
{
@@ -7,13 +7,12 @@ namespace YooAsset
{
None,
DownloadFile,
CheckResult,
Done,
}
private readonly DefaultWebFileSystem _fileSystem;
private readonly PackageBundle _bundle;
private DWFSDownloadWebFileOperation _downloadWebFileOp;
private DownloadHandlerAssetBundleOperation _downloadhanlderAssetBundleOp;
private ESteps _steps = ESteps.None;
@@ -33,32 +32,32 @@ namespace YooAsset
if (_steps == ESteps.DownloadFile)
{
if (_downloadWebFileOp == null)
if (_downloadhanlderAssetBundleOp == null)
{
int failedTryAgain = int.MaxValue;
int timeout = 60;
string mainURL = _fileSystem.RemoteServices.GetRemoteMainURL(_bundle.FileName);
string fallbackURL = _fileSystem.RemoteServices.GetRemoteFallbackURL(_bundle.FileName);
_downloadWebFileOp = new DWFSDownloadWebFileOperation(_fileSystem, _bundle, mainURL, fallbackURL, failedTryAgain, timeout);
_downloadhanlderAssetBundleOp = new DownloadHandlerAssetBundleOperation(_fileSystem, _bundle, mainURL, fallbackURL, failedTryAgain, timeout);
}
DownloadProgress = _downloadWebFileOp.DownloadProgress;
DownloadedBytes = _downloadWebFileOp.DownloadedBytes;
Progress = _downloadWebFileOp.Progress;
if (_downloadWebFileOp.IsDone == false)
DownloadProgress = _downloadhanlderAssetBundleOp.DownloadProgress;
DownloadedBytes = _downloadhanlderAssetBundleOp.DownloadedBytes;
Progress = _downloadhanlderAssetBundleOp.Progress;
if (_downloadhanlderAssetBundleOp.IsDone == false)
return;
if (_downloadWebFileOp.Status == EOperationStatus.Succeed)
if (_downloadhanlderAssetBundleOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
Result = _downloadWebFileOp.Result;
Result = _downloadhanlderAssetBundleOp.Result;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _downloadWebFileOp.Error;
Error = _downloadhanlderAssetBundleOp.Error;
}
}
}
@@ -77,8 +76,8 @@ namespace YooAsset
{
if (_steps == ESteps.DownloadFile)
{
if (_downloadWebFileOp != null)
_downloadWebFileOp.SetAbort();
if (_downloadhanlderAssetBundleOp != null)
_downloadhanlderAssetBundleOp.SetAbort();
}
}
}
@@ -1,22 +1,19 @@
using System.IO;
using UnityEngine;
using UnityEngine;
using UnityEngine.Networking;
namespace YooAsset
{
internal class DWFSDownloadWebFileOperation : DefaultDownloadFileOperation
internal class DownloadHandlerAssetBundleOperation : DefaultDownloadFileOperation
{
private readonly DefaultWebFileSystem _fileSystem;
private DownloadHandlerAssetBundle _downloadhandler;
private ESteps _steps = ESteps.None;
/// <summary>
/// 下载结果
/// </summary>
public AssetBundle Result { private set; get; }
internal DWFSDownloadWebFileOperation(DefaultWebFileSystem fileSystem, PackageBundle bundle, string mainURL, string fallbackURL, int failedTryAgain, int timeout)
internal DownloadHandlerAssetBundleOperation(DefaultWebFileSystem fileSystem, PackageBundle bundle,
string mainURL, string fallbackURL, int failedTryAgain, int timeout)
: base(bundle, mainURL, fallbackURL, failedTryAgain, timeout)
{
_fileSystem = fileSystem;
@@ -37,16 +34,7 @@ namespace YooAsset
_requestURL = GetRequestURL();
// 重置变量
_isAbort = false;
_latestDownloadBytes = 0;
_latestDownloadRealtime = Time.realtimeSinceStartup;
DownloadProgress = 0f;
DownloadedBytes = 0;
// 重置计时器
if (_tryAgainTimer > 0f)
YooLogger.Warning($"Try again download : {_requestURL}");
_tryAgainTimer = 0f;
ResetRequestFiled();
// 创建下载器
CreateWebRequest();
@@ -110,11 +98,21 @@ namespace YooAsset
private void CreateWebRequest()
{
_downloadhandler = CreateDownloadHandler();
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
_webRequest.downloadHandler = CreateDownloadHandler();
_webRequest.downloadHandler = _downloadhandler;
_webRequest.disposeDownloadHandlerOnDispose = true;
_webRequest.SendWebRequest();
}
private void DisposeWebRequest()
{
if (_webRequest != null)
{
//注意:引擎底层会自动调用Abort方法
_webRequest.Dispose();
_webRequest = null;
}
}
private DownloadHandlerAssetBundle CreateDownloadHandler()
{
if (_fileSystem.DisableUnityWebCache)
@@ -138,14 +136,5 @@ namespace YooAsset
return downloadhandler;
}
}
private void DisposeWebRequest()
{
if (_webRequest != null)
{
//注意:引擎底层会自动调用Abort方法
_webRequest.Dispose();
_webRequest = null;
}
}
}
}
@@ -60,7 +60,7 @@ namespace YooAsset
foreach (var wrapper in catalog.Wrappers)
{
var fileWrapper = new DefaultWebFileSystem.FileWrapper(wrapper.FileName);
_fileSystem.Record(wrapper.BundleGUID, fileWrapper);
_fileSystem.RecordFile(wrapper.BundleGUID, fileWrapper);
}
YooLogger.Log($"Package '{_fileSystem.PackageName}' catalog files count : {catalog.Wrappers.Count}");