From ef0cc05f5cf59afccb23fd1b3989d3923c76048a Mon Sep 17 00:00:00 2001 From: hevinci Date: Fri, 10 Feb 2023 16:13:08 +0800 Subject: [PATCH] update cache system --- .../Runtime/CacheSystem/CacheFileInfo.cs | 30 +++++++++++++++++-- .../Runtime/CacheSystem/CacheSystem.cs | 5 +--- .../Downloader/FileDownloader.cs | 22 +++++++------- .../Runtime/PatchSystem/PatchManifestTools.cs | 3 ++ .../YooAsset/Runtime/Utility/BufferWriter.cs | 8 +++++ 5 files changed, 51 insertions(+), 17 deletions(-) diff --git a/Assets/YooAsset/Runtime/CacheSystem/CacheFileInfo.cs b/Assets/YooAsset/Runtime/CacheSystem/CacheFileInfo.cs index 8097b02..e18e340 100644 --- a/Assets/YooAsset/Runtime/CacheSystem/CacheFileInfo.cs +++ b/Assets/YooAsset/Runtime/CacheSystem/CacheFileInfo.cs @@ -3,10 +3,34 @@ using System.IO; namespace YooAsset { - [Serializable] internal class CacheFileInfo { - public string FileCRC; - public long FileSize; + 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)) + { + 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(); + } } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/CacheSystem/CacheSystem.cs b/Assets/YooAsset/Runtime/CacheSystem/CacheSystem.cs index c7384ac..acb4b35 100644 --- a/Assets/YooAsset/Runtime/CacheSystem/CacheSystem.cs +++ b/Assets/YooAsset/Runtime/CacheSystem/CacheSystem.cs @@ -78,10 +78,7 @@ namespace YooAsset return EVerifyResult.InfoFileNotExisted; // 解析信息文件获取验证数据 - string jsonContent = FileUtility.ReadAllText(infoFilePath); - CacheFileInfo fileInfo = UnityEngine.JsonUtility.FromJson(jsonContent); - element.DataFileCRC = fileInfo.FileCRC; - element.DataFileSize = fileInfo.FileSize; + CacheFileInfo.ReadInfoFromFile(infoFilePath, out element.DataFileCRC, out element.DataFileSize); } catch (Exception) { diff --git a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs index 9fab6be..b1fc6ef 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs @@ -220,22 +220,24 @@ namespace YooAsset { try { - string destFilePath = _bundleInfo.Bundle.CachedDataFilePath; - if (File.Exists(destFilePath)) - File.Delete(destFilePath); + string infoFilePath = _bundleInfo.Bundle.CachedInfoFilePath; + string dataFilePath = _bundleInfo.Bundle.CachedDataFilePath; + string dataFileCRC = _bundleInfo.Bundle.FileCRC; + long dataFileSize = _bundleInfo.Bundle.FileSize; + + if (File.Exists(infoFilePath)) + File.Delete(infoFilePath); + if (File.Exists(dataFilePath)) + File.Delete(dataFilePath); FileInfo fileInfo = new FileInfo(_tempFilePath); - fileInfo.MoveTo(destFilePath); + fileInfo.MoveTo(dataFilePath); // 写入信息文件记录验证数据 - CacheFileInfo cacheInfo = new CacheFileInfo(); - cacheInfo.FileCRC = _bundleInfo.Bundle.FileCRC; - cacheInfo.FileSize = _bundleInfo.Bundle.FileSize; - string jsonContent = UnityEngine.JsonUtility.ToJson(cacheInfo); - FileUtility.CreateFile(_bundleInfo.Bundle.CachedInfoFilePath, jsonContent); + CacheFileInfo.WriteInfoToFile(infoFilePath, dataFileCRC, dataFileSize); // 记录缓存文件 - var wrapper = new PackageCache.RecordWrapper(_bundleInfo.Bundle.CachedInfoFilePath, _bundleInfo.Bundle.CachedDataFilePath, _bundleInfo.Bundle.FileCRC, _bundleInfo.Bundle.FileSize); + var wrapper = new PackageCache.RecordWrapper(infoFilePath, dataFilePath, dataFileCRC, dataFileSize); CacheSystem.RecordFile(_bundleInfo.Bundle.PackageName, _bundleInfo.Bundle.CacheGUID, wrapper); _lastError = string.Empty; diff --git a/Assets/YooAsset/Runtime/PatchSystem/PatchManifestTools.cs b/Assets/YooAsset/Runtime/PatchSystem/PatchManifestTools.cs index 9aa38ee..3ca72ec 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/PatchManifestTools.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/PatchManifestTools.cs @@ -8,6 +8,8 @@ namespace YooAsset { internal static class PatchManifestTools { + +#if UNITY_EDITOR /// /// 序列化(JSON文件) /// @@ -151,6 +153,7 @@ namespace YooAsset return manifest; } +#endif /// /// 生成Bundle文件的正式名称 diff --git a/Assets/YooAsset/Runtime/Utility/BufferWriter.cs b/Assets/YooAsset/Runtime/Utility/BufferWriter.cs index 225939e..940b438 100644 --- a/Assets/YooAsset/Runtime/Utility/BufferWriter.cs +++ b/Assets/YooAsset/Runtime/Utility/BufferWriter.cs @@ -28,6 +28,14 @@ namespace YooAsset get { return _buffer.Length; } } + /// + /// 清空缓冲区 + /// + public void Clear() + { + _index = 0; + } + /// /// 将有效数据写入文件流 ///