diff options
Diffstat (limited to 'src/SMAPI.Toolkit/Utilities/PathLookups/MinimalFileLookup.cs')
-rw-r--r-- | src/SMAPI.Toolkit/Utilities/PathLookups/MinimalFileLookup.cs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/SMAPI.Toolkit/Utilities/PathLookups/MinimalFileLookup.cs b/src/SMAPI.Toolkit/Utilities/PathLookups/MinimalFileLookup.cs new file mode 100644 index 00000000..414b569b --- /dev/null +++ b/src/SMAPI.Toolkit/Utilities/PathLookups/MinimalFileLookup.cs @@ -0,0 +1,52 @@ +using System.Collections.Generic; +using System.IO; + +namespace StardewModdingAPI.Toolkit.Utilities.PathLookups +{ + /// <summary>An API for file lookups within a root directory with minimal preprocessing.</summary> + internal class MinimalFileLookup : IFileLookup + { + /********* + ** Accessors + *********/ + /// <summary>The file lookups by root path.</summary> + private static readonly Dictionary<string, MinimalFileLookup> CachedRoots = new(); + + /// <summary>The root directory path for relative paths.</summary> + private readonly string RootPath; + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="rootPath">The root directory path for relative paths.</param> + public MinimalFileLookup(string rootPath) + { + this.RootPath = rootPath; + } + + /// <inheritdoc /> + public FileInfo GetFile(string relativePath) + { + return new( + Path.Combine(this.RootPath, PathUtilities.NormalizePath(relativePath)) + ); + } + + /// <inheritdoc /> + public void Add(string relativePath) { } + + /// <summary>Get a cached dictionary of relative paths within a root path, for case-insensitive file lookups.</summary> + /// <param name="rootPath">The root path to scan.</param> + public static MinimalFileLookup GetCachedFor(string rootPath) + { + rootPath = PathUtilities.NormalizePath(rootPath); + + if (!MinimalFileLookup.CachedRoots.TryGetValue(rootPath, out MinimalFileLookup? lookup)) + MinimalFileLookup.CachedRoots[rootPath] = lookup = new MinimalFileLookup(rootPath); + + return lookup; + } + } +} |