diff --git a/BepInExFasterLoadAssetBundles/Helpers/HashingHelper.cs b/BepInExFasterLoadAssetBundles/Helpers/HashingHelper.cs index 52ce3d1..27ac346 100644 --- a/BepInExFasterLoadAssetBundles/Helpers/HashingHelper.cs +++ b/BepInExFasterLoadAssetBundles/Helpers/HashingHelper.cs @@ -3,12 +3,13 @@ using System.Buffers.Binary; using System.Globalization; using System.IO; +using Unity.Collections; using UnityEngine; namespace BepInExFasterLoadAssetBundles.Helpers; internal class HashingHelper { - private const int c_BufferSize = 81920; + private const int c_BufferSize = 4096; public static byte[] HashFile(string path) { @@ -22,15 +23,13 @@ public static byte[] HashStream(Stream stream) var hash = new Hash128(); - var buffer = ArrayPool.Shared.Rent(c_BufferSize); + using var buffer = new NativeArray(c_BufferSize, Allocator.Temp, NativeArrayOptions.UninitializedMemory); int readBytes; - while ((readBytes = stream.Read(buffer, 0, buffer.Length)) > 0) + while ((readBytes = stream.Read(buffer)) > 0) { hash.Append(buffer, 0, readBytes); } - ArrayPool.Shared.Return(buffer); - var hashArray = new byte[16]; BinaryPrimitives.WriteUInt64LittleEndian(hashArray, hash.u64_0); BinaryPrimitives.WriteUInt64LittleEndian(hashArray.AsSpan()[8..], hash.u64_1); @@ -51,7 +50,7 @@ public static string HashToString(Span hash) return chars.ToString(); } - public static void WriteHash(Span destination, string hash) + public static int WriteHash(Span destination, string hash) { if ((hash.Length / 2) > destination.Length) { @@ -63,5 +62,7 @@ public static void WriteHash(Span destination, string hash) var s = hash.AsSpan(i, 2); destination[i / 2] = byte.Parse(s, NumberStyles.HexNumber, CultureInfo.InvariantCulture); } + + return hash.Length / 2; } } diff --git a/BepInExFasterLoadAssetBundles/Managers/AssetBundleManager.cs b/BepInExFasterLoadAssetBundles/Managers/AssetBundleManager.cs index eea4581..d9849ec 100644 --- a/BepInExFasterLoadAssetBundles/Managers/AssetBundleManager.cs +++ b/BepInExFasterLoadAssetBundles/Managers/AssetBundleManager.cs @@ -85,7 +85,7 @@ public bool TryRecompressAssetBundle(Stream stream, [NotNullWhen(true)] out stri } var hash = HashingHelper.HashStream(stream); - + path = null!; if (FindCachedBundleByHash(hash, out var newPath)) { @@ -262,6 +262,7 @@ private async Task DecompressAssetBundleAsync(WorkAsset workAsset) var result = op.result; var humanReadableResult = op.humanReadableResult; var success = op.success; + var newHash = HashingHelper.HashFile(outputPath); await AsyncHelper.SwitchToThreadPool(); @@ -279,7 +280,7 @@ private async Task DecompressAssetBundleAsync(WorkAsset workAsset) } // check if unity returned the same assetbundle (means that assetbundle is already decompressed) - if (workAsset.Hash.AsSpan().SequenceEqual(HashingHelper.HashFile(outputPath))) + if (workAsset.Hash.AsSpan().SequenceEqual(newHash)) { Patcher.Logger.LogDebug($"Assetbundle \"{originalFileName}\" is already uncompressed, adding to ignore list"); diff --git a/BepInExFasterLoadAssetBundles/Managers/MetadataManager.cs b/BepInExFasterLoadAssetBundles/Managers/MetadataManager.cs index a49c546..3519eaa 100644 --- a/BepInExFasterLoadAssetBundles/Managers/MetadataManager.cs +++ b/BepInExFasterLoadAssetBundles/Managers/MetadataManager.cs @@ -27,8 +27,8 @@ public MetadataManager(string metadataFile) { foreach (var metadata in m_Metadata) { - HashingHelper.WriteHash(tempHash, metadata.OriginalAssetBundleHash); - if (tempHash.SequenceEqual(hash)) + var writtenBytes = HashingHelper.WriteHash(tempHash, metadata.OriginalAssetBundleHash); + if (tempHash[..writtenBytes].SequenceEqual(hash)) { return metadata; } @@ -42,7 +42,7 @@ public void SaveMetadata(Metadata metadata) { lock (m_Lock) { - var index = m_Metadata.FindIndex(m => m.OriginalAssetBundleHash == metadata.OriginalAssetBundleHash); + var index = m_Metadata.FindIndex(m => m.OriginalAssetBundleHash.Equals(metadata.OriginalAssetBundleHash, StringComparison.InvariantCulture)); if (index == -1) { @@ -62,7 +62,7 @@ public void DeleteMetadata(Metadata metadata) var shouldSave = false; lock (m_Lock) { - var index = m_Metadata.FindIndex(m => m.OriginalAssetBundleHash == metadata.OriginalAssetBundleHash); + var index = m_Metadata.FindIndex(m => m.OriginalAssetBundleHash.Equals(metadata.OriginalAssetBundleHash, StringComparison.InvariantCulture)); if (index >= 0) {