add multiplayer sync events (#479)

This commit is contained in:
Jesse Plamondon-Willard 2018-04-27 18:49:13 -04:00
parent b7edf31c25
commit adda9611c7
6 changed files with 129 additions and 0 deletions

View File

@ -0,0 +1,58 @@
using System;
using StardewModdingAPI.Framework.Events;
namespace StardewModdingAPI.Events
{
/// <summary>Events raised during the multiplayer sync process.</summary>
public static class MultiplayerEvents
{
/*********
** Properties
*********/
/// <summary>The core event manager.</summary>
private static EventManager EventManager;
/*********
** Events
*********/
/// <summary>Raised before the game syncs changes from other players.</summary>
public static event EventHandler BeforeMainSync
{
add => MultiplayerEvents.EventManager.Multiplayer_BeforeMainSync.Add(value);
remove => MultiplayerEvents.EventManager.Multiplayer_BeforeMainSync.Remove(value);
}
/// <summary>Raised after the game syncs changes from other players.</summary>
public static event EventHandler AfterMainSync
{
add => MultiplayerEvents.EventManager.Multiplayer_AfterMainSync.Add(value);
remove => MultiplayerEvents.EventManager.Multiplayer_AfterMainSync.Remove(value);
}
/// <summary>Raised before the game broadcasts changes to other players.</summary>
public static event EventHandler BeforeMainBroadcast
{
add => MultiplayerEvents.EventManager.Multiplayer_BeforeMainBroadcast.Add(value);
remove => MultiplayerEvents.EventManager.Multiplayer_BeforeMainBroadcast.Remove(value);
}
/// <summary>Raised after the game broadcasts changes to other players.</summary>
public static event EventHandler AfterMainBroadcast
{
add => MultiplayerEvents.EventManager.Multiplayer_AfterMainBroadcast.Add(value);
remove => MultiplayerEvents.EventManager.Multiplayer_AfterMainBroadcast.Remove(value);
}
/*********
** Public methods
*********/
/// <summary>Initialise the events.</summary>
/// <param name="eventManager">The core event manager.</param>
internal static void Init(EventManager eventManager)
{
MultiplayerEvents.EventManager = eventManager;
}
}
}

View File

@ -125,6 +125,21 @@ namespace StardewModdingAPI.Framework.Events
/// <summary>Raised after a game menu is closed.</summary>
public readonly ManagedEvent<EventArgsClickableMenuClosed> Menu_Closed;
/****
** MultiplayerEvents
****/
/// <summary>Raised before the game syncs changes from other players.</summary>
public readonly ManagedEvent Multiplayer_BeforeMainSync;
/// <summary>Raised after the game syncs changes from other players.</summary>
public readonly ManagedEvent Multiplayer_AfterMainSync;
/// <summary>Raised before the game broadcasts changes to other players.</summary>
public readonly ManagedEvent Multiplayer_BeforeMainBroadcast;
/// <summary>Raised after the game broadcasts changes to other players.</summary>
public readonly ManagedEvent Multiplayer_AfterMainBroadcast;
/****
** MineEvents
****/
@ -228,6 +243,11 @@ namespace StardewModdingAPI.Framework.Events
this.Menu_Changed = ManageEventOf<EventArgsClickableMenuChanged>(nameof(MenuEvents), nameof(MenuEvents.MenuChanged));
this.Menu_Closed = ManageEventOf<EventArgsClickableMenuClosed>(nameof(MenuEvents), nameof(MenuEvents.MenuClosed));
this.Multiplayer_BeforeMainBroadcast = ManageEvent(nameof(MultiplayerEvents), nameof(MultiplayerEvents.BeforeMainBroadcast));
this.Multiplayer_AfterMainBroadcast = ManageEvent(nameof(MultiplayerEvents), nameof(MultiplayerEvents.AfterMainBroadcast));
this.Multiplayer_BeforeMainSync = ManageEvent(nameof(MultiplayerEvents), nameof(MultiplayerEvents.BeforeMainSync));
this.Multiplayer_AfterMainSync = ManageEvent(nameof(MultiplayerEvents), nameof(MultiplayerEvents.AfterMainSync));
this.Mine_LevelChanged = ManageEventOf<EventArgsMineLevelChanged>(nameof(MineEvents), nameof(MineEvents.MineLevelChanged));
this.Player_InventoryChanged = ManageEventOf<EventArgsInventoryChanged>(nameof(PlayerEvents), nameof(PlayerEvents.InventoryChanged));

View File

@ -155,6 +155,7 @@ namespace StardewModdingAPI.Framework
if (this.ContentCore == null) // shouldn't happen since CreateContentManager is called first, but let's init here just in case
this.ContentCore = new ContentCore(this.Content.ServiceProvider, this.Content.RootDirectory, Thread.CurrentThread.CurrentUICulture, this.Monitor, reflection);
Game1.input = new SInputState();
Game1.multiplayer = new SMultiplayer(monitor, eventManager);
// init watchers
Game1.locations = new ObservableCollection<GameLocation>();

View File

@ -0,0 +1,47 @@
using StardewModdingAPI.Framework.Events;
using StardewValley;
namespace StardewModdingAPI.Framework
{
/// <summary>SMAPI's implementation of the game's core multiplayer logic.</summary>
internal class SMultiplayer : Multiplayer
{
/*********
** Properties
*********/
/// <summary>Encapsulates monitoring and logging.</summary>
private readonly IMonitor Monitor;
/// <summary>Manages SMAPI events.</summary>
private readonly EventManager EventManager;
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="monitor">Encapsulates monitoring and logging.</param>
/// <param name="eventManager">Manages SMAPI events.</param>
public SMultiplayer(IMonitor monitor, EventManager eventManager)
{
this.Monitor = monitor;
this.EventManager = eventManager;
}
/// <summary>Handle sync messages from other players and perform other initial sync logic.</summary>
public override void UpdateEarly()
{
this.EventManager.Multiplayer_BeforeMainSync.Raise();
base.UpdateEarly();
this.EventManager.Multiplayer_AfterMainSync.Raise();
}
/// <summary>Broadcast sync messages to other players and perform other final sync logic.</summary>
public override void UpdateLate(bool forceSync = false)
{
this.EventManager.Multiplayer_BeforeMainBroadcast.Raise();
base.UpdateLate(forceSync);
this.EventManager.Multiplayer_AfterMainBroadcast.Raise();
}
}
}

View File

@ -157,6 +157,7 @@ namespace StardewModdingAPI
LocationEvents.Init(this.EventManager);
MenuEvents.Init(this.EventManager);
MineEvents.Init(this.EventManager);
MultiplayerEvents.Init(this.EventManager);
PlayerEvents.Init(this.EventManager);
SaveEvents.Init(this.EventManager);
SpecialisedEvents.Init(this.EventManager);

View File

@ -85,6 +85,7 @@
<Compile Include="..\..\build\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Events\MultiplayerEvents.cs" />
<Compile Include="Framework\Events\EventManager.cs" />
<Compile Include="Framework\Events\ManagedEvent.cs" />
<Compile Include="Events\SpecialisedEvents.cs" />
@ -132,6 +133,7 @@
<Compile Include="Framework\Serialisation\CrossplatformConverters\RectangleConverter.cs" />
<Compile Include="Framework\Serialisation\CrossplatformConverters\ColorConverter.cs" />
<Compile Include="Framework\Serialisation\CrossplatformConverters\PointConverter.cs" />
<Compile Include="Framework\SMultiplayer.cs" />
<Compile Include="Framework\StateTracking\Comparers\EquatableComparer.cs" />
<Compile Include="Framework\StateTracking\Comparers\ObjectReferenceComparer.cs" />
<Compile Include="Framework\StateTracking\FieldWatchers\BaseDisposableWatcher.cs" />