summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-07-02 19:42:05 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-07-02 19:42:05 -0400
commit698328c52f60e6f825086585ef79f8c6eedb944e (patch)
treea4b1f066bfcf643f5240aee1f5224d25ca1fdf4d /src/StardewModdingAPI
parente69d1615c4ff1cf93e51f83b66f7d32fe6baf942 (diff)
downloadSMAPI-698328c52f60e6f825086585ef79f8c6eedb944e.tar.gz
SMAPI-698328c52f60e6f825086585ef79f8c6eedb944e.tar.bz2
SMAPI-698328c52f60e6f825086585ef79f8c6eedb944e.zip
fix rare crash for some players when window loses focus (#306)
Diffstat (limited to 'src/StardewModdingAPI')
-rw-r--r--src/StardewModdingAPI/Framework/SGame.cs18
1 files changed, 15 insertions, 3 deletions
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();