From df65d59f8b376631337948cea2c4bd1746b2c904 Mon Sep 17 00:00:00 2001 From: marzent Date: Sat, 10 Feb 2024 13:03:11 +0100 Subject: [PATCH] add more exception handler options to dev menu --- Dalamud/Dalamud.cs | 52 +++++++++++++++---- .../Interface/Internal/DalamudInterface.cs | 14 ++++- 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/Dalamud/Dalamud.cs b/Dalamud/Dalamud.cs index 4ab617d0a..8c858ce7c 100644 --- a/Dalamud/Dalamud.cs +++ b/Dalamud/Dalamud.cs @@ -117,6 +117,14 @@ internal sealed class Dalamud : IServiceType } }); } + + this.DefaultExceptionFilter = NativeFunctions.SetUnhandledExceptionFilter(nint.Zero); + NativeFunctions.SetUnhandledExceptionFilter(this.DefaultExceptionFilter); + Log.Debug($"SE default exception filter at {this.DefaultExceptionFilter.ToInt64():X}"); + + var debugSig = "40 55 53 56 48 8D AC 24 ?? ?? ?? ?? B8 ?? ?? ?? ?? E8 ?? ?? ?? ?? 48 2B E0 48 8B 05 ?? ?? ?? ?? 48 33 C4 48 89 85 ?? ?? ?? ?? 48 83 3D ?? ?? ?? ?? ??"; + this.DebugExceptionFilter = Service.Get().ScanText(debugSig); + Log.Debug($"SE debug exception filter at {this.DebugExceptionFilter.ToInt64():X}"); } /// @@ -128,7 +136,17 @@ internal sealed class Dalamud : IServiceType /// Gets location of stored assets. /// internal DirectoryInfo AssetDirectory => new(this.StartInfo.AssetDirectory!); - + + /// + /// Gets the in-game default exception filter. + /// + private nint DefaultExceptionFilter { get; } + + /// + /// Gets the in-game debug exception filter. + /// + private nint DebugExceptionFilter { get; } + /// /// Signal to the crash handler process that we should restart the game. /// @@ -191,18 +209,32 @@ internal sealed class Dalamud : IServiceType } /// - /// Replace the built-in exception handler with a debug one. + /// Replace the current exception handler with the default one. /// - internal void ReplaceExceptionHandler() - { - var releaseSig = "40 55 53 56 48 8D AC 24 ?? ?? ?? ?? B8 ?? ?? ?? ?? E8 ?? ?? ?? ?? 48 2B E0 48 8B 05 ?? ?? ?? ?? 48 33 C4 48 89 85 ?? ?? ?? ?? 48 83 3D ?? ?? ?? ?? ??"; - var releaseFilter = Service.Get().ScanText(releaseSig); - Log.Debug($"SE debug filter at {releaseFilter.ToInt64():X}"); + internal void UseDefaultExceptionHandler() => + this.SetExceptionHandler(this.DefaultExceptionFilter); - var oldFilter = NativeFunctions.SetUnhandledExceptionFilter(releaseFilter); - Log.Debug("Reset ExceptionFilter, old: {0}", oldFilter); + /// + /// Replace the current exception handler with a debug one. + /// + internal void UseDebugExceptionHandler() => + this.SetExceptionHandler(this.DebugExceptionFilter); + + /// + /// Disable the current exception handler. + /// + internal void UseNoExceptionHandler() => + this.SetExceptionHandler(nint.Zero); + + /// + /// Helper function to set the exception handler. + /// + private void SetExceptionHandler(nint newFilter) + { + var oldFilter = NativeFunctions.SetUnhandledExceptionFilter(newFilter); + Log.Debug("Set ExceptionFilter to {0}, old: {1}", newFilter, oldFilter); } - + private void SetupClientStructsResolver(DirectoryInfo cacheDir) { using (Timings.Start("CS Resolver Init")) diff --git a/Dalamud/Interface/Internal/DalamudInterface.cs b/Dalamud/Interface/Internal/DalamudInterface.cs index 6035ca0ec..b8ca98584 100644 --- a/Dalamud/Interface/Internal/DalamudInterface.cs +++ b/Dalamud/Interface/Internal/DalamudInterface.cs @@ -863,9 +863,19 @@ internal class DalamudInterface : IDisposable, IServiceType if (ImGui.BeginMenu("Game")) { - if (ImGui.MenuItem("Replace ExceptionHandler")) + if (ImGui.MenuItem("Use in-game default ExceptionHandler")) { - this.dalamud.ReplaceExceptionHandler(); + this.dalamud.UseDefaultExceptionHandler(); + } + + if (ImGui.MenuItem("Use in-game debug ExceptionHandler")) + { + this.dalamud.UseDebugExceptionHandler(); + } + + if (ImGui.MenuItem("Disable in-game ExceptionHandler")) + { + this.dalamud.UseNoExceptionHandler(); } ImGui.EndMenu();