Created Pathfinding
This commit is contained in:
49
Pathfinding/RegionManager.cs
Normal file
49
Pathfinding/RegionManager.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user