diff --git a/Assets/YooAsset/Runtime/CacheSystem/CacheFileInfo.cs b/Assets/YooAsset/Runtime/CacheSystem/CacheFileInfo.cs index bdf1383c..0830053d 100644 --- a/Assets/YooAsset/Runtime/CacheSystem/CacheFileInfo.cs +++ b/Assets/YooAsset/Runtime/CacheSystem/CacheFileInfo.cs @@ -1,36 +1,19 @@ -using System; -using System.IO; - + namespace YooAsset { - internal class CacheFileInfo + public class CacheFileInfo { - private static readonly BufferWriter SharedBuffer = new BufferWriter(1024); + public string RemoteFileName { private set; get; } + public string FilePath { private set; get; } + public string FileCRC { private set; get; } + public long FileSize { private set; get; } - /// - /// 写入资源包信息 - /// - public static void WriteInfoToFile(string filePath, string dataFileCRC, long dataFileSize) + public CacheFileInfo(string remoteFileName, string filePath, string fileCRC, long fileSize) { - 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 static void ReadInfoFromFile(string filePath, out string dataFileCRC, out long dataFileSize) - { - byte[] binaryData = FileUtility.ReadAllBytes(filePath); - BufferReader buffer = new BufferReader(binaryData); - dataFileCRC = buffer.ReadUTF8(); - dataFileSize = buffer.ReadInt64(); + RemoteFileName = remoteFileName; + FilePath = filePath; + FileCRC = fileCRC; + FileSize = fileSize; } } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/CacheSystem/CacheHelper.cs b/Assets/YooAsset/Runtime/CacheSystem/CacheHelper.cs index 32427088..7bed804c 100644 --- a/Assets/YooAsset/Runtime/CacheSystem/CacheHelper.cs +++ b/Assets/YooAsset/Runtime/CacheSystem/CacheHelper.cs @@ -1,8 +1,5 @@ using System; using System.IO; -using System.Collections; -using System.Collections.Generic; -using System.Diagnostics; namespace YooAsset { @@ -13,6 +10,37 @@ namespace YooAsset /// public static bool DisableUnityCacheOnWebGL = false; + #region 资源信息文件相关 + private static readonly BufferWriter SharedBuffer = new BufferWriter(1024); + + /// + /// 写入资源包信息 + /// + public static void WriteInfoToFile(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 static void ReadInfoFromFile(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 资源文件验证相关 /// /// 验证缓存文件(子线程内操作) /// @@ -34,7 +62,7 @@ namespace YooAsset return EVerifyResult.InfoFileNotExisted; // 解析信息文件获取验证数据 - CacheFileInfo.ReadInfoFromFile(element.InfoFilePath, out element.DataFileCRC, out element.DataFileSize); + CacheHelper.ReadInfoFromFile(element.InfoFilePath, out element.DataFileCRC, out element.DataFileSize); } } catch (Exception) @@ -99,5 +127,6 @@ namespace YooAsset return EVerifyResult.Exception; } } + #endregion } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/CacheSystem/Operation/GetAllCacheFileInfosOperation.cs b/Assets/YooAsset/Runtime/CacheSystem/Operation/GetAllCacheFileInfosOperation.cs new file mode 100644 index 00000000..b256812b --- /dev/null +++ b/Assets/YooAsset/Runtime/CacheSystem/Operation/GetAllCacheFileInfosOperation.cs @@ -0,0 +1,99 @@ +using System; +using System.IO; +using System.Collections; +using System.Collections.Generic; + +namespace YooAsset +{ + public class GetAllCacheFileInfosOperation : AsyncOperationBase + { + private enum ESteps + { + None, + TryLoadCacheManifest, + GetCacheFileInfos, + Done, + } + + private readonly PersistentManager _persistent; + private readonly CacheManager _cache; + private readonly string _packageVersion; + private LoadCacheManifestOperation _tryLoadCacheManifestOp; + private PackageManifest _manifest; + private ESteps _steps = ESteps.None; + + private List _cacheFileInfos; + + /// + /// 搜索结果 + /// + public List Result + { + get { return _cacheFileInfos; } + } + + + internal GetAllCacheFileInfosOperation(PersistentManager persistent, CacheManager cache, string packageVersion) + { + _persistent = persistent; + _cache = cache; + _packageVersion = packageVersion; + } + internal override void InternalOnStart() + { + _steps = ESteps.TryLoadCacheManifest; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; + + if (_steps == ESteps.TryLoadCacheManifest) + { + if (_tryLoadCacheManifestOp == null) + { + _tryLoadCacheManifestOp = new LoadCacheManifestOperation(_persistent, _packageVersion); + OperationSystem.StartOperation(_cache.PackageName, _tryLoadCacheManifestOp); + } + + if (_tryLoadCacheManifestOp.IsDone == false) + return; + + if (_tryLoadCacheManifestOp.Status == EOperationStatus.Succeed) + { + _manifest = _tryLoadCacheManifestOp.Manifest; + _steps = ESteps.GetCacheFileInfos; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _tryLoadCacheManifestOp.Error; + } + } + + if (_steps == ESteps.GetCacheFileInfos) + { + var allCachedGUIDs = _cache.GetAllCachedGUIDs(); + _cacheFileInfos = new List(allCachedGUIDs.Count); + for (int i = 0; i < allCachedGUIDs.Count; i++) + { + var cachedGUID = allCachedGUIDs[i]; + var wrapper = _cache.TryGetWrapper(cachedGUID); + if (wrapper != null) + { + if (_manifest.TryGetPackageBundleByCacheGUID(cachedGUID, out var packageBundle)) + { + var cacheFileInfo = new CacheFileInfo(packageBundle.FileName, wrapper.DataFilePath, wrapper.DataFileCRC, wrapper.DataFileSize); + _cacheFileInfos.Add(cacheFileInfo); + } + } + } + + // 注意:总是返回成功 + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/CacheSystem/Operation/GetAllCacheFileInfosOperation.cs.meta b/Assets/YooAsset/Runtime/CacheSystem/Operation/GetAllCacheFileInfosOperation.cs.meta new file mode 100644 index 00000000..d029471d --- /dev/null +++ b/Assets/YooAsset/Runtime/CacheSystem/Operation/GetAllCacheFileInfosOperation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d37e37f5d78ddf8468adcf2dff1edfbb +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs index f7a2b995..c84f2653 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs @@ -207,7 +207,7 @@ namespace YooAsset fileInfo.MoveTo(dataFilePath); // 写入信息文件记录验证数据 - CacheFileInfo.WriteInfoToFile(infoFilePath, dataFileCRC, dataFileSize); + CacheHelper.WriteInfoToFile(infoFilePath, dataFileCRC, dataFileSize); // 记录缓存文件 _bundleInfo.CacheRecord(); diff --git a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/DeserializeManifestOperation.cs b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/DeserializeManifestOperation.cs index b1d062f0..21fcc6c2 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/DeserializeManifestOperation.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/DeserializeManifestOperation.cs @@ -190,6 +190,7 @@ namespace YooAsset Manifest.BundleList = new List(_packageBundleCount); Manifest.BundleDic1 = new Dictionary(_packageBundleCount); Manifest.BundleDic2 = new Dictionary(_packageBundleCount); + Manifest.BundleDic3 = new Dictionary(_packageBundleCount); _progressTotalValue = _packageBundleCount; _steps = ESteps.DeserializeBundleList; } @@ -212,8 +213,8 @@ namespace YooAsset Manifest.BundleDic2.Add(packageBundle.FileName, packageBundle); // 注意:原始文件可能存在相同的CacheGUID - if (Manifest.CacheGUIDs.Contains(packageBundle.CacheGUID) == false) - Manifest.CacheGUIDs.Add(packageBundle.CacheGUID); + if (Manifest.BundleDic3.ContainsKey(packageBundle.CacheGUID) == false) + Manifest.BundleDic3.Add(packageBundle.CacheGUID, packageBundle); _packageBundleCount--; Progress = 1f - _packageBundleCount / _progressTotalValue; diff --git a/Assets/YooAsset/Runtime/ResourcePackage/Operation/PreDownloadContentOperation.cs b/Assets/YooAsset/Runtime/ResourcePackage/Operation/PreDownloadContentOperation.cs index bab304bd..c2286328 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/Operation/PreDownloadContentOperation.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/Operation/PreDownloadContentOperation.cs @@ -134,7 +134,6 @@ namespace YooAsset TryLoadCacheManifest, DownloadManifest, LoadCacheManifest, - CheckDeserializeManifest, Done, } diff --git a/Assets/YooAsset/Runtime/ResourcePackage/PackageManifest.cs b/Assets/YooAsset/Runtime/ResourcePackage/PackageManifest.cs index a9df1c74..53a52fe7 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/PackageManifest.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/PackageManifest.cs @@ -75,6 +75,12 @@ namespace YooAsset [NonSerialized] public Dictionary BundleDic2; + /// + /// 资源包集合(提供CacheGUID获取PackageBundle) + /// + [NonSerialized] + public Dictionary BundleDic3; + /// /// 资源映射集合(提供AssetPath获取PackageAsset) /// @@ -93,12 +99,6 @@ namespace YooAsset [NonSerialized] public Dictionary AssetPathMapping2; - /// - /// 该资源清单所有文件的缓存GUID集合 - /// - [NonSerialized] - public HashSet CacheGUIDs = new HashSet(); - /// /// 尝试映射为资源路径 @@ -189,12 +189,20 @@ namespace YooAsset return BundleDic2.TryGetValue(fileName, out result); } + /// + /// 尝试获取包裹的资源包 + /// + public bool TryGetPackageBundleByCacheGUID(string cacheGUID, out PackageBundle result) + { + return BundleDic3.TryGetValue(cacheGUID, out result); + } + /// /// 是否包含资源文件 /// public bool IsIncludeBundleFile(string cacheGUID) { - return CacheGUIDs.Contains(cacheGUID); + return BundleDic3.ContainsKey(cacheGUID); } /// diff --git a/Assets/YooAsset/Runtime/ResourcePackage/ResourcePackage.cs b/Assets/YooAsset/Runtime/ResourcePackage/ResourcePackage.cs index d3b9394a..6b668412 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/ResourcePackage.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/ResourcePackage.cs @@ -314,28 +314,6 @@ namespace YooAsset return _playModeImpl.PreDownloadContentAsync(packageVersion, timeout); } - /// - /// 清理包裹未使用的缓存文件 - /// - public ClearUnusedCacheFilesOperation ClearUnusedCacheFilesAsync() - { - DebugCheckInitialize(); - var operation = new ClearUnusedCacheFilesOperation(this, _cacheMgr); - OperationSystem.StartOperation(PackageName, operation); - return operation; - } - - /// - /// 清理包裹本地所有的缓存文件 - /// - public ClearAllCacheFilesOperation ClearAllCacheFilesAsync() - { - DebugCheckInitialize(); - var operation = new ClearAllCacheFilesOperation(_cacheMgr); - OperationSystem.StartOperation(PackageName, operation); - return operation; - } - /// /// 获取本地包裹的版本信息 /// @@ -412,6 +390,37 @@ namespace YooAsset _persistentMgr.DeleteSandboxPackageFolder(); _cacheMgr.ClearAll(); } + + /// + /// 清理包裹未使用的缓存文件 + /// + public ClearUnusedCacheFilesOperation ClearUnusedCacheFilesAsync() + { + DebugCheckInitialize(); + var operation = new ClearUnusedCacheFilesOperation(this, _cacheMgr); + OperationSystem.StartOperation(PackageName, operation); + return operation; + } + + /// + /// 清理包裹本地所有的缓存文件 + /// + public ClearAllCacheFilesOperation ClearAllCacheFilesAsync() + { + DebugCheckInitialize(); + var operation = new ClearAllCacheFilesOperation(_cacheMgr); + OperationSystem.StartOperation(PackageName, operation); + return operation; + } + + public GetAllCacheFileInfosOperation GetAllCacheFileInfosAsync(string packageVersion) + { + DebugCheckInitialize(); + + var operation = new GetAllCacheFileInfosOperation(_persistentMgr, _cacheMgr, packageVersion); + OperationSystem.StartOperation(PackageName, operation); + return operation; + } #endregion #region 资源信息