diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2022-05-08 20:18:06 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2022-05-08 20:18:06 -0400 |
commit | 09f69d986f4f44521d8a2cd745269dce4b83320e (patch) | |
tree | 9397ece62492cb7d8d95dc01f665c5dca08b5086 /src/SMAPI.Toolkit/Utilities | |
parent | e7e6327b3c85d52ab666aad2a054fbbdbd9431da (diff) | |
parent | cbe8b597cb55285dea2bb2c34ab26f148a76bc17 (diff) | |
download | SMAPI-09f69d986f4f44521d8a2cd745269dce4b83320e.tar.gz SMAPI-09f69d986f4f44521d8a2cd745269dce4b83320e.tar.bz2 SMAPI-09f69d986f4f44521d8a2cd745269dce4b83320e.zip |
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI.Toolkit/Utilities')
-rw-r--r-- | src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitiveFileLookup.cs (renamed from src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitivePathLookup.cs) | 78 | ||||
-rw-r--r-- | src/SMAPI.Toolkit/Utilities/PathLookups/IFileLookup.cs | 16 | ||||
-rw-r--r-- | src/SMAPI.Toolkit/Utilities/PathLookups/IFilePathLookup.cs | 20 | ||||
-rw-r--r-- | src/SMAPI.Toolkit/Utilities/PathLookups/MinimalFileLookup.cs | 52 | ||||
-rw-r--r-- | src/SMAPI.Toolkit/Utilities/PathLookups/MinimalPathLookup.cs | 31 |
5 files changed, 92 insertions, 105 deletions
diff --git a/src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitivePathLookup.cs b/src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitiveFileLookup.cs index 9cc00737..496d54c3 100644 --- a/src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitivePathLookup.cs +++ b/src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitiveFileLookup.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/IFileLookup.cs b/src/SMAPI.Toolkit/Utilities/PathLookups/IFileLookup.cs new file mode 100644 index 00000000..d43b5141 --- /dev/null +++ b/src/SMAPI.Toolkit/Utilities/PathLookups/IFileLookup.cs @@ -0,0 +1,16 @@ +using System.IO; + +namespace StardewModdingAPI.Toolkit.Utilities.PathLookups +{ + /// <summary>An API for file lookups within a root directory.</summary> + internal interface IFileLookup + { + /// <summary>Get the file for a given relative file path, if it exists.</summary> + /// <param name="relativePath">The relative path.</param> + FileInfo GetFile(string relativePath); + + /// <summary>Add a relative path that was just created by a SMAPI API.</summary> + /// <param name="relativePath">The relative path.</param> + void Add(string relativePath); + } +} diff --git a/src/SMAPI.Toolkit/Utilities/PathLookups/IFilePathLookup.cs b/src/SMAPI.Toolkit/Utilities/PathLookups/IFilePathLookup.cs deleted file mode 100644 index 678e1383..00000000 --- a/src/SMAPI.Toolkit/Utilities/PathLookups/IFilePathLookup.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace StardewModdingAPI.Toolkit.Utilities.PathLookups -{ - /// <summary>An API for relative path lookups within a root directory.</summary> - internal interface IFilePathLookup - { - /// <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> - /// <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); - - /// <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> - void Add(string relativePath); - } -} 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; + } + } +} diff --git a/src/SMAPI.Toolkit/Utilities/PathLookups/MinimalPathLookup.cs b/src/SMAPI.Toolkit/Utilities/PathLookups/MinimalPathLookup.cs deleted file mode 100644 index 2cf14704..00000000 --- a/src/SMAPI.Toolkit/Utilities/PathLookups/MinimalPathLookup.cs +++ /dev/null @@ -1,31 +0,0 @@ -namespace StardewModdingAPI.Toolkit.Utilities.PathLookups -{ - /// <summary>An API for relative path lookups within a root directory with minimal preprocessing.</summary> - internal class MinimalPathLookup : IFilePathLookup - { - /********* - ** Accessors - *********/ - /// <summary>A singleton instance for reuse.</summary> - public static readonly MinimalPathLookup Instance = new(); - - - /********* - ** Public methods - *********/ - /// <inheritdoc /> - public string GetFilePath(string relativePath) - { - return PathUtilities.NormalizePath(relativePath); - } - - /// <inheritdoc /> - public string GetAssetName(string relativePath) - { - return PathUtilities.NormalizeAssetName(relativePath); - } - - /// <inheritdoc /> - public void Add(string relativePath) { } - } -} |