diff options
Diffstat (limited to 'src/SMAPI/Framework/ContentManagers')
3 files changed, 35 insertions, 1 deletions
diff --git a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs index ff0e2de4..18aae05b 100644 --- a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs @@ -6,6 +6,7 @@ using System.Globalization; using System.IO; using System.Linq; using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; using StardewModdingAPI.Framework.Content; using StardewModdingAPI.Framework.Exceptions; using StardewModdingAPI.Framework.Reflection; @@ -109,6 +110,34 @@ namespace StardewModdingAPI.Framework.ContentManagers } + /// <summary>Get a copy of the given asset if supported.</summary> + /// <typeparam name="T">The asset type.</typeparam> + /// <param name="asset">The asset to clone.</param> + public T CloneIfPossible<T>(T asset) + { + switch (asset as object) + { + case Texture2D source: + { + int[] pixels = new int[source.Width * source.Height]; + source.GetData(pixels); + + Texture2D clone = new Texture2D(source.GraphicsDevice, source.Width, source.Height); + clone.SetData(pixels); + return (T)(object)clone; + } + + case Dictionary<string, string> source: + return (T)(object)new Dictionary<string, string>(source); + + case Dictionary<int, string> source: + return (T)(object)new Dictionary<int, string>(source); + + default: + return asset; + } + } + /// <summary>Normalise path separators in a file path. For asset keys, see <see cref="AssertAndNormaliseAssetName"/> instead.</summary> /// <param name="path">The file path to normalise.</param> [Pure] diff --git a/src/SMAPI/Framework/ContentManagers/GameContentManager.cs b/src/SMAPI/Framework/ContentManagers/GameContentManager.cs index cfedb5af..a53840bc 100644 --- a/src/SMAPI/Framework/ContentManagers/GameContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/GameContentManager.cs @@ -161,7 +161,7 @@ namespace StardewModdingAPI.Framework.ContentManagers T data; try { - data = loader.Load<T>(info); + data = this.CloneIfPossible(loader.Load<T>(info)); this.Monitor.Log($"{mod.DisplayName} loaded asset '{info.AssetName}'.", LogLevel.Trace); } catch (Exception ex) diff --git a/src/SMAPI/Framework/ContentManagers/IContentManager.cs b/src/SMAPI/Framework/ContentManagers/IContentManager.cs index aa5be9b6..1eb8b0ac 100644 --- a/src/SMAPI/Framework/ContentManagers/IContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/IContentManager.cs @@ -47,6 +47,11 @@ namespace StardewModdingAPI.Framework.ContentManagers /// <param name="value">The asset value.</param> void Inject<T>(string assetName, T value); + /// <summary>Get a copy of the given asset if supported.</summary> + /// <typeparam name="T">The asset type.</typeparam> + /// <param name="asset">The asset to clone.</param> + T CloneIfPossible<T>(T asset); + /// <summary>Normalise path separators in a file path. For asset keys, see <see cref="AssertAndNormaliseAssetName"/> instead.</summary> /// <param name="path">The file path to normalise.</param> [Pure] |