diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2022-05-04 20:35:08 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2022-05-04 20:35:08 -0400 |
commit | c1342bd4cd6b75b24d11275bdd73ebf893f916ea (patch) | |
tree | 3c647582d242cbb6abc71ae7bbbbd14c63ce6558 /src/SMAPI.Toolkit/Utilities/PathLookups | |
parent | 42bf82d870ab744f13197ad781b7529a959861e1 (diff) | |
download | SMAPI-c1342bd4cd6b75b24d11275bdd73ebf893f916ea.tar.gz SMAPI-c1342bd4cd6b75b24d11275bdd73ebf893f916ea.tar.bz2 SMAPI-c1342bd4cd6b75b24d11275bdd73ebf893f916ea.zip |
disable case-insensitive paths by default pending performance rework
Diffstat (limited to 'src/SMAPI.Toolkit/Utilities/PathLookups')
3 files changed, 185 insertions, 0 deletions
diff --git a/src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitivePathLookup.cs b/src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitivePathLookup.cs new file mode 100644 index 00000000..9cc00737 --- /dev/null +++ b/src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitivePathLookup.cs @@ -0,0 +1,134 @@ +using System; +using System.Collections.Generic; +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 + { + /********* + ** Fields + *********/ + /// <summary>The root directory path for relative paths.</summary> + private readonly string RootPath; + + /// <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); + + + /********* + ** Public methods + *********/ + /// <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) + { + this.RootPath = rootPath; + this.RelativePathCache = new(() => this.GetRelativePathCache(searchOption)); + } + + /// <inheritdoc /> + public string GetFilePath(string relativePath) + { + return this.GetImpl(PathUtilities.NormalizePath(relativePath)); + } + + /// <inheritdoc /> + public string GetAssetName(string relativePath) + { + return this.GetImpl(PathUtilities.NormalizeAssetName(relativePath)); + } + + /// <inheritdoc /> + public void Add(string relativePath) + { + // skip if cache isn't created yet (no need to add files manually in that case) + if (!this.RelativePathCache.IsValueCreated) + return; + + // skip if already cached + if (this.RelativePathCache.Value.ContainsKey(relativePath)) + return; + + // make sure path exists + relativePath = PathUtilities.NormalizePath(relativePath); + if (!File.Exists(Path.Combine(this.RootPath, relativePath))) + 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); + } + + /// <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) + { + rootPath = PathUtilities.NormalizePath(rootPath); + + if (!CaseInsensitivePathLookup.CachedRoots.TryGetValue(rootPath, out CaseInsensitivePathLookup? cache)) + CaseInsensitivePathLookup.CachedRoots[rootPath] = cache = new CaseInsensitivePathLookup(rootPath); + + return cache; + } + + + /********* + ** 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) + { + Dictionary<string, string> cache = new(StringComparer.OrdinalIgnoreCase); + + foreach (string path in Directory.EnumerateFiles(this.RootPath, "*", searchOption)) + { + string relativePath = path.Substring(this.RootPath.Length + 1); + + this.CacheRawPath(cache, 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 new file mode 100644 index 00000000..678e1383 --- /dev/null +++ b/src/SMAPI.Toolkit/Utilities/PathLookups/IFilePathLookup.cs @@ -0,0 +1,20 @@ +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/MinimalPathLookup.cs b/src/SMAPI.Toolkit/Utilities/PathLookups/MinimalPathLookup.cs new file mode 100644 index 00000000..2cf14704 --- /dev/null +++ b/src/SMAPI.Toolkit/Utilities/PathLookups/MinimalPathLookup.cs @@ -0,0 +1,31 @@ +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) { } + } +} |