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
{
/// Provides extension methods for SMAPI's internal use.
internal static class InternalExtensions
{
/****
** IMonitor
****/
/// Safely raise an event, and intercept any exceptions thrown by its handlers.
/// Encapsulates monitoring and logging.
/// The event name for error messages.
/// The event handlers.
/// The event sender.
/// The event arguments (or null to pass ).
public static void SafelyRaisePlainEvent(this IMonitor monitor, string name, IEnumerable handlers, object sender = null, EventArgs args = null)
{
if (handlers == null)
return;
foreach (EventHandler handler in handlers.Cast())
{
// 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);
}
}
}
/// Safely raise an event, and intercept any exceptions thrown by its handlers.
/// The event argument object type.
/// Encapsulates monitoring and logging.
/// The event name for error messages.
/// The event handlers.
/// The event sender.
/// The event arguments.
public static void SafelyRaiseGenericEvent(this IMonitor monitor, string name, IEnumerable handlers, object sender, TEventArgs args)
{
if (handlers == null)
return;
foreach (EventHandler handler in handlers.Cast>())
{
try
{
handler.Invoke(sender, args);
}
catch (Exception ex)
{
monitor.Log($"A mod failed handling the {name} event:\n{ex.GetLogSummary()}", LogLevel.Error);
}
}
}
/// Log a message for the player or developer the first time it occurs.
/// The monitor through which to log the message.
/// The hash of logged messages.
/// The message to log.
/// The log severity level.
public static void LogOnce(this IMonitor monitor, HashSet hash, string message, LogLevel level = LogLevel.Trace)
{
if (!hash.Contains(message))
{
monitor.Log(message, level);
hash.Add(message);
}
}
/****
** Exceptions
****/
/// Get a string representation of an exception suitable for writing to the error log.
/// The error to summarise.
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();
}
}
/// Get the lowest exception in an exception stack.
/// The exception from which to search.
public static Exception GetInnermostException(this Exception exception)
{
while (exception.InnerException != null)
exception = exception.InnerException;
return exception;
}
/****
** Sprite batch
****/
/// Get whether the sprite batch is between a begin and end pair.
/// The sprite batch to check.
/// The reflection helper with which to access private fields.
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.GetField(Game1.spriteBatch, fieldName).GetValue();
}
}
}