diff options
-rw-r--r-- | release-notes.md | 1 | ||||
-rw-r--r-- | src/StardewModdingAPI/Inheritance/SGame.cs | 42 |
2 files changed, 38 insertions, 5 deletions
diff --git a/release-notes.md b/release-notes.md index 6b4f03cc..871ffabc 100644 --- a/release-notes.md +++ b/release-notes.md @@ -8,6 +8,7 @@ For players: * The installer will now automatically clean up old SMAPI files. * Each mod now has its own `.cache` folder, so removing the mod won't leave orphaned cache files behind. The installer will automatically remove the old `.cache` folder. * Improved installer wording to avoid confusion. + * SMAPI will now intercept mod errors in menu draw code, and exit the menu to prevent your game from crashing. * Fixed the installer not removing TrainerMod from appdata if it's already in the game mods directory. * Fixed the installer not moving mods out of appdata if the game isn't installed on the same Windows partition. * Fixed the SMAPI terminal not opening by default on Mac. Linux users are out of luck. diff --git a/src/StardewModdingAPI/Inheritance/SGame.cs b/src/StardewModdingAPI/Inheritance/SGame.cs index 93d56553..f70d0696 100644 --- a/src/StardewModdingAPI/Inheritance/SGame.cs +++ b/src/StardewModdingAPI/Inheritance/SGame.cs @@ -373,7 +373,7 @@ namespace StardewModdingAPI.Inheritance /// <summary>The method called to draw everything to the screen.</summary> /// <param name="gameTime">A snapshot of the game timing state.</param> - /// <remarks>This implementation is identical to <see cref="Game1.Draw"/>, except for minor formatting and added events.</remarks> + /// <remarks>This implementation is identical to <see cref="Game1.Draw"/>, except for try..catch around menu draw code, minor formatting, and added events.</remarks> protected override void Draw(GameTime gameTime) { // track frame rate @@ -388,9 +388,25 @@ namespace StardewModdingAPI.Inheritance if (Game1.options.showMenuBackground && Game1.activeClickableMenu != null && Game1.activeClickableMenu.showWithoutTransparencyIfOptionIsSet()) { Game1.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null); - Game1.activeClickableMenu.drawBackground(Game1.spriteBatch); + try + { + Game1.activeClickableMenu.drawBackground(Game1.spriteBatch); + } + catch (Exception ex) + { + this.Monitor.Log($"The {Game1.activeClickableMenu.GetType().FullName} menu crashed while drawing its background. SMAPI will force it to exit to avoid crashing the game.\n{ex.GetLogSummary()}", LogLevel.Error); + Game1.activeClickableMenu.exitThisMenu(); + } GraphicsEvents.InvokeOnPreRenderGuiEvent(this.Monitor); - Game1.activeClickableMenu.draw(Game1.spriteBatch); + try + { + Game1.activeClickableMenu.draw(Game1.spriteBatch); + } + catch (Exception ex) + { + this.Monitor.Log($"The {Game1.activeClickableMenu.GetType().FullName} menu crashed while drawing itself. SMAPI will force it to exit to avoid crashing the game.\n{ex.GetLogSummary()}", LogLevel.Error); + Game1.activeClickableMenu.exitThisMenu(); + } GraphicsEvents.InvokeOnPostRenderGuiEvent(this.Monitor); Game1.spriteBatch.End(); if (!this.ZoomLevelIsOne) @@ -434,7 +450,15 @@ namespace StardewModdingAPI.Inheritance if (Game1.showingEndOfNightStuff) { Game1.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null); - Game1.activeClickableMenu?.draw(Game1.spriteBatch); + try + { + Game1.activeClickableMenu?.draw(Game1.spriteBatch); + } + catch (Exception ex) + { + this.Monitor.Log($"The {Game1.activeClickableMenu.GetType().FullName} menu crashed while drawing itself. SMAPI will force it to exit to avoid crashing the game.\n{ex.GetLogSummary()}", LogLevel.Error); + Game1.activeClickableMenu.exitThisMenu(); + } Game1.spriteBatch.End(); if (!this.ZoomLevelIsOne) { @@ -742,7 +766,15 @@ namespace StardewModdingAPI.Inheritance if (Game1.activeClickableMenu != null) { GraphicsEvents.InvokeOnPreRenderGuiEvent(this.Monitor); - Game1.activeClickableMenu.draw(Game1.spriteBatch); + try + { + Game1.activeClickableMenu.draw(Game1.spriteBatch); + } + catch (Exception ex) + { + this.Monitor.Log($"The {Game1.activeClickableMenu.GetType().FullName} menu crashed while drawing itself. SMAPI will force it to exit to avoid crashing the game.\n{ex.GetLogSummary()}", LogLevel.Error); + Game1.activeClickableMenu.exitThisMenu(); + } GraphicsEvents.InvokeOnPostRenderGuiEvent(this.Monitor); } else |