Files
VoxelEngine/World/Persistence/InMemoryChunkModificationStore.cs
MDietermann 34f923cbca refactor: store-owned diff snapshot and drop dead ChunkJob.ExistingChunk
TryGetDiff now returns an independent copy so callers can safely retain the
reference; removes the defensive copy from ChunkStreamer.DispatchLoad.
ChunkJob.ExistingChunk was never populated (DispatchRemesh uses closures
instead), so the field is removed along with its misleading record-level
comment.
2026-06-01 13:31:05 +02:00

36 lines
1.1 KiB
C#

using OpenTK.Mathematics;
namespace VoxelEngine.World.Persistence;
public sealed class InMemoryChunkModificationStore : IChunkModificationStore
{
private readonly Dictionary<Vector3i, Dictionary<int, ushort>> _diffs = new();
public bool TryGetDiff(Vector3i chunkPos, out IReadOnlyDictionary<int, ushort>? diff)
{
if (this._diffs.TryGetValue(chunkPos, out Dictionary<int, ushort>? d))
{
// Returned snapshot: callers may safely retain and enumerate even
// after concurrent RecordEdit on the same chunk.
diff = new Dictionary<int, ushort>(d);
return true;
}
diff = null;
return false;
}
public void RecordEdit(Vector3i chunkPos, int localIndex, ushort blockId)
{
if (!this._diffs.TryGetValue(chunkPos, out Dictionary<int, ushort>? d))
{
d = new Dictionary<int, ushort>();
this._diffs[chunkPos] = d;
}
d[localIndex] = blockId;
}
public void ClearDiff(Vector3i chunkPos) => this._diffs.Remove(chunkPos);
public bool HasDiff(Vector3i chunkPos) => this._diffs.ContainsKey(chunkPos);
}