update file system

This commit is contained in:
何冠峰
2024-07-07 00:52:17 +08:00
parent 481711fd75
commit bafd15571a
68 changed files with 483 additions and 889 deletions
@@ -36,8 +36,8 @@ namespace YooAsset
{
_allBundleGUIDs = _fileSystem.GetAllCachedBundleGUIDs();
_fileTotalCount = _allBundleGUIDs.Count;
YooLogger.Log($"Found all cache file count : {_fileTotalCount}");
_steps = ESteps.ClearAllCacheFiles;
YooLogger.Log($"Found all cache files count : {_fileTotalCount}");
}
if (_steps == ESteps.ClearAllCacheFiles)
@@ -35,11 +35,11 @@ namespace YooAsset
return;
if (_steps == ESteps.GetUnusedCacheFiles)
{
_steps = ESteps.ClearUnusedCacheFiles;
{
_unusedBundleGUIDs = GetUnusedBundleGUIDs();
_unusedFileTotalCount = _unusedBundleGUIDs.Count;
YooLogger.Log($"Found unused cache file count : {_unusedFileTotalCount}");
_steps = ESteps.ClearUnusedCacheFiles;
YooLogger.Log($"Found unused cache files count : {_unusedFileTotalCount}");
}
if (_steps == ESteps.ClearUnusedCacheFiles)
@@ -11,9 +11,7 @@ namespace YooAsset
private string _tempFilePath;
private ESteps _steps = ESteps.None;
internal DCFSDownloadNormalFileOperation(DefaultCacheFileSystem fileSystem, PackageBundle bundle,
string mainURL, string fallbackURL, int failedTryAgain, int timeout)
: base(bundle, mainURL, fallbackURL, failedTryAgain, timeout)
internal DCFSDownloadNormalFileOperation(DefaultCacheFileSystem fileSystem, PackageBundle bundle, DownloadParam param) : base(bundle, param)
{
_fileSystem = fileSystem;
}
@@ -110,7 +108,7 @@ namespace YooAsset
}
else
{
Error = $"{_fileSystem.GetType().FullName} write file failed !";
Error = $"{_fileSystem.GetType().FullName} failed to write file !";
Status = EOperationStatus.Failed;
_steps = ESteps.Done;
YooLogger.Error(Error);
@@ -198,19 +196,17 @@ namespace YooAsset
private DownloadHandlerFileRange _downloadHandle;
private VerifyTempFileOperation _verifyOperation;
private long _fileOriginLength = 0;
private string _fileSavePath;
private string _tempFilePath;
private ESteps _steps = ESteps.None;
internal DCFSDownloadResumeFileOperation(DefaultCacheFileSystem fileSystem, PackageBundle bundle,
string mainURL, string fallbackURL, int failedTryAgain, int timeout)
: base(bundle, mainURL, fallbackURL, failedTryAgain, timeout)
internal DCFSDownloadResumeFileOperation(DefaultCacheFileSystem fileSystem, PackageBundle bundle, DownloadParam param) : base(bundle, param)
{
_fileSystem = fileSystem;
}
internal override void InternalOnStart()
{
_fileSavePath = _fileSystem.GetTempFilePath(Bundle);
_tempFilePath = _fileSystem.GetTempFilePath(Bundle);
_steps = ESteps.CheckExists;
}
internal override void InternalOnUpdate()
@@ -235,7 +231,7 @@ namespace YooAsset
// 创建下载器
if (_steps == ESteps.CreateRequest)
{
FileUtility.CreateFileDirectory(_fileSavePath);
FileUtility.CreateFileDirectory(_tempFilePath);
// 获取请求地址
_requestURL = GetRequestURL();
@@ -246,9 +242,9 @@ namespace YooAsset
// 获取下载起始位置
_fileOriginLength = 0;
long fileBeginLength = -1;
if (File.Exists(_fileSavePath))
if (File.Exists(_tempFilePath))
{
FileInfo fileInfo = new FileInfo(_fileSavePath);
FileInfo fileInfo = new FileInfo(_tempFilePath);
fileBeginLength = fileInfo.Length;
_fileOriginLength = fileBeginLength;
DownloadedBytes = _fileOriginLength;
@@ -258,8 +254,8 @@ namespace YooAsset
if (fileBeginLength >= Bundle.FileSize)
{
// 删除临时文件
if (File.Exists(_fileSavePath))
File.Delete(_fileSavePath);
if (File.Exists(_tempFilePath))
File.Delete(_tempFilePath);
}
// 创建下载器
@@ -295,7 +291,7 @@ namespace YooAsset
// 验证下载文件
if (_steps == ESteps.VerifyTempFile)
{
var element = new TempFileElement(_fileSavePath, Bundle.FileCRC, Bundle.FileSize);
var element = new TempFileElement(_tempFilePath, Bundle.FileCRC, Bundle.FileSize);
_verifyOperation = new VerifyTempFileOperation(element);
OperationSystem.StartOperation(_fileSystem.PackageName, _verifyOperation);
_steps = ESteps.CheckVerifyTempFile;
@@ -309,14 +305,14 @@ namespace YooAsset
if (_verifyOperation.Status == EOperationStatus.Succeed)
{
if (_fileSystem.WriteCacheFile(Bundle, _fileSavePath))
if (_fileSystem.WriteCacheFile(Bundle, _tempFilePath))
{
Status = EOperationStatus.Succeed;
_steps = ESteps.Done;
}
else
{
Error = $"{_fileSystem.GetType().FullName} write file failed : {_fileSavePath}";
Error = $"{_fileSystem.GetType().FullName} failed to write file !";
Status = EOperationStatus.Failed;
_steps = ESteps.Done;
}
@@ -328,8 +324,8 @@ namespace YooAsset
}
// 注意:验证完成后直接删除文件
if (File.Exists(_fileSavePath))
File.Delete(_fileSavePath);
if (File.Exists(_tempFilePath))
File.Delete(_tempFilePath);
}
// 重新尝试下载
@@ -381,7 +377,7 @@ namespace YooAsset
{
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
#if UNITY_2019_4_OR_NEWER
var handler = new DownloadHandlerFile(_fileSavePath, true);
var handler = new DownloadHandlerFile(_tempFilePath, true);
handler.removeFileOnAbort = false;
#else
var handler = new DownloadHandlerFileRange(FileSavePath, Bundle.FileSize, _webRequest);
@@ -416,8 +412,8 @@ namespace YooAsset
//说明:如果遇到以下错误返回码,验证失败直接删除文件
if (_fileSystem.ResumeDownloadResponseCodes.Contains(HttpCode))
{
if (File.Exists(_fileSavePath))
File.Delete(_fileSavePath);
if (File.Exists(_tempFilePath))
File.Delete(_tempFilePath);
}
}
}
@@ -1,6 +1,4 @@
using System.IO;
using UnityEngine;
namespace YooAsset
{
internal class DCFSInitializeOperation : FSInitializeFileSystemOperation
@@ -43,7 +41,7 @@ namespace YooAsset
{
_fileSytem.DeleteAllManifestFiles();
appFootPrint.Coverage(_fileSytem.PackageName);
YooLogger.Log("Delete manifest files when application foot print dirty !");
YooLogger.Warning("Delete manifest files when application foot print dirty !");
}
_steps = ESteps.SearchCacheFiles;
@@ -76,11 +74,18 @@ namespace YooAsset
if (_verifyCacheFilesOp.IsDone == false)
return;
// 注意:总是返回成功
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
YooLogger.Log($"Package '{_fileSytem.PackageName}' cached files count : {_fileSytem.FileCount}");
if (_verifyCacheFilesOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
YooLogger.Log($"Package '{_fileSytem.PackageName}' cached files count : {_fileSytem.FileCount}");
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _verifyCacheFilesOp.Error;
}
}
}
}
@@ -5,21 +5,22 @@ namespace YooAsset
{
internal class DCFSLoadAssetBundleOperation : FSLoadBundleOperation
{
private enum ESteps
protected enum ESteps
{
None,
CheckExist,
DownloadFile,
LoadAssetBundle,
CheckResult,
Done,
}
private readonly DefaultCacheFileSystem _fileSystem;
private readonly PackageBundle _bundle;
private FSDownloadFileOperation _downloadFileOp;
private AssetBundleCreateRequest _createRequest;
private bool _isWaitForAsyncComplete = false;
private ESteps _steps = ESteps.None;
protected readonly DefaultCacheFileSystem _fileSystem;
protected readonly PackageBundle _bundle;
protected FSDownloadFileOperation _downloadFileOp;
protected AssetBundleCreateRequest _createRequest;
protected bool _isWaitForAsyncComplete = false;
protected ESteps _steps = ESteps.None;
internal DCFSLoadAssetBundleOperation(DefaultCacheFileSystem fileSystem, PackageBundle bundle)
@@ -29,29 +30,33 @@ namespace YooAsset
}
internal override void InternalOnStart()
{
if (_fileSystem.NeedDownload(_bundle))
{
_steps = ESteps.DownloadFile;
}
else
{
DownloadProgress = 1f;
DownloadedBytes = _bundle.FileSize;
_steps = ESteps.LoadAssetBundle;
}
_steps = ESteps.CheckExist;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.CheckExist)
{
if (_fileSystem.Exists(_bundle))
{
DownloadProgress = 1f;
DownloadedBytes = _bundle.FileSize;
_steps = ESteps.LoadAssetBundle;
}
else
{
_steps = ESteps.DownloadFile;
}
}
if (_steps == ESteps.DownloadFile)
{
if (_downloadFileOp == null)
{
int failedTryAgain = int.MaxValue;
int timeout = 60;
_downloadFileOp = _fileSystem.DownloadFileAsync(_bundle, null, failedTryAgain, timeout);
DownloadParam downloadParam = new DownloadParam(int.MaxValue, 60);
_downloadFileOp = _fileSystem.DownloadFileAsync(_bundle, downloadParam);
}
DownloadProgress = _downloadFileOp.DownloadProgress;
@@ -67,7 +72,7 @@ namespace YooAsset
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _downloadFileOp.Error;
Error = _downloadFileOp.Error;
}
}
@@ -106,14 +111,14 @@ namespace YooAsset
if (Result != null)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
Status = EOperationStatus.Succeed;
}
else
{
// 注意:当缓存文件的校验等级为Low的时候,并不能保证缓存文件的完整性。
// 说明:在AssetBundle文件加载失败的情况下,我们需要重新验证文件的完整性!
EFileVerifyResult result = _fileSystem.VerifyCacheFile(_bundle);
if (result == EFileVerifyResult.Succeed)
EFileVerifyResult verifyResult = _fileSystem.VerifyCacheFile(_bundle);
if (verifyResult == EFileVerifyResult.Succeed)
{
// 注意:在安卓移动平台,华为和三星真机上有极小概率加载资源包失败。
// 说明:大多数情况在首次安装下载资源到沙盒内,游戏过程中切换到后台再回到游戏内有很大概率触发!
@@ -126,20 +131,20 @@ namespace YooAsset
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Failed to load assetBundle from memory : {_bundle.BundleName}";
Error = $"Failed to load assetBundle from memory : {_bundle.BundleName}";
YooLogger.Error(Error);
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
Status = EOperationStatus.Succeed;
}
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Failed to read assetBundle file bytes : {_bundle.BundleName}";
Error = $"Failed to read assetBundle file bytes : {_bundle.BundleName}";
YooLogger.Error(Error);
}
}
@@ -148,7 +153,7 @@ namespace YooAsset
_steps = ESteps.Done;
_fileSystem.DeleteCacheFile(_bundle.BundleGUID);
Status = EOperationStatus.Failed;
Error = $"Find corrupted file and delete the file : {_bundle.BundleName}";
Error = $"Find corrupted file and delete the file : {_bundle.BundleName}";
YooLogger.Error(Error);
}
}
@@ -193,19 +198,20 @@ namespace YooAsset
internal class DCFSLoadRawBundleOperation : FSLoadBundleOperation
{
private enum ESteps
protected enum ESteps
{
None,
CheckExist,
DownloadFile,
LoadRawBundle,
CheckResult,
Done,
}
private readonly DefaultCacheFileSystem _fileSystem;
private readonly PackageBundle _bundle;
private FSDownloadFileOperation _downloadFileOp;
private ESteps _steps = ESteps.None;
protected readonly DefaultCacheFileSystem _fileSystem;
protected readonly PackageBundle _bundle;
protected FSDownloadFileOperation _downloadFileOp;
protected ESteps _steps = ESteps.None;
internal DCFSLoadRawBundleOperation(DefaultCacheFileSystem fileSystem, PackageBundle bundle)
@@ -215,29 +221,33 @@ namespace YooAsset
}
internal override void InternalOnStart()
{
if (_fileSystem.NeedDownload(_bundle))
{
_steps = ESteps.DownloadFile;
}
else
{
DownloadProgress = 1f;
DownloadedBytes = _bundle.FileSize;
_steps = ESteps.LoadRawBundle;
}
_steps = ESteps.CheckExist;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.CheckExist)
{
if (_fileSystem.Exists(_bundle))
{
DownloadProgress = 1f;
DownloadedBytes = _bundle.FileSize;
_steps = ESteps.LoadRawBundle;
}
else
{
_steps = ESteps.DownloadFile;
}
}
if (_steps == ESteps.DownloadFile)
{
if (_downloadFileOp == null)
{
int failedTryAgain = int.MaxValue;
int timeout = 60;
_downloadFileOp = _fileSystem.DownloadFileAsync(_bundle, null, failedTryAgain, timeout);
DownloadParam downloadParam = new DownloadParam(int.MaxValue, 60);
_downloadFileOp = _fileSystem.DownloadFileAsync(_bundle, downloadParam);
}
DownloadProgress = _downloadFileOp.DownloadProgress;
@@ -253,7 +263,7 @@ namespace YooAsset
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _downloadFileOp.Error;
Error = _downloadFileOp.Error;
}
}
@@ -272,20 +282,20 @@ namespace YooAsset
if (File.Exists(filePath))
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Can not found raw bundle file : {filePath}";
Error = $"Can not found cache raw bundle file : {filePath}";
}
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Failed load raw bundle file : {_bundle.BundleName}";
Error = $"Failed to load cache raw bundle file : {_bundle.BundleName}";
}
}
}
@@ -125,7 +125,7 @@ namespace YooAsset
if (_loadCachePackageManifestOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
Result = _loadCachePackageManifestOp.Manifest;
Manifest = _loadCachePackageManifestOp.Manifest;
Status = EOperationStatus.Succeed;
}
else
@@ -144,7 +144,6 @@ namespace YooAsset
string manifestFilePath = _fileSystem.GetCachePackageManifestFilePath(_packageVersion);
if (File.Exists(manifestFilePath))
{
YooLogger.Warning($"Failed to load cache manifest file : {Error}");
YooLogger.Warning($"Invalid cache manifest file have been removed : {manifestFilePath}");
File.Delete(manifestFilePath);
}
@@ -13,7 +13,7 @@ namespace YooAsset
private readonly DefaultCacheFileSystem _fileSystem;
private readonly bool _appendTimeTicks;
private readonly int _timeout;
private DefaultGetRemotePackageVersionOperation _getRemotePackageVersionOp;
private RequestRemotePackageVersionOperation _requestRemotePackageVersionOp;
private ESteps _steps = ESteps.None;
@@ -34,31 +34,27 @@ namespace YooAsset
if (_steps == ESteps.GetPackageVersion)
{
if (_getRemotePackageVersionOp == null)
if (_requestRemotePackageVersionOp == null)
{
string packageName = _fileSystem.PackageName;
string fileName = YooAssetSettingsData.GetPackageVersionFileName(packageName);
string mainURL = _fileSystem.RemoteServices.GetRemoteMainURL(fileName);
string fallbackURL = _fileSystem.RemoteServices.GetRemoteFallbackURL(fileName);
_getRemotePackageVersionOp = new DefaultGetRemotePackageVersionOperation(packageName, mainURL, fallbackURL, _appendTimeTicks, _timeout);
OperationSystem.StartOperation(packageName, _getRemotePackageVersionOp);
_requestRemotePackageVersionOp = new RequestRemotePackageVersionOperation(_fileSystem, _appendTimeTicks, _timeout);
OperationSystem.StartOperation(_fileSystem.PackageName, _requestRemotePackageVersionOp);
}
Progress = _getRemotePackageVersionOp.Progress;
if (_getRemotePackageVersionOp.IsDone == false)
Progress = _requestRemotePackageVersionOp.Progress;
if (_requestRemotePackageVersionOp.IsDone == false)
return;
if (_getRemotePackageVersionOp.Status == EOperationStatus.Succeed)
if (_requestRemotePackageVersionOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
PackageVersion = _getRemotePackageVersionOp.PackageVersion;
PackageVersion = _requestRemotePackageVersionOp.PackageVersion;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _getRemotePackageVersionOp.Error;
Error = _requestRemotePackageVersionOp.Error;
}
}
}
@@ -57,7 +57,6 @@ namespace YooAsset
string savePath = _fileSystem.GetCachePackageHashFilePath(_packageVersion);
string fileName = YooAssetSettingsData.GetPackageHashFileName(_fileSystem.PackageName, _packageVersion);
string webURL = GetWebRequestURL(fileName);
YooLogger.Log($"Beginning to download package hash file : {webURL}");
_webFileRequestOp = new UnityWebFileRequestOperation(webURL, savePath, _timeout);
OperationSystem.StartOperation(_fileSystem.PackageName, _webFileRequestOp);
}
@@ -57,7 +57,6 @@ namespace YooAsset
string savePath = _fileSystem.GetCachePackageManifestFilePath(_packageVersion);
string fileName = YooAssetSettingsData.GetManifestBinaryFileName(_fileSystem.PackageName, _packageVersion);
string webURL = GetDownloadRequestURL(fileName);
YooLogger.Log($"Beginning to download package manifest file : {webURL}");
_webFileRequestOp = new UnityWebFileRequestOperation(webURL, savePath, _timeout);
OperationSystem.StartOperation(_fileSystem.PackageName, _webFileRequestOp);
}
@@ -42,7 +42,7 @@ namespace YooAsset
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Cache package hash file not found : {filePath}";
Error = $"Can not found cache package hash file : {filePath}";
return;
}
@@ -21,7 +21,7 @@ namespace YooAsset
private ESteps _steps = ESteps.None;
/// <summary>
/// 加载的清单实例
/// 包裹清单
/// </summary>
public PackageManifest Manifest { private set; get; }
@@ -53,7 +53,7 @@ namespace YooAsset
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Cache manifest file not found : {manifestFilePath}";
Error = $"Can not found cache manifest file : {manifestFilePath}";
}
}
@@ -0,0 +1,98 @@
namespace YooAsset
{
internal class RequestRemotePackageVersionOperation : AsyncOperationBase
{
private enum ESteps
{
None,
RequestPackageVersion,
Done,
}
private readonly DefaultCacheFileSystem _fileSystem;
private readonly bool _appendTimeTicks;
private readonly int _timeout;
private UnityWebTextRequestOperation _webTextRequestOp;
private int _requestCount = 0;
private ESteps _steps = ESteps.None;
/// <summary>
/// 包裹版本
/// </summary>
internal string PackageVersion { set; get; }
internal RequestRemotePackageVersionOperation(DefaultCacheFileSystem fileSystem, bool appendTimeTicks, int timeout)
{
_fileSystem = fileSystem;
_appendTimeTicks = appendTimeTicks;
_timeout = timeout;
}
internal override void InternalOnStart()
{
_requestCount = WebRequestCounter.GetRequestFailedCount(_fileSystem.PackageName, nameof(RequestRemotePackageVersionOperation));
_steps = ESteps.RequestPackageVersion;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.RequestPackageVersion)
{
if (_webTextRequestOp == null)
{
string fileName = YooAssetSettingsData.GetPackageVersionFileName(_fileSystem.PackageName);
string url = GetWebRequestURL(fileName);
_webTextRequestOp = new UnityWebTextRequestOperation(url, _timeout);
OperationSystem.StartOperation(_fileSystem.PackageName, _webTextRequestOp);
}
Progress = _webTextRequestOp.Progress;
if (_webTextRequestOp.IsDone == false)
return;
if (_webTextRequestOp.Status == EOperationStatus.Succeed)
{
PackageVersion = _webTextRequestOp.Result;
if (string.IsNullOrEmpty(PackageVersion))
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Remote package version file content is empty !";
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _webTextRequestOp.Error;
WebRequestCounter.RecordRequestFailed(_fileSystem.PackageName, nameof(RequestRemotePackageVersionOperation));
}
}
}
private string GetWebRequestURL(string fileName)
{
string url;
// 轮流返回请求地址
if (_requestCount % 2 == 0)
url = _fileSystem.RemoteServices.GetRemoteMainURL(fileName);
else
url = _fileSystem.RemoteServices.GetRemoteFallbackURL(fileName);
// 在URL末尾添加时间戳
if (_appendTimeTicks)
return $"{url}?{System.DateTime.UtcNow.Ticks}";
else
return url;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1b078a220cc4f7d4392d77ccc77c001e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -56,7 +56,6 @@ namespace YooAsset
if (SearchFiles())
return;
// 注意:总是返回成功
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
float costTime = UnityEngine.Time.realtimeSinceStartup - _verifyStartTime;
@@ -35,7 +35,7 @@ namespace YooAsset
}
catch (System.Exception e)
{
YooLogger.Warning($"Failed delete cache bundle folder : {e}");
YooLogger.Warning($"Failed to delete cache bundle folder : {e}");
}
}
}
@@ -165,7 +165,7 @@ namespace YooAsset
{
_failedCount++;
YooLogger.Warning($"Failed verify file {element.Result} and delete files : {element.FileRootPath}");
YooLogger.Warning($"Failed to verify file {element.Result} and delete files : {element.FileRootPath}");
element.DeleteFiles();
}
}
@@ -81,7 +81,7 @@ namespace YooAsset
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Failed verify file : {_element.TempFilePath} ! ErrorCode : {VerifyResult}";
Error = $"Failed to verify file : {_element.TempFilePath} ! ErrorCode : {VerifyResult}";
}
}
}