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.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
namespace MinecraftClient.Mapping
|
namespace MinecraftClient.Mapping
|
||||||
{
|
{
|
||||||
|
|
@ -19,6 +20,11 @@ namespace MinecraftClient.Mapping
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly Block[,,] blocks = new Block[SizeX, SizeY, SizeZ];
|
private readonly Block[,,] blocks = new Block[SizeX, SizeY, SizeZ];
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Lock for thread safety
|
||||||
|
/// </summary>
|
||||||
|
private readonly ReaderWriterLockSlim blockLock = new ReaderWriterLockSlim();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Read, or set the specified block
|
/// Read, or set the specified block
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -36,8 +42,17 @@ namespace MinecraftClient.Mapping
|
||||||
throw new ArgumentOutOfRangeException("blockY", "Must be between 0 and " + (SizeY - 1) + " (inclusive)");
|
throw new ArgumentOutOfRangeException("blockY", "Must be between 0 and " + (SizeY - 1) + " (inclusive)");
|
||||||
if (blockZ < 0 || blockZ >= SizeZ)
|
if (blockZ < 0 || blockZ >= SizeZ)
|
||||||
throw new ArgumentOutOfRangeException("blockZ", "Must be between 0 and " + (SizeZ - 1) + " (inclusive)");
|
throw new ArgumentOutOfRangeException("blockZ", "Must be between 0 and " + (SizeZ - 1) + " (inclusive)");
|
||||||
|
|
||||||
|
blockLock.EnterReadLock();
|
||||||
|
try
|
||||||
|
{
|
||||||
return blocks[blockX, blockY, blockZ];
|
return blocks[blockX, blockY, blockZ];
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
blockLock.ExitReadLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
if (blockX < 0 || blockX >= SizeX)
|
if (blockX < 0 || blockX >= SizeX)
|
||||||
|
|
@ -46,8 +61,17 @@ namespace MinecraftClient.Mapping
|
||||||
throw new ArgumentOutOfRangeException("blockY", "Must be between 0 and " + (SizeY - 1) + " (inclusive)");
|
throw new ArgumentOutOfRangeException("blockY", "Must be between 0 and " + (SizeY - 1) + " (inclusive)");
|
||||||
if (blockZ < 0 || blockZ >= SizeZ)
|
if (blockZ < 0 || blockZ >= SizeZ)
|
||||||
throw new ArgumentOutOfRangeException("blockZ", "Must be between 0 and " + (SizeZ - 1) + " (inclusive)");
|
throw new ArgumentOutOfRangeException("blockZ", "Must be between 0 and " + (SizeZ - 1) + " (inclusive)");
|
||||||
|
|
||||||
|
blockLock.EnterWriteLock();
|
||||||
|
try
|
||||||
|
{
|
||||||
blocks[blockX, blockY, blockZ] = value;
|
blocks[blockX, blockY, blockZ] = value;
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
blockLock.ExitWriteLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
namespace MinecraftClient.Mapping
|
namespace MinecraftClient.Mapping
|
||||||
{
|
{
|
||||||
|
|
@ -17,6 +18,11 @@ namespace MinecraftClient.Mapping
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly Chunk[] chunks = new Chunk[ColumnSize];
|
private readonly Chunk[] chunks = new Chunk[ColumnSize];
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Lock for thread safety
|
||||||
|
/// </summary>
|
||||||
|
private readonly ReaderWriterLockSlim chunkLock = new ReaderWriterLockSlim();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get or set the specified chunk column
|
/// Get or set the specified chunk column
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -26,13 +32,29 @@ namespace MinecraftClient.Mapping
|
||||||
public Chunk this[int chunkY]
|
public Chunk this[int chunkY]
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
{
|
||||||
|
chunkLock.EnterReadLock();
|
||||||
|
try
|
||||||
{
|
{
|
||||||
return chunks[chunkY];
|
return chunks[chunkY];
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
chunkLock.ExitReadLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
set
|
set
|
||||||
|
{
|
||||||
|
chunkLock.EnterWriteLock();
|
||||||
|
try
|
||||||
{
|
{
|
||||||
chunks[chunkY] = value;
|
chunks[chunkY] = value;
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
chunkLock.ExitWriteLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
namespace MinecraftClient.Mapping
|
namespace MinecraftClient.Mapping
|
||||||
{
|
{
|
||||||
|
|
@ -15,6 +16,11 @@ namespace MinecraftClient.Mapping
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private Dictionary<int, Dictionary<int, ChunkColumn>> chunks = new Dictionary<int, Dictionary<int, ChunkColumn>>();
|
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>
|
/// <summary>
|
||||||
/// Read, set or unload the specified chunk column
|
/// Read, set or unload the specified chunk column
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -24,6 +30,9 @@ namespace MinecraftClient.Mapping
|
||||||
public ChunkColumn this[int chunkX, int chunkZ]
|
public ChunkColumn this[int chunkX, int chunkZ]
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
{
|
||||||
|
chunksLock.EnterReadLock();
|
||||||
|
try
|
||||||
{
|
{
|
||||||
//Read a chunk
|
//Read a chunk
|
||||||
if (chunks.ContainsKey(chunkX))
|
if (chunks.ContainsKey(chunkX))
|
||||||
|
|
@ -31,7 +40,15 @@ namespace MinecraftClient.Mapping
|
||||||
return chunks[chunkX][chunkZ];
|
return chunks[chunkX][chunkZ];
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
chunksLock.ExitReadLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
set
|
set
|
||||||
|
{
|
||||||
|
chunksLock.EnterWriteLock();
|
||||||
|
try
|
||||||
{
|
{
|
||||||
if (value != null)
|
if (value != null)
|
||||||
{
|
{
|
||||||
|
|
@ -54,6 +71,11 @@ namespace MinecraftClient.Mapping
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
chunksLock.ExitWriteLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -117,7 +139,7 @@ namespace MinecraftClient.Mapping
|
||||||
{
|
{
|
||||||
Location doneloc = new Location(x, y, z);
|
Location doneloc = new Location(x, y, z);
|
||||||
Block doneblock = GetBlock(doneloc);
|
Block doneblock = GetBlock(doneloc);
|
||||||
Material blockType = GetBlock(doneloc).Type;
|
Material blockType = doneblock.Type;
|
||||||
if (blockType == block)
|
if (blockType == block)
|
||||||
{
|
{
|
||||||
list.Add(doneloc);
|
list.Add(doneloc);
|
||||||
|
|
@ -149,9 +171,17 @@ namespace MinecraftClient.Mapping
|
||||||
/// Clear all terrain data from the world
|
/// Clear all terrain data from the world
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Clear()
|
public void Clear()
|
||||||
|
{
|
||||||
|
chunksLock.EnterWriteLock();
|
||||||
|
try
|
||||||
{
|
{
|
||||||
chunks = new Dictionary<int, Dictionary<int, ChunkColumn>>();
|
chunks = new Dictionary<int, Dictionary<int, ChunkColumn>>();
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
chunksLock.ExitWriteLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get the location of block of the entity is looking
|
/// Get the location of block of the entity is looking
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue