diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-10-14 11:44:02 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-10-14 11:44:02 -0400 |
commit | 79118316065a01322d8ea12a14589ec016794c32 (patch) | |
tree | 7a26668a66ea0630a2b9367ac820fe7a6d99ac77 /src/StardewModdingAPI/Framework/InternalExtensions.cs | |
parent | af1a2bde8219c5d4b8660b13702725626a4a5647 (diff) | |
parent | 8aec1eff99858716afe7b8604b512181f78c214f (diff) | |
download | SMAPI-79118316065a01322d8ea12a14589ec016794c32.tar.gz SMAPI-79118316065a01322d8ea12a14589ec016794c32.tar.bz2 SMAPI-79118316065a01322d8ea12a14589ec016794c32.zip |
Merge branch 'develop' into stable
Diffstat (limited to 'src/StardewModdingAPI/Framework/InternalExtensions.cs')
-rw-r--r-- | src/StardewModdingAPI/Framework/InternalExtensions.cs | 117 |
1 files changed, 0 insertions, 117 deletions
diff --git a/src/StardewModdingAPI/Framework/InternalExtensions.cs b/src/StardewModdingAPI/Framework/InternalExtensions.cs deleted file mode 100644 index 2842bc83..00000000 --- a/src/StardewModdingAPI/Framework/InternalExtensions.cs +++ /dev/null @@ -1,117 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using Microsoft.Xna.Framework.Graphics; -using StardewModdingAPI.Framework.Reflection; -using StardewValley; - -namespace StardewModdingAPI.Framework -{ - /// <summary>Provides extension methods for SMAPI's internal use.</summary> - internal static class InternalExtensions - { - /**** - ** IMonitor - ****/ - /// <summary>Safely raise an <see cref="EventHandler"/> event, and intercept any exceptions thrown by its handlers.</summary> - /// <param name="monitor">Encapsulates monitoring and logging.</param> - /// <param name="name">The event name for error messages.</param> - /// <param name="handlers">The event handlers.</param> - /// <param name="sender">The event sender.</param> - /// <param name="args">The event arguments (or <c>null</c> to pass <see cref="EventArgs.Empty"/>).</param> - public static void SafelyRaisePlainEvent(this IMonitor monitor, string name, IEnumerable<Delegate> handlers, object sender = null, EventArgs args = null) - { - if (handlers == null) - return; - - foreach (EventHandler handler in handlers.Cast<EventHandler>()) - { - // handle SMAPI exiting - if (monitor.IsExiting) - { - monitor.Log($"SMAPI shutting down: aborting {name} event.", LogLevel.Warn); - return; - } - - // raise event - try - { - handler.Invoke(sender, args ?? EventArgs.Empty); - } - catch (Exception ex) - { - monitor.Log($"A mod failed handling the {name} event:\n{ex.GetLogSummary()}", LogLevel.Error); - } - } - } - - /// <summary>Safely raise an <see cref="EventHandler{TEventArgs}"/> event, and intercept any exceptions thrown by its handlers.</summary> - /// <typeparam name="TEventArgs">The event argument object type.</typeparam> - /// <param name="monitor">Encapsulates monitoring and logging.</param> - /// <param name="name">The event name for error messages.</param> - /// <param name="handlers">The event handlers.</param> - /// <param name="sender">The event sender.</param> - /// <param name="args">The event arguments.</param> - public static void SafelyRaiseGenericEvent<TEventArgs>(this IMonitor monitor, string name, IEnumerable<Delegate> handlers, object sender, TEventArgs args) - { - if (handlers == null) - return; - - foreach (EventHandler<TEventArgs> handler in handlers.Cast<EventHandler<TEventArgs>>()) - { - try - { - handler.Invoke(sender, args); - } - catch (Exception ex) - { - monitor.Log($"A mod failed handling the {name} event:\n{ex.GetLogSummary()}", LogLevel.Error); - } - } - } - - /**** - ** Exceptions - ****/ - /// <summary>Get a string representation of an exception suitable for writing to the error log.</summary> - /// <param name="exception">The error to summarise.</param> - public static string GetLogSummary(this Exception exception) - { - switch (exception) - { - case TypeLoadException ex: - return $"Failed loading type '{ex.TypeName}': {exception}"; - - case ReflectionTypeLoadException ex: - string summary = exception.ToString(); - foreach (Exception childEx in ex.LoaderExceptions) - summary += $"\n\n{childEx.GetLogSummary()}"; - return summary; - - default: - return exception.ToString(); - } - } - - /**** - ** Sprite batch - ****/ - /// <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, Reflector reflection) - { - // get field name - const string fieldName = -#if SMAPI_FOR_WINDOWS - "inBeginEndPair"; -#else - "_beginCalled"; -#endif - - // get result - return reflection.GetPrivateField<bool>(Game1.spriteBatch, fieldName).GetValue(); - } - } -} |