fix game content managers not cloning assets from IAssetLoader
This commit is contained in:
parent
7dd7920503
commit
fa36e80a11
|
@ -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>
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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]
|
||||
|
|
Loading…
Reference in New Issue