fix game content managers not cloning assets from IAssetLoader

This commit is contained in:
Jesse Plamondon-Willard 2018-05-30 21:16:46 -04:00
parent 7dd7920503
commit fa36e80a11
4 changed files with 36 additions and 23 deletions

View File

@ -5,7 +5,6 @@ using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using StardewModdingAPI.Framework.Content;
using StardewModdingAPI.Framework.ContentManagers;
using StardewModdingAPI.Framework.Reflection;
@ -157,27 +156,7 @@ namespace StardewModdingAPI.Framework
// get cloned asset
T data = contentManager.Load<T>(internalKey, language);
switch (data 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 data;
}
return contentManager.CloneIfPossible(data);
}
/// <summary>Purge assets from the cache that match one of the interceptors.</summary>

View File

@ -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]

View File

@ -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)

View File

@ -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]