From be84248a9a0a57ee1b2384e63b25fc9bb694fa4d Mon Sep 17 00:00:00 2001 From: SinZ Date: Fri, 11 Nov 2022 22:01:48 +1100 Subject: [PATCH 1/2] Add logic to remove from the multiplayer map cache for asset propagation --- src/SMAPI/Metadata/CoreAssetPropagator.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/SMAPI/Metadata/CoreAssetPropagator.cs b/src/SMAPI/Metadata/CoreAssetPropagator.cs index 1ef9a8f2..d94fe2ae 100644 --- a/src/SMAPI/Metadata/CoreAssetPropagator.cs +++ b/src/SMAPI/Metadata/CoreAssetPropagator.cs @@ -1166,6 +1166,13 @@ namespace StardewModdingAPI.Metadata GameLocation location = locationInfo.Location; Vector2? playerPos = Game1.player?.Position; + // clear cachedMultiplayerMaps so Asset Propegation works on farmhands and Map edits can be applied after an initial load + if (!Game1.IsMasterGame) + { + var multiplayer = this.Reflection.GetField(typeof(Game1), "multiplayer").GetValue(); + multiplayer.cachedMultiplayerMaps.Remove(locationInfo.Location.NameOrUniqueName); + } + // reload map location.interiorDoors.Clear(); // prevent errors when doors try to update tiles which no longer exist location.reloadMap(); From 286c2d244949f4eee62e2de440938d2d8ae3ce76 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 11 Nov 2022 20:55:24 -0500 Subject: [PATCH 2/2] pass multiplayer into asset propagator to avoid reflection --- src/SMAPI/Framework/ContentCoordinator.cs | 5 +++-- src/SMAPI/Framework/SCore.cs | 1 + src/SMAPI/Metadata/CoreAssetPropagator.cs | 16 +++++++++------- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/SMAPI/Framework/ContentCoordinator.cs b/src/SMAPI/Framework/ContentCoordinator.cs index cf26307f..86415a5f 100644 --- a/src/SMAPI/Framework/ContentCoordinator.cs +++ b/src/SMAPI/Framework/ContentCoordinator.cs @@ -129,6 +129,7 @@ namespace StardewModdingAPI.Framework /// The root directory to search for content. /// The current culture for which to localize content. /// Encapsulates monitoring and logging. + /// The multiplayer instance whose map cache to update during asset propagation. /// Simplifies access to private code. /// Encapsulates SMAPI's JSON file parsing. /// A callback to invoke the first time *any* game content manager loads an asset. @@ -136,7 +137,7 @@ namespace StardewModdingAPI.Framework /// Get a file 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, Func getFileLookup, Action> onAssetsInvalidated, Func requestAssetOperations) + public ContentCoordinator(IServiceProvider serviceProvider, string rootDirectory, CultureInfo currentCulture, IMonitor monitor, Multiplayer multiplayer, Reflector reflection, JsonHelper jsonHelper, Action onLoadingFirstAsset, Action onAssetLoaded, Func getFileLookup, Action> onAssetsInvalidated, Func requestAssetOperations) { this.GetFileLookup = getFileLookup; this.Monitor = monitor ?? throw new ArgumentNullException(nameof(monitor)); @@ -177,7 +178,7 @@ namespace StardewModdingAPI.Framework this.ContentManagers.Add(contentManagerForAssetPropagation); this.VanillaContentManager = new LocalizedContentManager(serviceProvider, rootDirectory); - this.CoreAssets = new CoreAssetPropagator(this.MainContentManager, contentManagerForAssetPropagation, this.Monitor, reflection, name => this.ParseAssetName(name, allowLocales: true)); + this.CoreAssets = new CoreAssetPropagator(this.MainContentManager, contentManagerForAssetPropagation, this.Monitor, multiplayer, reflection, name => this.ParseAssetName(name, allowLocales: true)); this.LocaleCodes = new Lazy>(() => this.GetLocaleCodes(customLanguages: Enumerable.Empty())); } diff --git a/src/SMAPI/Framework/SCore.cs b/src/SMAPI/Framework/SCore.cs index 40979b09..cf1e73fc 100644 --- a/src/SMAPI/Framework/SCore.cs +++ b/src/SMAPI/Framework/SCore.cs @@ -1327,6 +1327,7 @@ namespace StardewModdingAPI.Framework rootDirectory: rootDirectory, currentCulture: Thread.CurrentThread.CurrentUICulture, monitor: this.Monitor, + multiplayer: this.Multiplayer, reflection: this.Reflection, jsonHelper: this.Toolkit.JsonHelper, onLoadingFirstAsset: this.InitializeBeforeFirstAssetLoaded, diff --git a/src/SMAPI/Metadata/CoreAssetPropagator.cs b/src/SMAPI/Metadata/CoreAssetPropagator.cs index d94fe2ae..037e4573 100644 --- a/src/SMAPI/Metadata/CoreAssetPropagator.cs +++ b/src/SMAPI/Metadata/CoreAssetPropagator.cs @@ -40,6 +40,9 @@ namespace StardewModdingAPI.Metadata /// Writes messages to the console. private readonly IMonitor Monitor; + /// The multiplayer instance whose map cache to update. + private readonly Multiplayer Multiplayer; + /// Simplifies access to private game code. private readonly Reflector Reflection; @@ -70,13 +73,15 @@ namespace StardewModdingAPI.Metadata /// The main content manager through which to reload assets. /// An internal content manager used only for asset propagation. /// Writes messages to the console. + /// The multiplayer instance whose map cache to update. /// Simplifies access to private code. /// Parse a raw asset name. - public CoreAssetPropagator(LocalizedContentManager mainContent, GameContentManagerForAssetPropagation disposableContent, IMonitor monitor, Reflector reflection, Func parseAssetName) + public CoreAssetPropagator(LocalizedContentManager mainContent, GameContentManagerForAssetPropagation disposableContent, IMonitor monitor, Multiplayer multiplayer, Reflector reflection, Func parseAssetName) { this.MainContentManager = mainContent; this.DisposableContentManager = disposableContent; this.Monitor = monitor; + this.Multiplayer = multiplayer; this.Reflection = reflection; this.ParseAssetName = parseAssetName; } @@ -1166,12 +1171,9 @@ namespace StardewModdingAPI.Metadata GameLocation location = locationInfo.Location; Vector2? playerPos = Game1.player?.Position; - // clear cachedMultiplayerMaps so Asset Propegation works on farmhands and Map edits can be applied after an initial load - if (!Game1.IsMasterGame) - { - var multiplayer = this.Reflection.GetField(typeof(Game1), "multiplayer").GetValue(); - multiplayer.cachedMultiplayerMaps.Remove(locationInfo.Location.NameOrUniqueName); - } + // clear multiplayer cache for farmhands + if (!Context.IsMainPlayer) + this.Multiplayer.cachedMultiplayerMaps.Remove(location.NameOrUniqueName); // reload map location.interiorDoors.Clear(); // prevent errors when doors try to update tiles which no longer exist