diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-07-02 19:42:05 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-07-02 19:42:05 -0400 |
commit | 698328c52f60e6f825086585ef79f8c6eedb944e (patch) | |
tree | a4b1f066bfcf643f5240aee1f5224d25ca1fdf4d | |
parent | e69d1615c4ff1cf93e51f83b66f7d32fe6baf942 (diff) | |
download | SMAPI-698328c52f60e6f825086585ef79f8c6eedb944e.tar.gz SMAPI-698328c52f60e6f825086585ef79f8c6eedb944e.tar.bz2 SMAPI-698328c52f60e6f825086585ef79f8c6eedb944e.zip |
fix rare crash for some players when window loses focus (#306)
-rw-r--r-- | release-notes.md | 1 | ||||
-rw-r--r-- | src/StardewModdingAPI/Framework/SGame.cs | 18 |
2 files changed, 16 insertions, 3 deletions
diff --git a/release-notes.md b/release-notes.md index ae2f853d..b1917ef7 100644 --- a/release-notes.md +++ b/release-notes.md @@ -22,6 +22,7 @@ For players: * `list_items` now also matches translated item names when playing in another language. * `list_item_types` is a new command to see a list of item types. * Added clearer error when a `config.json` is invalid. +* Fixed rare crash when window loses focus for a few players (further to fix in 1.14). For modders: * You can now specify minimum dependency versions in `manifest.json`. diff --git a/src/StardewModdingAPI/Framework/SGame.cs b/src/StardewModdingAPI/Framework/SGame.cs index 80ae20ac..39713d4a 100644 --- a/src/StardewModdingAPI/Framework/SGame.cs +++ b/src/StardewModdingAPI/Framework/SGame.cs @@ -344,9 +344,21 @@ namespace StardewModdingAPI.Framework if (Game1.game1.IsActive) { // get latest state - KeyboardState keyState = Keyboard.GetState(); - MouseState mouseState = Mouse.GetState(); - Point mousePosition = new Point(Game1.getMouseX(), Game1.getMouseY()); + KeyboardState keyState; + MouseState mouseState; + Point mousePosition; + try + { + keyState = Keyboard.GetState(); + mouseState = Mouse.GetState(); + mousePosition = new Point(Game1.getMouseX(), Game1.getMouseY()); + } + catch (InvalidOperationException) // GetState() may crash for some players if window doesn't have focus but game1.IsActive == true + { + keyState = this.PreviousKeyState; + mouseState = this.PreviousMouseState; + mousePosition = this.PreviousMousePosition; + } // analyse state Keys[] currentlyPressedKeys = keyState.GetPressedKeys(); |