From 76e1fd3905fc16a99225ef212a21e9365adeab88 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 13 Jul 2018 20:19:02 -0400 Subject: fix console commands being invoked asynchronously (#562) --- src/SMAPI/Framework/SGame.cs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) (limited to 'src/SMAPI/Framework') diff --git a/src/SMAPI/Framework/SGame.cs b/src/SMAPI/Framework/SGame.cs index 777bc478..a685dfce 100644 --- a/src/SMAPI/Framework/SGame.cs +++ b/src/SMAPI/Framework/SGame.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics.CodeAnalysis; @@ -95,7 +96,7 @@ namespace StardewModdingAPI.Framework private bool IsInitialised; /// The number of update ticks which have already executed. - private uint TicksElapsed = 0; + private uint TicksElapsed; /// Whether the next content manager requested by the game will be for . private bool NextContentManagerIsMain; @@ -107,6 +108,9 @@ namespace StardewModdingAPI.Framework /// SMAPI's content manager. public ContentCoordinator ContentCore { get; private set; } + /// Manages console commands. + public CommandManager CommandManager { get; } = new CommandManager(); + /// Manages input visible to the game. public SInputState Input => (SInputState)Game1.input; @@ -116,6 +120,10 @@ namespace StardewModdingAPI.Framework /// Whether SMAPI should log more information about the game context. public bool VerboseLogging { get; set; } + /// A list of queued commands to execute. + /// This property must be threadsafe, since it's accessed from a separate console input thread. + public ConcurrentQueue CommandQueue { get; } = new ConcurrentQueue(); + /********* ** Protected methods @@ -229,7 +237,7 @@ namespace StardewModdingAPI.Framework { this.Monitor.Log("Game loader synchronising...", LogLevel.Trace); while (Game1.currentLoader?.MoveNext() == true) - continue; + ; Game1.currentLoader = null; this.Monitor.Log("Game loader done.", LogLevel.Trace); } @@ -256,6 +264,22 @@ namespace StardewModdingAPI.Framework return; } + /********* + ** Execute commands + *********/ + while (this.CommandQueue.TryDequeue(out string rawInput)) + { + try + { + if (!this.CommandManager.Trigger(rawInput)) + this.Monitor.Log("Unknown command; type 'help' for a list of available commands.", LogLevel.Error); + } + catch (Exception ex) + { + this.Monitor.Log($"The handler registered for that command failed:\n{ex.GetLogSummary()}", LogLevel.Error); + } + } + /********* ** Update input *********/ -- cgit