using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.Xna.Framework.Graphics;
using StardewModdingAPI.Framework.Events;
using StardewModdingAPI.Framework.Reflection;
using StardewValley.Menus;
namespace StardewModdingAPI.Framework
{
/// Provides extension methods for SMAPI's internal use.
internal static class InternalExtensions
{
/*********
** Public methods
*********/
/****
** IMonitor
****/
/// 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);
}
}
/****
** IModMetadata
****/
/// Log a message using the mod's monitor.
/// The mod whose monitor to use.
/// The message to log.
/// The log severity level.
public static void LogAsMod(this IModMetadata metadata, string message, LogLevel level = LogLevel.Trace)
{
if (metadata.Monitor is null)
throw new InvalidOperationException($"Can't log as mod {metadata.DisplayName}: mod is broken or a content pack. Logged message:\n[{level}] {message}");
metadata.Monitor.Log(message, level);
}
/// Log a message using the mod's monitor, but only if it hasn't already been logged since the last game launch.
/// The mod whose monitor to use.
/// The message to log.
/// The log severity level.
public static void LogAsModOnce(this IModMetadata metadata, string message, LogLevel level = LogLevel.Trace)
{
metadata.Monitor?.LogOnce(message, level);
}
/****
** ManagedEvent
****/
/// Raise the event using the default event args and notify all handlers.
/// The event args type to construct.
/// The event to raise.
public static void RaiseEmpty(this ManagedEvent @event) where TEventArgs : new()
{
if (@event.HasListeners)
@event.Raise(Singleton.Instance);
}
/****
** ReaderWriterLockSlim
****/
/// Run code within a read lock.
/// The lock to set.
/// The action to perform.
public static void InReadLock(this ReaderWriterLockSlim @lock, Action action)
{
@lock.EnterReadLock();
try
{
action();
}
finally
{
@lock.ExitReadLock();
}
}
/// Run code within a read lock.
/// The action's return value.
/// The lock to set.
/// The action to perform.
public static TReturn InReadLock(this ReaderWriterLockSlim @lock, Func action)
{
@lock.EnterReadLock();
try
{
return action();
}
finally
{
@lock.ExitReadLock();
}
}
/// Run code within a write lock.
/// The lock to set.
/// The action to perform.
public static void InWriteLock(this ReaderWriterLockSlim @lock, Action action)
{
@lock.EnterWriteLock();
try
{
action();
}
finally
{
@lock.ExitWriteLock();
}
}
/// Run code within a write lock.
/// The action's return value.
/// The lock to set.
/// The action to perform.
public static TReturn InWriteLock(this ReaderWriterLockSlim @lock, Func action)
{
@lock.EnterWriteLock();
try
{
return action();
}
finally
{
@lock.ExitWriteLock();
}
}
/****
** IActiveClickableMenu
****/
/// Get a string representation of the menu chain to the given menu (including the specified menu), in parent to child order.
/// The menu whose chain to get.
public static string GetMenuChainLabel(this IClickableMenu menu)
{
static IEnumerable GetAncestors(IClickableMenu menu)
{
for (; menu != null; menu = menu.GetParentMenu())
yield return menu;
}
return string.Join(" > ", GetAncestors(menu).Reverse().Select(p => p.GetType().FullName));
}
/****
** 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)
{
return reflection.GetField(spriteBatch, "_beginCalled").GetValue();
}
}
}