diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-04-27 18:49:13 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-04-27 18:49:13 -0400 |
commit | adda9611c73163270cbfcd34d6617560f81d54b0 (patch) | |
tree | a5c164a7dba15a45d3a64f21615be0f83626c292 /src | |
parent | b7edf31c257aad563d12988cf3e99b00a1434ef4 (diff) | |
download | SMAPI-adda9611c73163270cbfcd34d6617560f81d54b0.tar.gz SMAPI-adda9611c73163270cbfcd34d6617560f81d54b0.tar.bz2 SMAPI-adda9611c73163270cbfcd34d6617560f81d54b0.zip |
add multiplayer sync events (#479)
Diffstat (limited to 'src')
-rw-r--r-- | src/SMAPI/Events/MultiplayerEvents.cs | 58 | ||||
-rw-r--r-- | src/SMAPI/Framework/Events/EventManager.cs | 20 | ||||
-rw-r--r-- | src/SMAPI/Framework/SGame.cs | 1 | ||||
-rw-r--r-- | src/SMAPI/Framework/SMultiplayer.cs | 47 | ||||
-rw-r--r-- | src/SMAPI/Program.cs | 1 | ||||
-rw-r--r-- | src/SMAPI/StardewModdingAPI.csproj | 2 |
6 files changed, 129 insertions, 0 deletions
diff --git a/src/SMAPI/Events/MultiplayerEvents.cs b/src/SMAPI/Events/MultiplayerEvents.cs new file mode 100644 index 00000000..f96ecba5 --- /dev/null +++ b/src/SMAPI/Events/MultiplayerEvents.cs @@ -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; + } + } +} diff --git a/src/SMAPI/Framework/Events/EventManager.cs b/src/SMAPI/Framework/Events/EventManager.cs index d7c89a76..87ff760f 100644 --- a/src/SMAPI/Framework/Events/EventManager.cs +++ b/src/SMAPI/Framework/Events/EventManager.cs @@ -126,6 +126,21 @@ namespace StardewModdingAPI.Framework.Events 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 ****/ /// <summary>Raised after the player warps to a new level of the mine.</summary> @@ -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)); diff --git a/src/SMAPI/Framework/SGame.cs b/src/SMAPI/Framework/SGame.cs index 7ea83957..89bf18ef 100644 --- a/src/SMAPI/Framework/SGame.cs +++ b/src/SMAPI/Framework/SGame.cs @@ -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>(); diff --git a/src/SMAPI/Framework/SMultiplayer.cs b/src/SMAPI/Framework/SMultiplayer.cs new file mode 100644 index 00000000..687b1922 --- /dev/null +++ b/src/SMAPI/Framework/SMultiplayer.cs @@ -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(); + } + } +} diff --git a/src/SMAPI/Program.cs b/src/SMAPI/Program.cs index dc9e5308..2325e79a 100644 --- a/src/SMAPI/Program.cs +++ b/src/SMAPI/Program.cs @@ -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); diff --git a/src/SMAPI/StardewModdingAPI.csproj b/src/SMAPI/StardewModdingAPI.csproj index 560d7bf4..f6a16e5f 100644 --- a/src/SMAPI/StardewModdingAPI.csproj +++ b/src/SMAPI/StardewModdingAPI.csproj @@ -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" /> |