From ca9efad7a7949ca87f3ccf7ec4d01332d1fad84b Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 24 Jun 2022 17:00:39 -0400 Subject: [PATCH] avoid cancellation token for tracking exit state This apparently causes noticeable lag for a minority of players. --- src/SMAPI/Framework/SCore.cs | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/src/SMAPI/Framework/SCore.cs b/src/SMAPI/Framework/SCore.cs index cc332225..16c168a0 100644 --- a/src/SMAPI/Framework/SCore.cs +++ b/src/SMAPI/Framework/SCore.cs @@ -66,8 +66,8 @@ namespace StardewModdingAPI.Framework /**** ** Low-level components ****/ - /// Tracks whether the game should exit immediately and any pending initialization should be cancelled. - private readonly CancellationTokenSource CancellationToken = new(); + /// Whether the game should exit immediately and any pending initialization should be cancelled. + private bool IsExiting; /// Manages the SMAPI console window and log file. private readonly LogManager LogManager; @@ -272,16 +272,6 @@ namespace StardewModdingAPI.Framework new TitleMenuPatcher(this.OnLoadStageChanged) ); - // add exit handler - this.CancellationToken.Token.Register(() => - { - if (this.IsGameRunning) - { - this.LogManager.WriteCrashLog(); - this.Game.Exit(); - } - }); - // set window titles this.UpdateWindowTitles(); } @@ -358,8 +348,8 @@ namespace StardewModdingAPI.Framework // dispose core components this.IsGameRunning = false; + this.IsExiting = true; this.ContentCore?.Dispose(); - this.CancellationToken.Dispose(); this.Game?.Dispose(); this.LogManager.Dispose(); // dispose last to allow for any last-second log messages @@ -374,7 +364,7 @@ namespace StardewModdingAPI.Framework /// Initialize mods before the first game asset is loaded. At this point the core content managers are loaded (so mods can load their own assets), but the game is mostly uninitialized. private void InitializeBeforeFirstAssetLoaded() { - if (this.CancellationToken.IsCancellationRequested) + if (this.IsExiting) { this.Monitor.Log("SMAPI shutting down: aborting initialization.", LogLevel.Warn); return; @@ -436,7 +426,7 @@ namespace StardewModdingAPI.Framework commandManager: this.CommandManager, reloadTranslations: this.ReloadTranslations, handleInput: input => this.RawCommandQueue.Add(input), - continueWhile: () => this.IsGameRunning && !this.CancellationToken.IsCancellationRequested + continueWhile: () => this.IsGameRunning && !this.IsExiting ) ).Start(); } @@ -479,7 +469,7 @@ namespace StardewModdingAPI.Framework ** Special cases *********/ // Abort if SMAPI is exiting. - if (this.CancellationToken.IsCancellationRequested) + if (this.IsExiting) { this.Monitor.Log("SMAPI shutting down: aborting update."); return; @@ -2233,7 +2223,10 @@ namespace StardewModdingAPI.Framework private void ExitGameImmediately(string message) { this.Monitor.LogFatal(message); - this.CancellationToken.Cancel(); + this.LogManager.WriteCrashLog(); + + this.IsExiting = true; + this.Game.Exit(); } /// Get the screen ID that should be logged to distinguish between players in split-screen mode, if any.