summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/Framework
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-07-07 11:29:17 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-07-07 11:29:17 -0400
commitc5e106801e9137078decfd6b6e3761240b47f94e (patch)
tree6abc2e197ddce719525f220d00a3dea840d4589f /src/StardewModdingAPI/Framework
parent3b8d1e49f025e56e0442604c2e6956d77d673b0d (diff)
downloadSMAPI-c5e106801e9137078decfd6b6e3761240b47f94e.tar.gz
SMAPI-c5e106801e9137078decfd6b6e3761240b47f94e.tar.bz2
SMAPI-c5e106801e9137078decfd6b6e3761240b47f94e.zip
split reflection logic out of mod helper (#318)
Diffstat (limited to 'src/StardewModdingAPI/Framework')
-rw-r--r--src/StardewModdingAPI/Framework/InternalExtensions.cs5
-rw-r--r--src/StardewModdingAPI/Framework/Reflection/Reflector.cs (renamed from src/StardewModdingAPI/Framework/Reflection/ReflectionHelper.cs)44
-rw-r--r--src/StardewModdingAPI/Framework/SContentManager.cs2
-rw-r--r--src/StardewModdingAPI/Framework/SGame.cs5
4 files changed, 9 insertions, 47 deletions
diff --git a/src/StardewModdingAPI/Framework/InternalExtensions.cs b/src/StardewModdingAPI/Framework/InternalExtensions.cs
index b99d3798..2842bc83 100644
--- a/src/StardewModdingAPI/Framework/InternalExtensions.cs
+++ b/src/StardewModdingAPI/Framework/InternalExtensions.cs
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.Xna.Framework.Graphics;
+using StardewModdingAPI.Framework.Reflection;
using StardewValley;
namespace StardewModdingAPI.Framework
@@ -99,7 +100,7 @@ namespace StardewModdingAPI.Framework
/// <summary>Get whether the sprite batch is between a begin and end pair.</summary>
/// <param name="spriteBatch">The sprite batch to check.</param>
/// <param name="reflection">The reflection helper with which to access private fields.</param>
- public static bool IsOpen(this SpriteBatch spriteBatch, IReflectionHelper reflection)
+ public static bool IsOpen(this SpriteBatch spriteBatch, Reflector reflection)
{
// get field name
const string fieldName =
@@ -110,7 +111,7 @@ namespace StardewModdingAPI.Framework
#endif
// get result
- return reflection.GetPrivateValue<bool>(Game1.spriteBatch, fieldName);
+ return reflection.GetPrivateField<bool>(Game1.spriteBatch, fieldName).GetValue();
}
}
}
diff --git a/src/StardewModdingAPI/Framework/Reflection/ReflectionHelper.cs b/src/StardewModdingAPI/Framework/Reflection/Reflector.cs
index 7a5789dc..5c2d90fa 100644
--- a/src/StardewModdingAPI/Framework/Reflection/ReflectionHelper.cs
+++ b/src/StardewModdingAPI/Framework/Reflection/Reflector.cs
@@ -7,13 +7,13 @@ namespace StardewModdingAPI.Framework.Reflection
{
/// <summary>Provides helper methods for accessing private game code.</summary>
/// <remarks>This implementation searches up the type hierarchy, and caches the reflected fields and methods with a sliding expiry (to optimise performance without unnecessary memory usage).</remarks>
- internal class ReflectionHelper : IReflectionHelper
+ internal class Reflector
{
/*********
** Properties
*********/
/// <summary>The cached fields and methods found via reflection.</summary>
- private readonly MemoryCache Cache = new MemoryCache(typeof(ReflectionHelper).FullName);
+ private readonly MemoryCache Cache = new MemoryCache(typeof(Reflector).FullName);
/// <summary>The sliding cache expiration time.</summary>
private readonly TimeSpan SlidingCacheExpiry = TimeSpan.FromMinutes(5);
@@ -94,46 +94,6 @@ namespace StardewModdingAPI.Framework.Reflection
}
/****
- ** Field values
- ** (shorthand since this is the most common case)
- ****/
- /// <summary>Get the value of a private instance field.</summary>
- /// <typeparam name="TValue">The field type.</typeparam>
- /// <param name="obj">The object which has the field.</param>
- /// <param name="name">The field name.</param>
- /// <param name="required">Whether to throw an exception if the private field is not found.</param>
- /// <returns>Returns the field value, or the default value for <typeparamref name="TValue"/> if the field wasn't found and <paramref name="required"/> is false.</returns>
- /// <remarks>
- /// This is a shortcut for <see cref="GetPrivateField{TValue}(object,string,bool)"/> followed by <see cref="IPrivateField{TValue}.GetValue"/>.
- /// When <paramref name="required" /> is false, this will return the default value if reflection fails. If you need to check whether the field exists, use <see cref="GetPrivateField{TValue}(object,string,bool)" /> instead.
- /// </remarks>
- public TValue GetPrivateValue<TValue>(object obj, string name, bool required = true)
- {
- IPrivateField<TValue> field = this.GetPrivateField<TValue>(obj, name, required);
- return field != null
- ? field.GetValue()
- : default(TValue);
- }
-
- /// <summary>Get the value of a private static field.</summary>
- /// <typeparam name="TValue">The field type.</typeparam>
- /// <param name="type">The type which has the field.</param>
- /// <param name="name">The field name.</param>
- /// <param name="required">Whether to throw an exception if the private field is not found.</param>
- /// <returns>Returns the field value, or the default value for <typeparamref name="TValue"/> if the field wasn't found and <paramref name="required"/> is false.</returns>
- /// <remarks>
- /// This is a shortcut for <see cref="GetPrivateField{TValue}(Type,string,bool)"/> followed by <see cref="IPrivateField{TValue}.GetValue"/>.
- /// When <paramref name="required" /> is false, this will return the default value if reflection fails. If you need to check whether the field exists, use <see cref="GetPrivateField{TValue}(Type,string,bool)" /> instead.
- /// </remarks>
- public TValue GetPrivateValue<TValue>(Type type, string name, bool required = true)
- {
- IPrivateField<TValue> field = this.GetPrivateField<TValue>(type, name, required);
- return field != null
- ? field.GetValue()
- : default(TValue);
- }
-
- /****
** Methods
****/
/// <summary>Get a private instance method.</summary>
diff --git a/src/StardewModdingAPI/Framework/SContentManager.cs b/src/StardewModdingAPI/Framework/SContentManager.cs
index 42c3b0e6..669b0e7a 100644
--- a/src/StardewModdingAPI/Framework/SContentManager.cs
+++ b/src/StardewModdingAPI/Framework/SContentManager.cs
@@ -71,7 +71,7 @@ namespace StardewModdingAPI.Framework
throw new ArgumentNullException(nameof(monitor));
// initialise
- IReflectionHelper reflection = new ReflectionHelper();
+ var reflection = new Reflector();
this.Monitor = monitor;
// get underlying fields for interception
diff --git a/src/StardewModdingAPI/Framework/SGame.cs b/src/StardewModdingAPI/Framework/SGame.cs
index f2c5c0c9..c7784c60 100644
--- a/src/StardewModdingAPI/Framework/SGame.cs
+++ b/src/StardewModdingAPI/Framework/SGame.cs
@@ -10,6 +10,7 @@ using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using StardewModdingAPI.Events;
+using StardewModdingAPI.Framework.Reflection;
using StardewModdingAPI.Utilities;
using StardewValley;
using StardewValley.BellsAndWhistles;
@@ -150,7 +151,7 @@ namespace StardewModdingAPI.Framework
** Private wrappers
****/
/// <summary>Simplifies access to private game code.</summary>
- private static IReflectionHelper Reflection;
+ private static Reflector Reflection;
// ReSharper disable ArrangeStaticMemberQualifier, ArrangeThisQualifier, InconsistentNaming
/// <summary>Used to access private fields and methods.</summary>
@@ -184,7 +185,7 @@ namespace StardewModdingAPI.Framework
/// <summary>Construct an instance.</summary>
/// <param name="monitor">Encapsulates monitoring and logging.</param>
/// <param name="reflection">Simplifies access to private game code.</param>
- internal SGame(IMonitor monitor, IReflectionHelper reflection)
+ internal SGame(IMonitor monitor, Reflector reflection)
{
// initialise
this.Monitor = monitor;