summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/Framework/Content
diff options
context:
space:
mode:
Diffstat (limited to 'src/StardewModdingAPI/Framework/Content')
-rw-r--r--src/StardewModdingAPI/Framework/Content/AssetData.cs44
-rw-r--r--src/StardewModdingAPI/Framework/Content/AssetDataForDictionary.cs (renamed from src/StardewModdingAPI/Framework/Content/ContentEventHelperForDictionary.cs)4
-rw-r--r--src/StardewModdingAPI/Framework/Content/AssetDataForImage.cs (renamed from src/StardewModdingAPI/Framework/Content/ContentEventHelperForImage.cs)4
-rw-r--r--src/StardewModdingAPI/Framework/Content/AssetDataForObject.cs (renamed from src/StardewModdingAPI/Framework/Content/ContentEventHelper.cs)19
-rw-r--r--src/StardewModdingAPI/Framework/Content/AssetInfo.cs (renamed from src/StardewModdingAPI/Framework/Content/ContentEventData.cs)41
5 files changed, 67 insertions, 45 deletions
diff --git a/src/StardewModdingAPI/Framework/Content/AssetData.cs b/src/StardewModdingAPI/Framework/Content/AssetData.cs
new file mode 100644
index 00000000..1ab9eebd
--- /dev/null
+++ b/src/StardewModdingAPI/Framework/Content/AssetData.cs
@@ -0,0 +1,44 @@
+using System;
+
+namespace StardewModdingAPI.Framework.Content
+{
+ /// <summary>Base implementation for a content helper which encapsulates access and changes to content being read from a data file.</summary>
+ /// <typeparam name="TValue">The interface value type.</typeparam>
+ internal class AssetData<TValue> : AssetInfo, IAssetData<TValue>
+ {
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>The content data being read.</summary>
+ public TValue Data { get; protected set; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="locale">The content's locale code, if the content is localised.</param>
+ /// <param name="assetName">The normalised asset name being read.</param>
+ /// <param name="data">The content data being read.</param>
+ /// <param name="getNormalisedPath">Normalises an asset key to match the cache key.</param>
+ public AssetData(string locale, string assetName, TValue data, Func<string, string> getNormalisedPath)
+ : base(locale, assetName, data.GetType(), getNormalisedPath)
+ {
+ this.Data = data;
+ }
+
+ /// <summary>Replace the entire content value with the given value. This is generally not recommended, since it may break compatibility with other mods or different versions of the game.</summary>
+ /// <param name="value">The new content value.</param>
+ /// <exception cref="ArgumentNullException">The <paramref name="value"/> is null.</exception>
+ /// <exception cref="InvalidCastException">The <paramref name="value"/>'s type is not compatible with the loaded asset's type.</exception>
+ public void ReplaceWith(TValue value)
+ {
+ if (value == null)
+ throw new ArgumentNullException(nameof(value), "Can't set a loaded asset to a null value.");
+ if (!this.DataType.IsInstanceOfType(value))
+ throw new InvalidCastException($"Can't replace loaded asset of type {this.GetFriendlyTypeName(this.DataType)} with value of type {this.GetFriendlyTypeName(value.GetType())}. The new type must be compatible to prevent game errors.");
+
+ this.Data = value;
+ }
+ }
+}
diff --git a/src/StardewModdingAPI/Framework/Content/ContentEventHelperForDictionary.cs b/src/StardewModdingAPI/Framework/Content/AssetDataForDictionary.cs
index 26f059e4..e9b29b12 100644
--- a/src/StardewModdingAPI/Framework/Content/ContentEventHelperForDictionary.cs
+++ b/src/StardewModdingAPI/Framework/Content/AssetDataForDictionary.cs
@@ -5,7 +5,7 @@ using System.Linq;
namespace StardewModdingAPI.Framework.Content
{
/// <summary>Encapsulates access and changes to dictionary content being read from a data file.</summary>
- internal class ContentEventHelperForDictionary<TKey, TValue> : ContentEventData<IDictionary<TKey, TValue>>, IContentEventHelperForDictionary<TKey, TValue>
+ internal class AssetDataForDictionary<TKey, TValue> : AssetData<IDictionary<TKey, TValue>>, IAssetDataForDictionary<TKey, TValue>
{
/*********
** Public methods
@@ -15,7 +15,7 @@ namespace StardewModdingAPI.Framework.Content
/// <param name="assetName">The normalised asset name being read.</param>
/// <param name="data">The content data being read.</param>
/// <param name="getNormalisedPath">Normalises an asset key to match the cache key.</param>
- public ContentEventHelperForDictionary(string locale, string assetName, IDictionary<TKey, TValue> data, Func<string, string> getNormalisedPath)
+ public AssetDataForDictionary(string locale, string assetName, IDictionary<TKey, TValue> data, Func<string, string> getNormalisedPath)
: base(locale, assetName, data, getNormalisedPath) { }
/// <summary>Add or replace an entry in the dictionary.</summary>
diff --git a/src/StardewModdingAPI/Framework/Content/ContentEventHelperForImage.cs b/src/StardewModdingAPI/Framework/Content/AssetDataForImage.cs
index da30590b..45c5588b 100644
--- a/src/StardewModdingAPI/Framework/Content/ContentEventHelperForImage.cs
+++ b/src/StardewModdingAPI/Framework/Content/AssetDataForImage.cs
@@ -5,7 +5,7 @@ using Microsoft.Xna.Framework.Graphics;
namespace StardewModdingAPI.Framework.Content
{
/// <summary>Encapsulates access and changes to dictionary content being read from a data file.</summary>
- internal class ContentEventHelperForImage : ContentEventData<Texture2D>, IContentEventHelperForImage
+ internal class AssetDataForImage : AssetData<Texture2D>, IAssetDataForImage
{
/*********
** Public methods
@@ -15,7 +15,7 @@ namespace StardewModdingAPI.Framework.Content
/// <param name="assetName">The normalised asset name being read.</param>
/// <param name="data">The content data being read.</param>
/// <param name="getNormalisedPath">Normalises an asset key to match the cache key.</param>
- public ContentEventHelperForImage(string locale, string assetName, Texture2D data, Func<string, string> getNormalisedPath)
+ public AssetDataForImage(string locale, string assetName, Texture2D data, Func<string, string> getNormalisedPath)
: base(locale, assetName, data, getNormalisedPath) { }
/// <summary>Overwrite part of the image.</summary>
diff --git a/src/StardewModdingAPI/Framework/Content/ContentEventHelper.cs b/src/StardewModdingAPI/Framework/Content/AssetDataForObject.cs
index 9bf1ea17..f30003e4 100644
--- a/src/StardewModdingAPI/Framework/Content/ContentEventHelper.cs
+++ b/src/StardewModdingAPI/Framework/Content/AssetDataForObject.cs
@@ -5,7 +5,7 @@ using Microsoft.Xna.Framework.Graphics;
namespace StardewModdingAPI.Framework.Content
{
/// <summary>Encapsulates access and changes to content being read from a data file.</summary>
- internal class ContentEventHelper : ContentEventData<object>, IContentEventHelper
+ internal class AssetDataForObject : AssetData<object>, IAssetData
{
/*********
** Public methods
@@ -15,23 +15,30 @@ namespace StardewModdingAPI.Framework.Content
/// <param name="assetName">The normalised asset name being read.</param>
/// <param name="data">The content data being read.</param>
/// <param name="getNormalisedPath">Normalises an asset key to match the cache key.</param>
- public ContentEventHelper(string locale, string assetName, object data, Func<string, string> getNormalisedPath)
+ public AssetDataForObject(string locale, string assetName, object data, Func<string, string> getNormalisedPath)
: base(locale, assetName, data, getNormalisedPath) { }
+ /// <summary>Construct an instance.</summary>
+ /// <param name="info">The asset metadata.</param>
+ /// <param name="data">The content data being read.</param>
+ /// <param name="getNormalisedPath">Normalises an asset key to match the cache key.</param>
+ public AssetDataForObject(IAssetInfo info, object data, Func<string, string> getNormalisedPath)
+ : this(info.Locale, info.AssetName, data, getNormalisedPath) { }
+
/// <summary>Get a helper to manipulate the data as a dictionary.</summary>
/// <typeparam name="TKey">The expected dictionary key.</typeparam>
/// <typeparam name="TValue">The expected dictionary balue.</typeparam>
/// <exception cref="InvalidOperationException">The content being read isn't a dictionary.</exception>
- public IContentEventHelperForDictionary<TKey, TValue> AsDictionary<TKey, TValue>()
+ public IAssetDataForDictionary<TKey, TValue> AsDictionary<TKey, TValue>()
{
- return new ContentEventHelperForDictionary<TKey, TValue>(this.Locale, this.AssetName, this.GetData<IDictionary<TKey, TValue>>(), this.GetNormalisedPath);
+ return new AssetDataForDictionary<TKey, TValue>(this.Locale, this.AssetName, this.GetData<IDictionary<TKey, TValue>>(), this.GetNormalisedPath);
}
/// <summary>Get a helper to manipulate the data as an image.</summary>
/// <exception cref="InvalidOperationException">The content being read isn't an image.</exception>
- public IContentEventHelperForImage AsImage()
+ public IAssetDataForImage AsImage()
{
- return new ContentEventHelperForImage(this.Locale, this.AssetName, this.GetData<Texture2D>(), this.GetNormalisedPath);
+ return new AssetDataForImage(this.Locale, this.AssetName, this.GetData<Texture2D>(), this.GetNormalisedPath);
}
/// <summary>Get the data as a given type.</summary>
diff --git a/src/StardewModdingAPI/Framework/Content/ContentEventData.cs b/src/StardewModdingAPI/Framework/Content/AssetInfo.cs
index 1a1779d4..d580dc06 100644
--- a/src/StardewModdingAPI/Framework/Content/ContentEventData.cs
+++ b/src/StardewModdingAPI/Framework/Content/AssetInfo.cs
@@ -4,9 +4,7 @@ using Microsoft.Xna.Framework.Graphics;
namespace StardewModdingAPI.Framework.Content
{
- /// <summary>Base implementation for a content helper which encapsulates access and changes to content being read from a data file.</summary>
- /// <typeparam name="TValue">The interface value type.</typeparam>
- internal class ContentEventData<TValue> : EventArgs, IContentEventData<TValue>
+ internal class AssetInfo : IAssetInfo
{
/*********
** Properties
@@ -21,12 +19,9 @@ namespace StardewModdingAPI.Framework.Content
/// <summary>The content's locale code, if the content is localised.</summary>
public string Locale { get; }
- /// <summary>The normalised asset name being read. The format may change between platforms; see <see cref="IsAssetName"/> to compare with a known path.</summary>
+ /// <summary>The normalised asset name being read. The format may change between platforms; see <see cref="AssetNameEquals"/> to compare with a known path.</summary>
public string AssetName { get; }
- /// <summary>The content data being read.</summary>
- public TValue Data { get; protected set; }
-
/// <summary>The content data type.</summary>
public Type DataType { get; }
@@ -37,48 +32,24 @@ namespace StardewModdingAPI.Framework.Content
/// <summary>Construct an instance.</summary>
/// <param name="locale">The content's locale code, if the content is localised.</param>
/// <param name="assetName">The normalised asset name being read.</param>
- /// <param name="data">The content data being read.</param>
- /// <param name="getNormalisedPath">Normalises an asset key to match the cache key.</param>
- public ContentEventData(string locale, string assetName, TValue data, Func<string, string> getNormalisedPath)
- : this(locale, assetName, data, data.GetType(), getNormalisedPath) { }
-
- /// <summary>Construct an instance.</summary>
- /// <param name="locale">The content's locale code, if the content is localised.</param>
- /// <param name="assetName">The normalised asset name being read.</param>
- /// <param name="data">The content data being read.</param>
- /// <param name="dataType">The content data type being read.</param>
+ /// <param name="type">The content type being read.</param>
/// <param name="getNormalisedPath">Normalises an asset key to match the cache key.</param>
- public ContentEventData(string locale, string assetName, TValue data, Type dataType, Func<string, string> getNormalisedPath)
+ public AssetInfo(string locale, string assetName, Type type, Func<string, string> getNormalisedPath)
{
this.Locale = locale;
this.AssetName = assetName;
- this.Data = data;
- this.DataType = dataType;
+ this.DataType = type;
this.GetNormalisedPath = getNormalisedPath;
}
/// <summary>Get whether the asset name being loaded matches a given name after normalisation.</summary>
/// <param name="path">The expected asset path, relative to the game's content folder and without the .xnb extension or locale suffix (like 'Data\ObjectInformation').</param>
- public bool IsAssetName(string path)
+ public bool AssetNameEquals(string path)
{
path = this.GetNormalisedPath(path);
return this.AssetName.Equals(path, StringComparison.InvariantCultureIgnoreCase);
}
- /// <summary>Replace the entire content value with the given value. This is generally not recommended, since it may break compatibility with other mods or different versions of the game.</summary>
- /// <param name="value">The new content value.</param>
- /// <exception cref="ArgumentNullException">The <paramref name="value"/> is null.</exception>
- /// <exception cref="InvalidCastException">The <paramref name="value"/>'s type is not compatible with the loaded asset's type.</exception>
- public void ReplaceWith(TValue value)
- {
- if (value == null)
- throw new ArgumentNullException(nameof(value), "Can't set a loaded asset to a null value.");
- if (!this.DataType.IsInstanceOfType(value))
- throw new InvalidCastException($"Can't replace loaded asset of type {this.GetFriendlyTypeName(this.DataType)} with value of type {this.GetFriendlyTypeName(value.GetType())}. The new type must be compatible to prevent game errors.");
-
- this.Data = value;
- }
-
/*********
** Protected methods