mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
Add thead safety to terrain data (#1999)
Allow safely reading terrain data from other threads
This commit is contained in:
parent
d6220ff779
commit
aeca6a8f53
3 changed files with 101 additions and 25 deletions
|
|
@ -2,6 +2,7 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace MinecraftClient.Mapping
|
||||
{
|
||||
|
|
@ -19,6 +20,11 @@ namespace MinecraftClient.Mapping
|
|||
/// </summary>
|
||||
private readonly Block[,,] blocks = new Block[SizeX, SizeY, SizeZ];
|
||||
|
||||
/// <summary>
|
||||
/// Lock for thread safety
|
||||
/// </summary>
|
||||
private readonly ReaderWriterLockSlim blockLock = new ReaderWriterLockSlim();
|
||||
|
||||
/// <summary>
|
||||
/// Read, or set the specified block
|
||||
/// </summary>
|
||||
|
|
@ -36,8 +42,17 @@ namespace MinecraftClient.Mapping
|
|||
throw new ArgumentOutOfRangeException("blockY", "Must be between 0 and " + (SizeY - 1) + " (inclusive)");
|
||||
if (blockZ < 0 || blockZ >= SizeZ)
|
||||
throw new ArgumentOutOfRangeException("blockZ", "Must be between 0 and " + (SizeZ - 1) + " (inclusive)");
|
||||
|
||||
blockLock.EnterReadLock();
|
||||
try
|
||||
{
|
||||
return blocks[blockX, blockY, blockZ];
|
||||
}
|
||||
finally
|
||||
{
|
||||
blockLock.ExitReadLock();
|
||||
}
|
||||
}
|
||||
set
|
||||
{
|
||||
if (blockX < 0 || blockX >= SizeX)
|
||||
|
|
@ -46,8 +61,17 @@ namespace MinecraftClient.Mapping
|
|||
throw new ArgumentOutOfRangeException("blockY", "Must be between 0 and " + (SizeY - 1) + " (inclusive)");
|
||||
if (blockZ < 0 || blockZ >= SizeZ)
|
||||
throw new ArgumentOutOfRangeException("blockZ", "Must be between 0 and " + (SizeZ - 1) + " (inclusive)");
|
||||
|
||||
blockLock.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
blocks[blockX, blockY, blockZ] = value;
|
||||
}
|
||||
finally
|
||||
{
|
||||
blockLock.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace MinecraftClient.Mapping
|
||||
{
|
||||
|
|
@ -17,6 +18,11 @@ namespace MinecraftClient.Mapping
|
|||
/// </summary>
|
||||
private readonly Chunk[] chunks = new Chunk[ColumnSize];
|
||||
|
||||
/// <summary>
|
||||
/// Lock for thread safety
|
||||
/// </summary>
|
||||
private readonly ReaderWriterLockSlim chunkLock = new ReaderWriterLockSlim();
|
||||
|
||||
/// <summary>
|
||||
/// Get or set the specified chunk column
|
||||
/// </summary>
|
||||
|
|
@ -26,13 +32,29 @@ namespace MinecraftClient.Mapping
|
|||
public Chunk this[int chunkY]
|
||||
{
|
||||
get
|
||||
{
|
||||
chunkLock.EnterReadLock();
|
||||
try
|
||||
{
|
||||
return chunks[chunkY];
|
||||
}
|
||||
finally
|
||||
{
|
||||
chunkLock.ExitReadLock();
|
||||
}
|
||||
}
|
||||
set
|
||||
{
|
||||
chunkLock.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
chunks[chunkY] = value;
|
||||
}
|
||||
finally
|
||||
{
|
||||
chunkLock.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace MinecraftClient.Mapping
|
||||
{
|
||||
|
|
@ -15,6 +16,11 @@ namespace MinecraftClient.Mapping
|
|||
/// </summary>
|
||||
private Dictionary<int, Dictionary<int, ChunkColumn>> chunks = new Dictionary<int, Dictionary<int, ChunkColumn>>();
|
||||
|
||||
/// <summary>
|
||||
/// Lock for thread safety
|
||||
/// </summary>
|
||||
private readonly ReaderWriterLockSlim chunksLock = new ReaderWriterLockSlim();
|
||||
|
||||
/// <summary>
|
||||
/// Read, set or unload the specified chunk column
|
||||
/// </summary>
|
||||
|
|
@ -24,6 +30,9 @@ namespace MinecraftClient.Mapping
|
|||
public ChunkColumn this[int chunkX, int chunkZ]
|
||||
{
|
||||
get
|
||||
{
|
||||
chunksLock.EnterReadLock();
|
||||
try
|
||||
{
|
||||
//Read a chunk
|
||||
if (chunks.ContainsKey(chunkX))
|
||||
|
|
@ -31,7 +40,15 @@ namespace MinecraftClient.Mapping
|
|||
return chunks[chunkX][chunkZ];
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
chunksLock.ExitReadLock();
|
||||
}
|
||||
}
|
||||
set
|
||||
{
|
||||
chunksLock.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
if (value != null)
|
||||
{
|
||||
|
|
@ -54,6 +71,11 @@ namespace MinecraftClient.Mapping
|
|||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
chunksLock.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -117,7 +139,7 @@ namespace MinecraftClient.Mapping
|
|||
{
|
||||
Location doneloc = new Location(x, y, z);
|
||||
Block doneblock = GetBlock(doneloc);
|
||||
Material blockType = GetBlock(doneloc).Type;
|
||||
Material blockType = doneblock.Type;
|
||||
if (blockType == block)
|
||||
{
|
||||
list.Add(doneloc);
|
||||
|
|
@ -149,9 +171,17 @@ namespace MinecraftClient.Mapping
|
|||
/// Clear all terrain data from the world
|
||||
/// </summary>
|
||||
public void Clear()
|
||||
{
|
||||
chunksLock.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
chunks = new Dictionary<int, Dictionary<int, ChunkColumn>>();
|
||||
}
|
||||
finally
|
||||
{
|
||||
chunksLock.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the location of block of the entity is looking
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue