Skip to content

Commit

Permalink
Use a Mutex with 500ms timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryQuan committed Oct 1, 2023
1 parent c3aed81 commit 60d02e9
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Nodsoft.WowsReplaysUnpack.Tests/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public static class Utilities
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MemoryStream LoadReplay(string replayPath)
{
using Mutex mutex = new Mutex(true, "WowsReplaysUnpack.Tests.LoadReplay");
mutex.WaitOne(500);
using FileStream fs = File.Open(Path.Join(_sampleFolder, replayPath), FileMode.Open, FileAccess.Read, FileShare.Read);
MemoryStream ms = new();
fs.CopyTo(ms);
Expand Down
13 changes: 2 additions & 11 deletions Nodsoft.WowsReplaysUnpack/Services/ReplayUnpackerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,21 +164,12 @@ private static void Decrypt(BinaryReader binaryReader, Stream targetStream)
}
}

private static readonly object _decompressLock = new object();
private static void Decompress(Stream compressedStream, Stream decompressedStream)
{
// DeflateStream doesn't strip the header so we strip it manually.
compressedStream.Seek(2, SeekOrigin.Begin);
byte[] buffer = new byte[4096];
int bytesRead;
lock (_decompressLock)
{
using DeflateStream deflateStream = new(compressedStream, CompressionMode.Decompress);
while ((bytesRead = deflateStream.Read(buffer, 0, buffer.Length)) > 0)
{
decompressedStream.Write(buffer, 0, bytesRead);
}
}
using DeflateStream deflateStream = new(compressedStream, CompressionMode.Decompress);
deflateStream.CopyTo(decompressedStream);
decompressedStream.Seek(0, SeekOrigin.Begin);
}
}
Expand Down

0 comments on commit 60d02e9

Please sign in to comment.