Skip to content

Commit

Permalink
Fix finding metadata by hash failing
Browse files Browse the repository at this point in the history
  • Loading branch information
DiFFoZ committed Jun 16, 2024
1 parent 243bc20 commit 1f88329
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
13 changes: 7 additions & 6 deletions BepInExFasterLoadAssetBundles/Helpers/HashingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -22,15 +23,13 @@ public static byte[] HashStream(Stream stream)

var hash = new Hash128();

var buffer = ArrayPool<byte>.Shared.Rent(c_BufferSize);
using var buffer = new NativeArray<byte>(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<byte>.Shared.Return(buffer);

var hashArray = new byte[16];
BinaryPrimitives.WriteUInt64LittleEndian(hashArray, hash.u64_0);
BinaryPrimitives.WriteUInt64LittleEndian(hashArray.AsSpan()[8..], hash.u64_1);
Expand All @@ -51,7 +50,7 @@ public static string HashToString(Span<byte> hash)
return chars.ToString();
}

public static void WriteHash(Span<byte> destination, string hash)
public static int WriteHash(Span<byte> destination, string hash)
{
if ((hash.Length / 2) > destination.Length)
{
Expand All @@ -63,5 +62,7 @@ public static void WriteHash(Span<byte> destination, string hash)
var s = hash.AsSpan(i, 2);
destination[i / 2] = byte.Parse(s, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
}

return hash.Length / 2;
}
}
5 changes: 3 additions & 2 deletions BepInExFasterLoadAssetBundles/Managers/AssetBundleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down Expand Up @@ -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();

Expand All @@ -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");

Expand Down
8 changes: 4 additions & 4 deletions BepInExFasterLoadAssetBundles/Managers/MetadataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand Down

0 comments on commit 1f88329

Please sign in to comment.