refactor : cache file system

pull/332/merge
何冠峰 2024-12-12 15:00:08 +08:00
parent bf1e3da298
commit b1338a9ffd
23 changed files with 336 additions and 221 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

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

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,52 @@
using System.Collections.Generic;
namespace YooAsset
{
internal interface ICacheSystem
{
/// <summary>
/// 获取缓存文件的根目录
/// </summary>
string GetCacheFileRoot();
/// <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

@ -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)

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)

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,26 +10,10 @@ namespace YooAsset
/// 缓存文件系统 /// 缓存文件系统
/// 说明正在进行的下载器会在ResourcePackage销毁的时候执行Abort操作 /// 说明正在进行的下载器会在ResourcePackage销毁的时候执行Abort操作
/// </summary> /// </summary>
internal class DefaultCacheFileSystem : IFileSystem internal class DefaultCacheFileSystem : IFileSystem, ICacheSystem
{ {
public class FileWrapper
{
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, DefaultDownloadFileOperation> _downloaders = new Dictionary<string, DefaultDownloadFileOperation>(1000);
protected readonly Dictionary<string, FileWrapper> _wrappers = new Dictionary<string, FileWrapper>(10000); protected readonly Dictionary<string, CacheWrapper> _wrappers = new Dictionary<string, CacheWrapper>(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);
@ -128,13 +112,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;
} }
@ -372,7 +356,100 @@ namespace YooAsset
} }
} }
#region 内部方法 #region 缓存系统
public string GetCacheFileRoot()
{
return _saveFileRoot;
}
public List<string> GetAllCachedBundleGUIDs()
{
return _wrappers.Keys.ToList();
}
public bool IsRecordFile(string bundleGUID)
{
return _wrappers.ContainsKey(bundleGUID);
}
public bool RecordFile(string bundleGUID, CacheWrapper wrapper)
{
if (_wrappers.ContainsKey(bundleGUID))
{
YooLogger.Error($"{nameof(DefaultCacheFileSystem)} has element : {bundleGUID}");
return false;
}
_wrappers.Add(bundleGUID, wrapper);
return true;
}
public EFileVerifyResult VerifyCacheFile(PackageBundle bundle)
{
if (_wrappers.TryGetValue(bundle.BundleGUID, out CacheWrapper wrapper) == false)
return EFileVerifyResult.CacheNotFound;
EFileVerifyResult result = FileSystemHelper.FileVerify(wrapper.DataFilePath, wrapper.DataFileSize, wrapper.DataFileCRC, EFileVerifyLevel.High);
return result;
}
public bool WriteCacheFile(PackageBundle bundle, string copyPath)
{
if (_wrappers.ContainsKey(bundle.BundleGUID))
{
throw new Exception("Should never get here !");
}
string infoFilePath = GetInfoFilePath(bundle);
string dataFilePath = GetDataFilePath(bundle);
try
{
if (File.Exists(infoFilePath))
File.Delete(infoFilePath);
if (File.Exists(dataFilePath))
File.Delete(dataFilePath);
FileUtility.CreateFileDirectory(dataFilePath);
// 拷贝数据文件
FileInfo fileInfo = new FileInfo(copyPath);
fileInfo.CopyTo(dataFilePath);
// 写入文件信息
WriteInfoFile(infoFilePath, bundle.FileCRC, bundle.FileSize);
}
catch (Exception e)
{
YooLogger.Error($"Failed to write cache file ! {e.Message}");
return false;
}
var wrapper = new CacheWrapper(infoFilePath, dataFilePath, bundle.FileCRC, bundle.FileSize);
return RecordFile(bundle.BundleGUID, wrapper);
}
public bool DeleteCacheFile(string bundleGUID)
{
if (_wrappers.TryGetValue(bundleGUID, out CacheWrapper wrapper))
{
try
{
string dataFilePath = wrapper.DataFilePath;
FileInfo fileInfo = new FileInfo(dataFilePath);
if (fileInfo.Exists)
fileInfo.Directory.Delete(true);
_wrappers.Remove(bundleGUID);
return true;
}
catch (Exception e)
{
YooLogger.Error($"Failed to delete cache file ! {e.Message}");
return false;
}
}
else
{
return false;
}
}
private readonly BufferWriter _sharedBuffer = new BufferWriter(1024); private readonly BufferWriter _sharedBuffer = new BufferWriter(1024);
public void WriteInfoFile(string filePath, string dataFileCRC, long dataFileSize) public void WriteInfoFile(string filePath, string dataFileCRC, long dataFileSize)
{ {
@ -392,7 +469,9 @@ namespace YooAsset
dataFileCRC = buffer.ReadUTF8(); dataFileCRC = buffer.ReadUTF8();
dataFileSize = buffer.ReadInt64(); dataFileSize = buffer.ReadInt64();
} }
#endregion
#region 内部方法
protected string GetDefaultRoot() protected string GetDefaultRoot()
{ {
#if UNITY_EDITOR #if UNITY_EDITOR
@ -441,10 +520,6 @@ namespace YooAsset
{ {
return GetDataFilePath(bundle); return GetDataFilePath(bundle);
} }
public string GetCacheFilesRoot()
{
return _saveFileRoot;
}
public string GetCachePackageHashFilePath(string packageVersion) public string GetCachePackageHashFilePath(string packageVersion)
{ {
string fileName = YooAssetSettingsData.GetPackageHashFileName(PackageName, packageVersion); string fileName = YooAssetSettingsData.GetPackageHashFileName(PackageName, packageVersion);
@ -460,108 +535,6 @@ namespace YooAsset
return PathUtility.Combine(_manifestFileRoot, DefaultCacheFileSystemDefine.AppFootPrintFileName); return PathUtility.Combine(_manifestFileRoot, DefaultCacheFileSystemDefine.AppFootPrintFileName);
} }
/// <summary>
/// 是否已经记录了文件
/// </summary>
public bool IsRecordFile(string bundleGUID)
{
return _wrappers.ContainsKey(bundleGUID);
}
/// <summary>
/// 记录文件信息
/// </summary>
public bool RecordFile(string bundleGUID, FileWrapper wrapper)
{
if (_wrappers.ContainsKey(bundleGUID))
{
YooLogger.Error($"{nameof(DefaultCacheFileSystem)} has element : {bundleGUID}");
return false;
}
_wrappers.Add(bundleGUID, wrapper);
return true;
}
/// <summary>
/// 验证缓存文件
/// </summary>
public EFileVerifyResult VerifyCacheFile(PackageBundle bundle)
{
if (_wrappers.TryGetValue(bundle.BundleGUID, out FileWrapper wrapper) == false)
return EFileVerifyResult.CacheNotFound;
EFileVerifyResult result = FileSystemHelper.FileVerify(wrapper.DataFilePath, wrapper.DataFileSize, wrapper.DataFileCRC, EFileVerifyLevel.High);
return result;
}
/// <summary>
/// 写入缓存文件
/// </summary>
public bool WriteCacheFile(PackageBundle bundle, string copyPath)
{
if (_wrappers.ContainsKey(bundle.BundleGUID))
{
throw new Exception("Should never get here !");
}
string infoFilePath = GetInfoFilePath(bundle);
string dataFilePath = GetDataFilePath(bundle);
try
{
if (File.Exists(infoFilePath))
File.Delete(infoFilePath);
if (File.Exists(dataFilePath))
File.Delete(dataFilePath);
FileUtility.CreateFileDirectory(dataFilePath);
// 拷贝数据文件
FileInfo fileInfo = new FileInfo(copyPath);
fileInfo.CopyTo(dataFilePath);
// 写入文件信息
WriteInfoFile(infoFilePath, bundle.FileCRC, bundle.FileSize);
}
catch (Exception e)
{
YooLogger.Error($"Failed to write cache file ! {e.Message}");
return false;
}
FileWrapper wrapper = new FileWrapper(infoFilePath, dataFilePath, bundle.FileCRC, bundle.FileSize);
return RecordFile(bundle.BundleGUID, wrapper);
}
/// <summary>
/// 删除缓存文件
/// </summary>
public bool DeleteCacheFile(string bundleGUID)
{
if (_wrappers.TryGetValue(bundleGUID, out FileWrapper wrapper))
{
try
{
string dataFilePath = wrapper.DataFilePath;
FileInfo fileInfo = new FileInfo(dataFilePath);
if (fileInfo.Exists)
fileInfo.Directory.Delete(true);
_wrappers.Remove(bundleGUID);
return true;
}
catch (Exception e)
{
YooLogger.Error($"Failed to delete cache file ! {e.Message}");
return false;
}
}
else
{
return false;
}
}
/// <summary> /// <summary>
/// 删除所有清单文件 /// 删除所有清单文件
/// </summary> /// </summary>
@ -573,14 +546,6 @@ namespace YooAsset
} }
} }
/// <summary>
/// 获取所有缓存文件GUID
/// </summary>
public List<string> GetAllCachedBundleGUIDs()
{
return _wrappers.Keys.ToList();
}
/// <summary> /// <summary>
/// 加载加密资源文件 /// 加载加密资源文件
/// </summary> /// </summary>

View File

@ -57,7 +57,7 @@ namespace YooAsset
{ {
if (_searchCacheFilesOp == null) if (_searchCacheFilesOp == null)
{ {
_searchCacheFilesOp = new SearchCacheFilesOperation(_fileSytem); _searchCacheFilesOp = new SearchCacheFilesOperation(_fileSytem, _fileSytem.PackageName, _fileSytem.AppendFileExtension);
OperationSystem.StartOperation(_fileSytem.PackageName, _searchCacheFilesOp); OperationSystem.StartOperation(_fileSytem.PackageName, _searchCacheFilesOp);
} }
@ -72,7 +72,7 @@ namespace YooAsset
{ {
if (_verifyCacheFilesOp == null) if (_verifyCacheFilesOp == null)
{ {
_verifyCacheFilesOp = new VerifyCacheFilesOperation(_fileSytem, _searchCacheFilesOp.Result); _verifyCacheFilesOp = new VerifyCacheFilesOperation(_fileSytem, _fileSytem.FileVerifyLevel, _searchCacheFilesOp.Result);
OperationSystem.StartOperation(_fileSytem.PackageName, _verifyCacheFilesOp); OperationSystem.StartOperation(_fileSytem.PackageName, _verifyCacheFilesOp);
} }