extend base content helper to support null content (#173)

This commit is contained in:
Jesse Plamondon-Willard 2017-03-10 12:00:11 -05:00
parent ff39e9b171
commit e3522edddd
6 changed files with 25 additions and 9 deletions

View File

@ -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> /// <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> /// <typeparam name="TValue">The interface value type.</typeparam>
internal class ContentEventBaseHelper<TValue> : EventArgs, IContentEventData<TValue> internal class ContentEventData<TValue> : EventArgs, IContentEventData<TValue>
{ {
/********* /*********
** Properties ** Properties
@ -27,6 +27,9 @@ namespace StardewModdingAPI.Framework.Content
/// <summary>The content data being read.</summary> /// <summary>The content data being read.</summary>
public TValue Data { get; protected set; } public TValue Data { get; protected set; }
/// <summary>The content data type.</summary>
public Type DataType { get; }
/********* /*********
** Public methods ** Public methods
@ -36,11 +39,21 @@ namespace StardewModdingAPI.Framework.Content
/// <param name="assetName">The normalised asset name being read.</param> /// <param name="assetName">The normalised asset name being read.</param>
/// <param name="data">The content data 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> /// <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.Locale = locale;
this.AssetName = assetName; this.AssetName = assetName;
this.Data = data; this.Data = data;
this.DataType = dataType;
this.GetNormalisedPath = getNormalisedPath; this.GetNormalisedPath = getNormalisedPath;
} }
@ -60,8 +73,8 @@ namespace StardewModdingAPI.Framework.Content
{ {
if (value == null) if (value == null)
throw new ArgumentNullException(nameof(value), "Can't set a loaded asset to a null value."); throw new ArgumentNullException(nameof(value), "Can't set a loaded asset to a null value.");
if (!this.Data.GetType().IsInstanceOfType(value)) if (!this.DataType.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."); 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; this.Data = value;
} }

View File

@ -5,7 +5,7 @@ using Microsoft.Xna.Framework.Graphics;
namespace StardewModdingAPI.Framework.Content namespace StardewModdingAPI.Framework.Content
{ {
/// <summary>Encapsulates access and changes to content being read from a data file.</summary> /// <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 ** Public methods

View File

@ -5,7 +5,7 @@ using System.Linq;
namespace StardewModdingAPI.Framework.Content namespace StardewModdingAPI.Framework.Content
{ {
/// <summary>Encapsulates access and changes to dictionary content being read from a data file.</summary> /// <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 ** Public methods

View File

@ -5,7 +5,7 @@ using Microsoft.Xna.Framework.Graphics;
namespace StardewModdingAPI.Framework.Content namespace StardewModdingAPI.Framework.Content
{ {
/// <summary>Encapsulates access and changes to dictionary content being read from a data file.</summary> /// <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 ** Public methods

View File

@ -18,6 +18,9 @@ namespace StardewModdingAPI
/// <summary>The content data being read.</summary> /// <summary>The content data being read.</summary>
TValue Data { get; } TValue Data { get; }
/// <summary>The content data type.</summary>
Type DataType { get; }
/********* /*********
** Public methods ** Public methods

View File

@ -150,7 +150,7 @@
<Compile Include="Framework\AssemblyDefinitionResolver.cs" /> <Compile Include="Framework\AssemblyDefinitionResolver.cs" />
<Compile Include="Framework\AssemblyParseResult.cs" /> <Compile Include="Framework\AssemblyParseResult.cs" />
<Compile Include="Framework\CommandManager.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\ContentEventHelper.cs" />
<Compile Include="Framework\Content\ContentEventHelperForDictionary.cs" /> <Compile Include="Framework\Content\ContentEventHelperForDictionary.cs" />
<Compile Include="Framework\Content\ContentEventHelperForImage.cs" /> <Compile Include="Framework\Content\ContentEventHelperForImage.cs" />
@ -165,10 +165,10 @@
<Compile Include="Framework\Serialisation\SelectiveStringEnumConverter.cs" /> <Compile Include="Framework\Serialisation\SelectiveStringEnumConverter.cs" />
<Compile Include="Framework\Serialisation\SemanticVersionConverter.cs" /> <Compile Include="Framework\Serialisation\SemanticVersionConverter.cs" />
<Compile Include="ICommandHelper.cs" /> <Compile Include="ICommandHelper.cs" />
<Compile Include="IContentEventData.cs" />
<Compile Include="IContentEventHelper.cs" /> <Compile Include="IContentEventHelper.cs" />
<Compile Include="IContentEventHelperForDictionary.cs" /> <Compile Include="IContentEventHelperForDictionary.cs" />
<Compile Include="IContentEventHelperForImage.cs" /> <Compile Include="IContentEventHelperForImage.cs" />
<Compile Include="IContentEventData.cs" />
<Compile Include="IModRegistry.cs" /> <Compile Include="IModRegistry.cs" />
<Compile Include="Events\LocationEvents.cs" /> <Compile Include="Events\LocationEvents.cs" />
<Compile Include="Events\MenuEvents.cs" /> <Compile Include="Events\MenuEvents.cs" />