diff options
Diffstat (limited to 'src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitivePathLookup.cs')
-rw-r--r-- | src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitivePathLookup.cs | 78 |
1 files changed, 24 insertions, 54 deletions
diff --git a/src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitivePathLookup.cs b/src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitivePathLookup.cs index 9cc00737..496d54c3 100644 --- a/src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitivePathLookup.cs +++ b/src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitivePathLookup.cs @@ -4,8 +4,8 @@ using System.IO; namespace StardewModdingAPI.Toolkit.Utilities.PathLookups { - /// <summary>An API for case-insensitive relative path lookups within a root directory.</summary> - internal class CaseInsensitivePathLookup : IFilePathLookup + /// <summary>An API for case-insensitive file lookups within a root directory.</summary> + internal class CaseInsensitiveFileLookup : IFileLookup { /********* ** Fields @@ -16,8 +16,8 @@ namespace StardewModdingAPI.Toolkit.Utilities.PathLookups /// <summary>A case-insensitive lookup of file paths within the <see cref="RootPath"/>. Each path is listed in both file path and asset name format, so it's usable in both contexts without needing to re-parse paths.</summary> private readonly Lazy<Dictionary<string, string>> RelativePathCache; - /// <summary>The case-insensitive path caches by root path.</summary> - private static readonly Dictionary<string, CaseInsensitivePathLookup> CachedRoots = new(StringComparer.OrdinalIgnoreCase); + /// <summary>The case-insensitive file lookups by root path.</summary> + private static readonly Dictionary<string, CaseInsensitiveFileLookup> CachedRoots = new(StringComparer.OrdinalIgnoreCase); /********* @@ -26,22 +26,28 @@ namespace StardewModdingAPI.Toolkit.Utilities.PathLookups /// <summary>Construct an instance.</summary> /// <param name="rootPath">The root directory path for relative paths.</param> /// <param name="searchOption">Which directories to scan from the root.</param> - public CaseInsensitivePathLookup(string rootPath, SearchOption searchOption = SearchOption.AllDirectories) + public CaseInsensitiveFileLookup(string rootPath, SearchOption searchOption = SearchOption.AllDirectories) { - this.RootPath = rootPath; + this.RootPath = PathUtilities.NormalizePath(rootPath); this.RelativePathCache = new(() => this.GetRelativePathCache(searchOption)); } /// <inheritdoc /> - public string GetFilePath(string relativePath) + public FileInfo GetFile(string relativePath) { - return this.GetImpl(PathUtilities.NormalizePath(relativePath)); - } + // invalid path + if (string.IsNullOrWhiteSpace(relativePath)) + throw new InvalidOperationException("Can't get a file from an empty relative path."); - /// <inheritdoc /> - public string GetAssetName(string relativePath) - { - return this.GetImpl(PathUtilities.NormalizeAssetName(relativePath)); + // already cached + if (this.RelativePathCache.Value.TryGetValue(relativePath, out string? resolved)) + return new(Path.Combine(this.RootPath, resolved)); + + // keep capitalization as-is + FileInfo file = new(Path.Combine(this.RootPath, relativePath)); + if (file.Exists) + this.RelativePathCache.Value[relativePath] = relativePath; + return file; } /// <inheritdoc /> @@ -61,17 +67,17 @@ namespace StardewModdingAPI.Toolkit.Utilities.PathLookups throw new InvalidOperationException($"Can't add relative path '{relativePath}' to the case-insensitive cache for '{this.RootPath}' because that file doesn't exist."); // cache path - this.CacheRawPath(this.RelativePathCache.Value, relativePath); + this.RelativePathCache.Value[relativePath] = 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 CaseInsensitivePathLookup GetCachedFor(string rootPath) + public static CaseInsensitiveFileLookup GetCachedFor(string rootPath) { rootPath = PathUtilities.NormalizePath(rootPath); - if (!CaseInsensitivePathLookup.CachedRoots.TryGetValue(rootPath, out CaseInsensitivePathLookup? cache)) - CaseInsensitivePathLookup.CachedRoots[rootPath] = cache = new CaseInsensitivePathLookup(rootPath); + if (!CaseInsensitiveFileLookup.CachedRoots.TryGetValue(rootPath, out CaseInsensitiveFileLookup? cache)) + CaseInsensitiveFileLookup.CachedRoots[rootPath] = cache = new CaseInsensitiveFileLookup(rootPath); return cache; } @@ -80,29 +86,6 @@ namespace StardewModdingAPI.Toolkit.Utilities.PathLookups /********* ** Private methods *********/ - /// <summary>Get the exact capitalization for a given relative path.</summary> - /// <param name="relativePath">The relative path. This must already be normalized into asset name or file path format (i.e. using <see cref="PathUtilities.NormalizeAssetName"/> or <see cref="PathUtilities.NormalizePath"/> respectively).</param> - /// <remarks>Returns the resolved path in the same format if found, else returns the path as-is.</remarks> - private string GetImpl(string relativePath) - { - // invalid path - if (string.IsNullOrWhiteSpace(relativePath)) - return relativePath; - - // already cached - if (this.RelativePathCache.Value.TryGetValue(relativePath, out string? resolved)) - return resolved; - - // keep capitalization as-is - if (File.Exists(Path.Combine(this.RootPath, relativePath))) - { - // file exists but isn't cached for some reason - // cache it now so any later references to it are case-insensitive - this.CacheRawPath(this.RelativePathCache.Value, relativePath); - } - return relativePath; - } - /// <summary>Get a case-insensitive lookup of file paths (see <see cref="RelativePathCache"/>).</summary> /// <param name="searchOption">Which directories to scan from the root.</param> private Dictionary<string, string> GetRelativePathCache(SearchOption searchOption) @@ -112,23 +95,10 @@ namespace StardewModdingAPI.Toolkit.Utilities.PathLookups foreach (string path in Directory.EnumerateFiles(this.RootPath, "*", searchOption)) { string relativePath = path.Substring(this.RootPath.Length + 1); - - this.CacheRawPath(cache, relativePath); + cache[relativePath] = relativePath; } return cache; } - - /// <summary>Add a raw relative path to the cache.</summary> - /// <param name="cache">The cache to update.</param> - /// <param name="relativePath">The relative path to cache, with its exact filesystem capitalization.</param> - private void CacheRawPath(IDictionary<string, string> cache, string relativePath) - { - string filePath = PathUtilities.NormalizePath(relativePath); - string assetName = PathUtilities.NormalizeAssetName(relativePath); - - cache[filePath] = filePath; - cache[assetName] = assetName; - } } } |