Make GetRegion more readable / Less convoluted and complicated due to multithreading.

This commit is contained in:
glax 2023-05-17 19:06:48 +02:00
parent e53d1086cc
commit 371989b34d

View File

@ -23,17 +23,13 @@ namespace Pathfinding
public Region? GetRegion(ulong id) public Region? GetRegion(ulong id)
{ {
if (!_regions.ContainsKey(id))
if(_regions.TryGetValue(id, out Region? retRegion)) {
return retRegion; Region? loadedRegion = RegionFromId(id);
if (loadedRegion is not null)
Region? loadedRegion = RegionFromId(id); _regions.TryAdd(loadedRegion.regionHash, loadedRegion);
return _regions[id]; //return from _regions instead of loadedRegion for multithreading/pointers
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);
return _regions[id]; return _regions[id];
} }
@ -46,7 +42,7 @@ namespace Pathfinding
{ {
if (!File.Exists(filePath)) if (!File.Exists(filePath))
{ {
throw new FileNotFoundException(filePath); //throw new FileNotFoundException(filePath);
return null; return null;
} }