2023-02-03 23:35:22 +01:00
|
|
|
using OSMDatastructure;
|
2023-02-06 17:32:55 +01:00
|
|
|
using Pathfinding;
|
2023-02-03 23:35:22 +01:00
|
|
|
|
|
|
|
namespace OSMImporter
|
|
|
|
{
|
|
|
|
public class RegionManager
|
|
|
|
{
|
|
|
|
private string workingDirectory { get; }
|
|
|
|
private readonly Dictionary<ulong, Region> _regions = new();
|
|
|
|
|
|
|
|
public RegionManager(string workingDirectory)
|
|
|
|
{
|
|
|
|
this.workingDirectory = workingDirectory;
|
|
|
|
}
|
2023-02-05 20:02:22 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Checks wether the Region is already loaded and returns the Region, or tries to load the Region from file in workingDirectory
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="coordinates">Coordinates of the Region (or within the Region) to load</param>
|
|
|
|
/// <returns>The Region at the specified Coordinates containing Nodes and Connections</returns>
|
|
|
|
/// <exception cref="FileNotFoundException">If the Regionfile can not be found.</exception>
|
|
|
|
public Region GetRegion(Coordinates coordinates)
|
2023-02-03 23:35:22 +01:00
|
|
|
{
|
|
|
|
if(this._regions.ContainsKey(coordinates.GetRegionHash()))
|
|
|
|
return this._regions[coordinates.GetRegionHash()];
|
|
|
|
else
|
|
|
|
{
|
2023-02-05 20:02:22 +01:00
|
|
|
Region loadedRegion = LoadRegion(coordinates);
|
|
|
|
this._regions.Add(loadedRegion.regionHash, value: loadedRegion);
|
2023-02-03 23:35:22 +01:00
|
|
|
return loadedRegion;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Region[] GetAllRegions()
|
|
|
|
{
|
|
|
|
return this._regions.Values.ToArray();
|
|
|
|
}
|
|
|
|
|
2023-02-05 20:02:22 +01:00
|
|
|
/// <summary>
|
|
|
|
///
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="coordinates">Coordinates of the Region (or within the Region) to load</param>
|
|
|
|
/// <returns>The Region at the specified Coordinates containing Nodes and Connections</returns>
|
|
|
|
/// <exception cref="FileNotFoundException">If the Regionfile can not be found.</exception>
|
|
|
|
private Region LoadRegion(Coordinates coordinates)
|
2023-02-03 23:35:22 +01:00
|
|
|
{
|
|
|
|
string fullPath = Path.Combine(workingDirectory, coordinates.GetRegionHash().ToString());
|
2023-02-05 20:44:43 +01:00
|
|
|
Console.WriteLine("Loading {0}", fullPath);
|
2023-02-03 23:35:22 +01:00
|
|
|
if (!File.Exists(fullPath))
|
2023-02-05 20:02:22 +01:00
|
|
|
{
|
|
|
|
throw new FileNotFoundException(string.Format("Region does not exist: {0}", fullPath));
|
|
|
|
}
|
2023-02-03 23:35:22 +01:00
|
|
|
|
|
|
|
FileStream fileStream = new FileStream(fullPath, FileMode.Open);
|
|
|
|
|
|
|
|
byte[] regionBytes = new byte[fileStream.Length];
|
2023-02-05 20:02:22 +01:00
|
|
|
int _ = fileStream.Read(regionBytes, 0, regionBytes.Length);
|
2023-02-03 23:35:22 +01:00
|
|
|
fileStream.Close();
|
|
|
|
|
|
|
|
return ByteConverter.ToRegion(regionBytes);
|
|
|
|
}
|
2023-02-05 20:02:22 +01:00
|
|
|
|
2023-02-06 17:32:55 +01:00
|
|
|
public OsmNode? GetNode(Coordinates coordinates)
|
2023-02-05 20:02:22 +01:00
|
|
|
{
|
2023-02-06 17:32:55 +01:00
|
|
|
Region regionWithNode = GetRegion(coordinates);
|
2023-02-05 20:02:22 +01:00
|
|
|
return regionWithNode.GetNode(coordinates);
|
|
|
|
}
|
2023-02-03 23:35:22 +01:00
|
|
|
}
|
|
|
|
}
|