summaryrefslogtreecommitdiff
path: root/src/SMAPI.Toolkit
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2022-05-07 23:12:33 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2022-05-07 23:12:33 -0400
commit3db035312641b629aaa5517569d0d30cf71bac29 (patch)
tree8111c155735d5bdef4a5a91de8f59625d4224742 /src/SMAPI.Toolkit
parentd4ff9f3f5c108493452879938aa224adb556b7c3 (diff)
downloadSMAPI-3db035312641b629aaa5517569d0d30cf71bac29.tar.gz
SMAPI-3db035312641b629aaa5517569d0d30cf71bac29.tar.bz2
SMAPI-3db035312641b629aaa5517569d0d30cf71bac29.zip
simplify and rewrite case-insensitive file path feature
Diffstat (limited to 'src/SMAPI.Toolkit')
-rw-r--r--src/SMAPI.Toolkit/Framework/ModScanning/ModScanner.cs22
-rw-r--r--src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitivePathLookup.cs78
-rw-r--r--src/SMAPI.Toolkit/Utilities/PathLookups/IFilePathLookup.cs18
-rw-r--r--src/SMAPI.Toolkit/Utilities/PathLookups/MinimalPathLookup.cs39
4 files changed, 73 insertions, 84 deletions
diff --git a/src/SMAPI.Toolkit/Framework/ModScanning/ModScanner.cs b/src/SMAPI.Toolkit/Framework/ModScanning/ModScanner.cs
index aa4c3338..a85ef109 100644
--- a/src/SMAPI.Toolkit/Framework/ModScanning/ModScanner.cs
+++ b/src/SMAPI.Toolkit/Framework/ModScanning/ModScanner.cs
@@ -114,10 +114,11 @@ namespace StardewModdingAPI.Toolkit.Framework.ModScanning
/// <summary>Extract information from a mod folder.</summary>
/// <param name="root">The root folder containing mods.</param>
/// <param name="searchFolder">The folder to search for a mod.</param>
- public ModFolder ReadFolder(DirectoryInfo root, DirectoryInfo searchFolder)
+ /// <param name="useCaseInsensitiveFilePaths">Whether to match file paths case-insensitively, even on Linux.</param>
+ public ModFolder ReadFolder(DirectoryInfo root, DirectoryInfo searchFolder, bool useCaseInsensitiveFilePaths)
{
// find manifest.json
- FileInfo? manifestFile = this.FindManifest(searchFolder);
+ FileInfo? manifestFile = this.FindManifest(searchFolder, useCaseInsensitiveFilePaths);
// set appropriate invalid-mod error
if (manifestFile == null)
@@ -225,7 +226,7 @@ namespace StardewModdingAPI.Toolkit.Framework.ModScanning
// treat as mod folder
else
- yield return this.ReadFolder(root, folder);
+ yield return this.ReadFolder(root, folder, useCaseInsensitiveFilePaths);
}
/// <summary>Consolidate adjacent folders into one mod folder, if possible.</summary>
@@ -250,7 +251,8 @@ namespace StardewModdingAPI.Toolkit.Framework.ModScanning
/// <summary>Find the manifest for a mod folder.</summary>
/// <param name="folder">The folder to search.</param>
- private FileInfo? FindManifest(DirectoryInfo folder)
+ /// <param name="useCaseInsensitiveFilePaths">Whether to match file paths case-insensitively, even on Linux.</param>
+ private FileInfo? FindManifest(DirectoryInfo folder, bool useCaseInsensitiveFilePaths)
{
// check for conventional manifest in current folder
const string defaultName = "manifest.json";
@@ -259,14 +261,14 @@ namespace StardewModdingAPI.Toolkit.Framework.ModScanning
return file;
// check for manifest with incorrect capitalization
+ if (useCaseInsensitiveFilePaths)
{
- CaseInsensitivePathLookup pathLookup = new(folder.FullName, SearchOption.TopDirectoryOnly); // don't use GetCachedFor, since we only need it temporarily
- string realName = pathLookup.GetFilePath(defaultName);
- if (realName != defaultName)
- file = new(Path.Combine(folder.FullName, realName));
+ CaseInsensitiveFileLookup fileLookup = new(folder.FullName, SearchOption.TopDirectoryOnly); // don't use GetCachedFor, since we only need it temporarily
+ file = fileLookup.GetFile(defaultName);
+ return file.Exists
+ ? file
+ : null;
}
- if (file.Exists)
- return file;
// not found
return null;
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;
+ }
}
}