Skip to content

Commit

Permalink
Merge pull request #817 from SnipUndercover/disable-crash-handler-bef…
Browse files Browse the repository at this point in the history
…ore-init

Fix crash handler crashing because of an early crash
  • Loading branch information
maddie480 committed Sep 7, 2024
1 parent 643142e commit 110727f
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Celeste.Mod.mm/Patches/Monocle/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public patch_Engine(int width, int height, int windowWidth, int windowHeight, st
}

private static readonly FieldInfo f_Game_RunApplication = typeof(Game).GetField("RunApplication", BindingFlags.NonPublic | BindingFlags.Instance);
private static readonly FieldInfo f_Game_hasInitialized = typeof(Game).GetField("hasInitialized", BindingFlags.NonPublic | BindingFlags.Instance);
private static readonly MethodInfo m_Game_RunLoop = typeof(Game).GetMethod("RunLoop", BindingFlags.NonPublic | BindingFlags.Instance);
private static readonly MethodInfo m_Game_AfterLoop = typeof(Game).GetMethod("AfterLoop", BindingFlags.NonPublic | BindingFlags.Instance);

Expand All @@ -57,10 +58,15 @@ public patch_Engine(int width, int height, int windowWidth, int windowHeight, st
}
} catch (Exception ex) {
if (continueLoop && ex is TargetInvocationException)
ex = ex.InnerException;
// TargetInvocationException always has a non-null InnerException
ex = ex.InnerException!;

// Try to handle the error by opening an in-game handler first (unless the exception occurred while exiting)
if ((bool) f_Game_RunApplication.GetValue(this)) {
// also, make sure the game has initialized, else the crash handler will... crash. (how ironic)
bool isExiting = !(bool) f_Game_RunApplication.GetValue(this)!;
bool hasInitialized = (bool) f_Game_hasInitialized.GetValue(this)!;

if (!isExiting && hasInitialized) {
ExceptionDispatchInfo dispatch = CriticalErrorHandler.HandleCriticalError(ExceptionDispatchInfo.Capture(ex));
if (dispatch == null) {
// Restart the update loop
Expand Down

0 comments on commit 110727f

Please sign in to comment.