From c79825025862e8dcb9252448718779f0437d6638 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E5=86=A0=E5=B3=B0?= Date: Wed, 27 Aug 2025 19:19:00 +0800 Subject: [PATCH] =?UTF-8?q?feat=20:=20CRC=E5=B7=A5=E5=85=B7=E7=B1=BB?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96=E6=95=B4=E5=BD=A2=E5=80=BC?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Runtime/Utility/CRC32Algorithm.cs | 11 ++++++ Assets/YooAsset/Runtime/Utility/YooUtility.cs | 36 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/Assets/YooAsset/Runtime/Utility/CRC32Algorithm.cs b/Assets/YooAsset/Runtime/Utility/CRC32Algorithm.cs index b729ea9d..f8689c60 100644 --- a/Assets/YooAsset/Runtime/Utility/CRC32Algorithm.cs +++ b/Assets/YooAsset/Runtime/Utility/CRC32Algorithm.cs @@ -73,6 +73,17 @@ namespace YooAsset { private uint _currentCrc; + /// + /// Gets the computed hash value. + /// + public uint CRCValue + { + get + { + return _currentCrc; + } + } + /// /// Initializes a new instance of the class. /// diff --git a/Assets/YooAsset/Runtime/Utility/YooUtility.cs b/Assets/YooAsset/Runtime/Utility/YooUtility.cs index c8df5589..740f96aa 100644 --- a/Assets/YooAsset/Runtime/Utility/YooUtility.cs +++ b/Assets/YooAsset/Runtime/Utility/YooUtility.cs @@ -339,6 +339,11 @@ namespace YooAsset byte[] buffer = Encoding.UTF8.GetBytes(str); return BytesCRC32(buffer); } + public static uint StringCRC32Value(string str) + { + byte[] buffer = Encoding.UTF8.GetBytes(str); + return BytesCRC32Value(buffer); + } /// /// 获取文件的CRC32 @@ -350,6 +355,13 @@ namespace YooAsset return StreamCRC32(fs); } } + public static uint FileCRC32Value(string filePath) + { + using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) + { + return StreamCRC32Value(fs); + } + } /// /// 获取文件的CRC32 @@ -366,6 +378,18 @@ namespace YooAsset return string.Empty; } } + public static uint FileCRC32ValueSafely(string filePath) + { + try + { + return FileCRC32Value(filePath); + } + catch (Exception e) + { + YooLogger.Exception(e); + return 0; + } + } /// /// 获取数据流的CRC32 @@ -376,6 +400,12 @@ namespace YooAsset byte[] hashBytes = hash.ComputeHash(stream); return ToString(hashBytes); } + public static uint StreamCRC32Value(Stream stream) + { + CRC32Algorithm hash = new CRC32Algorithm(); + hash.ComputeHash(stream); + return hash.CRCValue; + } /// /// 获取字节数组的CRC32 @@ -386,6 +416,12 @@ namespace YooAsset byte[] hashBytes = hash.ComputeHash(buffer); return ToString(hashBytes); } + public static uint BytesCRC32Value(byte[] buffer) + { + CRC32Algorithm hash = new CRC32Algorithm(); + hash.ComputeHash(buffer); + return hash.CRCValue; + } #endregion } } \ No newline at end of file