diff options
Diffstat (limited to 'src/SMAPI.Toolkit/Utilities/PathLookups/MinimalPathLookup.cs')
-rw-r--r-- | src/SMAPI.Toolkit/Utilities/PathLookups/MinimalPathLookup.cs | 39 |
1 files changed, 30 insertions, 9 deletions
diff --git a/src/SMAPI.Toolkit/Utilities/PathLookups/MinimalPathLookup.cs b/src/SMAPI.Toolkit/Utilities/PathLookups/MinimalPathLookup.cs index 2cf14704..414b569b 100644 --- a/src/SMAPI.Toolkit/Utilities/PathLookups/MinimalPathLookup.cs +++ b/src/SMAPI.Toolkit/Utilities/PathLookups/MinimalPathLookup.cs @@ -1,31 +1,52 @@ +using System.Collections.Generic; +using System.IO; + namespace StardewModdingAPI.Toolkit.Utilities.PathLookups { - /// <summary>An API for relative path lookups within a root directory with minimal preprocessing.</summary> - internal class MinimalPathLookup : IFilePathLookup + /// <summary>An API for file lookups within a root directory with minimal preprocessing.</summary> + internal class MinimalFileLookup : IFileLookup { /********* ** Accessors *********/ - /// <summary>A singleton instance for reuse.</summary> - public static readonly MinimalPathLookup Instance = new(); + /// <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 *********/ - /// <inheritdoc /> - public string GetFilePath(string relativePath) + /// <summary>Construct an instance.</summary> + /// <param name="rootPath">The root directory path for relative paths.</param> + public MinimalFileLookup(string rootPath) { - return PathUtilities.NormalizePath(relativePath); + this.RootPath = rootPath; } /// <inheritdoc /> - public string GetAssetName(string relativePath) + public FileInfo GetFile(string relativePath) { - return PathUtilities.NormalizeAssetName(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; + } } } |