diff options
Diffstat (limited to 'src/SMAPI.Toolkit/Utilities')
3 files changed, 61 insertions, 74 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; - } } } diff --git a/src/SMAPI.Toolkit/Utilities/PathLookups/IFilePathLookup.cs b/src/SMAPI.Toolkit/Utilities/PathLookups/IFilePathLookup.cs index 678e1383..d43b5141 100644 --- a/src/SMAPI.Toolkit/Utilities/PathLookups/IFilePathLookup.cs +++ b/src/SMAPI.Toolkit/Utilities/PathLookups/IFilePathLookup.cs @@ -1,20 +1,16 @@ +using System.IO; + namespace StardewModdingAPI.Toolkit.Utilities.PathLookups { - /// <summary>An API for relative path lookups within a root directory.</summary> - internal interface IFilePathLookup + /// <summary>An API for file lookups within a root directory.</summary> + internal interface IFileLookup { - /// <summary>Get the actual path for a given relative file path.</summary> - /// <param name="relativePath">The relative path.</param> - /// <remarks>Returns the resolved path in file path format, else the normalized <paramref name="relativePath"/>.</remarks> - string GetFilePath(string relativePath); - - /// <summary>Get the actual path for a given asset name.</summary> + /// <summary>Get the file for a given relative file path, if it exists.</summary> /// <param name="relativePath">The relative path.</param> - /// <remarks>Returns the resolved path in asset name format, else the normalized <paramref name="relativePath"/>.</remarks> - string GetAssetName(string relativePath); + FileInfo GetFile(string relativePath); /// <summary>Add a relative path that was just created by a SMAPI API.</summary> - /// <param name="relativePath">The relative path. This must already be normalized in asset name or file path format.</param> + /// <param name="relativePath">The relative path.</param> void Add(string relativePath); } } 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; + } } } |