qstbak/SmartNav/SmartNav.Data/WaterMapService.cs
2026-08-22 10:25:22 +10:00

46 lines
1.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using Microsoft.Extensions.Logging;
using SmartNav.Model.Navigation;
namespace SmartNav.Data;
public sealed class WaterMapService
{
private readonly Lazy<IReadOnlyDictionary<ushort, TerritoryWaterMap>> _maps;
public WaterMapService(SmartNavDataOptions dataOptions, ILogger<WaterMapService> logger)
{
_maps = new Lazy<IReadOnlyDictionary<ushort, TerritoryWaterMap>>(delegate
{
try
{
var (text, readOnlyDictionary) = AssemblyWaterMapLoader.GetWaterMaps(dataOptions.DevSourceDirectory?.FullName);
logger.LogInformation("WaterMapService: {Count} territories with water maps loaded (game version {GameVersion})", readOnlyDictionary.Count, text ?? "<none>");
return readOnlyDictionary;
}
catch (Exception exception)
{
logger.LogError(exception, "Water cache could not be loaded - routing proceeds without water data");
return new Dictionary<ushort, TerritoryWaterMap>();
}
}, LazyThreadSafetyMode.ExecutionAndPublication);
}
internal WaterMapService(IReadOnlyDictionary<ushort, TerritoryWaterMap> maps)
{
_maps = new Lazy<IReadOnlyDictionary<ushort, TerritoryWaterMap>>(maps);
}
public bool TryGetMap(uint territoryId, [NotNullWhen(true)] out TerritoryWaterMap? map)
{
if (territoryId > 65535)
{
map = null;
return false;
}
return _maps.Value.TryGetValue((ushort)territoryId, out map);
}
}