From c74b21141cfad4d02f899462e02e36818154e152 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 10 Oct 2017 00:49:54 -0400 Subject: work around race condition in game code --- src/SMAPI/Framework/SContentManager.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'src/SMAPI/Framework') diff --git a/src/SMAPI/Framework/SContentManager.cs b/src/SMAPI/Framework/SContentManager.cs index 43de6e96..f3a1dd9a 100644 --- a/src/SMAPI/Framework/SContentManager.cs +++ b/src/SMAPI/Framework/SContentManager.cs @@ -51,6 +51,9 @@ namespace StardewModdingAPI.Framework /// A lookup of the content managers which loaded each asset. private readonly IDictionary> AssetLoaders = new Dictionary>(); + /// An object locked to prevent concurrent changes to the underlying assets. + private readonly object Lock = new object(); + /********* ** Accessors @@ -128,7 +131,7 @@ namespace StardewModdingAPI.Framework /// The asset path relative to the loader root directory, not including the .xnb extension. public bool IsLoaded(string assetName) { - lock (this.Cache) + lock (this.Lock) { assetName = this.NormaliseAssetName(assetName); return this.IsNormalisedKeyLoaded(assetName); @@ -149,7 +152,7 @@ namespace StardewModdingAPI.Framework /// The content manager instance for which to load the asset. public T LoadFor(string assetName, ContentManager instance) { - lock (this.Cache) + lock (this.Lock) { assetName = this.NormaliseAssetName(assetName); @@ -192,7 +195,7 @@ namespace StardewModdingAPI.Framework /// The asset value. public void Inject(string assetName, T value) { - lock (this.Cache) + lock (this.Lock) { assetName = this.NormaliseAssetName(assetName); this.Cache[assetName] = value; @@ -209,7 +212,7 @@ namespace StardewModdingAPI.Framework /// Get the cached asset keys. public IEnumerable GetAssetKeys() { - lock (this.Cache) + lock (this.Lock) { IEnumerable GetAllAssetKeys() { @@ -260,7 +263,7 @@ namespace StardewModdingAPI.Framework /// Returns whether any cache entries were invalidated. public bool InvalidateCache(Func predicate, bool dispose = false) { - lock (this.Cache) + lock (this.Lock) { // find matching asset keys HashSet purgeCacheKeys = new HashSet(StringComparer.InvariantCultureIgnoreCase); -- cgit