From 8e9b3741734e32a559b79326f850585a0132e08d Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 22 Apr 2018 13:40:24 -0400 Subject: fix crash when closing game window in multiplayer mode (#453) --- src/SMAPI/Framework/SGame.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'src/SMAPI/Framework') diff --git a/src/SMAPI/Framework/SGame.cs b/src/SMAPI/Framework/SGame.cs index b383fcce..b983de49 100644 --- a/src/SMAPI/Framework/SGame.cs +++ b/src/SMAPI/Framework/SGame.cs @@ -114,6 +114,9 @@ namespace StardewModdingAPI.Framework /// A callback to invoke after the game finishes initialising. private readonly Action OnGameInitialised; + /// A callback to invoke when the game exits. + private readonly Action OnGameExiting; + /// Simplifies access to private game code. private readonly Reflector Reflection; @@ -136,7 +139,8 @@ namespace StardewModdingAPI.Framework /// Simplifies access to private game code. /// Manages SMAPI events for mods. /// A callback to invoke after the game finishes initialising. - internal SGame(IMonitor monitor, Reflector reflection, EventManager eventManager, Action onGameInitialised) + /// A callback to invoke when the game exits. + internal SGame(IMonitor monitor, Reflector reflection, EventManager eventManager, Action onGameInitialised, Action onGameExiting) { // init XNA Game1.graphics.GraphicsProfile = GraphicsProfile.HiDef; @@ -147,6 +151,7 @@ namespace StardewModdingAPI.Framework this.FirstUpdate = true; this.Reflection = reflection; this.OnGameInitialised = onGameInitialised; + this.OnGameExiting = onGameExiting; 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); @@ -167,6 +172,16 @@ namespace StardewModdingAPI.Framework }); } + /// Perform cleanup logic when the game exits. + /// The event sender. + /// The event args. + /// This overrides the logic in and to let SMAPI clean up before exit. + protected override void OnExiting(object sender, EventArgs args) + { + Game1.multiplayer.Disconnect(); + this.OnGameExiting?.Invoke(); + } + /**** ** Intercepted methods & events ****/ -- cgit