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 --- src/SMAPI/Framework/ContentCoordinator.cs | 10 +++++++-- .../Framework/ContentManagers/ModContentManager.cs | 13 +++++------ src/SMAPI/Framework/ContentPack.cs | 9 ++++---- src/SMAPI/Framework/ModHelpers/ModContentHelper.cs | 18 ++++++++-------- src/SMAPI/Framework/ModLoading/ModResolver.cs | 12 +++++++---- src/SMAPI/Framework/Models/SConfig.cs | 12 ++++++++--- src/SMAPI/Framework/SCore.cs | 25 ++++++++++++++++------ 7 files changed, 64 insertions(+), 35 deletions(-) (limited to 'src/SMAPI/Framework') diff --git a/src/SMAPI/Framework/ContentCoordinator.cs b/src/SMAPI/Framework/ContentCoordinator.cs index 84fff250..1a82d194 100644 --- a/src/SMAPI/Framework/ContentCoordinator.cs +++ b/src/SMAPI/Framework/ContentCoordinator.cs @@ -16,6 +16,7 @@ using StardewModdingAPI.Internal; using StardewModdingAPI.Metadata; using StardewModdingAPI.Toolkit.Serialization; using StardewModdingAPI.Toolkit.Utilities; +using StardewModdingAPI.Toolkit.Utilities.PathLookups; using StardewValley; using StardewValley.GameData; using xTile; @@ -34,6 +35,9 @@ namespace StardewModdingAPI.Framework /// Whether to enable more aggressive memory optimizations. private readonly bool AggressiveMemoryOptimizations; + /// Get a file path lookup for the given directory. + private readonly Func GetFilePathLookup; + /// Encapsulates monitoring and logging. private readonly IMonitor Monitor; @@ -115,11 +119,13 @@ namespace StardewModdingAPI.Framework /// A callback to invoke the first time *any* game content manager loads an asset. /// A callback to invoke when an asset is fully loaded. /// Whether to enable more aggressive memory optimizations. + /// Get a file path lookup for the given directory. /// A callback to invoke when any asset names have been invalidated from the cache. /// Get the load/edit operations to apply to an asset by querying registered event handlers. - public ContentCoordinator(IServiceProvider serviceProvider, string rootDirectory, CultureInfo currentCulture, IMonitor monitor, Reflector reflection, JsonHelper jsonHelper, Action onLoadingFirstAsset, Action onAssetLoaded, bool aggressiveMemoryOptimizations, Action> onAssetsInvalidated, Func> requestAssetOperations) + public ContentCoordinator(IServiceProvider serviceProvider, string rootDirectory, CultureInfo currentCulture, IMonitor monitor, Reflector reflection, JsonHelper jsonHelper, Action onLoadingFirstAsset, Action onAssetLoaded, bool aggressiveMemoryOptimizations, Func getFilePathLookup, Action> onAssetsInvalidated, Func> requestAssetOperations) { this.AggressiveMemoryOptimizations = aggressiveMemoryOptimizations; + this.GetFilePathLookup = getFilePathLookup; this.Monitor = monitor ?? throw new ArgumentNullException(nameof(monitor)); this.Reflection = reflection; this.JsonHelper = jsonHelper; @@ -208,7 +214,7 @@ namespace StardewModdingAPI.Framework jsonHelper: this.JsonHelper, onDisposing: this.OnDisposing, aggressiveMemoryOptimizations: this.AggressiveMemoryOptimizations, - relativePathCache: CaseInsensitivePathLookup.GetCachedFor(rootDirectory) + relativePathLookup: this.GetFilePathLookup(rootDirectory) ); this.ContentManagers.Add(manager); return manager; diff --git a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs index 8f64c5a8..91de769f 100644 --- a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs @@ -10,6 +10,7 @@ using StardewModdingAPI.Framework.Exceptions; using StardewModdingAPI.Framework.Reflection; using StardewModdingAPI.Toolkit.Serialization; using StardewModdingAPI.Toolkit.Utilities; +using StardewModdingAPI.Toolkit.Utilities.PathLookups; using StardewValley; using xTile; using xTile.Format; @@ -32,8 +33,8 @@ namespace StardewModdingAPI.Framework.ContentManagers /// The game content manager used for map tilesheets not provided by the mod. private readonly IContentManager GameContentManager; - /// A case-insensitive lookup of relative paths within the . - private readonly CaseInsensitivePathLookup RelativePathCache; + /// A lookup for relative paths within the . + private readonly IFilePathLookup RelativePathLookup; /// If a map tilesheet's image source has no file extensions, the file extensions to check for in the local mod folder. private readonly string[] LocalTilesheetExtensions = { ".png", ".xnb" }; @@ -55,12 +56,12 @@ namespace StardewModdingAPI.Framework.ContentManagers /// Encapsulates SMAPI's JSON file parsing. /// A callback to invoke when the content manager is being disposed. /// Whether to enable more aggressive memory optimizations. - /// A case-insensitive lookup of relative paths within the . - public ModContentManager(string name, IContentManager gameContentManager, IServiceProvider serviceProvider, string modName, string rootDirectory, CultureInfo currentCulture, ContentCoordinator coordinator, IMonitor monitor, Reflector reflection, JsonHelper jsonHelper, Action onDisposing, bool aggressiveMemoryOptimizations, CaseInsensitivePathLookup relativePathCache) + /// A lookup for relative paths within the . + public ModContentManager(string name, IContentManager gameContentManager, IServiceProvider serviceProvider, string modName, string rootDirectory, CultureInfo currentCulture, ContentCoordinator coordinator, IMonitor monitor, Reflector reflection, JsonHelper jsonHelper, Action onDisposing, bool aggressiveMemoryOptimizations, IFilePathLookup relativePathLookup) : base(name, serviceProvider, rootDirectory, currentCulture, coordinator, monitor, reflection, onDisposing, isNamespaced: true, aggressiveMemoryOptimizations: aggressiveMemoryOptimizations) { this.GameContentManager = gameContentManager; - this.RelativePathCache = relativePathCache; + this.RelativePathLookup = relativePathLookup; this.JsonHelper = jsonHelper; this.ModName = modName; @@ -257,7 +258,7 @@ namespace StardewModdingAPI.Framework.ContentManagers private FileInfo GetModFile(string path) { // map to case-insensitive path if needed - path = this.RelativePathCache.GetFilePath(path); + path = this.RelativePathLookup.GetFilePath(path); // try exact match FileInfo file = new(Path.Combine(this.FullRootDirectory, path)); diff --git a/src/SMAPI/Framework/ContentPack.cs b/src/SMAPI/Framework/ContentPack.cs index dde33c95..9503a0e6 100644 --- a/src/SMAPI/Framework/ContentPack.cs +++ b/src/SMAPI/Framework/ContentPack.cs @@ -3,6 +3,7 @@ using System.IO; using StardewModdingAPI.Framework.ModHelpers; using StardewModdingAPI.Toolkit.Serialization; using StardewModdingAPI.Toolkit.Utilities; +using StardewModdingAPI.Toolkit.Utilities.PathLookups; namespace StardewModdingAPI.Framework { @@ -15,8 +16,8 @@ namespace StardewModdingAPI.Framework /// Encapsulates SMAPI's JSON file parsing. private readonly JsonHelper JsonHelper; - /// A case-insensitive lookup of relative paths within the . - private readonly CaseInsensitivePathLookup RelativePathCache; + /// A lookup for relative paths within the . + private readonly IFilePathLookup RelativePathCache; /********* @@ -47,8 +48,8 @@ namespace StardewModdingAPI.Framework /// Provides an API for loading content assets from the content pack's folder. /// Provides translations stored in the content pack's i18n folder. /// Encapsulates SMAPI's JSON file parsing. - /// A case-insensitive lookup of relative paths within the . - public ContentPack(string directoryPath, IManifest manifest, IModContentHelper content, TranslationHelper translation, JsonHelper jsonHelper, CaseInsensitivePathLookup relativePathCache) + /// A lookup for relative paths within the . + public ContentPack(string directoryPath, IManifest manifest, IModContentHelper content, TranslationHelper translation, JsonHelper jsonHelper, IFilePathLookup relativePathCache) { this.DirectoryPath = directoryPath; this.Manifest = manifest; diff --git a/src/SMAPI/Framework/ModHelpers/ModContentHelper.cs b/src/SMAPI/Framework/ModHelpers/ModContentHelper.cs index def0b728..74ea73de 100644 --- a/src/SMAPI/Framework/ModHelpers/ModContentHelper.cs +++ b/src/SMAPI/Framework/ModHelpers/ModContentHelper.cs @@ -4,7 +4,7 @@ using StardewModdingAPI.Framework.Content; using StardewModdingAPI.Framework.ContentManagers; using StardewModdingAPI.Framework.Exceptions; using StardewModdingAPI.Framework.Reflection; -using StardewModdingAPI.Toolkit.Utilities; +using StardewModdingAPI.Toolkit.Utilities.PathLookups; namespace StardewModdingAPI.Framework.ModHelpers { @@ -23,8 +23,8 @@ namespace StardewModdingAPI.Framework.ModHelpers /// The friendly mod name for use in errors. private readonly string ModName; - /// A case-insensitive lookup of relative paths within the . - private readonly CaseInsensitivePathLookup RelativePathCache; + /// A lookup for relative paths within the . + private readonly IFilePathLookup RelativePathLookup; /// Simplifies access to private code. private readonly Reflector Reflection; @@ -39,9 +39,9 @@ namespace StardewModdingAPI.Framework.ModHelpers /// The mod using this instance. /// The friendly mod name for use in errors. /// The game content manager used for map tilesheets not provided by the mod. - /// A case-insensitive lookup of relative paths within the . + /// A lookup for relative paths within the . /// Simplifies access to private code. - public ModContentHelper(ContentCoordinator contentCore, string modFolderPath, IModMetadata mod, string modName, IContentManager gameContentManager, CaseInsensitivePathLookup relativePathCache, Reflector reflection) + public ModContentHelper(ContentCoordinator contentCore, string modFolderPath, IModMetadata mod, string modName, IContentManager gameContentManager, IFilePathLookup relativePathLookup, Reflector reflection) : base(mod) { string managedAssetPrefix = contentCore.GetManagedAssetPrefix(mod.Manifest.UniqueID); @@ -49,7 +49,7 @@ namespace StardewModdingAPI.Framework.ModHelpers this.ContentCore = contentCore; this.ModContentManager = contentCore.CreateModContentManager(managedAssetPrefix, modName, modFolderPath, gameContentManager); this.ModName = modName; - this.RelativePathCache = relativePathCache; + this.RelativePathLookup = relativePathLookup; this.Reflection = reflection; } @@ -57,7 +57,7 @@ namespace StardewModdingAPI.Framework.ModHelpers public T Load(string relativePath) where T : notnull { - relativePath = this.RelativePathCache.GetAssetName(relativePath); + relativePath = this.RelativePathLookup.GetAssetName(relativePath); IAssetName assetName = this.ContentCore.ParseAssetName(relativePath, allowLocales: false); @@ -74,7 +74,7 @@ namespace StardewModdingAPI.Framework.ModHelpers /// public IAssetName GetInternalAssetName(string relativePath) { - relativePath = this.RelativePathCache.GetAssetName(relativePath); + relativePath = this.RelativePathLookup.GetAssetName(relativePath); return this.ModContentManager.GetInternalAssetKey(relativePath); } @@ -86,7 +86,7 @@ namespace StardewModdingAPI.Framework.ModHelpers throw new ArgumentNullException(nameof(data), "Can't get a patch helper for a null value."); relativePath = relativePath != null - ? this.RelativePathCache.GetAssetName(relativePath) + ? this.RelativePathLookup.GetAssetName(relativePath) : $"temp/{Guid.NewGuid():N}"; return new AssetDataForObject( diff --git a/src/SMAPI/Framework/ModLoading/ModResolver.cs b/src/SMAPI/Framework/ModLoading/ModResolver.cs index 74e7cb32..1b1fa04e 100644 --- a/src/SMAPI/Framework/ModLoading/ModResolver.cs +++ b/src/SMAPI/Framework/ModLoading/ModResolver.cs @@ -9,6 +9,7 @@ using StardewModdingAPI.Toolkit.Framework.ModScanning; using StardewModdingAPI.Toolkit.Framework.UpdateData; using StardewModdingAPI.Toolkit.Serialization.Models; using StardewModdingAPI.Toolkit.Utilities; +using StardewModdingAPI.Toolkit.Utilities.PathLookups; namespace StardewModdingAPI.Framework.ModLoading { @@ -22,10 +23,11 @@ namespace StardewModdingAPI.Framework.ModLoading /// The mod toolkit. /// The root path to search for mods. /// Handles access to SMAPI's internal mod metadata list. + /// Whether to match file paths case-insensitively, even on Linux. /// Returns the manifests by relative folder. - public IEnumerable ReadManifests(ModToolkit toolkit, string rootPath, ModDatabase modDatabase) + public IEnumerable ReadManifests(ModToolkit toolkit, string rootPath, ModDatabase modDatabase, bool useCaseInsensitiveFilePaths) { - foreach (ModFolder folder in toolkit.GetModFolders(rootPath)) + foreach (ModFolder folder in toolkit.GetModFolders(rootPath, useCaseInsensitiveFilePaths)) { Manifest? manifest = folder.Manifest; @@ -56,10 +58,11 @@ namespace StardewModdingAPI.Framework.ModLoading /// The mod manifests to validate. /// The current SMAPI version. /// Get an update URL for an update key (if valid). + /// Get a file path lookup for the given directory. /// Whether to validate that files referenced in the manifest (like ) exist on disk. This can be disabled to only validate the manifest itself. [SuppressMessage("ReSharper", "ConstantConditionalAccessQualifier", Justification = "Manifest values may be null before they're validated.")] [SuppressMessage("ReSharper", "ConditionIsAlwaysTrueOrFalse", Justification = "Manifest values may be null before they're validated.")] - public void ValidateManifests(IEnumerable mods, ISemanticVersion apiVersion, Func getUpdateUrl, bool validateFilesExist = true) + public void ValidateManifests(IEnumerable mods, ISemanticVersion apiVersion, Func getUpdateUrl, Func getFilePathLookup, bool validateFilesExist = true) { mods = mods.ToArray(); @@ -144,7 +147,8 @@ namespace StardewModdingAPI.Framework.ModLoading // file doesn't exist if (validateFilesExist) { - string fileName = CaseInsensitivePathLookup.GetCachedFor(mod.DirectoryPath).GetFilePath(mod.Manifest.EntryDll!); + IFilePathLookup pathLookup = getFilePathLookup(mod.DirectoryPath); + string fileName = pathLookup.GetFilePath(mod.Manifest.EntryDll!); if (!File.Exists(Path.Combine(mod.DirectoryPath, fileName))) { mod.SetStatus(ModMetadataStatus.Failed, ModFailReason.InvalidManifest, $"its DLL '{mod.Manifest.EntryDll}' doesn't exist."); diff --git a/src/SMAPI/Framework/Models/SConfig.cs b/src/SMAPI/Framework/Models/SConfig.cs index 1a43c1fc..b4184abb 100644 --- a/src/SMAPI/Framework/Models/SConfig.cs +++ b/src/SMAPI/Framework/Models/SConfig.cs @@ -23,7 +23,8 @@ namespace StardewModdingAPI.Framework.Models [nameof(LogNetworkTraffic)] = false, [nameof(RewriteMods)] = true, [nameof(AggressiveMemoryOptimizations)] = false, - [nameof(UsePintail)] = true + [nameof(UsePintail)] = true, + [nameof(UseCaseInsensitivePaths)] = false }; /// The default values for , to log changes if different. @@ -68,6 +69,9 @@ namespace StardewModdingAPI.Framework.Models /// Whether to use the experimental Pintail API proxying library, instead of the original proxying built into SMAPI itself. public bool UsePintail { get; } + /// Whether to make SMAPI file APIs case-insensitive, even on Linux. + public bool UseCaseInsensitivePaths { get; } + /// Whether SMAPI should log network traffic. Best combined with , which includes network metadata. public bool LogNetworkTraffic { get; } @@ -92,10 +96,11 @@ namespace StardewModdingAPI.Framework.Models /// Whether SMAPI should rewrite mods for compatibility. /// Whether to enable more aggressive memory optimizations. /// Whether to use the experimental Pintail API proxying library, instead of the original proxying built into SMAPI itself. + /// >Whether to make SMAPI file APIs case-insensitive, even on Linux. /// Whether SMAPI should log network traffic. /// The colors to use for text written to the SMAPI console. /// The mod IDs SMAPI should ignore when performing update checks or validating update keys. - public SConfig(bool developerMode, bool checkForUpdates, bool? paranoidWarnings, bool? useBetaChannel, string gitHubProjectName, string webApiBaseUrl, bool verboseLogging, bool? rewriteMods, bool? aggressiveMemoryOptimizations, bool usePintail, bool logNetworkTraffic, ColorSchemeConfig consoleColors, string[]? suppressUpdateChecks) + public SConfig(bool developerMode, bool checkForUpdates, bool? paranoidWarnings, bool? useBetaChannel, string gitHubProjectName, string webApiBaseUrl, bool verboseLogging, bool? rewriteMods, bool? aggressiveMemoryOptimizations, bool? usePintail, bool? useCaseInsensitivePaths, bool logNetworkTraffic, ColorSchemeConfig consoleColors, string[]? suppressUpdateChecks) { this.DeveloperMode = developerMode; this.CheckForUpdates = checkForUpdates; @@ -106,7 +111,8 @@ namespace StardewModdingAPI.Framework.Models this.VerboseLogging = verboseLogging; this.RewriteMods = rewriteMods ?? (bool)SConfig.DefaultValues[nameof(SConfig.RewriteMods)]; this.AggressiveMemoryOptimizations = aggressiveMemoryOptimizations ?? (bool)SConfig.DefaultValues[nameof(SConfig.AggressiveMemoryOptimizations)]; - this.UsePintail = usePintail; + this.UsePintail = usePintail ?? (bool)SConfig.DefaultValues[nameof(SConfig.UsePintail)]; + this.UseCaseInsensitivePaths = useCaseInsensitivePaths ?? (bool)SConfig.DefaultValues[nameof(SConfig.UseCaseInsensitivePaths)]; this.LogNetworkTraffic = logNetworkTraffic; this.ConsoleColors = consoleColors; this.SuppressUpdateChecks = suppressUpdateChecks ?? Array.Empty(); diff --git a/src/SMAPI/Framework/SCore.cs b/src/SMAPI/Framework/SCore.cs index e64318a5..50765083 100644 --- a/src/SMAPI/Framework/SCore.cs +++ b/src/SMAPI/Framework/SCore.cs @@ -43,6 +43,7 @@ using StardewModdingAPI.Toolkit.Framework.Clients.WebApi; using StardewModdingAPI.Toolkit.Framework.ModData; using StardewModdingAPI.Toolkit.Serialization; using StardewModdingAPI.Toolkit.Utilities; +using StardewModdingAPI.Toolkit.Utilities.PathLookups; using StardewModdingAPI.Utilities; using StardewValley; using StardewValley.Menus; @@ -396,7 +397,7 @@ namespace StardewModdingAPI.Framework } // load manifests - IModMetadata[] mods = resolver.ReadManifests(toolkit, this.ModsPath, modDatabase).ToArray(); + IModMetadata[] mods = resolver.ReadManifests(toolkit, this.ModsPath, modDatabase, useCaseInsensitiveFilePaths: this.Settings.UseCaseInsensitivePaths).ToArray(); // filter out ignored mods foreach (IModMetadata mod in mods.Where(p => p.IsIgnored)) @@ -404,7 +405,7 @@ namespace StardewModdingAPI.Framework mods = mods.Where(p => !p.IsIgnored).ToArray(); // load mods - resolver.ValidateManifests(mods, Constants.ApiVersion, toolkit.GetUpdateUrl); + resolver.ValidateManifests(mods, Constants.ApiVersion, toolkit.GetUpdateUrl, getFilePathLookup: this.GetFilePathLookup); mods = resolver.ProcessDependencies(mods, modDatabase).ToArray(); this.LoadMods(mods, this.Toolkit.JsonHelper, this.ContentCore, modDatabase); @@ -1253,6 +1254,7 @@ namespace StardewModdingAPI.Framework onAssetLoaded: this.OnAssetLoaded, onAssetsInvalidated: this.OnAssetsInvalidated, aggressiveMemoryOptimizations: this.Settings.AggressiveMemoryOptimizations, + getFilePathLookup: this.GetFilePathLookup, requestAssetOperations: this.RequestAssetOperations ); if (this.ContentCore.Language != this.Translator.LocaleEnum) @@ -1753,7 +1755,7 @@ namespace StardewModdingAPI.Framework if (mod.IsContentPack) { IMonitor monitor = this.LogManager.GetMonitor(mod.DisplayName); - CaseInsensitivePathLookup relativePathCache = CaseInsensitivePathLookup.GetCachedFor(mod.DirectoryPath); + IFilePathLookup relativePathCache = this.GetFilePathLookup(mod.DirectoryPath); GameContentHelper gameContentHelper = new(this.ContentCore, mod, mod.DisplayName, monitor, this.Reflection); IModContentHelper modContentHelper = new ModContentHelper(this.ContentCore, mod.DirectoryPath, mod, mod.DisplayName, gameContentHelper.GetUnderlyingContentManager(), relativePathCache, this.Reflection); TranslationHelper translationHelper = new(mod, contentCore.GetLocale(), contentCore.Language); @@ -1772,7 +1774,7 @@ namespace StardewModdingAPI.Framework // get mod info string assemblyPath = Path.Combine( mod.DirectoryPath, - CaseInsensitivePathLookup.GetCachedFor(mod.DirectoryPath).GetFilePath(manifest.EntryDll!) + this.GetFilePathLookup(mod.DirectoryPath).GetFilePath(manifest.EntryDll!) ); // load mod @@ -1838,7 +1840,7 @@ namespace StardewModdingAPI.Framework { IMonitor packMonitor = this.LogManager.GetMonitor(packManifest.Name); - CaseInsensitivePathLookup relativePathCache = CaseInsensitivePathLookup.GetCachedFor(packDirPath); + IFilePathLookup relativePathCache = this.GetFilePathLookup(packDirPath); GameContentHelper gameContentHelper = new(contentCore, mod, packManifest.Name, packMonitor, this.Reflection); IModContentHelper packContentHelper = new ModContentHelper(contentCore, packDirPath, mod, packManifest.Name, gameContentHelper.GetUnderlyingContentManager(), relativePathCache, this.Reflection); @@ -1852,12 +1854,12 @@ namespace StardewModdingAPI.Framework IModEvents events = new ModEvents(mod, this.EventManager); ICommandHelper commandHelper = new CommandHelper(mod, this.CommandManager); - CaseInsensitivePathLookup relativePathCache = CaseInsensitivePathLookup.GetCachedFor(mod.DirectoryPath); + IFilePathLookup relativePathLookup = this.GetFilePathLookup(mod.DirectoryPath); #pragma warning disable CS0612 // deprecated code ContentHelper contentHelper = new(contentCore, mod.DirectoryPath, mod, monitor, this.Reflection); #pragma warning restore CS0612 GameContentHelper gameContentHelper = new(contentCore, mod, mod.DisplayName, monitor, this.Reflection); - IModContentHelper modContentHelper = new ModContentHelper(contentCore, mod.DirectoryPath, mod, mod.DisplayName, gameContentHelper.GetUnderlyingContentManager(), relativePathCache, this.Reflection); + IModContentHelper modContentHelper = new ModContentHelper(contentCore, mod.DirectoryPath, mod, mod.DisplayName, gameContentHelper.GetUnderlyingContentManager(), relativePathLookup, this.Reflection); IContentPackHelper contentPackHelper = new ContentPackHelper(mod, new Lazy(GetContentPacks), CreateFakeContentPack); IDataHelper dataHelper = new DataHelper(mod, mod.DirectoryPath, jsonHelper); IReflectionHelper reflectionHelper = new ReflectionHelper(mod, mod.DisplayName, this.Reflection); @@ -2035,6 +2037,15 @@ namespace StardewModdingAPI.Framework return translations; } + /// Get a file path lookup for the given directory. + /// The root path to scan. + private IFilePathLookup GetFilePathLookup(string rootDirectory) + { + return this.Settings.UseCaseInsensitivePaths + ? CaseInsensitivePathLookup.GetCachedFor(rootDirectory) + : MinimalPathLookup.Instance; + } + /// Get the map display device which applies SMAPI features like tile rotation to loaded maps. /// This is separate to let mods like PyTK wrap it with their own functionality. private IDisplayDevice GetMapDisplayDevice() -- cgit From 295ad29b8d60fdbae0c0030e8d887a2adab779a3 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 4 May 2022 21:02:41 -0400 Subject: remove aggressive memory optimizations option --- docs/release-notes.md | 6 ++- src/SMAPI/Framework/ContentCoordinator.cs | 30 ++----------- .../ContentManagers/BaseContentManager.cs | 16 +------ .../ContentManagers/GameContentManager.cs | 5 +-- .../GameContentManagerForAssetPropagation.cs | 4 +- .../Framework/ContentManagers/ModContentManager.cs | 5 +-- src/SMAPI/Framework/Models/SConfig.cs | 8 +--- src/SMAPI/Framework/SCore.cs | 1 - src/SMAPI/Metadata/CoreAssetPropagator.cs | 51 +++++----------------- src/SMAPI/SMAPI.config.json | 7 --- 10 files changed, 27 insertions(+), 106 deletions(-) (limited to 'src/SMAPI/Framework') diff --git a/docs/release-notes.md b/docs/release-notes.md index 11cccee2..2fff0c58 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -3,8 +3,10 @@ # Release notes ## Upcoming release * For players: - * Case-insensitive file paths (introduced in 3.14.0) are now disabled by default. - _You can enable them via `smapi-internal/config.json` if needed. These will be re-enabled in a later version after reworking them to reduce performance impact._ + * Disabled case-insensitive file paths (introduced in 3.14.0) by default. + _You can enable them by editing `smapi-internal/config.json` if needed. They'll be re-enabled in a later version after they're reworked to reduce performance impact._ + * 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._ * Updated compatibility list. ## 3.14.0 diff --git a/src/SMAPI/Framework/ContentCoordinator.cs b/src/SMAPI/Framework/ContentCoordinator.cs index 1a82d194..ef442fbe 100644 --- a/src/SMAPI/Framework/ContentCoordinator.cs +++ b/src/SMAPI/Framework/ContentCoordinator.cs @@ -32,9 +32,6 @@ namespace StardewModdingAPI.Framework /// An asset key prefix for assets from SMAPI mod folders. private readonly string ManagedPrefix = "SMAPI"; - /// Whether to enable more aggressive memory optimizations. - private readonly bool AggressiveMemoryOptimizations; - /// Get a file path lookup for the given directory. private readonly Func GetFilePathLookup; @@ -118,13 +115,11 @@ namespace StardewModdingAPI.Framework /// Encapsulates SMAPI's JSON file parsing. /// A callback to invoke the first time *any* game content manager loads an asset. /// A callback to invoke when an asset is fully loaded. - /// Whether to enable more aggressive memory optimizations. /// Get a file path lookup for the given directory. /// A callback to invoke when any asset names have been invalidated from the cache. /// Get the load/edit operations to apply to an asset by querying registered event handlers. - public ContentCoordinator(IServiceProvider serviceProvider, string rootDirectory, CultureInfo currentCulture, IMonitor monitor, Reflector reflection, JsonHelper jsonHelper, Action onLoadingFirstAsset, Action onAssetLoaded, bool aggressiveMemoryOptimizations, Func getFilePathLookup, Action> onAssetsInvalidated, Func> requestAssetOperations) + public ContentCoordinator(IServiceProvider serviceProvider, string rootDirectory, CultureInfo currentCulture, IMonitor monitor, Reflector reflection, JsonHelper jsonHelper, Action onLoadingFirstAsset, Action onAssetLoaded, Func getFilePathLookup, Action> onAssetsInvalidated, Func> requestAssetOperations) { - this.AggressiveMemoryOptimizations = aggressiveMemoryOptimizations; this.GetFilePathLookup = getFilePathLookup; this.Monitor = monitor ?? throw new ArgumentNullException(nameof(monitor)); this.Reflection = reflection; @@ -145,26 +140,11 @@ namespace StardewModdingAPI.Framework reflection: reflection, onDisposing: this.OnDisposing, onLoadingFirstAsset: onLoadingFirstAsset, - onAssetLoaded: onAssetLoaded, - aggressiveMemoryOptimizations: aggressiveMemoryOptimizations + onAssetLoaded: onAssetLoaded ) ); - var contentManagerForAssetPropagation = new GameContentManagerForAssetPropagation( - name: nameof(GameContentManagerForAssetPropagation), - serviceProvider: serviceProvider, - rootDirectory: rootDirectory, - currentCulture: currentCulture, - coordinator: this, - monitor: monitor, - reflection: reflection, - onDisposing: this.OnDisposing, - onLoadingFirstAsset: onLoadingFirstAsset, - onAssetLoaded: onAssetLoaded, - aggressiveMemoryOptimizations: aggressiveMemoryOptimizations - ); - this.ContentManagers.Add(contentManagerForAssetPropagation); this.VanillaContentManager = new LocalizedContentManager(serviceProvider, rootDirectory); - this.CoreAssets = new CoreAssetPropagator(this.MainContentManager, contentManagerForAssetPropagation, this.Monitor, reflection, aggressiveMemoryOptimizations, name => this.ParseAssetName(name, allowLocales: true)); + this.CoreAssets = new CoreAssetPropagator(this.MainContentManager, this.Monitor, reflection, name => this.ParseAssetName(name, allowLocales: true)); this.LocaleCodes = new Lazy>(() => this.GetLocaleCodes(customLanguages: Enumerable.Empty())); } @@ -184,8 +164,7 @@ namespace StardewModdingAPI.Framework reflection: this.Reflection, onDisposing: this.OnDisposing, onLoadingFirstAsset: this.OnLoadingFirstAsset, - onAssetLoaded: this.OnAssetLoaded, - aggressiveMemoryOptimizations: this.AggressiveMemoryOptimizations + onAssetLoaded: this.OnAssetLoaded ); this.ContentManagers.Add(manager); return manager; @@ -213,7 +192,6 @@ namespace StardewModdingAPI.Framework reflection: this.Reflection, jsonHelper: this.JsonHelper, onDisposing: this.OnDisposing, - aggressiveMemoryOptimizations: this.AggressiveMemoryOptimizations, relativePathLookup: this.GetFilePathLookup(rootDirectory) ); this.ContentManagers.Add(manager); diff --git a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs index b2e3ec0f..db2934a0 100644 --- a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs @@ -11,7 +11,6 @@ using StardewModdingAPI.Framework.Content; using StardewModdingAPI.Framework.Exceptions; using StardewModdingAPI.Framework.Reflection; using StardewValley; -using xTile; namespace StardewModdingAPI.Framework.ContentManagers { @@ -33,9 +32,6 @@ namespace StardewModdingAPI.Framework.ContentManagers /// Simplifies access to private code. protected readonly Reflector Reflection; - /// Whether to enable more aggressive memory optimizations. - protected readonly bool AggressiveMemoryOptimizations; - /// Whether to automatically try resolving keys to a localized form if available. protected bool TryLocalizeKeys = true; @@ -82,8 +78,7 @@ namespace StardewModdingAPI.Framework.ContentManagers /// Simplifies access to private code. /// A callback to invoke when the content manager is being disposed. /// Whether this content manager handles managed asset keys (e.g. to load assets from a mod folder). - /// Whether to enable more aggressive memory optimizations. - protected BaseContentManager(string name, IServiceProvider serviceProvider, string rootDirectory, CultureInfo currentCulture, ContentCoordinator coordinator, IMonitor monitor, Reflector reflection, Action onDisposing, bool isNamespaced, bool aggressiveMemoryOptimizations) + protected BaseContentManager(string name, IServiceProvider serviceProvider, string rootDirectory, CultureInfo currentCulture, ContentCoordinator coordinator, IMonitor monitor, Reflector reflection, Action onDisposing, bool isNamespaced) : base(serviceProvider, rootDirectory, currentCulture) { // init @@ -95,7 +90,6 @@ namespace StardewModdingAPI.Framework.ContentManagers this.Reflection = reflection; this.OnDisposing = onDisposing; this.IsNamespaced = isNamespaced; - this.AggressiveMemoryOptimizations = aggressiveMemoryOptimizations; // get asset data this.BaseDisposableReferences = reflection.GetField?>(this, "disposableAssets").GetValue() @@ -231,14 +225,6 @@ namespace StardewModdingAPI.Framework.ContentManagers removeAssets[baseAssetName] = asset; remove = true; } - - // dispose if safe - if (remove && this.AggressiveMemoryOptimizations) - { - if (asset is Map map) - map.DisposeTileSheets(Game1.mapDisplayDevice); - } - return remove; }, dispose); diff --git a/src/SMAPI/Framework/ContentManagers/GameContentManager.cs b/src/SMAPI/Framework/ContentManagers/GameContentManager.cs index 083df454..c53040e1 100644 --- a/src/SMAPI/Framework/ContentManagers/GameContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/GameContentManager.cs @@ -51,9 +51,8 @@ namespace StardewModdingAPI.Framework.ContentManagers /// A callback to invoke when the content manager is being disposed. /// A callback to invoke the first time *any* game content manager loads an asset. /// A callback to invoke when an asset is fully loaded. - /// Whether to enable more aggressive memory optimizations. - public GameContentManager(string name, IServiceProvider serviceProvider, string rootDirectory, CultureInfo currentCulture, ContentCoordinator coordinator, IMonitor monitor, Reflector reflection, Action onDisposing, Action onLoadingFirstAsset, Action onAssetLoaded, bool aggressiveMemoryOptimizations) - : base(name, serviceProvider, rootDirectory, currentCulture, coordinator, monitor, reflection, onDisposing, isNamespaced: false, aggressiveMemoryOptimizations: aggressiveMemoryOptimizations) + public GameContentManager(string name, IServiceProvider serviceProvider, string rootDirectory, CultureInfo currentCulture, ContentCoordinator coordinator, IMonitor monitor, Reflector reflection, Action onDisposing, Action onLoadingFirstAsset, Action onAssetLoaded) + : base(name, serviceProvider, rootDirectory, currentCulture, coordinator, monitor, reflection, onDisposing, isNamespaced: false) { this.OnLoadingFirstAsset = onLoadingFirstAsset; this.OnAssetLoaded = onAssetLoaded; diff --git a/src/SMAPI/Framework/ContentManagers/GameContentManagerForAssetPropagation.cs b/src/SMAPI/Framework/ContentManagers/GameContentManagerForAssetPropagation.cs index 1b0e1016..5c574a1a 100644 --- a/src/SMAPI/Framework/ContentManagers/GameContentManagerForAssetPropagation.cs +++ b/src/SMAPI/Framework/ContentManagers/GameContentManagerForAssetPropagation.cs @@ -21,8 +21,8 @@ namespace StardewModdingAPI.Framework.ContentManagers ** Public methods *********/ /// - public GameContentManagerForAssetPropagation(string name, IServiceProvider serviceProvider, string rootDirectory, CultureInfo currentCulture, ContentCoordinator coordinator, IMonitor monitor, Reflector reflection, Action onDisposing, Action onLoadingFirstAsset, Action onAssetLoaded, bool aggressiveMemoryOptimizations) - : base(name, serviceProvider, rootDirectory, currentCulture, coordinator, monitor, reflection, onDisposing, onLoadingFirstAsset, onAssetLoaded, aggressiveMemoryOptimizations) { } + public GameContentManagerForAssetPropagation(string name, IServiceProvider serviceProvider, string rootDirectory, CultureInfo currentCulture, ContentCoordinator coordinator, IMonitor monitor, Reflector reflection, Action onDisposing, Action onLoadingFirstAsset, Action onAssetLoaded) + : base(name, serviceProvider, rootDirectory, currentCulture, coordinator, monitor, reflection, onDisposing, onLoadingFirstAsset, onAssetLoaded) { } /// public override T LoadExact(IAssetName assetName, bool useCache) diff --git a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs index 91de769f..65dffd8b 100644 --- a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs @@ -55,10 +55,9 @@ namespace StardewModdingAPI.Framework.ContentManagers /// Simplifies access to private code. /// Encapsulates SMAPI's JSON file parsing. /// A callback to invoke when the content manager is being disposed. - /// Whether to enable more aggressive memory optimizations. /// A lookup for relative paths within the . - public ModContentManager(string name, IContentManager gameContentManager, IServiceProvider serviceProvider, string modName, string rootDirectory, CultureInfo currentCulture, ContentCoordinator coordinator, IMonitor monitor, Reflector reflection, JsonHelper jsonHelper, Action onDisposing, bool aggressiveMemoryOptimizations, IFilePathLookup relativePathLookup) - : base(name, serviceProvider, rootDirectory, currentCulture, coordinator, monitor, reflection, onDisposing, isNamespaced: true, aggressiveMemoryOptimizations: aggressiveMemoryOptimizations) + public ModContentManager(string name, IContentManager gameContentManager, IServiceProvider serviceProvider, string modName, string rootDirectory, CultureInfo currentCulture, ContentCoordinator coordinator, IMonitor monitor, Reflector reflection, JsonHelper jsonHelper, Action onDisposing, IFilePathLookup relativePathLookup) + : base(name, serviceProvider, rootDirectory, currentCulture, coordinator, monitor, reflection, onDisposing, isNamespaced: true) { this.GameContentManager = gameContentManager; this.RelativePathLookup = relativePathLookup; diff --git a/src/SMAPI/Framework/Models/SConfig.cs b/src/SMAPI/Framework/Models/SConfig.cs index b4184abb..80d0d9ba 100644 --- a/src/SMAPI/Framework/Models/SConfig.cs +++ b/src/SMAPI/Framework/Models/SConfig.cs @@ -22,7 +22,6 @@ namespace StardewModdingAPI.Framework.Models [nameof(VerboseLogging)] = false, [nameof(LogNetworkTraffic)] = false, [nameof(RewriteMods)] = true, - [nameof(AggressiveMemoryOptimizations)] = false, [nameof(UsePintail)] = true, [nameof(UseCaseInsensitivePaths)] = false }; @@ -63,9 +62,6 @@ namespace StardewModdingAPI.Framework.Models /// Whether SMAPI should rewrite mods for compatibility. public bool RewriteMods { get; } - /// Whether to enable more aggressive memory optimizations. - public bool AggressiveMemoryOptimizations { get; } - /// Whether to use the experimental Pintail API proxying library, instead of the original proxying built into SMAPI itself. public bool UsePintail { get; } @@ -94,13 +90,12 @@ namespace StardewModdingAPI.Framework.Models /// The base URL for SMAPI's web API, used to perform update checks. /// Whether SMAPI should log more information about the game context. /// Whether SMAPI should rewrite mods for compatibility. - /// Whether to enable more aggressive memory optimizations. /// Whether to use the experimental Pintail API proxying library, instead of the original proxying built into SMAPI itself. /// >Whether to make SMAPI file APIs case-insensitive, even on Linux. /// Whether SMAPI should log network traffic. /// The colors to use for text written to the SMAPI console. /// The mod IDs SMAPI should ignore when performing update checks or validating update keys. - public SConfig(bool developerMode, bool checkForUpdates, bool? paranoidWarnings, bool? useBetaChannel, string gitHubProjectName, string webApiBaseUrl, bool verboseLogging, bool? rewriteMods, bool? aggressiveMemoryOptimizations, bool? usePintail, bool? useCaseInsensitivePaths, bool logNetworkTraffic, ColorSchemeConfig consoleColors, string[]? suppressUpdateChecks) + public SConfig(bool developerMode, bool checkForUpdates, bool? paranoidWarnings, bool? useBetaChannel, string gitHubProjectName, string webApiBaseUrl, bool verboseLogging, bool? rewriteMods, bool? usePintail, bool? useCaseInsensitivePaths, bool logNetworkTraffic, ColorSchemeConfig consoleColors, string[]? suppressUpdateChecks) { this.DeveloperMode = developerMode; this.CheckForUpdates = checkForUpdates; @@ -110,7 +105,6 @@ namespace StardewModdingAPI.Framework.Models this.WebApiBaseUrl = webApiBaseUrl; this.VerboseLogging = verboseLogging; this.RewriteMods = rewriteMods ?? (bool)SConfig.DefaultValues[nameof(SConfig.RewriteMods)]; - this.AggressiveMemoryOptimizations = aggressiveMemoryOptimizations ?? (bool)SConfig.DefaultValues[nameof(SConfig.AggressiveMemoryOptimizations)]; this.UsePintail = usePintail ?? (bool)SConfig.DefaultValues[nameof(SConfig.UsePintail)]; this.UseCaseInsensitivePaths = useCaseInsensitivePaths ?? (bool)SConfig.DefaultValues[nameof(SConfig.UseCaseInsensitivePaths)]; this.LogNetworkTraffic = logNetworkTraffic; diff --git a/src/SMAPI/Framework/SCore.cs b/src/SMAPI/Framework/SCore.cs index 50765083..a9296d9b 100644 --- a/src/SMAPI/Framework/SCore.cs +++ b/src/SMAPI/Framework/SCore.cs @@ -1253,7 +1253,6 @@ namespace StardewModdingAPI.Framework onLoadingFirstAsset: this.InitializeBeforeFirstAssetLoaded, onAssetLoaded: this.OnAssetLoaded, onAssetsInvalidated: this.OnAssetsInvalidated, - aggressiveMemoryOptimizations: this.Settings.AggressiveMemoryOptimizations, getFilePathLookup: this.GetFilePathLookup, requestAssetOperations: this.RequestAssetOperations ); diff --git a/src/SMAPI/Metadata/CoreAssetPropagator.cs b/src/SMAPI/Metadata/CoreAssetPropagator.cs index 5dee2c4d..12b73515 100644 --- a/src/SMAPI/Metadata/CoreAssetPropagator.cs +++ b/src/SMAPI/Metadata/CoreAssetPropagator.cs @@ -5,7 +5,6 @@ using System.IO; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; -using StardewModdingAPI.Framework.ContentManagers; using StardewModdingAPI.Framework.Reflection; using StardewModdingAPI.Internal; using StardewModdingAPI.Toolkit.Utilities; @@ -33,18 +32,12 @@ namespace StardewModdingAPI.Metadata /// The main content manager through which to reload assets. private readonly LocalizedContentManager MainContentManager; - /// An internal content manager used only for asset propagation. See remarks on . - private readonly GameContentManagerForAssetPropagation DisposableContentManager; - /// Writes messages to the console. private readonly IMonitor Monitor; /// Simplifies access to private game code. private readonly Reflector Reflection; - /// Whether to enable more aggressive memory optimizations. - private readonly bool AggressiveMemoryOptimizations; - /// Parse a raw asset name. private readonly Func ParseAssetName; @@ -67,18 +60,14 @@ namespace StardewModdingAPI.Metadata *********/ /// Initialize the core asset data. /// The main content manager through which to reload assets. - /// An internal content manager used only for asset propagation. /// Writes messages to the console. /// Simplifies access to private code. - /// Whether to enable more aggressive memory optimizations. /// Parse a raw asset name. - public CoreAssetPropagator(LocalizedContentManager mainContent, GameContentManagerForAssetPropagation disposableContent, IMonitor monitor, Reflector reflection, bool aggressiveMemoryOptimizations, Func parseAssetName) + public CoreAssetPropagator(LocalizedContentManager mainContent, IMonitor monitor, Reflector reflection, Func parseAssetName) { this.MainContentManager = mainContent; - this.DisposableContentManager = disposableContent; this.Monitor = monitor; this.Reflection = reflection; - this.AggressiveMemoryOptimizations = aggressiveMemoryOptimizations; this.ParseAssetName = parseAssetName; } @@ -230,7 +219,7 @@ namespace StardewModdingAPI.Metadata ** Buildings ****/ case "buildings/houses": // Farm - Farm.houseTextures = this.LoadAndDisposeIfNeeded(Farm.houseTextures, key); + Farm.houseTextures = this.LoadTexture(key); return true; case "buildings/houses_paintmask": // Farm @@ -247,7 +236,7 @@ namespace StardewModdingAPI.Metadata ** Content\Characters\Farmer ****/ case "characters/farmer/accessories": // Game1.LoadContent - FarmerRenderer.accessoriesTexture = this.LoadAndDisposeIfNeeded(FarmerRenderer.accessoriesTexture, key); + FarmerRenderer.accessoriesTexture = this.LoadTexture(key); return true; case "characters/farmer/farmer_base": // Farmer @@ -257,19 +246,19 @@ namespace StardewModdingAPI.Metadata return !ignoreWorld && this.ReloadPlayerSprites(assetName); case "characters/farmer/hairstyles": // Game1.LoadContent - FarmerRenderer.hairStylesTexture = this.LoadAndDisposeIfNeeded(FarmerRenderer.hairStylesTexture, key); + FarmerRenderer.hairStylesTexture = this.LoadTexture(key); return true; case "characters/farmer/hats": // Game1.LoadContent - FarmerRenderer.hatsTexture = this.LoadAndDisposeIfNeeded(FarmerRenderer.hatsTexture, key); + FarmerRenderer.hatsTexture = this.LoadTexture(key); return true; case "characters/farmer/pants": // Game1.LoadContent - FarmerRenderer.pantsTexture = this.LoadAndDisposeIfNeeded(FarmerRenderer.pantsTexture, key); + FarmerRenderer.pantsTexture = this.LoadTexture(key); return true; case "characters/farmer/shirts": // Game1.LoadContent - FarmerRenderer.shirtsTexture = this.LoadAndDisposeIfNeeded(FarmerRenderer.shirtsTexture, key); + FarmerRenderer.shirtsTexture = this.LoadTexture(key); return true; /**** @@ -905,9 +894,6 @@ namespace StardewModdingAPI.Metadata GameLocation location = locationInfo.Location; Vector2? playerPos = Game1.player?.Position; - if (this.AggressiveMemoryOptimizations) - location.map.DisposeTileSheets(Game1.mapDisplayDevice); - // reload map location.interiorDoors.Clear(); // prevent errors when doors try to update tiles which no longer exist location.reloadMap(); @@ -973,7 +959,7 @@ namespace StardewModdingAPI.Metadata // update sprite foreach (var target in characters) { - target.Npc.Sprite.spriteTexture = this.LoadAndDisposeIfNeeded(target.Npc.Sprite.spriteTexture, target.AssetName.BaseName); + target.Npc.Sprite.spriteTexture = this.LoadTexture(target.AssetName.BaseName); propagated[target.AssetName] = true; } } @@ -1012,7 +998,7 @@ namespace StardewModdingAPI.Metadata // update portrait foreach (var target in characters) { - target.Npc.Portrait = this.LoadAndDisposeIfNeeded(target.Npc.Portrait, target.AssetName.BaseName); + target.Npc.Portrait = this.LoadTexture(target.AssetName.BaseName); propagated[target.AssetName] = true; } } @@ -1284,25 +1270,10 @@ namespace StardewModdingAPI.Metadata : Array.Empty(); } - /// Load a texture, and dispose the old one if is enabled and it's different from the new instance. - /// The previous texture to dispose. + /// Load a texture from the main content manager. /// The asset key to load. - private Texture2D LoadAndDisposeIfNeeded(Texture2D? oldTexture, string key) + private Texture2D LoadTexture(string key) { - // if aggressive memory optimizations are enabled, load the asset from the disposable - // content manager and dispose the old instance if needed. - if (this.AggressiveMemoryOptimizations) - { - GameContentManagerForAssetPropagation content = this.DisposableContentManager; - - Texture2D newTexture = content.Load(key); - if (oldTexture?.IsDisposed == false && !object.ReferenceEquals(oldTexture, newTexture) && content.IsResponsibleFor(oldTexture)) - oldTexture.Dispose(); - - return newTexture; - } - - // else just (re)load it from the main content manager return this.MainContentManager.Load(key); } diff --git a/src/SMAPI/SMAPI.config.json b/src/SMAPI/SMAPI.config.json index 544aeaec..bdd6374a 100644 --- a/src/SMAPI/SMAPI.config.json +++ b/src/SMAPI/SMAPI.config.json @@ -39,13 +39,6 @@ copy all the settings, or you may cause bugs due to overridden changes in future */ "RewriteMods": true, - /** - * Whether to enable more aggressive memory optimizations. - * If you get frequent 'OutOfMemoryException' errors, you can try enabling this to reduce their - * frequency. This may cause crashes for farmhands in multiplayer. - */ - "AggressiveMemoryOptimizations": false, - /** * Whether to make SMAPI file APIs case-insensitive, even on Linux. * This is experimental, and the initial implementation may impact load times. -- cgit From 87d5288287520f750651667839ebbe7bbeb97bba Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 6 May 2022 18:05:40 -0400 Subject: fix content managers' LoadBaseString not recognizing localized mod assets --- docs/release-notes.md | 1 + .../ContentManagers/BaseContentManager.cs | 49 ++++++++++++++++++++++ 2 files changed, 50 insertions(+) (limited to 'src/SMAPI/Framework') diff --git a/docs/release-notes.md b/docs/release-notes.md index 2fff0c58..99269bc6 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -7,6 +7,7 @@ _You can enable them by editing `smapi-internal/config.json` if needed. They'll be re-enabled in a later version after they're reworked to reduce performance impact._ * 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. * Updated compatibility list. ## 3.14.0 diff --git a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs index db2934a0..e4695588 100644 --- a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs @@ -110,6 +110,26 @@ namespace StardewModdingAPI.Framework.ContentManagers return this.Load(assetName, LanguageCode.en); } + /// + [SuppressMessage("ReSharper", "ConditionIsAlwaysTrueOrFalse", Justification = "Copied as-is from game code")] + public sealed override string LoadBaseString(string path) + { + try + { + // copied as-is from LocalizedContentManager.LoadBaseString + // This is only changed to call this.Load instead of base.Load, to support mod assets + this.ParseStringPath(path, out string assetName, out string key); + Dictionary strings = this.Load>(assetName, LanguageCode.en); + return strings != null && strings.ContainsKey(key) + ? this.GetString(strings, key) + : path; + } + catch (Exception ex) + { + throw new InvalidOperationException($"Failed loading string path '{path}' from '{this.Name}'.", ex); + } + } + /// public sealed override T Load(string assetName) { @@ -331,5 +351,34 @@ namespace StardewModdingAPI.Framework.ContentManagers // avoid hard disposable references; see remarks on the field this.BaseDisposableReferences.Clear(); } + + /**** + ** Private methods copied from the game code + ****/ +#pragma warning disable CS1574 // can't be resolved: the reference is valid but private + /// Parse a string path like assetName:key. + /// The string path. + /// The extracted asset name. + /// The extracted entry key. + /// The string path is not in a valid format. + /// This is copied as-is from . + private void ParseStringPath(string path, out string assetName, out string key) + { + int length = path.IndexOf(':'); + assetName = length != -1 ? path.Substring(0, length) : throw new ContentLoadException("Unable to parse string path: " + path); + key = path.Substring(length + 1, path.Length - length - 1); + } + + /// Get a string value from a dictionary asset. + /// The asset to read. + /// The string key to find. + /// This is copied as-is from . + private string GetString(Dictionary strings, string key) + { + return strings.TryGetValue(key + ".desktop", out string? str) + ? str + : strings[key]; + } +#pragma warning restore CS1574 } } -- 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/Framework') 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 From a969828e9357306aaac5163c430e108ab57f578b Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 6 May 2022 18:26:35 -0400 Subject: cache asset operation instances created legacy interceptors --- docs/release-notes.md | 3 +- src/SMAPI/Framework/ContentCoordinator.cs | 74 +++++++++++++++++++------------ 2 files changed, 48 insertions(+), 29 deletions(-) (limited to 'src/SMAPI/Framework') diff --git a/docs/release-notes.md b/docs/release-notes.md index a0d8826b..eb30ef7c 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -3,8 +3,9 @@ # Release notes ## Upcoming release * For players: + * Improved performance for many mods still using the older content API. * Disabled case-insensitive file paths (introduced in 3.14.0) by default. - _You can enable them by editing `smapi-internal/config.json` if needed. They'll be re-enabled in a later version after they're reworked to reduce performance impact._ + _You can enable them by editing `smapi-internal/config.json` if needed. They'll be re-enabled in an upcoming version after they're reworked a bit._ * 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. diff --git a/src/SMAPI/Framework/ContentCoordinator.cs b/src/SMAPI/Framework/ContentCoordinator.cs index ef442fbe..8ce0dba1 100644 --- a/src/SMAPI/Framework/ContentCoordinator.cs +++ b/src/SMAPI/Framework/ContentCoordinator.cs @@ -81,6 +81,14 @@ namespace StardewModdingAPI.Framework /// The cached asset load/edit operations to apply, indexed by asset name. private readonly TickCacheDictionary AssetOperationsByKey = new(); + /// A cache of asset operation groups created for legacy implementations. + [Obsolete] + private readonly Dictionary LegacyLoaderCache = new(ReferenceEqualityComparer.Instance); + + /// A cache of asset operation groups created for legacy implementations. + [Obsolete] + private readonly Dictionary LegacyEditorCache = new(ReferenceEqualityComparer.Instance); + /********* ** Accessors @@ -598,21 +606,26 @@ namespace StardewModdingAPI.Framework } // add operation - yield return new AssetOperationGroup( - mod: loader.Mod, - loadOperations: new[] - { - new AssetLoadOperation( - mod: loader.Mod, - priority: AssetLoadPriority.Exclusive, - onBehalfOf: null, - getData: assetInfo => loader.Data.Load( - this.GetLegacyAssetInfo(assetInfo) + if (!this.LegacyLoaderCache.TryGetValue(loader.Data, out AssetOperationGroup? group)) + { + this.LegacyLoaderCache[loader.Data] = group = new AssetOperationGroup( + mod: loader.Mod, + loadOperations: new[] + { + new AssetLoadOperation( + mod: loader.Mod, + priority: AssetLoadPriority.Exclusive, + onBehalfOf: null, + getData: assetInfo => loader.Data.Load( + this.GetLegacyAssetInfo(assetInfo) + ) ) - ) - }, - editOperations: Array.Empty() - ); + }, + editOperations: Array.Empty() + ); + } + + yield return group; } // legacy edit operations @@ -652,21 +665,26 @@ namespace StardewModdingAPI.Framework }; // add operation - yield return new AssetOperationGroup( - mod: editor.Mod, - loadOperations: Array.Empty(), - editOperations: new[] - { - new AssetEditOperation( - mod: editor.Mod, - priority: priority, - onBehalfOf: null, - applyEdit: assetData => editor.Data.Edit( - this.GetLegacyAssetData(assetData) + if (!this.LegacyEditorCache.TryGetValue(editor.Data, out AssetOperationGroup? group)) + { + this.LegacyEditorCache[editor.Data] = group = new AssetOperationGroup( + mod: editor.Mod, + loadOperations: Array.Empty(), + editOperations: new[] + { + new AssetEditOperation( + mod: editor.Mod, + priority: priority, + onBehalfOf: null, + applyEdit: assetData => editor.Data.Edit( + this.GetLegacyAssetData(assetData) + ) ) - ) - } - ); + } + ); + } + + yield return group; } #pragma warning restore CS0612, CS0618 } -- cgit From 09c52fb3f530bb450de5a7b4ba2ca09c1035b3cd Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 6 May 2022 19:39:51 -0400 Subject: cache legacy asset operations by target type --- src/SMAPI/Framework/ContentCoordinator.cs | 50 +++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 16 deletions(-) (limited to 'src/SMAPI/Framework') diff --git a/src/SMAPI/Framework/ContentCoordinator.cs b/src/SMAPI/Framework/ContentCoordinator.cs index 8ce0dba1..4f52d57e 100644 --- a/src/SMAPI/Framework/ContentCoordinator.cs +++ b/src/SMAPI/Framework/ContentCoordinator.cs @@ -83,11 +83,11 @@ namespace StardewModdingAPI.Framework /// A cache of asset operation groups created for legacy implementations. [Obsolete] - private readonly Dictionary LegacyLoaderCache = new(ReferenceEqualityComparer.Instance); + private readonly Dictionary> LegacyLoaderCache = new(ReferenceEqualityComparer.Instance); /// A cache of asset operation groups created for legacy implementations. [Obsolete] - private readonly Dictionary LegacyEditorCache = new(ReferenceEqualityComparer.Instance); + private readonly Dictionary> LegacyEditorCache = new(ReferenceEqualityComparer.Instance); /********* @@ -606,9 +606,11 @@ namespace StardewModdingAPI.Framework } // add operation - if (!this.LegacyLoaderCache.TryGetValue(loader.Data, out AssetOperationGroup? group)) - { - this.LegacyLoaderCache[loader.Data] = group = new AssetOperationGroup( + yield return this.GetOrCreateLegacyOperationGroup( + cache: this.LegacyLoaderCache, + editor: loader.Data, + dataType: info.DataType, + createGroup: () => new AssetOperationGroup( mod: loader.Mod, loadOperations: new[] { @@ -622,10 +624,8 @@ namespace StardewModdingAPI.Framework ) }, editOperations: Array.Empty() - ); - } - - yield return group; + ) + ); } // legacy edit operations @@ -665,9 +665,11 @@ namespace StardewModdingAPI.Framework }; // add operation - if (!this.LegacyEditorCache.TryGetValue(editor.Data, out AssetOperationGroup? group)) - { - this.LegacyEditorCache[editor.Data] = group = new AssetOperationGroup( + yield return this.GetOrCreateLegacyOperationGroup( + cache: this.LegacyEditorCache, + editor: editor.Data, + dataType: info.DataType, + createGroup: () => new AssetOperationGroup( mod: editor.Mod, loadOperations: Array.Empty(), editOperations: new[] @@ -681,14 +683,30 @@ namespace StardewModdingAPI.Framework ) ) } - ); - } - - yield return group; + ) + ); } #pragma warning restore CS0612, CS0618 } + /// Get a cached asset operation group for a legacy or instance, creating it if needed. + /// The editor type (one of or ). + /// The cached operation groups for the interceptor type. + /// The legacy asset interceptor. + /// The asset data type. + /// Create the asset operation group if it's not cached yet. + private AssetOperationGroup GetOrCreateLegacyOperationGroup(Dictionary> cache, TInterceptor editor, Type dataType, Func createGroup) + where TInterceptor : class + { + if (!cache.TryGetValue(editor, out Dictionary? cacheByType)) + cache[editor] = cacheByType = new Dictionary(); + + if (!cacheByType.TryGetValue(dataType, out AssetOperationGroup? group)) + cacheByType[dataType] = group = createGroup(); + + return group; + } + /// Get an asset info compatible with legacy and instances, which always expect the base name. /// The asset info. private IAssetInfo GetLegacyAssetInfo(IAssetInfo asset) -- cgit