diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-07-13 20:19:02 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-07-13 20:19:02 -0400 |
commit | 76e1fd3905fc16a99225ef212a21e9365adeab88 (patch) | |
tree | 3b3f5b2479e49eb6d77e635cba44c7d47e11ce09 /src/SMAPI/Framework/SGame.cs | |
parent | 6bd2c55a76cdd09f327145e9368f9e831ddcbfff (diff) | |
download | SMAPI-76e1fd3905fc16a99225ef212a21e9365adeab88.tar.gz SMAPI-76e1fd3905fc16a99225ef212a21e9365adeab88.tar.bz2 SMAPI-76e1fd3905fc16a99225ef212a21e9365adeab88.zip |
fix console commands being invoked asynchronously (#562)
Diffstat (limited to 'src/SMAPI/Framework/SGame.cs')
-rw-r--r-- | src/SMAPI/Framework/SGame.cs | 28 |
1 files changed, 26 insertions, 2 deletions
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; /// <summary>The number of update ticks which have already executed.</summary> - private uint TicksElapsed = 0; + private uint TicksElapsed; /// <summary>Whether the next content manager requested by the game will be for <see cref="Game1.content"/>.</summary> private bool NextContentManagerIsMain; @@ -107,6 +108,9 @@ namespace StardewModdingAPI.Framework /// <summary>SMAPI's content manager.</summary> public ContentCoordinator ContentCore { get; private set; } + /// <summary>Manages console commands.</summary> + public CommandManager CommandManager { get; } = new CommandManager(); + /// <summary>Manages input visible to the game.</summary> public SInputState Input => (SInputState)Game1.input; @@ -116,6 +120,10 @@ namespace StardewModdingAPI.Framework /// <summary>Whether SMAPI should log more information about the game context.</summary> public bool VerboseLogging { get; set; } + /// <summary>A list of queued commands to execute.</summary> + /// <remarks>This property must be threadsafe, since it's accessed from a separate console input thread.</remarks> + public ConcurrentQueue<string> CommandQueue { get; } = new ConcurrentQueue<string>(); + /********* ** 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); } @@ -257,6 +265,22 @@ namespace StardewModdingAPI.Framework } /********* + ** 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 *********/ // This should *always* run, even when suppressing mod events, since the game uses |