From 371989b34d8b575a30a81e5a854cb1d2d4d7256e Mon Sep 17 00:00:00 2001 From: glax Date: Wed, 17 May 2023 19:06:48 +0200 Subject: [PATCH] Make GetRegion more readable / Less convoluted and complicated due to multithreading. --- Pathfinding/RegionManager.cs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/Pathfinding/RegionManager.cs b/Pathfinding/RegionManager.cs index 4c2d8d3..dc9078c 100644 --- a/Pathfinding/RegionManager.cs +++ b/Pathfinding/RegionManager.cs @@ -23,17 +23,13 @@ namespace Pathfinding public Region? GetRegion(ulong id) { - - if(_regions.TryGetValue(id, out Region? retRegion)) - return retRegion; - - Region? loadedRegion = RegionFromId(id); - - if(_regions.TryGetValue(id, out Region? retRegion2)) //Prevent other thread from changing collection - return retRegion2; - if(loadedRegion is not null) - _regions.TryAdd(loadedRegion.regionHash, loadedRegion); - + if (!_regions.ContainsKey(id)) + { + Region? loadedRegion = RegionFromId(id); + if (loadedRegion is not null) + _regions.TryAdd(loadedRegion.regionHash, loadedRegion); + return _regions[id]; //return from _regions instead of loadedRegion for multithreading/pointers + } return _regions[id]; } @@ -46,7 +42,7 @@ namespace Pathfinding { if (!File.Exists(filePath)) { - throw new FileNotFoundException(filePath); + //throw new FileNotFoundException(filePath); return null; }