Compare commits

..

7 Commits

Author SHA1 Message Date
何冠峰 14ea408fec fix #386 2024-12-12 18:13:33 +08:00
何冠峰 038a52f7fc Update ResourcePackage.cs 2024-12-12 17:59:04 +08:00
何冠峰 341bd5947f update extension sample 2024-12-12 17:55:39 +08:00
何冠峰 b5d857d2f1 fix #380
新增示例文件 CopyBuildinManifestOperation
2024-12-12 17:53:34 +08:00
何冠峰 97f9a3d4b1 refactor : default cache file system 2024-12-12 17:35:42 +08:00
何冠峰 9607d7135b refactor : cache system 2024-12-12 15:16:34 +08:00
何冠峰 b1338a9ffd refactor : cache file system 2024-12-12 15:00:08 +08:00
43 changed files with 848 additions and 481 deletions

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: da6402a4b93d31943b26fb99cebc0dfd
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,38 @@
using System.IO;
namespace YooAsset
{
internal class CacheFileElement
{
public string PackageName { private set; get; }
public string BundleGUID { private set; get; }
public string FileRootPath { private set; get; }
public string DataFilePath { private set; get; }
public string InfoFilePath { private set; get; }
public EFileVerifyResult Result;
public string DataFileCRC;
public long DataFileSize;
public CacheFileElement(string packageName, string bundleGUID, string fileRootPath, string dataFilePath, string infoFilePath)
{
PackageName = packageName;
BundleGUID = bundleGUID;
FileRootPath = fileRootPath;
DataFilePath = dataFilePath;
InfoFilePath = infoFilePath;
}
public void DeleteFiles()
{
try
{
Directory.Delete(FileRootPath, true);
}
catch (System.Exception e)
{
YooLogger.Warning($"Failed to delete cache bundle folder : {e}");
}
}
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: d6f813e2460f55e4ba3f54527e6999e3 guid: 0c361449cdcbd8746ba3fb948798ae1b
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View File

@ -0,0 +1,25 @@
using System;
using System.IO;
namespace YooAsset
{
internal class CacheHelper
{
/// <summary>
/// 获取默认的缓存根目录
/// </summary>
public static string GetDefaultCacheRoot()
{
#if UNITY_EDITOR
// 注意:为了方便调试查看,编辑器下把存储目录放到项目里。
string projectPath = Path.GetDirectoryName(UnityEngine.Application.dataPath);
projectPath = PathUtility.RegularPath(projectPath);
return PathUtility.Combine(projectPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
#elif UNITY_STANDALONE
return PathUtility.Combine(UnityEngine.Application.dataPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
#else
return PathUtility.Combine(UnityEngine.Application.persistentDataPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
#endif
}
}
}

View File

@ -0,0 +1,19 @@

namespace YooAsset
{
internal class CacheWrapper
{
public string InfoFilePath { private set; get; }
public string DataFilePath { private set; get; }
public string DataFileCRC { private set; get; }
public long DataFileSize { private set; get; }
public CacheWrapper(string infoFilePath, string dataFilePath, string dataFileCRC, long dataFileSize)
{
InfoFilePath = infoFilePath;
DataFilePath = dataFilePath;
DataFileCRC = dataFileCRC;
DataFileSize = dataFileSize;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ae2176c4ea6fbb3478bf8757def34cd7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: ce78de82d3ce1a841a1e6bad099e0bab
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,67 @@
using System.Collections.Generic;
namespace YooAsset
{
internal interface ICacheSystem
{
/// <summary>
/// 获取缓存文件的根目录
/// </summary>
string GetCacheFileRoot();
/// <summary>
/// 获取临时缓存文件路径
/// </summary>
string GetTempFilePath(PackageBundle bundle);
/// <summary>
/// 获取数据文件路径
/// </summary>
string GetDataFilePath(PackageBundle bundle);
/// <summary>
/// 获取信息文件路径
/// </summary>
string GetInfoFilePath(PackageBundle bundle);
/// <summary>
/// 获取所有缓存文件的GUID
/// </summary>
List<string> GetAllCachedBundleGUIDs();
/// <summary>
/// 是否记录了文件
/// </summary>
bool IsRecordFile(string bundleGUID);
/// <summary>
/// 记录指定文件
/// </summary>
bool RecordFile(string bundleGUID, CacheWrapper wrapper);
/// <summary>
/// 验证缓存文件
/// </summary>
EFileVerifyResult VerifyCacheFile(PackageBundle bundle);
/// <summary>
/// 写入缓存文件
/// </summary>
bool WriteCacheFile(PackageBundle bundle, string copyPath);
/// <summary>
/// 删除缓存文件
/// </summary>
bool DeleteCacheFile(string bundleGUID);
/// <summary>
/// 写入文件信息
/// </summary>
void WriteInfoFile(string filePath, string dataFileCRC, long dataFileSize);
/// <summary>
/// 读取文件信息
/// </summary>
void ReadInfoFile(string filePath, out string dataFileCRC, out long dataFileSize);
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f878bc9a12f5d2d40aee754ddfaada86
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 8cae5a51fced527429445b140b8a0843
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -3,7 +3,7 @@ using System.Collections.Generic;
namespace YooAsset namespace YooAsset
{ {
internal sealed class DCFSClearAllBundleFilesOperation : FSClearAllBundleFilesOperation internal sealed class ClearAllCacheFilesOperation : FSClearAllBundleFilesOperation
{ {
private enum ESteps private enum ESteps
{ {
@ -13,15 +13,15 @@ namespace YooAsset
Done, Done,
} }
private readonly DefaultCacheFileSystem _fileSystem; private readonly ICacheSystem _cacheSystem;
private List<string> _allBundleGUIDs; private List<string> _allBundleGUIDs;
private int _fileTotalCount = 0; private int _fileTotalCount = 0;
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
internal DCFSClearAllBundleFilesOperation(DefaultCacheFileSystem fileSystem) internal ClearAllCacheFilesOperation(ICacheSystem cacheSystem)
{ {
_fileSystem = fileSystem; _cacheSystem = cacheSystem;
} }
internal override void InternalOnStart() internal override void InternalOnStart()
{ {
@ -34,7 +34,7 @@ namespace YooAsset
if (_steps == ESteps.GetAllCacheFiles) if (_steps == ESteps.GetAllCacheFiles)
{ {
_allBundleGUIDs = _fileSystem.GetAllCachedBundleGUIDs(); _allBundleGUIDs = _cacheSystem.GetAllCachedBundleGUIDs();
_fileTotalCount = _allBundleGUIDs.Count; _fileTotalCount = _allBundleGUIDs.Count;
_steps = ESteps.ClearAllCacheFiles; _steps = ESteps.ClearAllCacheFiles;
YooLogger.Log($"Found all cache files count : {_fileTotalCount}"); YooLogger.Log($"Found all cache files count : {_fileTotalCount}");
@ -45,7 +45,7 @@ namespace YooAsset
for (int i = _allBundleGUIDs.Count - 1; i >= 0; i--) for (int i = _allBundleGUIDs.Count - 1; i >= 0; i--)
{ {
string bundleGUID = _allBundleGUIDs[i]; string bundleGUID = _allBundleGUIDs[i];
_fileSystem.DeleteCacheFile(bundleGUID); _cacheSystem.DeleteCacheFile(bundleGUID);
_allBundleGUIDs.RemoveAt(i); _allBundleGUIDs.RemoveAt(i);
if (OperationSystem.IsBusy) if (OperationSystem.IsBusy)
break; break;

View File

@ -3,7 +3,7 @@ using System.Collections.Generic;
namespace YooAsset namespace YooAsset
{ {
internal sealed class DCFSClearUnusedBundleFilesOperation : FSClearUnusedBundleFilesOperation internal sealed class ClearUnusedCacheFilesOperation : FSClearUnusedBundleFilesOperation
{ {
private enum ESteps private enum ESteps
{ {
@ -13,16 +13,16 @@ namespace YooAsset
Done, Done,
} }
private readonly DefaultCacheFileSystem _fileSystem; private readonly ICacheSystem _cacheSystem;
private readonly PackageManifest _manifest; private readonly PackageManifest _manifest;
private List<string> _unusedBundleGUIDs; private List<string> _unusedBundleGUIDs;
private int _unusedFileTotalCount = 0; private int _unusedFileTotalCount = 0;
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
internal DCFSClearUnusedBundleFilesOperation(DefaultCacheFileSystem fileSystem, PackageManifest manifest) internal ClearUnusedCacheFilesOperation(ICacheSystem cacheSystem, PackageManifest manifest)
{ {
_fileSystem = fileSystem; _cacheSystem = cacheSystem;
_manifest = manifest; _manifest = manifest;
} }
internal override void InternalOnStart() internal override void InternalOnStart()
@ -47,7 +47,7 @@ namespace YooAsset
for (int i = _unusedBundleGUIDs.Count - 1; i >= 0; i--) for (int i = _unusedBundleGUIDs.Count - 1; i >= 0; i--)
{ {
string bundleGUID = _unusedBundleGUIDs[i]; string bundleGUID = _unusedBundleGUIDs[i];
_fileSystem.DeleteCacheFile(bundleGUID); _cacheSystem.DeleteCacheFile(bundleGUID);
_unusedBundleGUIDs.RemoveAt(i); _unusedBundleGUIDs.RemoveAt(i);
if (OperationSystem.IsBusy) if (OperationSystem.IsBusy)
break; break;
@ -68,7 +68,7 @@ namespace YooAsset
private List<string> GetUnusedBundleGUIDs() private List<string> GetUnusedBundleGUIDs()
{ {
var allBundleGUIDs = _fileSystem.GetAllCachedBundleGUIDs(); var allBundleGUIDs = _cacheSystem.GetAllCachedBundleGUIDs();
List<string> result = new List<string>(allBundleGUIDs.Count); List<string> result = new List<string>(allBundleGUIDs.Count);
foreach (var bundleGUID in allBundleGUIDs) foreach (var bundleGUID in allBundleGUIDs)
{ {

View File

@ -0,0 +1,199 @@
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
namespace YooAsset
{
internal sealed class DownloadNormalFileOperation : DefaultDownloadFileOperation
{
private readonly IFileSystem _fileSystem;
private readonly ICacheSystem _cacheSystem;
private VerifyTempFileOperation _verifyOperation;
private string _tempFilePath;
private ESteps _steps = ESteps.None;
internal DownloadNormalFileOperation(IFileSystem fileSystem, ICacheSystem cacheSystem, PackageBundle bundle, DownloadParam param) : base(bundle, param)
{
_fileSystem = fileSystem;
_cacheSystem = cacheSystem;
}
internal override void InternalOnStart()
{
_tempFilePath = _cacheSystem.GetTempFilePath(Bundle);
_steps = ESteps.CheckExists;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
// 检测文件是否存在
if (_steps == ESteps.CheckExists)
{
if (_fileSystem.Exists(Bundle))
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.CreateRequest;
}
}
// 创建下载器
if (_steps == ESteps.CreateRequest)
{
FileUtility.CreateFileDirectory(_tempFilePath);
// 删除临时文件
if (File.Exists(_tempFilePath))
File.Delete(_tempFilePath);
// 获取请求地址
_requestURL = GetRequestURL();
// 重置请求
ResetRequestFiled();
// 创建下载器
CreateWebRequest();
_steps = ESteps.CheckRequest;
}
// 检测下载结果
if (_steps == ESteps.CheckRequest)
{
DownloadProgress = _webRequest.downloadProgress;
DownloadedBytes = (long)_webRequest.downloadedBytes;
Progress = DownloadProgress;
if (_webRequest.isDone == false)
{
CheckRequestTimeout();
return;
}
// 检查网络错误
if (CheckRequestResult())
_steps = ESteps.VerifyTempFile;
else
_steps = ESteps.TryAgain;
// 注意:最终释放请求器
DisposeWebRequest();
}
// 验证下载文件
if (_steps == ESteps.VerifyTempFile)
{
var element = new TempFileElement(_tempFilePath, Bundle.FileCRC, Bundle.FileSize);
_verifyOperation = new VerifyTempFileOperation(element);
OperationSystem.StartOperation(_fileSystem.PackageName, _verifyOperation);
_steps = ESteps.CheckVerifyTempFile;
}
// 等待验证完成
if (_steps == ESteps.CheckVerifyTempFile)
{
if (IsWaitForAsyncComplete)
_verifyOperation.WaitForAsyncComplete();
if (_verifyOperation.IsDone == false)
return;
if (_verifyOperation.Status == EOperationStatus.Succeed)
{
if (_cacheSystem.WriteCacheFile(Bundle, _tempFilePath))
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"{_fileSystem.GetType().FullName} failed to write file !";
YooLogger.Error(Error);
}
}
else
{
_steps = ESteps.TryAgain;
Error = _verifyOperation.Error;
}
// 注意:验证完成后直接删除文件
if (File.Exists(_tempFilePath))
File.Delete(_tempFilePath);
}
// 重新尝试下载
if (_steps == ESteps.TryAgain)
{
if (FailedTryAgain <= 0)
{
Status = EOperationStatus.Failed;
_steps = ESteps.Done;
YooLogger.Error(Error);
return;
}
_tryAgainTimer += Time.unscaledDeltaTime;
if (_tryAgainTimer > 1f)
{
FailedTryAgain--;
_steps = ESteps.CreateRequest;
YooLogger.Warning(Error);
}
}
}
internal override void InternalOnAbort()
{
_steps = ESteps.Done;
DisposeWebRequest();
}
internal override void InternalWaitForAsyncComplete()
{
bool isReuqestLocalFile = IsRequestLocalFile();
while (true)
{
// 注意:如果是导入或解压本地文件,执行等待完毕
if (isReuqestLocalFile)
{
InternalOnUpdate();
if (IsDone)
break;
}
else
{
if (ExecuteWhileDone())
{
_steps = ESteps.Done;
break;
}
}
}
}
private void CreateWebRequest()
{
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
DownloadHandlerFile handler = new DownloadHandlerFile(_tempFilePath);
handler.removeFileOnAbort = true;
_webRequest.downloadHandler = handler;
_webRequest.disposeDownloadHandlerOnDispose = true;
_webRequest.SendWebRequest();
}
private void DisposeWebRequest()
{
if (_webRequest != null)
{
//注意引擎底层会自动调用Abort方法
_webRequest.Dispose();
_webRequest = null;
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 759967d543776a0469b625eff171d235
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,203 +1,15 @@
using System.IO; using System.Collections.Generic;
using System.IO;
using UnityEngine; using UnityEngine;
using UnityEngine.Networking; using UnityEngine.Networking;
namespace YooAsset namespace YooAsset
{ {
internal sealed class DCFSDownloadNormalFileOperation : DefaultDownloadFileOperation internal sealed class DownloadResumeFileOperation : DefaultDownloadFileOperation
{ {
private readonly DefaultCacheFileSystem _fileSystem; private readonly IFileSystem _fileSystem;
private VerifyTempFileOperation _verifyOperation; private readonly ICacheSystem _cacheSystem;
private string _tempFilePath; private readonly List<long> _responseCodes;
private ESteps _steps = ESteps.None;
internal DCFSDownloadNormalFileOperation(DefaultCacheFileSystem fileSystem, PackageBundle bundle, DownloadParam param) : base(bundle, param)
{
_fileSystem = fileSystem;
}
internal override void InternalOnStart()
{
_tempFilePath = _fileSystem.GetTempFilePath(Bundle);
_steps = ESteps.CheckExists;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
// 检测文件是否存在
if (_steps == ESteps.CheckExists)
{
if (_fileSystem.Exists(Bundle))
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.CreateRequest;
}
}
// 创建下载器
if (_steps == ESteps.CreateRequest)
{
FileUtility.CreateFileDirectory(_tempFilePath);
// 删除临时文件
if (File.Exists(_tempFilePath))
File.Delete(_tempFilePath);
// 获取请求地址
_requestURL = GetRequestURL();
// 重置请求
ResetRequestFiled();
// 创建下载器
CreateWebRequest();
_steps = ESteps.CheckRequest;
}
// 检测下载结果
if (_steps == ESteps.CheckRequest)
{
DownloadProgress = _webRequest.downloadProgress;
DownloadedBytes = (long)_webRequest.downloadedBytes;
Progress = DownloadProgress;
if (_webRequest.isDone == false)
{
CheckRequestTimeout();
return;
}
// 检查网络错误
if (CheckRequestResult())
_steps = ESteps.VerifyTempFile;
else
_steps = ESteps.TryAgain;
// 注意:最终释放请求器
DisposeWebRequest();
}
// 验证下载文件
if (_steps == ESteps.VerifyTempFile)
{
var element = new TempFileElement(_tempFilePath, Bundle.FileCRC, Bundle.FileSize);
_verifyOperation = new VerifyTempFileOperation(element);
OperationSystem.StartOperation(_fileSystem.PackageName, _verifyOperation);
_steps = ESteps.CheckVerifyTempFile;
}
// 等待验证完成
if (_steps == ESteps.CheckVerifyTempFile)
{
if (IsWaitForAsyncComplete)
_verifyOperation.WaitForAsyncComplete();
if (_verifyOperation.IsDone == false)
return;
if (_verifyOperation.Status == EOperationStatus.Succeed)
{
if (_fileSystem.WriteCacheFile(Bundle, _tempFilePath))
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"{_fileSystem.GetType().FullName} failed to write file !";
YooLogger.Error(Error);
}
}
else
{
_steps = ESteps.TryAgain;
Error = _verifyOperation.Error;
}
// 注意:验证完成后直接删除文件
if (File.Exists(_tempFilePath))
File.Delete(_tempFilePath);
}
// 重新尝试下载
if (_steps == ESteps.TryAgain)
{
if (FailedTryAgain <= 0)
{
Status = EOperationStatus.Failed;
_steps = ESteps.Done;
YooLogger.Error(Error);
return;
}
_tryAgainTimer += Time.unscaledDeltaTime;
if (_tryAgainTimer > 1f)
{
FailedTryAgain--;
_steps = ESteps.CreateRequest;
YooLogger.Warning(Error);
}
}
}
internal override void InternalOnAbort()
{
_steps = ESteps.Done;
DisposeWebRequest();
}
internal override void InternalWaitForAsyncComplete()
{
bool isReuqestLocalFile = IsRequestLocalFile();
while (true)
{
// 注意:如果是导入或解压本地文件,执行等待完毕
if (isReuqestLocalFile)
{
InternalOnUpdate();
if (IsDone)
break;
}
else
{
if (ExecuteWhileDone())
{
_steps = ESteps.Done;
break;
}
}
}
}
private void CreateWebRequest()
{
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
DownloadHandlerFile handler = new DownloadHandlerFile(_tempFilePath);
handler.removeFileOnAbort = true;
_webRequest.downloadHandler = handler;
_webRequest.disposeDownloadHandlerOnDispose = true;
_webRequest.SendWebRequest();
}
private void DisposeWebRequest()
{
if (_webRequest != null)
{
//注意引擎底层会自动调用Abort方法
_webRequest.Dispose();
_webRequest = null;
}
}
}
internal sealed class DCFSDownloadResumeFileOperation : DefaultDownloadFileOperation
{
private readonly DefaultCacheFileSystem _fileSystem;
private DownloadHandlerFileRange _downloadHandle; private DownloadHandlerFileRange _downloadHandle;
private VerifyTempFileOperation _verifyOperation; private VerifyTempFileOperation _verifyOperation;
private long _fileOriginLength = 0; private long _fileOriginLength = 0;
@ -205,13 +17,15 @@ namespace YooAsset
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
internal DCFSDownloadResumeFileOperation(DefaultCacheFileSystem fileSystem, PackageBundle bundle, DownloadParam param) : base(bundle, param) internal DownloadResumeFileOperation(IFileSystem fileSystem, ICacheSystem cacheSystem, PackageBundle bundle, DownloadParam param, List<long> responseCodes) : base(bundle, param)
{ {
_fileSystem = fileSystem; _fileSystem = fileSystem;
_cacheSystem = cacheSystem;
_responseCodes = responseCodes;
} }
internal override void InternalOnStart() internal override void InternalOnStart()
{ {
_tempFilePath = _fileSystem.GetTempFilePath(Bundle); _tempFilePath = _cacheSystem.GetTempFilePath(Bundle);
_steps = ESteps.CheckExists; _steps = ESteps.CheckExists;
} }
internal override void InternalOnUpdate() internal override void InternalOnUpdate()
@ -313,7 +127,7 @@ namespace YooAsset
if (_verifyOperation.Status == EOperationStatus.Succeed) if (_verifyOperation.Status == EOperationStatus.Succeed)
{ {
if (_fileSystem.WriteCacheFile(Bundle, _tempFilePath)) if (_cacheSystem.WriteCacheFile(Bundle, _tempFilePath))
{ {
Status = EOperationStatus.Succeed; Status = EOperationStatus.Succeed;
_steps = ESteps.Done; _steps = ESteps.Done;
@ -418,11 +232,11 @@ namespace YooAsset
} }
private void ClearTempFileWhenError() private void ClearTempFileWhenError()
{ {
if (_fileSystem.ResumeDownloadResponseCodes == null) if (_responseCodes == null)
return; return;
//说明:如果遇到以下错误返回码,验证失败直接删除文件 //说明:如果遇到以下错误返回码,验证失败直接删除文件
if (_fileSystem.ResumeDownloadResponseCodes.Contains(HttpCode)) if (_responseCodes.Contains(HttpCode))
{ {
if (File.Exists(_tempFilePath)) if (File.Exists(_tempFilePath))
File.Delete(_tempFilePath); File.Delete(_tempFilePath);

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9bfb785912326f942a31dc77762eb16a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -15,7 +15,9 @@ namespace YooAsset
Done, Done,
} }
private readonly DefaultCacheFileSystem _fileSystem; private readonly ICacheSystem _cacheSystem;
private readonly string _packageName;
private readonly bool _appendFileExtension;
private IEnumerator<DirectoryInfo> _filesEnumerator = null; private IEnumerator<DirectoryInfo> _filesEnumerator = null;
private float _verifyStartTime; private float _verifyStartTime;
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
@ -26,9 +28,11 @@ namespace YooAsset
public readonly List<CacheFileElement> Result = new List<CacheFileElement>(5000); public readonly List<CacheFileElement> Result = new List<CacheFileElement>(5000);
internal SearchCacheFilesOperation(DefaultCacheFileSystem fileSystem) internal SearchCacheFilesOperation(ICacheSystem cacheSystem, string packageName, bool appendFileExtension)
{ {
_fileSystem = fileSystem; _cacheSystem = cacheSystem;
_packageName = packageName;
_appendFileExtension = appendFileExtension;
} }
internal override void InternalOnStart() internal override void InternalOnStart()
{ {
@ -42,7 +46,7 @@ namespace YooAsset
if (_steps == ESteps.Prepare) if (_steps == ESteps.Prepare)
{ {
DirectoryInfo rootDirectory = new DirectoryInfo(_fileSystem.GetCacheFilesRoot()); DirectoryInfo rootDirectory = new DirectoryInfo(_cacheSystem.GetCacheFileRoot());
if (rootDirectory.Exists) if (rootDirectory.Exists)
{ {
var directorieInfos = rootDirectory.EnumerateDirectories(); var directorieInfos = rootDirectory.EnumerateDirectories();
@ -80,7 +84,7 @@ namespace YooAsset
foreach (var chidDirectory in childDirectories) foreach (var chidDirectory in childDirectories)
{ {
string bundleGUID = chidDirectory.Name; string bundleGUID = chidDirectory.Name;
if (_fileSystem.IsRecordFile(bundleGUID)) if (_cacheSystem.IsRecordFile(bundleGUID))
continue; continue;
// 创建验证元素类 // 创建验证元素类
@ -89,14 +93,14 @@ namespace YooAsset
string infoFilePath = $"{fileRootPath}/{ DefaultCacheFileSystemDefine.SaveBundleInfoFileName}"; string infoFilePath = $"{fileRootPath}/{ DefaultCacheFileSystemDefine.SaveBundleInfoFileName}";
// 存储的数据文件追加文件格式 // 存储的数据文件追加文件格式
if (_fileSystem.AppendFileExtension) if (_appendFileExtension)
{ {
string dataFileExtension = FindDataFileExtension(chidDirectory); string dataFileExtension = FindDataFileExtension(chidDirectory);
if (string.IsNullOrEmpty(dataFileExtension) == false) if (string.IsNullOrEmpty(dataFileExtension) == false)
dataFilePath += dataFileExtension; dataFilePath += dataFileExtension;
} }
var element = new CacheFileElement(_fileSystem.PackageName, bundleGUID, fileRootPath, dataFilePath, infoFilePath); var element = new CacheFileElement(_packageName, bundleGUID, fileRootPath, dataFilePath, infoFilePath);
Result.Add(element); Result.Add(element);
} }

View File

@ -6,44 +6,10 @@ using System.Threading;
namespace YooAsset namespace YooAsset
{ {
internal class CacheFileElement
{
public string PackageName { private set; get; }
public string BundleGUID { private set; get; }
public string FileRootPath { private set; get; }
public string DataFilePath { private set; get; }
public string InfoFilePath { private set; get; }
public EFileVerifyResult Result;
public string DataFileCRC;
public long DataFileSize;
public CacheFileElement(string packageName, string bundleGUID, string fileRootPath, string dataFilePath, string infoFilePath)
{
PackageName = packageName;
BundleGUID = bundleGUID;
FileRootPath = fileRootPath;
DataFilePath = dataFilePath;
InfoFilePath = infoFilePath;
}
public void DeleteFiles()
{
try
{
Directory.Delete(FileRootPath, true);
}
catch (System.Exception e)
{
YooLogger.Warning($"Failed to delete cache bundle folder : {e}");
}
}
}
/// <summary> /// <summary>
/// 缓存文件验证(线程版) /// 缓存文件验证(线程版)
/// </summary> /// </summary>
internal class VerifyCacheFilesOperation : AsyncOperationBase internal sealed class VerifyCacheFilesOperation : AsyncOperationBase
{ {
private enum ESteps private enum ESteps
{ {
@ -54,10 +20,10 @@ namespace YooAsset
} }
private readonly ThreadSyncContext _syncContext = new ThreadSyncContext(); private readonly ThreadSyncContext _syncContext = new ThreadSyncContext();
private readonly DefaultCacheFileSystem _fileSystem; private readonly ICacheSystem _cacheSystem;
private readonly EFileVerifyLevel _verifyLevel;
private List<CacheFileElement> _waitingList; private List<CacheFileElement> _waitingList;
private List<CacheFileElement> _verifyingList; private List<CacheFileElement> _verifyingList;
private EFileVerifyLevel _verifyLevel = EFileVerifyLevel.Middle;
private int _verifyMaxNum; private int _verifyMaxNum;
private int _verifyTotalCount; private int _verifyTotalCount;
private float _verifyStartTime; private float _verifyStartTime;
@ -66,11 +32,11 @@ namespace YooAsset
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
internal VerifyCacheFilesOperation(DefaultCacheFileSystem fileSystem, List<CacheFileElement> elements) internal VerifyCacheFilesOperation(ICacheSystem cacheSystem, EFileVerifyLevel verifyLevel, List<CacheFileElement> elements)
{ {
_fileSystem = fileSystem; _cacheSystem = cacheSystem;
_verifyLevel = verifyLevel;
_waitingList = elements; _waitingList = elements;
_verifyLevel = _fileSystem.FileVerifyLevel;
} }
internal override void InternalOnStart() internal override void InternalOnStart()
{ {
@ -158,8 +124,8 @@ namespace YooAsset
if (element.Result == EFileVerifyResult.Succeed) if (element.Result == EFileVerifyResult.Succeed)
{ {
_succeedCount++; _succeedCount++;
var fileWrapper = new DefaultCacheFileSystem.FileWrapper(element.InfoFilePath, element.DataFilePath, element.DataFileCRC, element.DataFileSize); var fileWrapper = new CacheWrapper(element.InfoFilePath, element.DataFilePath, element.DataFileCRC, element.DataFileSize);
_fileSystem.RecordFile(element.BundleGUID, fileWrapper); _cacheSystem.RecordFile(element.BundleGUID, fileWrapper);
} }
else else
{ {
@ -191,7 +157,7 @@ namespace YooAsset
return EFileVerifyResult.InfoFileNotExisted; return EFileVerifyResult.InfoFileNotExisted;
// 解析信息文件获取验证数据 // 解析信息文件获取验证数据
_fileSystem.ReadInfoFile(element.InfoFilePath, out element.DataFileCRC, out element.DataFileSize); _cacheSystem.ReadInfoFile(element.InfoFilePath, out element.DataFileCRC, out element.DataFileSize);
} }
} }
catch (Exception) catch (Exception)
@ -199,7 +165,7 @@ namespace YooAsset
return EFileVerifyResult.Exception; return EFileVerifyResult.Exception;
} }
return FileSystemHelper.FileVerify(element.DataFilePath, element.DataFileSize, element.DataFileCRC, verifyLevel); return FileVerifyHelper.FileVerify(element.DataFilePath, element.DataFileSize, element.DataFileCRC, verifyLevel);
} }
} }
} }

View File

@ -3,29 +3,10 @@ using System.Threading;
namespace YooAsset namespace YooAsset
{ {
internal class TempFileElement
{
public string TempFilePath { private set; get; }
public string TempFileCRC { private set; get; }
public long TempFileSize { private set; get; }
/// <summary>
/// 注意:原子操作对象
/// </summary>
public int Result = 0;
public TempFileElement(string filePath, string fileCRC, long fileSize)
{
TempFilePath = filePath;
TempFileCRC = fileCRC;
TempFileSize = fileSize;
}
}
/// <summary> /// <summary>
/// 下载文件验证(线程版) /// 下载文件验证(线程版)
/// </summary> /// </summary>
internal class VerifyTempFileOperation : AsyncOperationBase internal sealed class VerifyTempFileOperation : AsyncOperationBase
{ {
private enum ESteps private enum ESteps
{ {
@ -41,7 +22,7 @@ namespace YooAsset
/// <summary> /// <summary>
/// 验证结果 /// 验证结果
/// </summary> /// </summary>
public EFileVerifyResult VerifyResult { protected set; get; } public EFileVerifyResult VerifyResult { private set; get; }
internal VerifyTempFileOperation(TempFileElement element) internal VerifyTempFileOperation(TempFileElement element)
@ -103,7 +84,7 @@ namespace YooAsset
private void VerifyInThread(object obj) private void VerifyInThread(object obj)
{ {
TempFileElement element = (TempFileElement)obj; TempFileElement element = (TempFileElement)obj;
int result = (int)FileSystemHelper.FileVerify(element.TempFilePath, element.TempFileSize, element.TempFileCRC, EFileVerifyLevel.High); int result = (int)FileVerifyHelper.FileVerify(element.TempFilePath, element.TempFileSize, element.TempFileCRC, EFileVerifyLevel.High);
element.Result = result; element.Result = result;
} }
} }

View File

@ -0,0 +1,22 @@

namespace YooAsset
{
internal class TempFileElement
{
public string TempFilePath { private set; get; }
public string TempFileCRC { private set; get; }
public long TempFileSize { private set; get; }
/// <summary>
/// 注意:原子操作对象
/// </summary>
public int Result = 0;
public TempFileElement(string filePath, string fileCRC, long fileSize)
{
TempFilePath = filePath;
TempFileCRC = fileCRC;
TempFileSize = fileSize;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 220136fdc4a13d84e97b1249cfc65f3b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -10,36 +10,6 @@ namespace YooAsset
/// </summary> /// </summary>
internal class DefaultBuildinFileSystem : IFileSystem internal class DefaultBuildinFileSystem : IFileSystem
{ {
private class UnpackRemoteServices : IRemoteServices
{
private readonly string _buildinPackageRoot;
protected readonly Dictionary<string, string> _mapping = new Dictionary<string, string>(10000);
public UnpackRemoteServices(string buildinPackRoot)
{
_buildinPackageRoot = buildinPackRoot;
}
string IRemoteServices.GetRemoteMainURL(string fileName)
{
return GetFileLoadURL(fileName);
}
string IRemoteServices.GetRemoteFallbackURL(string fileName)
{
return GetFileLoadURL(fileName);
}
private string GetFileLoadURL(string fileName)
{
if (_mapping.TryGetValue(fileName, out string url) == false)
{
string filePath = PathUtility.Combine(_buildinPackageRoot, fileName);
url = DownloadSystemHelper.ConvertToWWWPath(filePath);
_mapping.Add(fileName, url);
}
return url;
}
}
public class FileWrapper public class FileWrapper
{ {
public string FileName { private set; get; } public string FileName { private set; get; }
@ -220,7 +190,7 @@ namespace YooAsset
_packageRoot = PathUtility.Combine(rootDirectory, packageName); _packageRoot = PathUtility.Combine(rootDirectory, packageName);
// 创建解压文件系统 // 创建解压文件系统
var remoteServices = new UnpackRemoteServices(_packageRoot); var remoteServices = new DefaultUnpackRemoteServices(_packageRoot);
_unpackFileSystem = new DefaultUnpackFileSystem(); _unpackFileSystem = new DefaultUnpackFileSystem();
_unpackFileSystem.SetParameter(FileSystemParametersDefine.REMOTE_SERVICES, remoteServices); _unpackFileSystem.SetParameter(FileSystemParametersDefine.REMOTE_SERVICES, remoteServices);
_unpackFileSystem.SetParameter(FileSystemParametersDefine.FILE_VERIFY_LEVEL, FileVerifyLevel); _unpackFileSystem.SetParameter(FileSystemParametersDefine.FILE_VERIFY_LEVEL, FileVerifyLevel);
@ -231,6 +201,7 @@ namespace YooAsset
} }
public virtual void OnUpdate() public virtual void OnUpdate()
{ {
_unpackFileSystem.OnUpdate();
} }
public virtual bool Belong(PackageBundle bundle) public virtual bool Belong(PackageBundle bundle)

View File

@ -10,33 +10,18 @@ namespace YooAsset
/// 缓存文件系统 /// 缓存文件系统
/// 说明正在进行的下载器会在ResourcePackage销毁的时候执行Abort操作 /// 说明正在进行的下载器会在ResourcePackage销毁的时候执行Abort操作
/// </summary> /// </summary>
internal class DefaultCacheFileSystem : IFileSystem internal class DefaultCacheFileSystem : IFileSystem, ICacheSystem
{ {
public class FileWrapper protected readonly Dictionary<string, CacheWrapper> _wrappers = new Dictionary<string, CacheWrapper>(10000);
{
public string InfoFilePath { private set; get; }
public string DataFilePath { private set; get; }
public string DataFileCRC { private set; get; }
public long DataFileSize { private set; get; }
public FileWrapper(string infoFilePath, string dataFilePath, string dataFileCRC, long dataFileSize)
{
InfoFilePath = infoFilePath;
DataFilePath = dataFilePath;
DataFileCRC = dataFileCRC;
DataFileSize = dataFileSize;
}
}
protected readonly Dictionary<string, DefaultDownloadFileOperation> _downloaders = new Dictionary<string, DefaultDownloadFileOperation>(1000);
protected readonly Dictionary<string, FileWrapper> _wrappers = new Dictionary<string, FileWrapper>(10000);
protected readonly Dictionary<string, Stream> _loadedStream = new Dictionary<string, Stream>(10000); protected readonly Dictionary<string, Stream> _loadedStream = new Dictionary<string, Stream>(10000);
protected readonly Dictionary<string, string> _dataFilePaths = new Dictionary<string, string>(10000); protected readonly Dictionary<string, string> _dataFilePaths = new Dictionary<string, string>(10000);
protected readonly Dictionary<string, string> _infoFilePaths = new Dictionary<string, string>(10000); protected readonly Dictionary<string, string> _infoFilePaths = new Dictionary<string, string>(10000);
protected readonly Dictionary<string, string> _tempFilePaths = new Dictionary<string, string>(10000); protected readonly Dictionary<string, string> _tempFilePaths = new Dictionary<string, string>(10000);
protected readonly Dictionary<string, DefaultDownloadFileOperation> _downloaders = new Dictionary<string, DefaultDownloadFileOperation>(1000);
protected readonly List<string> _removeList = new List<string>(1000); protected readonly List<string> _removeList = new List<string>(1000);
protected string _packageRoot; protected string _packageRoot;
protected string _saveFileRoot; protected string _cacheFileRoot;
protected string _tempFileRoot; protected string _tempFileRoot;
protected string _manifestFileRoot; protected string _manifestFileRoot;
@ -128,13 +113,13 @@ namespace YooAsset
} }
public virtual FSClearAllBundleFilesOperation ClearAllBundleFilesAsync() public virtual FSClearAllBundleFilesOperation ClearAllBundleFilesAsync()
{ {
var operation = new DCFSClearAllBundleFilesOperation(this); var operation = new ClearAllCacheFilesOperation(this);
OperationSystem.StartOperation(PackageName, operation); OperationSystem.StartOperation(PackageName, operation);
return operation; return operation;
} }
public virtual FSClearUnusedBundleFilesOperation ClearUnusedBundleFilesAsync(PackageManifest manifest) public virtual FSClearUnusedBundleFilesOperation ClearUnusedBundleFilesAsync(PackageManifest manifest)
{ {
var operation = new DCFSClearUnusedBundleFilesOperation(this, manifest); var operation = new ClearUnusedCacheFilesOperation(this, manifest);
OperationSystem.StartOperation(PackageName, operation); OperationSystem.StartOperation(PackageName, operation);
return operation; return operation;
} }
@ -163,7 +148,7 @@ namespace YooAsset
if (bundle.FileSize >= ResumeDownloadMinimumSize) if (bundle.FileSize >= ResumeDownloadMinimumSize)
{ {
var newDownloader = new DCFSDownloadResumeFileOperation(this, bundle, param); var newDownloader = new DownloadResumeFileOperation(this, this, bundle, param, ResumeDownloadResponseCodes);
newDownloader.Reference(); newDownloader.Reference();
_downloaders.Add(bundle.BundleGUID, newDownloader); _downloaders.Add(bundle.BundleGUID, newDownloader);
OperationSystem.StartOperation(PackageName, newDownloader); OperationSystem.StartOperation(PackageName, newDownloader);
@ -171,7 +156,7 @@ namespace YooAsset
} }
else else
{ {
var newDownloader = new DCFSDownloadNormalFileOperation(this, bundle, param); var newDownloader = new DownloadNormalFileOperation(this, this, bundle, param);
newDownloader.Reference(); newDownloader.Reference();
_downloaders.Add(bundle.BundleGUID, newDownloader); _downloaders.Add(bundle.BundleGUID, newDownloader);
OperationSystem.StartOperation(PackageName, newDownloader); OperationSystem.StartOperation(PackageName, newDownloader);
@ -254,10 +239,10 @@ namespace YooAsset
PackageName = packageName; PackageName = packageName;
if (string.IsNullOrEmpty(rootDirectory)) if (string.IsNullOrEmpty(rootDirectory))
rootDirectory = GetDefaultRoot(); rootDirectory = CacheHelper.GetDefaultCacheRoot();
_packageRoot = PathUtility.Combine(rootDirectory, packageName); _packageRoot = PathUtility.Combine(rootDirectory, packageName);
_saveFileRoot = PathUtility.Combine(_packageRoot, DefaultCacheFileSystemDefine.SaveFilesFolderName); _cacheFileRoot = PathUtility.Combine(_packageRoot, DefaultCacheFileSystemDefine.SaveFilesFolderName);
_tempFileRoot = PathUtility.Combine(_packageRoot, DefaultCacheFileSystemDefine.TempFilesFolderName); _tempFileRoot = PathUtility.Combine(_packageRoot, DefaultCacheFileSystemDefine.TempFilesFolderName);
_manifestFileRoot = PathUtility.Combine(_packageRoot, DefaultCacheFileSystemDefine.ManifestFilesFolderName); _manifestFileRoot = PathUtility.Combine(_packageRoot, DefaultCacheFileSystemDefine.ManifestFilesFolderName);
} }
@ -372,61 +357,10 @@ namespace YooAsset
} }
} }
#region 内部方法 #region 缓存系统
private readonly BufferWriter _sharedBuffer = new BufferWriter(1024); public string GetCacheFileRoot()
public void WriteInfoFile(string filePath, string dataFileCRC, long dataFileSize)
{ {
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read)) return _cacheFileRoot;
{
_sharedBuffer.Clear();
_sharedBuffer.WriteUTF8(dataFileCRC);
_sharedBuffer.WriteInt64(dataFileSize);
_sharedBuffer.WriteToStream(fs);
fs.Flush();
}
}
public void ReadInfoFile(string filePath, out string dataFileCRC, out long dataFileSize)
{
byte[] binaryData = FileUtility.ReadAllBytes(filePath);
BufferReader buffer = new BufferReader(binaryData);
dataFileCRC = buffer.ReadUTF8();
dataFileSize = buffer.ReadInt64();
}
protected string GetDefaultRoot()
{
#if UNITY_EDITOR
// 注意:为了方便调试查看,编辑器下把存储目录放到项目里。
string projectPath = Path.GetDirectoryName(UnityEngine.Application.dataPath);
projectPath = PathUtility.RegularPath(projectPath);
return PathUtility.Combine(projectPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
#elif UNITY_STANDALONE
return PathUtility.Combine(UnityEngine.Application.dataPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
#else
return PathUtility.Combine(UnityEngine.Application.persistentDataPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
#endif
}
protected string GetDataFilePath(PackageBundle bundle)
{
if (_dataFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false)
{
string folderName = bundle.FileHash.Substring(0, 2);
filePath = PathUtility.Combine(_saveFileRoot, folderName, bundle.BundleGUID, DefaultCacheFileSystemDefine.SaveBundleDataFileName);
if (AppendFileExtension)
filePath += bundle.FileExtension;
_dataFilePaths.Add(bundle.BundleGUID, filePath);
}
return filePath;
}
protected string GetInfoFilePath(PackageBundle bundle)
{
if (_infoFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false)
{
string folderName = bundle.FileHash.Substring(0, 2);
filePath = PathUtility.Combine(_saveFileRoot, folderName, bundle.BundleGUID, DefaultCacheFileSystemDefine.SaveBundleInfoFileName);
_infoFilePaths.Add(bundle.BundleGUID, filePath);
}
return filePath;
} }
public string GetTempFilePath(PackageBundle bundle) public string GetTempFilePath(PackageBundle bundle)
{ {
@ -437,41 +371,38 @@ namespace YooAsset
} }
return filePath; return filePath;
} }
public string GetCacheFileLoadPath(PackageBundle bundle) public string GetDataFilePath(PackageBundle bundle)
{ {
return GetDataFilePath(bundle); if (_dataFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false)
{
string folderName = bundle.FileHash.Substring(0, 2);
filePath = PathUtility.Combine(_cacheFileRoot, folderName, bundle.BundleGUID, DefaultCacheFileSystemDefine.SaveBundleDataFileName);
if (AppendFileExtension)
filePath += bundle.FileExtension;
_dataFilePaths.Add(bundle.BundleGUID, filePath);
}
return filePath;
} }
public string GetCacheFilesRoot() public string GetInfoFilePath(PackageBundle bundle)
{ {
return _saveFileRoot; if (_infoFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false)
{
string folderName = bundle.FileHash.Substring(0, 2);
filePath = PathUtility.Combine(_cacheFileRoot, folderName, bundle.BundleGUID, DefaultCacheFileSystemDefine.SaveBundleInfoFileName);
_infoFilePaths.Add(bundle.BundleGUID, filePath);
}
return filePath;
} }
public string GetCachePackageHashFilePath(string packageVersion) public List<string> GetAllCachedBundleGUIDs()
{ {
string fileName = YooAssetSettingsData.GetPackageHashFileName(PackageName, packageVersion); return _wrappers.Keys.ToList();
return PathUtility.Combine(_manifestFileRoot, fileName);
}
public string GetCachePackageManifestFilePath(string packageVersion)
{
string fileName = YooAssetSettingsData.GetManifestBinaryFileName(PackageName, packageVersion);
return PathUtility.Combine(_manifestFileRoot, fileName);
}
public string GetSandboxAppFootPrintFilePath()
{
return PathUtility.Combine(_manifestFileRoot, DefaultCacheFileSystemDefine.AppFootPrintFileName);
} }
/// <summary>
/// 是否已经记录了文件
/// </summary>
public bool IsRecordFile(string bundleGUID) public bool IsRecordFile(string bundleGUID)
{ {
return _wrappers.ContainsKey(bundleGUID); return _wrappers.ContainsKey(bundleGUID);
} }
public bool RecordFile(string bundleGUID, CacheWrapper wrapper)
/// <summary>
/// 记录文件信息
/// </summary>
public bool RecordFile(string bundleGUID, FileWrapper wrapper)
{ {
if (_wrappers.ContainsKey(bundleGUID)) if (_wrappers.ContainsKey(bundleGUID))
{ {
@ -483,21 +414,14 @@ namespace YooAsset
return true; return true;
} }
/// <summary>
/// 验证缓存文件
/// </summary>
public EFileVerifyResult VerifyCacheFile(PackageBundle bundle) public EFileVerifyResult VerifyCacheFile(PackageBundle bundle)
{ {
if (_wrappers.TryGetValue(bundle.BundleGUID, out FileWrapper wrapper) == false) if (_wrappers.TryGetValue(bundle.BundleGUID, out CacheWrapper wrapper) == false)
return EFileVerifyResult.CacheNotFound; return EFileVerifyResult.CacheNotFound;
EFileVerifyResult result = FileSystemHelper.FileVerify(wrapper.DataFilePath, wrapper.DataFileSize, wrapper.DataFileCRC, EFileVerifyLevel.High); EFileVerifyResult result = FileVerifyHelper.FileVerify(wrapper.DataFilePath, wrapper.DataFileSize, wrapper.DataFileCRC, EFileVerifyLevel.High);
return result; return result;
} }
/// <summary>
/// 写入缓存文件
/// </summary>
public bool WriteCacheFile(PackageBundle bundle, string copyPath) public bool WriteCacheFile(PackageBundle bundle, string copyPath)
{ {
if (_wrappers.ContainsKey(bundle.BundleGUID)) if (_wrappers.ContainsKey(bundle.BundleGUID))
@ -530,16 +454,12 @@ namespace YooAsset
return false; return false;
} }
FileWrapper wrapper = new FileWrapper(infoFilePath, dataFilePath, bundle.FileCRC, bundle.FileSize); var wrapper = new CacheWrapper(infoFilePath, dataFilePath, bundle.FileCRC, bundle.FileSize);
return RecordFile(bundle.BundleGUID, wrapper); return RecordFile(bundle.BundleGUID, wrapper);
} }
/// <summary>
/// 删除缓存文件
/// </summary>
public bool DeleteCacheFile(string bundleGUID) public bool DeleteCacheFile(string bundleGUID)
{ {
if (_wrappers.TryGetValue(bundleGUID, out FileWrapper wrapper)) if (_wrappers.TryGetValue(bundleGUID, out CacheWrapper wrapper))
{ {
try try
{ {
@ -562,6 +482,47 @@ namespace YooAsset
} }
} }
private readonly BufferWriter _sharedBuffer = new BufferWriter(1024);
public void WriteInfoFile(string filePath, string dataFileCRC, long dataFileSize)
{
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read))
{
_sharedBuffer.Clear();
_sharedBuffer.WriteUTF8(dataFileCRC);
_sharedBuffer.WriteInt64(dataFileSize);
_sharedBuffer.WriteToStream(fs);
fs.Flush();
}
}
public void ReadInfoFile(string filePath, out string dataFileCRC, out long dataFileSize)
{
byte[] binaryData = FileUtility.ReadAllBytes(filePath);
BufferReader buffer = new BufferReader(binaryData);
dataFileCRC = buffer.ReadUTF8();
dataFileSize = buffer.ReadInt64();
}
#endregion
#region 内部方法
public string GetCacheFileLoadPath(PackageBundle bundle)
{
return GetDataFilePath(bundle);
}
public string GetCachePackageHashFilePath(string packageVersion)
{
string fileName = YooAssetSettingsData.GetPackageHashFileName(PackageName, packageVersion);
return PathUtility.Combine(_manifestFileRoot, fileName);
}
public string GetCachePackageManifestFilePath(string packageVersion)
{
string fileName = YooAssetSettingsData.GetManifestBinaryFileName(PackageName, packageVersion);
return PathUtility.Combine(_manifestFileRoot, fileName);
}
public string GetSandboxAppFootPrintFilePath()
{
return PathUtility.Combine(_manifestFileRoot, DefaultCacheFileSystemDefine.AppFootPrintFileName);
}
/// <summary> /// <summary>
/// 删除所有清单文件 /// 删除所有清单文件
/// </summary> /// </summary>
@ -573,14 +534,6 @@ namespace YooAsset
} }
} }
/// <summary>
/// 获取所有缓存文件GUID
/// </summary>
public List<string> GetAllCachedBundleGUIDs()
{
return _wrappers.Keys.ToList();
}
/// <summary> /// <summary>
/// 加载加密资源文件 /// 加载加密资源文件
/// </summary> /// </summary>

View File

@ -12,7 +12,7 @@ namespace YooAsset
Done, Done,
} }
private readonly DefaultCacheFileSystem _fileSytem; private readonly DefaultCacheFileSystem _fileSystem;
private SearchCacheFilesOperation _searchCacheFilesOp; private SearchCacheFilesOperation _searchCacheFilesOp;
private VerifyCacheFilesOperation _verifyCacheFilesOp; private VerifyCacheFilesOperation _verifyCacheFilesOp;
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
@ -20,7 +20,7 @@ namespace YooAsset
internal DCFSInitializeOperation(DefaultCacheFileSystem fileSystem) internal DCFSInitializeOperation(DefaultCacheFileSystem fileSystem)
{ {
_fileSytem = fileSystem; _fileSystem = fileSystem;
} }
internal override void InternalOnStart() internal override void InternalOnStart()
{ {
@ -39,14 +39,14 @@ namespace YooAsset
if (_steps == ESteps.CheckAppFootPrint) if (_steps == ESteps.CheckAppFootPrint)
{ {
var appFootPrint = new ApplicationFootPrint(_fileSytem); var appFootPrint = new ApplicationFootPrint(_fileSystem);
appFootPrint.Load(_fileSytem.PackageName); appFootPrint.Load(_fileSystem.PackageName);
// 如果水印发生变化,则说明覆盖安装后首次打开游戏 // 如果水印发生变化,则说明覆盖安装后首次打开游戏
if (appFootPrint.IsDirty()) if (appFootPrint.IsDirty())
{ {
_fileSytem.DeleteAllManifestFiles(); _fileSystem.DeleteAllManifestFiles();
appFootPrint.Coverage(_fileSytem.PackageName); appFootPrint.Coverage(_fileSystem.PackageName);
YooLogger.Warning("Delete manifest files when application foot print dirty !"); YooLogger.Warning("Delete manifest files when application foot print dirty !");
} }
@ -57,8 +57,8 @@ namespace YooAsset
{ {
if (_searchCacheFilesOp == null) if (_searchCacheFilesOp == null)
{ {
_searchCacheFilesOp = new SearchCacheFilesOperation(_fileSytem); _searchCacheFilesOp = new SearchCacheFilesOperation(_fileSystem, _fileSystem.PackageName, _fileSystem.AppendFileExtension);
OperationSystem.StartOperation(_fileSytem.PackageName, _searchCacheFilesOp); OperationSystem.StartOperation(_fileSystem.PackageName, _searchCacheFilesOp);
} }
Progress = _searchCacheFilesOp.Progress; Progress = _searchCacheFilesOp.Progress;
@ -72,8 +72,8 @@ namespace YooAsset
{ {
if (_verifyCacheFilesOp == null) if (_verifyCacheFilesOp == null)
{ {
_verifyCacheFilesOp = new VerifyCacheFilesOperation(_fileSytem, _searchCacheFilesOp.Result); _verifyCacheFilesOp = new VerifyCacheFilesOperation(_fileSystem, _fileSystem.FileVerifyLevel, _searchCacheFilesOp.Result);
OperationSystem.StartOperation(_fileSytem.PackageName, _verifyCacheFilesOp); OperationSystem.StartOperation(_fileSystem.PackageName, _verifyCacheFilesOp);
} }
Progress = _verifyCacheFilesOp.Progress; Progress = _verifyCacheFilesOp.Progress;
@ -84,7 +84,7 @@ namespace YooAsset
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Succeed; Status = EOperationStatus.Succeed;
YooLogger.Log($"Package '{_fileSytem.PackageName}' cached files count : {_fileSytem.FileCount}"); YooLogger.Log($"Package '{_fileSystem.PackageName}' cached files count : {_fileSystem.FileCount}");
} }
else else
{ {

View File

@ -14,7 +14,7 @@ namespace YooAsset
base.OnCreate(packageName, rootDirectory); base.OnCreate(packageName, rootDirectory);
// 注意:重写保存根目录和临时目录 // 注意:重写保存根目录和临时目录
_saveFileRoot = PathUtility.Combine(_packageRoot, DefaultUnpackFileSystemDefine.SaveFilesFolderName); _cacheFileRoot = PathUtility.Combine(_packageRoot, DefaultUnpackFileSystemDefine.SaveFilesFolderName);
_tempFileRoot = PathUtility.Combine(_packageRoot, DefaultUnpackFileSystemDefine.TempFilesFolderName); _tempFileRoot = PathUtility.Combine(_packageRoot, DefaultUnpackFileSystemDefine.TempFilesFolderName);
} }
} }

View File

@ -0,0 +1,34 @@
using System.Collections.Generic;
namespace YooAsset
{
internal class DefaultUnpackRemoteServices : IRemoteServices
{
private readonly string _buildinPackageRoot;
protected readonly Dictionary<string, string> _mapping = new Dictionary<string, string>(10000);
public DefaultUnpackRemoteServices(string buildinPackRoot)
{
_buildinPackageRoot = buildinPackRoot;
}
string IRemoteServices.GetRemoteMainURL(string fileName)
{
return GetFileLoadURL(fileName);
}
string IRemoteServices.GetRemoteFallbackURL(string fileName)
{
return GetFileLoadURL(fileName);
}
private string GetFileLoadURL(string fileName)
{
if (_mapping.TryGetValue(fileName, out string url) == false)
{
string filePath = PathUtility.Combine(_buildinPackageRoot, fileName);
url = DownloadSystemHelper.ConvertToWWWPath(filePath);
_mapping.Add(fileName, url);
}
return url;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 32c355598c9dff847af87f4a867a2d5c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -3,7 +3,7 @@ using System.IO;
namespace YooAsset namespace YooAsset
{ {
internal class FileSystemHelper internal class FileVerifyHelper
{ {
/// <summary> /// <summary>
/// 文件校验 /// 文件校验

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: db3336ad3b5a8f349a3144e77f23fcf8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -210,10 +210,24 @@ namespace YooAsset
return BundleDic3.ContainsKey(bundleGUID); return BundleDic3.ContainsKey(bundleGUID);
} }
/// <summary>
/// 获取所有的资源信息
/// </summary>
public AssetInfo[] GetAllAssetInfos()
{
List<AssetInfo> result = new List<AssetInfo>(AssetList.Count);
foreach (var packageAsset in AssetList)
{
AssetInfo assetInfo = new AssetInfo(PackageName, packageAsset, null);
result.Add(assetInfo);
}
return result.ToArray();
}
/// <summary> /// <summary>
/// 获取资源信息列表 /// 获取资源信息列表
/// </summary> /// </summary>
public AssetInfo[] GetAssetsInfoByTags(string[] tags) public AssetInfo[] GetAssetInfosByTags(string[] tags)
{ {
List<AssetInfo> result = new List<AssetInfo>(100); List<AssetInfo> result = new List<AssetInfo>(100);
foreach (var packageAsset in AssetList) foreach (var packageAsset in AssetList)

View File

@ -176,26 +176,6 @@ namespace YooAsset
} }
#endif #endif
} }
// 检测文件系统参数
if (_playMode == EPlayMode.WebPlayMode)
{
var webPlayModeParams = parameters as WebPlayModeParameters;
if (webPlayModeParams.WebServerFileSystemParameters != null)
{
var fileSystemClassName = webPlayModeParams.WebServerFileSystemParameters.FileSystemClass;
if (fileSystemClassName == typeof(DefaultCacheFileSystem).FullName
|| fileSystemClassName == typeof(DefaultBuildinFileSystem).FullName)
throw new Exception($"{fileSystemClassName} not support {nameof(EPlayMode.WebPlayMode)}");
}
if (webPlayModeParams.WebRemoteFileSystemParameters != null)
{
var fileSystemClassName = webPlayModeParams.WebRemoteFileSystemParameters.FileSystemClass;
if (fileSystemClassName == typeof(DefaultCacheFileSystem).FullName
|| fileSystemClassName == typeof(DefaultBuildinFileSystem).FullName)
throw new Exception($"{fileSystemClassName} not support {nameof(EPlayMode.WebPlayMode)}");
}
}
} }
private void InitializeOperation_Completed(AsyncOperationBase op) private void InitializeOperation_Completed(AsyncOperationBase op)
{ {
@ -359,6 +339,15 @@ namespace YooAsset
return IsNeedDownloadFromRemoteInternal(assetInfo); return IsNeedDownloadFromRemoteInternal(assetInfo);
} }
/// <summary>
/// 获取所有的资源信息
/// </summary>
public AssetInfo[] GetAllAssetInfos()
{
DebugCheckInitialize();
return _playModeImpl.ActiveManifest.GetAllAssetInfos();
}
/// <summary> /// <summary>
/// 获取资源信息列表 /// 获取资源信息列表
/// </summary> /// </summary>
@ -367,7 +356,7 @@ namespace YooAsset
{ {
DebugCheckInitialize(); DebugCheckInitialize();
string[] tags = new string[] { tag }; string[] tags = new string[] { tag };
return _playModeImpl.ActiveManifest.GetAssetsInfoByTags(tags); return _playModeImpl.ActiveManifest.GetAssetInfosByTags(tags);
} }
/// <summary> /// <summary>
@ -377,7 +366,7 @@ namespace YooAsset
public AssetInfo[] GetAssetInfos(string[] tags) public AssetInfo[] GetAssetInfos(string[] tags)
{ {
DebugCheckInitialize(); DebugCheckInitialize();
return _playModeImpl.ActiveManifest.GetAssetsInfoByTags(tags); return _playModeImpl.ActiveManifest.GetAssetInfosByTags(tags);
} }
/// <summary> /// <summary>

View File

@ -3,7 +3,7 @@ using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using YooAsset; using YooAsset;
public static class AssetOperationHandleExtension public static class AssetHandleExtension
{ {
/// <summary> /// <summary>
/// 等待异步执行完毕 /// 等待异步执行完毕

View File

@ -0,0 +1,159 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Security.Policy;
using UnityEngine;
using YooAsset;
/// <summary>
/// 拷贝内置清单文件到沙盒目录
/// </summary>
public class CopyBuildinManifestOperation : GameAsyncOperation
{
private enum ESteps
{
None,
CheckHashFile,
UnpackHashFile,
CheckManifestFile,
UnpackManifestFile,
Done,
}
private readonly string _packageName;
private readonly string _packageVersion;
private ESteps _steps = ESteps.None;
private UnityWebFileRequestOperation _hashFileRequestOp;
private UnityWebFileRequestOperation _manifestFileRequestOp;
public CopyBuildinManifestOperation(string packageName, string packageVersion)
{
_packageName = packageName;
_packageVersion = packageVersion;
}
protected override void OnStart()
{
_steps = ESteps.CheckHashFile;
}
protected override void OnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.CheckHashFile)
{
string hashFilePath = GetCacheHashFilePath();
if (File.Exists(hashFilePath))
{
_steps = ESteps.CheckManifestFile;
return;
}
_steps = ESteps.UnpackHashFile;
}
if (_steps == ESteps.UnpackHashFile)
{
if(_hashFileRequestOp == null)
{
string sourcePath = GetBuildinHashFilePath();
string destPath = GetCacheHashFilePath();
string url = DownloadSystemHelper.ConvertToWWWPath(sourcePath);
_hashFileRequestOp = new UnityWebFileRequestOperation(url, destPath);
OperationSystem.StartOperation(_packageName, _hashFileRequestOp);
}
if (_hashFileRequestOp.IsDone == false)
return;
if (_hashFileRequestOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.CheckManifestFile;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _hashFileRequestOp.Error;
}
}
if (_steps == ESteps.CheckManifestFile)
{
string manifestFilePath = GetCacheManifestFilePath();
if (File.Exists(manifestFilePath))
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
return;
}
_steps = ESteps.UnpackManifestFile;
}
if (_steps == ESteps.UnpackManifestFile)
{
if (_manifestFileRequestOp == null)
{
string sourcePath = GetBuildinManifestFilePath();
string destPath = GetCacheManifestFilePath();
string url = DownloadSystemHelper.ConvertToWWWPath(sourcePath);
_manifestFileRequestOp = new UnityWebFileRequestOperation(url, destPath);
OperationSystem.StartOperation(_packageName, _manifestFileRequestOp);
}
if (_manifestFileRequestOp.IsDone == false)
return;
if (_manifestFileRequestOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _manifestFileRequestOp.Error;
}
}
}
protected override void OnAbort()
{
}
private string GetBuildinYooRoot()
{
return PathUtility.Combine(Application.streamingAssetsPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
}
private string GetBuildinHashFilePath()
{
string fileRoot = GetBuildinYooRoot();
string fileName = YooAssetSettingsData.GetPackageHashFileName(_packageName, _packageVersion);
return PathUtility.Combine(fileRoot, fileName);
}
private string GetBuildinManifestFilePath()
{
string fileRoot = GetBuildinYooRoot();
string fileName = YooAssetSettingsData.GetManifestBinaryFileName(_packageName, _packageVersion);
return PathUtility.Combine(fileRoot, fileName);
}
private string GetCacheYooRoot()
{
return PathUtility.Combine(UnityEngine.Application.persistentDataPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
}
private string GetCacheHashFilePath()
{
string fileRoot = GetCacheYooRoot();
string fileName = YooAssetSettingsData.GetPackageHashFileName(_packageName, _packageVersion);
return PathUtility.Combine(fileRoot, fileName);
}
private string GetCacheManifestFilePath()
{
string fileRoot = GetCacheYooRoot();
string fileName = YooAssetSettingsData.GetManifestBinaryFileName(_packageName, _packageVersion);
return PathUtility.Combine(fileRoot, fileName);
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 41ab464f315df234fb40b0c24e97ee23
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: