subclass chatbox to log game errors

This commit is contained in:
Jesse Plamondon-Willard 2021-01-17 12:21:33 -05:00
parent a5ba931770
commit b58d432a22
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
3 changed files with 57 additions and 0 deletions

View File

@ -11,6 +11,7 @@
* For modders:
* Expanded `PerScreen<T>` API: you can now get/set the value for any screen, get all active values, or clear all values.
* Expanded player info received from multiplayer API/events with new `IsSplitScreen` and `ScreenID` fields.
* Game errors shown in the chatbox are now logged.
* Added an option to disable rewriting mods for compatibility (thanks to Bpendragon!). This may prevent older mods from loading, but bypasses a Visual Studio crash when debugging.
* For the Error Handler mod:

View File

@ -0,0 +1,49 @@
using StardewValley;
using StardewValley.Menus;
namespace StardewModdingAPI.Framework
{
/// <summary>SMAPI's implementation of the chatbox which intercepts errors for logging.</summary>
internal class SChatBox : ChatBox
{
/*********
** Fields
*********/
/// <summary>Encapsulates monitoring and logging.</summary>
private readonly IMonitor Monitor;
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="monitor">Encapsulates monitoring and logging.</param>
public SChatBox(IMonitor monitor)
{
this.Monitor = monitor;
}
/// <inheritdoc />
protected override void runCommand(string command)
{
this.Monitor.Log($"> chat command: {command}");
base.runCommand(command);
}
/// <inheritdoc />
public override void receiveChatMessage(long sourceFarmer, int chatKind, LocalizedContentManager.LanguageCode language, string message)
{
if (chatKind == ChatBox.errorMessage)
{
// log error
this.Monitor.Log(message, LogLevel.Error);
// add event details if applicable
if (Game1.CurrentEvent != null && message.StartsWith("Event script error:"))
this.Monitor.Log($"In event #{Game1.CurrentEvent.id} for location {Game1.currentLocation?.NameOrUniqueName}", LogLevel.Error);
}
base.receiveChatMessage(sourceFarmer, chatKind, language, message);
}
}
}

View File

@ -1054,6 +1054,13 @@ namespace StardewModdingAPI.Framework
this.OnReturnedToTitle();
}
// override chatbox
if (newStage == LoadStage.Loaded)
{
Game1.onScreenMenus.Remove(Game1.chatBox);
Game1.onScreenMenus.Add(Game1.chatBox = new SChatBox(this.LogManager.MonitorForGame));
}
// raise events
this.EventManager.LoadStageChanged.Raise(new LoadStageChangedEventArgs(oldStage, newStage));
if (newStage == LoadStage.None)