From c5be446701d4e24a03d8464e9b080ce74d158223 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 6 Jan 2021 00:29:39 -0500 Subject: rework vanilla tilesheet checking to avoid keeping a copy of the vanilla maps in memory --- src/SMAPI/Framework/ContentCoordinator.cs | 43 +++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 11 deletions(-) (limited to 'src/SMAPI/Framework/ContentCoordinator.cs') diff --git a/src/SMAPI/Framework/ContentCoordinator.cs b/src/SMAPI/Framework/ContentCoordinator.cs index 3d5bb29d..27fb3dbb 100644 --- a/src/SMAPI/Framework/ContentCoordinator.cs +++ b/src/SMAPI/Framework/ContentCoordinator.cs @@ -54,6 +54,9 @@ namespace StardewModdingAPI.Framework /// The game may adds content managers in asynchronous threads (e.g. when populating the load screen). private readonly ReaderWriterLockSlim ContentManagerLock = new ReaderWriterLockSlim(); + /// A cache of ordered tilesheet IDs used by vanilla maps. + private readonly IDictionary VanillaTilesheets = new Dictionary(StringComparer.OrdinalIgnoreCase); + /// An unmodified content manager which doesn't intercept assets, used to compare asset data. private readonly LocalizedContentManager VanillaContentManager; @@ -293,21 +296,21 @@ namespace StardewModdingAPI.Framework }); } - /// Get a vanilla asset without interception. - /// The type of asset to load. + /// Get the tilesheet ID order used by the unmodified version of a map asset. /// The asset path relative to the loader root directory, not including the .xnb extension. - public bool TryLoadVanillaAsset(string assetName, out T asset) + public TilesheetReference[] GetVanillaTilesheetIds(string assetName) { - try + if (!this.VanillaTilesheets.TryGetValue(assetName, out TilesheetReference[] tilesheets)) { - asset = this.VanillaContentManager.Load(assetName); - return true; - } - catch - { - asset = default; - return false; + tilesheets = this.TryLoadVanillaAsset(assetName, out Map map) + ? map.TileSheets.Select((sheet, index) => new TilesheetReference(index, sheet.Id, sheet.ImageSource)).ToArray() + : null; + + this.VanillaTilesheets[assetName] = tilesheets; + this.VanillaContentManager.Unload(); } + + return tilesheets ?? new TilesheetReference[0]; } /// Dispose held resources. @@ -341,5 +344,23 @@ namespace StardewModdingAPI.Framework this.ContentManagers.Remove(contentManager) ); } + + /// Get a vanilla asset without interception. + /// The type of asset to load. + /// The asset path relative to the loader root directory, not including the .xnb extension. + /// The loaded asset data. + private bool TryLoadVanillaAsset(string assetName, out T asset) + { + try + { + asset = this.VanillaContentManager.Load(assetName); + return true; + } + catch + { + asset = default; + return false; + } + } } } -- cgit