summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-03-10 12:00:11 -0500
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-03-10 12:00:11 -0500
commite3522edddd0636116bd4772b4e4dbfce9c7c0f8d (patch)
tree3d6fdc4c1cc571d8c679018f56a75907a57c0618
parentff39e9b1716104e02c92dfd56bb919887873bd3d (diff)
downloadSMAPI-e3522edddd0636116bd4772b4e4dbfce9c7c0f8d.tar.gz
SMAPI-e3522edddd0636116bd4772b4e4dbfce9c7c0f8d.tar.bz2
SMAPI-e3522edddd0636116bd4772b4e4dbfce9c7c0f8d.zip
extend base content helper to support null content (#173)
-rw-r--r--src/StardewModdingAPI/Framework/Content/ContentEventData.cs (renamed from src/StardewModdingAPI/Framework/Content/ContentEventBaseHelper.cs)21
-rw-r--r--src/StardewModdingAPI/Framework/Content/ContentEventHelper.cs2
-rw-r--r--src/StardewModdingAPI/Framework/Content/ContentEventHelperForDictionary.cs2
-rw-r--r--src/StardewModdingAPI/Framework/Content/ContentEventHelperForImage.cs2
-rw-r--r--src/StardewModdingAPI/IContentEventData.cs3
-rw-r--r--src/StardewModdingAPI/StardewModdingAPI.csproj4
6 files changed, 25 insertions, 9 deletions
diff --git a/src/StardewModdingAPI/Framework/Content/ContentEventBaseHelper.cs b/src/StardewModdingAPI/Framework/Content/ContentEventData.cs
index a89fe337..1a1779d4 100644
--- a/src/StardewModdingAPI/Framework/Content/ContentEventBaseHelper.cs
+++ b/src/StardewModdingAPI/Framework/Content/ContentEventData.cs
@@ -6,7 +6,7 @@ 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 ContentEventBaseHelper<TValue> : EventArgs, IContentEventData<TValue>
+ internal class ContentEventData<TValue> : EventArgs, IContentEventData<TValue>
{
/*********
** Properties
@@ -27,6 +27,9 @@ namespace StardewModdingAPI.Framework.Content
/// <summary>The content data being read.</summary>
public TValue Data { get; protected set; }
+ /// <summary>The content data type.</summary>
+ public Type DataType { get; }
+
/*********
** Public methods
@@ -36,11 +39,21 @@ 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 ContentEventBaseHelper(string locale, string assetName, TValue data, Func<string, string> getNormalisedPath)
+ 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="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)
{
this.Locale = locale;
this.AssetName = assetName;
this.Data = data;
+ this.DataType = dataType;
this.GetNormalisedPath = getNormalisedPath;
}
@@ -60,8 +73,8 @@ namespace StardewModdingAPI.Framework.Content
{
if (value == null)
throw new ArgumentNullException(nameof(value), "Can't set a loaded asset to a null value.");
- if (!this.Data.GetType().IsInstanceOfType(value))
- throw new InvalidCastException($"Can't replace loaded asset of type {this.GetFriendlyTypeName(this.Data.GetType())} with value of type {this.GetFriendlyTypeName(value.GetType())}. The new type must be compatible to prevent game errors.");
+ 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/ContentEventHelper.cs b/src/StardewModdingAPI/Framework/Content/ContentEventHelper.cs
index 26292cab..9bf1ea17 100644
--- a/src/StardewModdingAPI/Framework/Content/ContentEventHelper.cs
+++ b/src/StardewModdingAPI/Framework/Content/ContentEventHelper.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 : ContentEventBaseHelper<object>, IContentEventHelper
+ internal class ContentEventHelper : ContentEventData<object>, IContentEventHelper
{
/*********
** Public methods
diff --git a/src/StardewModdingAPI/Framework/Content/ContentEventHelperForDictionary.cs b/src/StardewModdingAPI/Framework/Content/ContentEventHelperForDictionary.cs
index 8a8a4845..26f059e4 100644
--- a/src/StardewModdingAPI/Framework/Content/ContentEventHelperForDictionary.cs
+++ b/src/StardewModdingAPI/Framework/Content/ContentEventHelperForDictionary.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> : ContentEventBaseHelper<IDictionary<TKey, TValue>>, IContentEventHelperForDictionary<TKey, TValue>
+ internal class ContentEventHelperForDictionary<TKey, TValue> : ContentEventData<IDictionary<TKey, TValue>>, IContentEventHelperForDictionary<TKey, TValue>
{
/*********
** Public methods
diff --git a/src/StardewModdingAPI/Framework/Content/ContentEventHelperForImage.cs b/src/StardewModdingAPI/Framework/Content/ContentEventHelperForImage.cs
index 23760f0f..da30590b 100644
--- a/src/StardewModdingAPI/Framework/Content/ContentEventHelperForImage.cs
+++ b/src/StardewModdingAPI/Framework/Content/ContentEventHelperForImage.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 : ContentEventBaseHelper<Texture2D>, IContentEventHelperForImage
+ internal class ContentEventHelperForImage : ContentEventData<Texture2D>, IContentEventHelperForImage
{
/*********
** Public methods
diff --git a/src/StardewModdingAPI/IContentEventData.cs b/src/StardewModdingAPI/IContentEventData.cs
index a21861d2..7e2d4df1 100644
--- a/src/StardewModdingAPI/IContentEventData.cs
+++ b/src/StardewModdingAPI/IContentEventData.cs
@@ -18,6 +18,9 @@ namespace StardewModdingAPI
/// <summary>The content data being read.</summary>
TValue Data { get; }
+ /// <summary>The content data type.</summary>
+ Type DataType { get; }
+
/*********
** Public methods
diff --git a/src/StardewModdingAPI/StardewModdingAPI.csproj b/src/StardewModdingAPI/StardewModdingAPI.csproj
index 445f6f37..ab808948 100644
--- a/src/StardewModdingAPI/StardewModdingAPI.csproj
+++ b/src/StardewModdingAPI/StardewModdingAPI.csproj
@@ -150,7 +150,7 @@
<Compile Include="Framework\AssemblyDefinitionResolver.cs" />
<Compile Include="Framework\AssemblyParseResult.cs" />
<Compile Include="Framework\CommandManager.cs" />
- <Compile Include="Framework\Content\ContentEventBaseHelper.cs" />
+ <Compile Include="Framework\Content\ContentEventData.cs" />
<Compile Include="Framework\Content\ContentEventHelper.cs" />
<Compile Include="Framework\Content\ContentEventHelperForDictionary.cs" />
<Compile Include="Framework\Content\ContentEventHelperForImage.cs" />
@@ -165,10 +165,10 @@
<Compile Include="Framework\Serialisation\SelectiveStringEnumConverter.cs" />
<Compile Include="Framework\Serialisation\SemanticVersionConverter.cs" />
<Compile Include="ICommandHelper.cs" />
+ <Compile Include="IContentEventData.cs" />
<Compile Include="IContentEventHelper.cs" />
<Compile Include="IContentEventHelperForDictionary.cs" />
<Compile Include="IContentEventHelperForImage.cs" />
- <Compile Include="IContentEventData.cs" />
<Compile Include="IModRegistry.cs" />
<Compile Include="Events\LocationEvents.cs" />
<Compile Include="Events\MenuEvents.cs" />