OSMServer/Pathfinding/RegionManager.cs
2023-02-03 23:35:22 +01:00

49 lines
1.5 KiB
C#

using OSMDatastructure;
namespace OSMImporter
{
public class RegionManager
{
private string workingDirectory { get; }
private readonly Dictionary<ulong, Region> _regions = new();
public RegionManager(string workingDirectory)
{
this.workingDirectory = workingDirectory;
}
public Region? GetRegion(Coordinates coordinates)
{
if(this._regions.ContainsKey(coordinates.GetRegionHash()))
return this._regions[coordinates.GetRegionHash()];
else
{
Region? loadedRegion = LoadRegion(coordinates);
if(loadedRegion != null)
this._regions.Add(loadedRegion.regionHash, value: loadedRegion);
return loadedRegion;
}
}
public Region[] GetAllRegions()
{
return this._regions.Values.ToArray();
}
private Region? LoadRegion(Coordinates coordinates)
{
string fullPath = Path.Combine(workingDirectory, coordinates.GetRegionHash().ToString());
Console.WriteLine(fullPath);
if (!File.Exists(fullPath))
return null;
FileStream fileStream = new FileStream(fullPath, FileMode.Open);
byte[] regionBytes = new byte[fileStream.Length];
fileStream.Read(regionBytes, 0, regionBytes.Length);
fileStream.Close();
return ByteConverter.ToRegion(regionBytes);
}
}
}