using System;
using StardewModdingAPI.Framework;
namespace StardewModdingAPI.Events
{
/// Events raised when the game changes state.
public static class GameEvents
{
/*********
** Properties
*********/
/// Manages deprecation warnings.
private static DeprecationManager DeprecationManager;
/*********
** Events
*********/
/// Raised during launch after configuring XNA or MonoGame. The game window hasn't been opened by this point. Called after .
internal static event EventHandler InitializeInternal;
/// Raised during launch after configuring Stardew Valley, loading it into memory, and opening the game window. The window is still blank by this point.
internal static event EventHandler GameLoadedInternal;
/// Raised during launch after configuring XNA or MonoGame. The game window hasn't been opened by this point. Called after .
[Obsolete("The " + nameof(Mod) + "." + nameof(Mod.Entry) + " method is now called after the " + nameof(GameEvents.Initialize) + " event, so any contained logic can be done directly in " + nameof(Mod.Entry) + ".")]
public static event EventHandler Initialize;
/// Raised before XNA loads or reloads graphics resources. Called during .
[Obsolete("The " + nameof(Mod) + "." + nameof(Mod.Entry) + " method is now called after the " + nameof(GameEvents.LoadContent) + " event, so any contained logic can be done directly in " + nameof(Mod.Entry) + ".")]
public static event EventHandler LoadContent;
/// Raised during launch after configuring Stardew Valley, loading it into memory, and opening the game window. The window is still blank by this point.
[Obsolete("The " + nameof(Mod) + "." + nameof(Mod.Entry) + " method is now called after the game loads, so any contained logic can be done directly in " + nameof(Mod.Entry) + ".")]
public static event EventHandler GameLoaded;
/// Raised during the first game update tick.
[Obsolete("The " + nameof(Mod) + "." + nameof(Mod.Entry) + " method is now called after the game loads, so any contained logic can be done directly in " + nameof(Mod.Entry) + ".")]
public static event EventHandler FirstUpdateTick;
/// Raised when the game updates its state (≈60 times per second).
public static event EventHandler UpdateTick;
/// Raised every other tick (≈30 times per second).
public static event EventHandler SecondUpdateTick;
/// Raised every fourth tick (≈15 times per second).
public static event EventHandler FourthUpdateTick;
/// Raised every eighth tick (≈8 times per second).
public static event EventHandler EighthUpdateTick;
/// Raised every 15th tick (≈4 times per second).
public static event EventHandler QuarterSecondTick;
/// Raised every 30th tick (≈twice per second).
public static event EventHandler HalfSecondTick;
/// Raised every 60th tick (≈once per second).
public static event EventHandler OneSecondTick;
/*********
** Internal methods
*********/
/// Injects types required for backwards compatibility.
/// Manages deprecation warnings.
internal static void Shim(DeprecationManager deprecationManager)
{
GameEvents.DeprecationManager = deprecationManager;
}
/// Raise an event.
/// Encapsulates logging and monitoring.
internal static void InvokeInitialize(IMonitor monitor)
{
// notify SMAPI
monitor.SafelyRaisePlainEvent($"{nameof(GameEvents)}.{nameof(GameEvents.InitializeInternal)}", GameEvents.InitializeInternal?.GetInvocationList());
// notify mods
if (GameEvents.Initialize == null)
return;
string name = $"{nameof(GameEvents)}.{nameof(GameEvents.Initialize)}";
Delegate[] handlers = GameEvents.Initialize.GetInvocationList();
GameEvents.DeprecationManager.WarnForEvent(handlers, name, "1.10", DeprecationLevel.Info);
monitor.SafelyRaisePlainEvent(name, handlers);
}
/// Raise a event.
/// Encapsulates logging and monitoring.
internal static void InvokeLoadContent(IMonitor monitor)
{
if (GameEvents.LoadContent == null)
return;
string name = $"{nameof(GameEvents)}.{nameof(GameEvents.LoadContent)}";
Delegate[] handlers = GameEvents.LoadContent.GetInvocationList();
GameEvents.DeprecationManager.WarnForEvent(handlers, name, "1.10", DeprecationLevel.Info);
monitor.SafelyRaisePlainEvent(name, handlers);
}
/// Raise a event.
/// Encapsulates monitoring and logging.
internal static void InvokeGameLoaded(IMonitor monitor)
{
// notify SMAPI
monitor.SafelyRaisePlainEvent($"{nameof(GameEvents)}.{nameof(GameEvents.GameLoadedInternal)}", GameEvents.GameLoadedInternal?.GetInvocationList());
// notify mods
if (GameEvents.GameLoaded == null)
return;
string name = $"{nameof(GameEvents)}.{nameof(GameEvents.GameLoaded)}";
Delegate[] handlers = GameEvents.GameLoaded.GetInvocationList();
GameEvents.DeprecationManager.WarnForEvent(handlers, name, "1.12", DeprecationLevel.Info);
monitor.SafelyRaisePlainEvent(name, handlers);
}
/// Raise a event.
/// Encapsulates monitoring and logging.
internal static void InvokeFirstUpdateTick(IMonitor monitor)
{
if (GameEvents.FirstUpdateTick == null)
return;
string name = $"{nameof(GameEvents)}.{nameof(GameEvents.FirstUpdateTick)}";
Delegate[] handlers = GameEvents.FirstUpdateTick.GetInvocationList();
GameEvents.DeprecationManager.WarnForEvent(handlers, name, "1.12", DeprecationLevel.Info);
monitor.SafelyRaisePlainEvent(name, handlers);
}
/// Raise an event.
/// Encapsulates logging and monitoring.
internal static void InvokeUpdateTick(IMonitor monitor)
{
monitor.SafelyRaisePlainEvent($"{nameof(GameEvents)}.{nameof(GameEvents.UpdateTick)}", GameEvents.UpdateTick?.GetInvocationList());
}
/// Raise a event.
/// Encapsulates monitoring and logging.
internal static void InvokeSecondUpdateTick(IMonitor monitor)
{
monitor.SafelyRaisePlainEvent($"{nameof(GameEvents)}.{nameof(GameEvents.SecondUpdateTick)}", GameEvents.SecondUpdateTick?.GetInvocationList());
}
/// Raise a event.
/// Encapsulates monitoring and logging.
internal static void InvokeFourthUpdateTick(IMonitor monitor)
{
monitor.SafelyRaisePlainEvent($"{nameof(GameEvents)}.{nameof(GameEvents.FourthUpdateTick)}", GameEvents.FourthUpdateTick?.GetInvocationList());
}
/// Raise a event.
/// Encapsulates monitoring and logging.
internal static void InvokeEighthUpdateTick(IMonitor monitor)
{
monitor.SafelyRaisePlainEvent($"{nameof(GameEvents)}.{nameof(GameEvents.EighthUpdateTick)}", GameEvents.EighthUpdateTick?.GetInvocationList());
}
/// Raise a event.
/// Encapsulates monitoring and logging.
internal static void InvokeQuarterSecondTick(IMonitor monitor)
{
monitor.SafelyRaisePlainEvent($"{nameof(GameEvents)}.{nameof(GameEvents.QuarterSecondTick)}", GameEvents.QuarterSecondTick?.GetInvocationList());
}
/// Raise a event.
/// Encapsulates monitoring and logging.
internal static void InvokeHalfSecondTick(IMonitor monitor)
{
monitor.SafelyRaisePlainEvent($"{nameof(GameEvents)}.{nameof(GameEvents.HalfSecondTick)}", GameEvents.HalfSecondTick?.GetInvocationList());
}
/// Raise a event.
/// Encapsulates monitoring and logging.
internal static void InvokeOneSecondTick(IMonitor monitor)
{
monitor.SafelyRaisePlainEvent($"{nameof(GameEvents)}.{nameof(GameEvents.OneSecondTick)}", GameEvents.OneSecondTick?.GetInvocationList());
}
}
}