| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.IO; |
| | 4 | | using System.Linq; |
| | 5 | | using System.Text.Json; |
| | 6 | | using Itinero.Instructions.Configuration; |
| | 7 | | using Itinero.Instructions.ToText; |
| | 8 | | using Itinero.Instructions.Types.Generators; |
| | 9 | | // ReSharper disable MemberCanBePrivate.Global |
| | 10 | |
|
| | 11 | | namespace Itinero.Instructions; |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// Settings for the route instruction generator. |
| | 15 | | /// </summary> |
| | 16 | | public class RouteInstructionGeneratorSettings |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// Individual instruction generators. |
| | 20 | | /// </summary> |
| 4 | 21 | | public List<IInstructionGenerator> Generators { get; private set; } = new(); |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Text generators per language code. |
| | 25 | | /// </summary> |
| 6 | 26 | | public Dictionary<string, IInstructionToText> Languages { get; private set; } = new(); |
| | 27 | |
|
| 0 | 28 | | private static readonly Lazy<RouteInstructionGeneratorSettings> DefaultLazy = new(() => |
| 0 | 29 | | { |
| 0 | 30 | | using var stream = |
| 0 | 31 | | typeof(IndexedRoute).Assembly.GetManifestResourceStream( |
| 0 | 32 | | "Itinero.Instructions.Configuration.default.json"); |
| 0 | 33 | | return FromStream(stream ?? throw new InvalidOperationException("Default not found")); |
| 0 | 34 | | }); |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Gets the default settings. |
| | 38 | | /// </summary> |
| 0 | 39 | | public static RouteInstructionGeneratorSettings Default => DefaultLazy.Value; |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Loads settings from a file. |
| | 43 | | /// </summary> |
| | 44 | | /// <param name="path">The path to the settings file.</param> |
| | 45 | | /// <param name="customGenerators">The custom generators.</param> |
| | 46 | | /// <returns>The settings.</returns> |
| | 47 | | public static RouteInstructionGeneratorSettings FromConfigFile(string path, |
| | 48 | | IEnumerable<IInstructionGenerator>? customGenerators = null) |
| 0 | 49 | | { |
| 0 | 50 | | return FromStream(File.OpenRead(path), customGenerators); |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Parses settings from the given stream. |
| | 55 | | /// </summary> |
| | 56 | | /// <param name="stream">The stream.</param> |
| | 57 | | /// <param name="customGenerators">Any custom generators.</param> |
| | 58 | | /// <returns>The settings.</returns> |
| | 59 | | public static RouteInstructionGeneratorSettings FromStream(Stream stream, |
| | 60 | | IEnumerable<IInstructionGenerator>? customGenerators = null) |
| 2 | 61 | | { |
| 2 | 62 | | using var streamReader = new StreamReader(stream); |
| 2 | 63 | | return FromJson(streamReader.ReadToEnd(), customGenerators); |
| 2 | 64 | | } |
| | 65 | |
|
| | 66 | | /// <summary> |
| | 67 | | /// Parses settings from the given json string. |
| | 68 | | /// </summary> |
| | 69 | | /// <param name="json">A string with the json.</param> |
| | 70 | | /// <param name="customGenerators">Any custom generators.</param> |
| | 71 | | /// <returns>The settings.</returns> |
| | 72 | | public static RouteInstructionGeneratorSettings FromJson(string json, |
| | 73 | | IEnumerable<IInstructionGenerator>? customGenerators = null) |
| 2 | 74 | | { |
| | 75 | | // collect all the hardcoded generators. |
| 2 | 76 | | var allGenerators = new Dictionary<string, IInstructionGenerator>(AllGenerators.Generators |
| 34 | 77 | | .ToDictionary(x => x.Name, x => x)); |
| | 78 | |
|
| | 79 | | // add custom generators. |
| 2 | 80 | | if (customGenerators != null) |
| 0 | 81 | | { |
| 0 | 82 | | foreach (var customGenerator in customGenerators) |
| 0 | 83 | | { |
| 0 | 84 | | allGenerators[customGenerator.Name] = customGenerator; |
| 0 | 85 | | } |
| 0 | 86 | | } |
| | 87 | |
|
| | 88 | | // read settings. |
| 2 | 89 | | var (generators, translations) = |
| 2 | 90 | | ConfigurationParser.ParseRouteToInstructions( |
| 2 | 91 | | JsonDocument.Parse(json).RootElement, allGenerators); |
| | 92 | |
|
| 2 | 93 | | return new RouteInstructionGeneratorSettings() |
| 2 | 94 | | { |
| 2 | 95 | | Generators = new List<IInstructionGenerator>(generators), |
| 2 | 96 | | Languages = translations |
| 2 | 97 | | }; |
| 2 | 98 | | } |
| | 99 | | } |