From 3ed16a8ea67cf847d08d463deef969a3edf99b2e Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 3 May 2017 11:36:36 -0400 Subject: recover game after draw errors to prevent crash --- src/StardewModdingAPI/Framework/SGame.cs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/StardewModdingAPI/Framework/SGame.cs') diff --git a/src/StardewModdingAPI/Framework/SGame.cs b/src/StardewModdingAPI/Framework/SGame.cs index fe7d3aa3..3a82963d 100644 --- a/src/StardewModdingAPI/Framework/SGame.cs +++ b/src/StardewModdingAPI/Framework/SGame.cs @@ -947,7 +947,28 @@ namespace StardewModdingAPI.Framework } catch (Exception ex) { + // log error this.Monitor.Log($"An error occured in the overridden draw loop: {ex.GetLogSummary()}", LogLevel.Error); + + // fix sprite batch + try + { + bool isSpriteBatchOpen = +#if SMAPI_FOR_WINDOWS + SGame.Reflection.GetPrivateValue(Game1.spriteBatch, "inBeginEndPair"); +#else + SGame.Reflection.GetPrivateValue(Game1.spriteBatch, "_beginCalled"); +#endif + if (isSpriteBatchOpen) + { + this.Monitor.Log("Recovering sprite batch from error...", LogLevel.Trace); + Game1.spriteBatch.End(); + } + } + catch (Exception innerEx) + { + this.Monitor.Log($"Could not recover sprite batch state: {innerEx.GetLogSummary()}", LogLevel.Error); + } } Context.IsInDrawLoop = false; } -- cgit From 0a2f7e52b9fc1e60fe491b8fd28cf8377f79290b Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 3 May 2017 12:46:48 -0400 Subject: fix code style --- src/StardewModdingAPI/Framework/SGame.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/StardewModdingAPI/Framework/SGame.cs') diff --git a/src/StardewModdingAPI/Framework/SGame.cs b/src/StardewModdingAPI/Framework/SGame.cs index 3a82963d..1f2bf3ac 100644 --- a/src/StardewModdingAPI/Framework/SGame.cs +++ b/src/StardewModdingAPI/Framework/SGame.cs @@ -1366,8 +1366,8 @@ namespace StardewModdingAPI.Framework /// The enumeration of items to hash. private int GetHash(IEnumerable enumerable) { - var hash = 0; - foreach (var v in enumerable) + int hash = 0; + foreach (object v in enumerable) hash ^= v.GetHashCode(); return hash; } -- cgit