Skip to content

Commit

Permalink
Added: Safety Assert for RefCount on Packing Partial Compressed SOLIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Jul 23, 2024
1 parent c8ec4b1 commit 7573429
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion NexusMods.Archives.Nx/FileProviders/FromExistingNxBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal class FromExistingNxBlock : IFileDataProvider
/// <summary>
/// Contains the decompressed chunk of data.
/// </summary>
private LazyRefCounterDecompressedNxBlock LazyRefCounterDecompressedNxBlock { get; init; }
internal LazyRefCounterDecompressedNxBlock LazyRefCounterDecompressedNxBlock { get; init; }

/// <summary>
/// Size of the file in the decompressed block.
Expand Down
27 changes: 27 additions & 0 deletions NexusMods.Archives.Nx/Packing/Pack/NxPacker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
using System.Runtime.InteropServices;
#endif

#if DEBUG
using NexusMods.Archives.Nx.FileProviders;
#endif

namespace NexusMods.Archives.Nx.Packing.Pack;

/// <summary>
Expand Down Expand Up @@ -125,6 +129,8 @@ private static unsafe void PackWithBlocksAndFiles<TWithRelativePath>(Span<TWithR
// Implicit dispose of `sched` waits for all jobs to complete.
}

DebugAssertPartialBlockRefCount(blocks);

// Truncate stream.
stream.SetLength(stream.Position);

Expand Down Expand Up @@ -170,6 +176,27 @@ private static void PackBlock(object? obj)
// Compress that block!
data.Block.ProcessBlock(tocBuilder, settings, data.BlockIndex, context.PackerPool);
}

private static void DebugAssertPartialBlockRefCount(List<IBlock<PackerFile>> blocks)
{
#if DEBUG
foreach (var block in blocks)
{
if (block is not SolidBlock<PackerFile> solidBlock)
continue;

foreach (var item in solidBlock.Items)
{
if (item.FileDataProvider is not FromExistingNxBlock fromExisting)
continue;

var refCount = fromExisting.LazyRefCounterDecompressedNxBlock.InternalGetRefCount();
if (refCount is < 0 or > 0)
throw new Exception("Potential threading issue with partial block decompression.");
}
}
#endif
}
}

internal static class PackerExtensions
Expand Down

0 comments on commit 7573429

Please sign in to comment.