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> _maps; public WaterMapService(SmartNavDataOptions dataOptions, ILogger logger) { _maps = new Lazy>(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 ?? ""); return readOnlyDictionary; } catch (Exception exception) { logger.LogError(exception, "Water cache could not be loaded - routing proceeds without water data"); return new Dictionary(); } }, LazyThreadSafetyMode.ExecutionAndPublication); } internal WaterMapService(IReadOnlyDictionary maps) { _maps = new Lazy>(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); } }