use shared reflection helper

This commit is contained in:
Jesse Plamondon-Willard 2017-05-09 00:11:39 -04:00
parent 85f609dc6c
commit 486ac29796
3 changed files with 17 additions and 8 deletions

View File

@ -1,6 +1,5 @@
using System;
using System.IO;
using StardewModdingAPI.Framework.Reflection;
using StardewModdingAPI.Framework.Serialisation;
namespace StardewModdingAPI.Framework
@ -25,7 +24,7 @@ namespace StardewModdingAPI.Framework
public IContentHelper Content { get; }
/// <summary>Simplifies access to private game code.</summary>
public IReflectionHelper Reflection { get; } = new ReflectionHelper();
public IReflectionHelper Reflection { get; }
/// <summary>Metadata about loaded mods.</summary>
public IModRegistry ModRegistry { get; }
@ -44,9 +43,10 @@ namespace StardewModdingAPI.Framework
/// <param name="modRegistry">Metadata about loaded mods.</param>
/// <param name="commandManager">Manages console commands.</param>
/// <param name="contentManager">The content manager which loads content assets.</param>
/// <param name="reflection">Simplifies access to private game code.</param>
/// <exception cref="ArgumentNullException">An argument is null or empty.</exception>
/// <exception cref="InvalidOperationException">The <paramref name="modDirectory"/> path does not exist on disk.</exception>
public ModHelper(IManifest manifest, string modDirectory, JsonHelper jsonHelper, IModRegistry modRegistry, CommandManager commandManager, SContentManager contentManager)
public ModHelper(IManifest manifest, string modDirectory, JsonHelper jsonHelper, IModRegistry modRegistry, CommandManager commandManager, SContentManager contentManager, IReflectionHelper reflection)
{
// validate
if (string.IsNullOrWhiteSpace(modDirectory))
@ -64,6 +64,7 @@ namespace StardewModdingAPI.Framework
this.Content = new ContentHelper(contentManager, modDirectory, manifest.Name);
this.ModRegistry = modRegistry;
this.ConsoleCommands = new CommandHelper(manifest.Name, commandManager);
this.Reflection = reflection;
}
/****

View File

@ -9,7 +9,6 @@ using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using StardewModdingAPI.Events;
using StardewModdingAPI.Framework.Reflection;
using StardewValley;
using StardewValley.BellsAndWhistles;
using StardewValley.Locations;
@ -157,9 +156,11 @@ namespace StardewModdingAPI.Framework
/****
** Private wrappers
****/
/// <summary>Simplifies access to private game code.</summary>
private static IReflectionHelper Reflection;
// ReSharper disable ArrangeStaticMemberQualifier, ArrangeThisQualifier, InconsistentNaming
/// <summary>Used to access private fields and methods.</summary>
private static readonly IReflectionHelper Reflection = new ReflectionHelper();
private static List<float> _fpsList => SGame.Reflection.GetPrivateField<List<float>>(typeof(Game1), nameof(_fpsList)).GetValue();
private static Stopwatch _fpsStopwatch => SGame.Reflection.GetPrivateField<Stopwatch>(typeof(Game1), nameof(SGame._fpsStopwatch)).GetValue();
private static float _fps
@ -176,6 +177,7 @@ namespace StardewModdingAPI.Framework
private readonly Action renderScreenBuffer = () => SGame.Reflection.GetPrivateMethod(SGame.Instance, nameof(renderScreenBuffer)).Invoke(new object[0]);
// ReSharper restore ArrangeStaticMemberQualifier, ArrangeThisQualifier, InconsistentNaming
/*********
** Accessors
*********/
@ -188,11 +190,13 @@ namespace StardewModdingAPI.Framework
*********/
/// <summary>Construct an instance.</summary>
/// <param name="monitor">Encapsulates monitoring and logging.</param>
internal SGame(IMonitor monitor)
/// <param name="reflection">Simplifies access to private game code.</param>
internal SGame(IMonitor monitor, IReflectionHelper reflection)
{
this.Monitor = monitor;
this.FirstUpdate = true;
SGame.Instance = this;
SGame.Reflection = reflection;
Game1.graphics.GraphicsProfile = GraphicsProfile.HiDef; // required by Stardew Valley
}

View File

@ -15,6 +15,7 @@ using StardewModdingAPI.Events;
using StardewModdingAPI.Framework;
using StardewModdingAPI.Framework.Logging;
using StardewModdingAPI.Framework.Models;
using StardewModdingAPI.Framework.Reflection;
using StardewModdingAPI.Framework.Serialisation;
using StardewValley;
using Monitor = StardewModdingAPI.Framework.Monitor;
@ -40,6 +41,9 @@ namespace StardewModdingAPI
/// <summary>Tracks whether the game should exit immediately and any pending initialisation should be cancelled.</summary>
private readonly CancellationTokenSource CancellationTokenSource = new CancellationTokenSource();
/// <summary>Simplifies access to private game code.</summary>
private readonly IReflectionHelper Reflection = new ReflectionHelper();
/// <summary>The underlying game instance.</summary>
private SGame GameInstance;
@ -141,7 +145,7 @@ namespace StardewModdingAPI
AppDomain.CurrentDomain.UnhandledException += (sender, e) => this.Monitor.Log($"Critical app domain exception: {e.ExceptionObject}", LogLevel.Error);
// override game
this.GameInstance = new SGame(this.Monitor);
this.GameInstance = new SGame(this.Monitor, this.Reflection);
StardewValley.Program.gamePtr = this.GameInstance;
// add exit handler
@ -599,7 +603,7 @@ namespace StardewModdingAPI
// inject data
// get helper
mod.ModManifest = manifest;
mod.Helper = new ModHelper(manifest, directory.FullName, jsonHelper, this.ModRegistry, this.CommandManager, (SContentManager)Game1.content);
mod.Helper = new ModHelper(manifest, directory.FullName, jsonHelper, this.ModRegistry, this.CommandManager, (SContentManager)Game1.content, this.Reflection);
mod.Monitor = this.GetSecondaryMonitor(manifest.Name);
mod.PathOnDisk = directory.FullName;