fix rare crash for some players when window loses focus (#306)

This commit is contained in:
Jesse Plamondon-Willard 2017-07-02 19:42:05 -04:00
parent e69d1615c4
commit 698328c52f
2 changed files with 16 additions and 3 deletions

View File

@ -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`.

View File

@ -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();