From 78e8a6a4a088d6bff017bfcf22007f9fc8950071 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 4 Nov 2016 18:24:10 -0400 Subject: remove extensions from public interface, refactor & document --- src/StardewModdingAPI/Inheritance/SGame.cs | 51 +++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 8 deletions(-) (limited to 'src/StardewModdingAPI/Inheritance/SGame.cs') diff --git a/src/StardewModdingAPI/Inheritance/SGame.cs b/src/StardewModdingAPI/Inheritance/SGame.cs index 74a3d5d5..bd6a3491 100644 --- a/src/StardewModdingAPI/Inheritance/SGame.cs +++ b/src/StardewModdingAPI/Inheritance/SGame.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -134,15 +135,15 @@ namespace StardewModdingAPI.Inheritance /// The game's current render target. public RenderTarget2D Screen { - get { return typeof(Game1).GetBaseFieldValue(Program.gamePtr, "screen"); } - set { typeof(Game1).SetBaseFieldValue(this, "screen", value); } + get { return this.GetBaseFieldValue("screen"); } + set { this.SetBaseFieldValue("screen", value); } } /// The game's current background color. public Color BgColour { - get { return (Color)typeof(Game1).GetBaseFieldValue(Program.gamePtr, "bgColor"); } - set { typeof(Game1).SetBaseFieldValue(this, "bgColor", value); } + get { return (Color)this.GetBaseFieldValue("bgColor"); } + set { this.SetBaseFieldValue("bgColor", value); } } /// The current game instance. @@ -879,10 +880,10 @@ namespace StardewModdingAPI.Inheritance } // raise location list changed - if (Game1.locations.GetHash() != this.PreviousGameLocations) + if (this.GetHash(Game1.locations) != this.PreviousGameLocations) { LocationEvents.InvokeLocationsChanged(Game1.locations); - this.PreviousGameLocations = Game1.locations.GetHash(); + this.PreviousGameLocations = this.GetHash(Game1.locations); } // raise current location changed @@ -944,7 +945,7 @@ namespace StardewModdingAPI.Inheritance } // raise current location's object list changed - int? objectHash = Game1.currentLocation?.objects?.GetHash(); + int? objectHash = Game1.currentLocation?.objects != null ? this.GetHash(Game1.currentLocation.objects) : (int?)null; if (objectHash != null && this.PreviousLocationObjects != objectHash) { LocationEvents.InvokeOnNewLocationObject(Game1.currentLocation.objects); @@ -1032,5 +1033,39 @@ namespace StardewModdingAPI.Inheritance } } } + + /// Get a hash value for an enumeration. + /// The enumeration of items to hash. + private int GetHash(IEnumerable enumerable) + { + var hash = 0; + foreach (var v in enumerable) + hash ^= v.GetHashCode(); + return hash; + } + + /// Get reflection metadata for a private field. + /// The field name. + private FieldInfo GetBaseFieldInfo(string name) + { + return typeof(Game1).GetField(name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static); + } + + /// Get the value of a private field. + /// The expected value type. + /// The field name. + private TValue GetBaseFieldValue(string name) where TValue : class + { + return this.GetBaseFieldInfo(name).GetValue(Program.gamePtr) as TValue; + } + + /// Set the value of a private field. + /// The expected value type. + /// The field name. + /// The value to set. + public void SetBaseFieldValue(string name, object value) where TValue : class + { + this.GetBaseFieldInfo(name).SetValue(Program.gamePtr, value as TValue); + } } -} \ No newline at end of file +} -- cgit