add DayEnding event (#310)

This commit is contained in:
Jesse Plamondon-Willard 2018-10-06 00:51:45 -04:00
parent 14fab29370
commit 79705448f5
7 changed files with 63 additions and 0 deletions

View File

@ -0,0 +1,7 @@
using System;
namespace StardewModdingAPI.Events
{
/// <summary>Event arguments for an <see cref="IGameLoopEvents.DayEnding"/> event.</summary>
public class DayEndingEventArgs : EventArgs { }
}

View File

@ -31,5 +31,8 @@ namespace StardewModdingAPI.Events
/// <summary>Raised after the game begins a new day (including when the player loads a save).</summary>
event EventHandler<DayStartedEventArgs> DayStarted;
/// <summary>Raised before the game ends the current day. This happens before it starts setting up the next day and before <see cref="Saving"/>.</summary>
event EventHandler<DayEndingEventArgs> DayEnding;
}
}

View File

@ -41,6 +41,9 @@ namespace StardewModdingAPI.Framework.Events
/// <summary>Raised after the game begins a new day, including when loading a save.</summary>
public readonly ManagedEvent<DayStartedEventArgs> DayStarted;
/// <summary>Raised before the game ends the current day. This happens before it starts setting up the next day and before <see cref="Saving"/>.</summary>
public readonly ManagedEvent<DayEndingEventArgs> DayEnding;
/****
** Input
****/
@ -291,6 +294,7 @@ namespace StardewModdingAPI.Framework.Events
this.Saved = ManageEventOf<SavedEventArgs>(nameof(IModEvents.GameLoop), nameof(IGameLoopEvents.Saved));
this.SaveLoaded = ManageEventOf<SaveLoadedEventArgs>(nameof(IModEvents.GameLoop), nameof(IGameLoopEvents.SaveLoaded));
this.DayStarted = ManageEventOf<DayStartedEventArgs>(nameof(IModEvents.GameLoop), nameof(IGameLoopEvents.DayStarted));
this.DayEnding = ManageEventOf<DayEndingEventArgs>(nameof(IModEvents.GameLoop), nameof(IGameLoopEvents.DayEnding));
this.ButtonPressed = ManageEventOf<ButtonPressedEventArgs>(nameof(IModEvents.Input), nameof(IInputEvents.ButtonPressed));
this.ButtonReleased = ManageEventOf<ButtonReleasedEventArgs>(nameof(IModEvents.Input), nameof(IInputEvents.ButtonReleased));

View File

@ -72,6 +72,13 @@ namespace StardewModdingAPI.Framework.Events
remove => this.EventManager.DayStarted.Remove(value);
}
/// <summary>Raised before the game ends the current day. This happens before it starts setting up the next day and before <see cref="IGameLoopEvents.Saving"/>.</summary>
public event EventHandler<DayEndingEventArgs> DayEnding
{
add => this.EventManager.DayEnding.Add(value);
remove => this.EventManager.DayEnding.Remove(value);
}
/*********
** Public methods

View File

@ -151,6 +151,7 @@ namespace StardewModdingAPI.Framework
this.OnGameExiting = onGameExiting;
Game1.input = new SInputState();
Game1.multiplayer = new SMultiplayer(monitor, eventManager);
Game1.hooks = new SModHooks(this.OnNewDayAfterFade);
// init observables
Game1.locations = new ObservableCollection<GameLocation>();
@ -182,6 +183,12 @@ namespace StardewModdingAPI.Framework
/****
** Intercepted methods & events
****/
/// <summary>A callback invoked before <see cref="Game1.newDayAfterFade"/> runs.</summary>
protected void OnNewDayAfterFade()
{
this.Events.DayEnding.RaiseEmpty();
}
/// <summary>Constructor a content manager to read XNB files.</summary>
/// <param name="serviceProvider">The service provider to use to locate services.</param>
/// <param name="rootDirectory">The root directory to search for content.</param>

View File

@ -0,0 +1,34 @@
using System;
using StardewValley;
namespace StardewModdingAPI.Framework
{
/// <summary>Invokes callbacks for mod hooks provided by the game.</summary>
internal class SModHooks : ModHooks
{
/*********
** Properties
*********/
/// <summary>A callback to invoke before <see cref="Game1.newDayAfterFade"/> runs.</summary>
private readonly Action BeforeNewDayAfterFade;
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="beforeNewDayAfterFade">A callback to invoke before <see cref="Game1.newDayAfterFade"/> runs.</param>
public SModHooks(Action beforeNewDayAfterFade)
{
this.BeforeNewDayAfterFade = beforeNewDayAfterFade;
}
/// <summary>A hook invoked when <see cref="Game1.newDayAfterFade"/> is called.</summary>
/// <param name="action">The vanilla <see cref="Game1.newDayAfterFade"/> logic.</param>
public override void OnGame1_NewDayAfterFade(Action action)
{
this.BeforeNewDayAfterFade?.Invoke();
action();
}
}
}

View File

@ -79,6 +79,7 @@
<Compile Include="..\..\build\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Events\DayEndingEventArgs.cs" />
<Compile Include="Events\DayStartedEventArgs.cs" />
<Compile Include="Events\SaveCreatingEventArgs.cs" />
<Compile Include="Events\SavedEventArgs.cs" />