Skip to content

Commit

Permalink
Removed cts
Browse files Browse the repository at this point in the history
  • Loading branch information
Ven0maus committed Oct 5, 2023
1 parent 00b6249 commit bc9b4a3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 31 deletions.
12 changes: 3 additions & 9 deletions Venomaus.FlowVitae/Chunking/ChunkLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Venomaus.FlowVitae.Cells;
using Venomaus.FlowVitae.Chunking.Generators;
Expand Down Expand Up @@ -34,10 +33,9 @@ internal class ChunkLoader<TCellType, TCell, TChunkData>

public bool LoadChunksInParallel { get; set; } = true;

private readonly CancellationToken _cancellationToken;
private readonly int _chunksOutsideViewportRadiusToLoad;

public ChunkLoader(int viewPortWidth, int viewPortHeight, int width, int height, IProceduralGen<TCellType, TCell, TChunkData> generator, Func<int, int, TCellType, TCell?> cellTypeConverter, int chunksOutsideViewportRadiusToLoad, CancellationToken cancellationToken)
public ChunkLoader(int viewPortWidth, int viewPortHeight, int width, int height, IProceduralGen<TCellType, TCell, TChunkData> generator, Func<int, int, TCellType, TCell?> cellTypeConverter, int chunksOutsideViewportRadiusToLoad)
{
_chunkWidth = width;
_chunkHeight = height;
Expand All @@ -46,7 +44,6 @@ public ChunkLoader(int viewPortWidth, int viewPortHeight, int width, int height,
_seed = generator.Seed;
_generator = generator;
_cellTypeConverter = cellTypeConverter;
_cancellationToken = cancellationToken;
_tempLoadedChunks = new ConcurrentHashSet<(int x, int y)>(new TupleComparer<int>());
var initialAmount = GetChunksToLoad(0, 0).AllChunks.Count;
_chunks = new ConcurrentDictionary<(int x, int y), (TCellType[], TChunkData?)>(Environment.ProcessorCount, initialAmount, new TupleComparer<int>());
Expand Down Expand Up @@ -184,7 +181,6 @@ public void SetCurrentChunk(int x, int y,
{
try
{
_cancellationToken.ThrowIfCancellationRequested();
if (LoadChunk(chunk.x, chunk.y, out _))
{
onChunkLoad?.Invoke(null, new ChunkUpdateArgs(chunk, _chunkWidth, _chunkHeight, false));
Expand All @@ -202,7 +198,6 @@ public void SetCurrentChunk(int x, int y,
{
foreach (var chunk in chunksOutsideViewport)
{
_cancellationToken.ThrowIfCancellationRequested();
if (LoadChunk(chunk.x, chunk.y, out _))
{
onChunkLoad?.Invoke(null, new ChunkUpdateArgs(chunk, _chunkWidth, _chunkHeight, false));
Expand All @@ -220,10 +215,10 @@ public void SetCurrentChunk(int x, int y,
Task.Run(() =>
{
throw ex;
}, _cancellationToken);
});
return;
}
}, _cancellationToken);
});
}
}

Expand Down Expand Up @@ -446,7 +441,6 @@ public bool LoadChunk(int x, int y, out (int x, int y) chunkCoordinate)
{
lock (loadChunkLock)
{
if (_cancellationToken.IsCancellationRequested) return false;
// Recheck the condition inside the lock
if (!_chunks.ContainsKey(chunkCoordinate) ||
_chunks[chunkCoordinate].chunkCells == null)
Expand Down
23 changes: 2 additions & 21 deletions Venomaus.FlowVitae/Grids/GridBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Venomaus.FlowVitae.Grids
/// <typeparam name="TCellType">The cell type to be used within the <see cref="GridBase{TCellType, TCell, TChunkData}"/></typeparam>
/// <typeparam name="TCell">The wrapper object used to wrap around the cell type</typeparam>
/// <typeparam name="TChunkData">The custom chunk data type to be returned from chunks</typeparam>
public abstract class GridBase<TCellType, TCell, TChunkData> : IDisposable
public abstract class GridBase<TCellType, TCell, TChunkData>
where TCellType : struct
where TCell : class, ICell<TCellType>, new()
where TChunkData : class, IChunkData
Expand Down Expand Up @@ -135,7 +135,6 @@ public bool RaiseOnlyOnCellTypeChange
}

private bool _viewPortInitialized = false;
private readonly CancellationTokenSource? _cancellationTokenSource;

/// <summary>
/// Constructor for <see cref="GridBase{TCellType, TCell}"/>
Expand Down Expand Up @@ -173,15 +172,13 @@ public GridBase(int viewPortWidth, int viewPortHeight, int chunkWidth, int chunk
if (chunkWidth <= 0 || chunkHeight <= 0)
throw new Exception("Cannot define a grid with a chunk width/height smaller or equal to 0");

_cancellationTokenSource = new CancellationTokenSource();

// Disable for initial load
UseThreading = false;
ChunkWidth = chunkWidth;
ChunkHeight = chunkHeight;

// Initialize chunkloader if grid uses chunks
_chunkLoader = new ChunkLoader<TCellType, TCell, TChunkData>(viewPortWidth, viewPortHeight, chunkWidth, chunkHeight, generator, Convert, chunksOutsideViewportRadiusToLoad, _cancellationTokenSource.Token);
_chunkLoader = new ChunkLoader<TCellType, TCell, TChunkData>(viewPortWidth, viewPortHeight, chunkWidth, chunkHeight, generator, Convert, chunksOutsideViewportRadiusToLoad);
var (x, y) = GetChunkCoordinate(viewPortWidth / 2, viewPortHeight / 2);
_chunkLoader.SetCurrentChunk(x, y);

Expand Down Expand Up @@ -209,11 +206,6 @@ public GridBase(int viewPortWidth, int viewPortHeight, IProceduralGen<TCellType,
: this(viewPortWidth, viewPortHeight, viewPortWidth, viewPortHeight, generator, chunksOutsideViewportRadiusToLoad)
{ }

/// <summary>
/// Disposes data for this grid
/// </summary>
~GridBase() { Dispose(); }

/// <summary>
/// Returns all the cell positions within the chunk of the specified coordinate.
/// </summary>
Expand Down Expand Up @@ -773,17 +765,6 @@ public void UpdateScreenCells()
OnCellUpdate?.Invoke(null, new CellUpdateArgs<TCellType, TCell>(screenCoordinate, GetCell(x, y)));
}
}

/// <inheritdoc/>
public void Dispose()
{
if (_cancellationTokenSource != null)
{
_cancellationTokenSource.Cancel();
_cancellationTokenSource.Dispose();
}
GC.SuppressFinalize(this);
}
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Venomaus.FlowVitae/Venomaus.FlowVitae.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<NeutralLanguage>en</NeutralLanguage>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<VersionPrefix>1.3.7</VersionPrefix>
<VersionPrefix>1.3.8</VersionPrefix>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down

0 comments on commit bc9b4a3

Please sign in to comment.