From c1342bd4cd6b75b24d11275bdd73ebf893f916ea Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 4 May 2022 20:35:08 -0400 Subject: disable case-insensitive paths by default pending performance rework --- .../Framework/ModScanning/ModScanner.cs | 18 +-- src/SMAPI.Toolkit/ModToolkit.cs | 10 +- .../Utilities/CaseInsensitivePathLookup.cs | 139 --------------------- .../PathLookups/CaseInsensitivePathLookup.cs | 134 ++++++++++++++++++++ .../Utilities/PathLookups/IFilePathLookup.cs | 20 +++ .../Utilities/PathLookups/MinimalPathLookup.cs | 31 +++++ 6 files changed, 201 insertions(+), 151 deletions(-) delete mode 100644 src/SMAPI.Toolkit/Utilities/CaseInsensitivePathLookup.cs create mode 100644 src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitivePathLookup.cs create mode 100644 src/SMAPI.Toolkit/Utilities/PathLookups/IFilePathLookup.cs create mode 100644 src/SMAPI.Toolkit/Utilities/PathLookups/MinimalPathLookup.cs (limited to 'src/SMAPI.Toolkit') diff --git a/src/SMAPI.Toolkit/Framework/ModScanning/ModScanner.cs b/src/SMAPI.Toolkit/Framework/ModScanning/ModScanner.cs index 24485620..aa4c3338 100644 --- a/src/SMAPI.Toolkit/Framework/ModScanning/ModScanner.cs +++ b/src/SMAPI.Toolkit/Framework/ModScanning/ModScanner.cs @@ -5,7 +5,7 @@ using System.Linq; using System.Text.RegularExpressions; using StardewModdingAPI.Toolkit.Serialization; using StardewModdingAPI.Toolkit.Serialization.Models; -using StardewModdingAPI.Toolkit.Utilities; +using StardewModdingAPI.Toolkit.Utilities.PathLookups; namespace StardewModdingAPI.Toolkit.Framework.ModScanning { @@ -95,19 +95,20 @@ namespace StardewModdingAPI.Toolkit.Framework.ModScanning /// Extract information about all mods in the given folder. /// The root folder containing mods. - public IEnumerable GetModFolders(string rootPath) + /// Whether to match file paths case-insensitively, even on Linux. + public IEnumerable GetModFolders(string rootPath, bool useCaseInsensitiveFilePaths) { DirectoryInfo root = new(rootPath); - return this.GetModFolders(root, root); + return this.GetModFolders(root, root, useCaseInsensitiveFilePaths); } /// Extract information about all mods in the given folder. /// The root folder containing mods. Only the will be searched, but this field allows it to be treated as a potential mod folder of its own. /// The mod path to search. - // /// If the folder contains multiple XNB mods, treat them as subfolders of a single mod. This is useful when reading a single mod archive, as opposed to a mods folder. - public IEnumerable GetModFolders(string rootPath, string modPath) + /// Whether to match file paths case-insensitively, even on Linux. + public IEnumerable GetModFolders(string rootPath, string modPath, bool useCaseInsensitiveFilePaths) { - return this.GetModFolders(root: new DirectoryInfo(rootPath), folder: new DirectoryInfo(modPath)); + return this.GetModFolders(root: new DirectoryInfo(rootPath), folder: new DirectoryInfo(modPath), useCaseInsensitiveFilePaths: useCaseInsensitiveFilePaths); } /// Extract information from a mod folder. @@ -195,7 +196,8 @@ namespace StardewModdingAPI.Toolkit.Framework.ModScanning /// Recursively extract information about all mods in the given folder. /// The root mod folder. /// The folder to search for mods. - private IEnumerable GetModFolders(DirectoryInfo root, DirectoryInfo folder) + /// Whether to match file paths case-insensitively, even on Linux. + private IEnumerable GetModFolders(DirectoryInfo root, DirectoryInfo folder, bool useCaseInsensitiveFilePaths) { bool isRoot = folder.FullName == root.FullName; @@ -214,7 +216,7 @@ namespace StardewModdingAPI.Toolkit.Framework.ModScanning // find mods in subfolders if (this.IsModSearchFolder(root, folder)) { - IEnumerable subfolders = folder.EnumerateDirectories().SelectMany(sub => this.GetModFolders(root, sub)); + IEnumerable subfolders = folder.EnumerateDirectories().SelectMany(sub => this.GetModFolders(root, sub, useCaseInsensitiveFilePaths)); if (!isRoot) subfolders = this.TryConsolidate(root, folder, subfolders.ToArray()); foreach (ModFolder subfolder in subfolders) diff --git a/src/SMAPI.Toolkit/ModToolkit.cs b/src/SMAPI.Toolkit/ModToolkit.cs index 51f6fa24..ce14b057 100644 --- a/src/SMAPI.Toolkit/ModToolkit.cs +++ b/src/SMAPI.Toolkit/ModToolkit.cs @@ -72,17 +72,19 @@ namespace StardewModdingAPI.Toolkit /// Extract information about all mods in the given folder. /// The root folder containing mods. - public IEnumerable GetModFolders(string rootPath) + /// Whether to match file paths case-insensitively, even on Linux. + public IEnumerable GetModFolders(string rootPath, bool useCaseInsensitiveFilePaths) { - return new ModScanner(this.JsonHelper).GetModFolders(rootPath); + return new ModScanner(this.JsonHelper).GetModFolders(rootPath, useCaseInsensitiveFilePaths); } /// Extract information about all mods in the given folder. /// The root folder containing mods. Only the will be searched, but this field allows it to be treated as a potential mod folder of its own. /// The mod path to search. - public IEnumerable GetModFolders(string rootPath, string modPath) + /// Whether to match file paths case-insensitively, even on Linux. + public IEnumerable GetModFolders(string rootPath, string modPath, bool useCaseInsensitiveFilePaths) { - return new ModScanner(this.JsonHelper).GetModFolders(rootPath, modPath); + return new ModScanner(this.JsonHelper).GetModFolders(rootPath, modPath, useCaseInsensitiveFilePaths); } /// Get an update URL for an update key (if valid). diff --git a/src/SMAPI.Toolkit/Utilities/CaseInsensitivePathLookup.cs b/src/SMAPI.Toolkit/Utilities/CaseInsensitivePathLookup.cs deleted file mode 100644 index 12fad008..00000000 --- a/src/SMAPI.Toolkit/Utilities/CaseInsensitivePathLookup.cs +++ /dev/null @@ -1,139 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; - -namespace StardewModdingAPI.Toolkit.Utilities -{ - /// Provides an API for case-insensitive relative path lookups within a root directory. - internal class CaseInsensitivePathLookup - { - /********* - ** Fields - *********/ - /// The root directory path for relative paths. - private readonly string RootPath; - - /// A case-insensitive lookup of file paths within the . 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. - private readonly Lazy> RelativePathCache; - - /// The case-insensitive path caches by root path. - private static readonly Dictionary CachedRoots = new(StringComparer.OrdinalIgnoreCase); - - - /********* - ** Public methods - *********/ - /// Construct an instance. - /// The root directory path for relative paths. - /// Which directories to scan from the root. - public CaseInsensitivePathLookup(string rootPath, SearchOption searchOption = SearchOption.AllDirectories) - { - this.RootPath = rootPath; - this.RelativePathCache = new(() => this.GetRelativePathCache(searchOption)); - } - - /// Get the exact capitalization for a given relative file path. - /// The relative path. - /// Returns the resolved path in file path format, else the normalized . - public string GetFilePath(string relativePath) - { - return this.GetImpl(PathUtilities.NormalizePath(relativePath)); - } - - /// Get the exact capitalization for a given asset name. - /// The relative path. - /// Returns the resolved path in asset name format, else the normalized . - public string GetAssetName(string relativePath) - { - return this.GetImpl(PathUtilities.NormalizeAssetName(relativePath)); - } - - /// Add a relative path that was just created by a SMAPI API. - /// The relative path. This must already be normalized in asset name or file path format. - 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); - } - - /// Get a cached dictionary of relative paths within a root path, for case-insensitive file lookups. - /// The root path to scan. - 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 - *********/ - /// Get the exact capitalization for a given relative path. - /// The relative path. This must already be normalized into asset name or file path format (i.e. using or respectively). - /// Returns the resolved path in the same format if found, else returns the path as-is. - 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; - } - - /// Get a case-insensitive lookup of file paths (see ). - /// Which directories to scan from the root. - private Dictionary GetRelativePathCache(SearchOption searchOption) - { - Dictionary 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; - } - - /// Add a raw relative path to the cache. - /// The cache to update. - /// The relative path to cache, with its exact filesystem capitalization. - private void CacheRawPath(IDictionary 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/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 +{ + /// An API for case-insensitive relative path lookups within a root directory. + internal class CaseInsensitivePathLookup : IFilePathLookup + { + /********* + ** Fields + *********/ + /// The root directory path for relative paths. + private readonly string RootPath; + + /// A case-insensitive lookup of file paths within the . 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. + private readonly Lazy> RelativePathCache; + + /// The case-insensitive path caches by root path. + private static readonly Dictionary CachedRoots = new(StringComparer.OrdinalIgnoreCase); + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The root directory path for relative paths. + /// Which directories to scan from the root. + public CaseInsensitivePathLookup(string rootPath, SearchOption searchOption = SearchOption.AllDirectories) + { + this.RootPath = rootPath; + this.RelativePathCache = new(() => this.GetRelativePathCache(searchOption)); + } + + /// + public string GetFilePath(string relativePath) + { + return this.GetImpl(PathUtilities.NormalizePath(relativePath)); + } + + /// + public string GetAssetName(string relativePath) + { + return this.GetImpl(PathUtilities.NormalizeAssetName(relativePath)); + } + + /// + 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); + } + + /// Get a cached dictionary of relative paths within a root path, for case-insensitive file lookups. + /// The root path to scan. + 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 + *********/ + /// Get the exact capitalization for a given relative path. + /// The relative path. This must already be normalized into asset name or file path format (i.e. using or respectively). + /// Returns the resolved path in the same format if found, else returns the path as-is. + 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; + } + + /// Get a case-insensitive lookup of file paths (see ). + /// Which directories to scan from the root. + private Dictionary GetRelativePathCache(SearchOption searchOption) + { + Dictionary 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; + } + + /// Add a raw relative path to the cache. + /// The cache to update. + /// The relative path to cache, with its exact filesystem capitalization. + private void CacheRawPath(IDictionary 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 +{ + /// An API for relative path lookups within a root directory. + internal interface IFilePathLookup + { + /// Get the actual path for a given relative file path. + /// The relative path. + /// Returns the resolved path in file path format, else the normalized . + string GetFilePath(string relativePath); + + /// Get the actual path for a given asset name. + /// The relative path. + /// Returns the resolved path in asset name format, else the normalized . + string GetAssetName(string relativePath); + + /// Add a relative path that was just created by a SMAPI API. + /// The relative path. This must already be normalized in asset name or file path format. + 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 +{ + /// An API for relative path lookups within a root directory with minimal preprocessing. + internal class MinimalPathLookup : IFilePathLookup + { + /********* + ** Accessors + *********/ + /// A singleton instance for reuse. + public static readonly MinimalPathLookup Instance = new(); + + + /********* + ** Public methods + *********/ + /// + public string GetFilePath(string relativePath) + { + return PathUtilities.NormalizePath(relativePath); + } + + /// + public string GetAssetName(string relativePath) + { + return PathUtilities.NormalizeAssetName(relativePath); + } + + /// + public void Add(string relativePath) { } + } +} -- cgit From b834ed7ef5095203529f8b77aee3f25f5387fbcc Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 6 May 2022 18:06:47 -0400 Subject: fix errors reading empty JSON files --- docs/release-notes.md | 1 + src/SMAPI.Toolkit/Serialization/JsonHelper.cs | 5 ++--- src/SMAPI/Framework/SMultiplayer.cs | 29 ++++++++++++++++++++++----- 3 files changed, 27 insertions(+), 8 deletions(-) (limited to 'src/SMAPI.Toolkit') diff --git a/docs/release-notes.md b/docs/release-notes.md index 99269bc6..a0d8826b 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -8,6 +8,7 @@ * Removed experimental 'aggressive memory optimizations' option. _This was disabled by default and is no longer needed in most cases. Memory usage will be better reduced by reworked asset propagation in the upcoming SMAPI 4.0.0._ * Fixed 'content file was not found' error when the game tries to load unlocalized text from a localizable mod data asset. + * Fixed error reading empty JSON files. These are now treated as if they didn't exist like before 3.14.0. * Updated compatibility list. ## 3.14.0 diff --git a/src/SMAPI.Toolkit/Serialization/JsonHelper.cs b/src/SMAPI.Toolkit/Serialization/JsonHelper.cs index 3c9308f2..1a003c51 100644 --- a/src/SMAPI.Toolkit/Serialization/JsonHelper.cs +++ b/src/SMAPI.Toolkit/Serialization/JsonHelper.cs @@ -108,12 +108,11 @@ namespace StardewModdingAPI.Toolkit.Serialization /// Deserialize JSON text if possible. /// The model type. /// The raw JSON text. - public TModel Deserialize(string json) + public TModel? Deserialize(string json) { try { - return JsonConvert.DeserializeObject(json, this.JsonSettings) - ?? throw new InvalidOperationException($"Couldn't deserialize model type '{typeof(TModel)}' from empty or null JSON."); + return JsonConvert.DeserializeObject(json, this.JsonSettings); } catch (JsonReaderException) { diff --git a/src/SMAPI/Framework/SMultiplayer.cs b/src/SMAPI/Framework/SMultiplayer.cs index e41e7edc..2badcbbf 100644 --- a/src/SMAPI/Framework/SMultiplayer.cs +++ b/src/SMAPI/Framework/SMultiplayer.cs @@ -9,6 +9,7 @@ using StardewModdingAPI.Events; using StardewModdingAPI.Framework.Events; using StardewModdingAPI.Framework.Networking; using StardewModdingAPI.Framework.Reflection; +using StardewModdingAPI.Internal; using StardewModdingAPI.Toolkit.Serialization; using StardewModdingAPI.Utilities; using StardewValley; @@ -489,8 +490,8 @@ namespace StardewModdingAPI.Framework private RemoteContextModel? ReadContext(BinaryReader reader) { string data = reader.ReadString(); - RemoteContextModel model = this.JsonHelper.Deserialize(data); - return model.ApiVersion != null + RemoteContextModel? model = this.JsonHelper.Deserialize(data); + return model?.ApiVersion != null ? model : null; // no data available for vanilla players } @@ -499,13 +500,31 @@ namespace StardewModdingAPI.Framework /// The raw message to parse. private void ReceiveModMessage(IncomingMessage message) { - // parse message + // read message JSON string json = message.Reader.ReadString(); - ModMessageModel model = this.JsonHelper.Deserialize(json); - HashSet playerIDs = new HashSet(model.ToPlayerIDs ?? this.GetKnownPlayerIDs()); if (this.LogNetworkTraffic) this.Monitor.Log($"Received message: {json}."); + // deserialize model + ModMessageModel? model; + try + { + model = this.JsonHelper.Deserialize(json); + if (model is null) + { + this.Monitor.Log($"Received invalid mod message from {message.FarmerID}.\nRaw message data: {json}"); + return; + } + } + catch (Exception ex) + { + this.Monitor.Log($"Received invalid mod message from {message.FarmerID}.\nRaw message data: {json}\nError details: {ex.GetLogSummary()}"); + return; + } + + // get player IDs + HashSet playerIDs = new HashSet(model.ToPlayerIDs ?? this.GetKnownPlayerIDs()); + // notify local mods if (playerIDs.Contains(Game1.player.UniqueMultiplayerID)) this.OnModMessageReceived(model); -- cgit