Skip to content

Commit

Permalink
Fix StackTraceFixes not being installed
Browse files Browse the repository at this point in the history
  • Loading branch information
ghorsington committed Oct 9, 2020
1 parent e052a90 commit d1a0a90
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
31 changes: 18 additions & 13 deletions Harmony/Internal/Util/StackTraceFixes.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using HarmonyLib.Internal.Util;
using HarmonyLib.Tools;
using MonoMod.RuntimeDetour;

namespace HarmonyLib.Internal.RuntimeFixes
Expand All @@ -28,18 +27,24 @@ public static void Install()
if (_applied)
return;

var refreshDet = new Detour(AccessTools.Method(AccessTools.Inner(typeof(ILHook), "Context"), "Refresh"),
AccessTools.Method(typeof(StackTraceFixes), nameof(OnILChainRefresh)));
_origRefresh = refreshDet.GenerateTrampoline<Action<object>>();

var getMethodDet = new Detour(AccessTools.Method(typeof(StackFrame), nameof(StackFrame.GetMethod)),
AccessTools.Method(typeof(StackTraceFixes), nameof(GetMethodFix)));
_origGetMethod = getMethodDet.GenerateTrampoline<Func<StackFrame, MethodBase>>();
try
{
var refreshDet = new Detour(AccessTools.Method(AccessTools.Inner(typeof(ILHook), "Context"), "Refresh"),
AccessTools.Method(typeof(StackTraceFixes), nameof(OnILChainRefresh)));
_origRefresh = refreshDet.GenerateTrampoline<Action<object>>();

var nat = new NativeDetour(AccessTools.Method(typeof(Assembly), nameof(Assembly.GetExecutingAssembly)),
AccessTools.Method(typeof(StackTraceFixes), nameof(GetAssemblyFix)));
_realGetAss = nat.GenerateTrampoline<Func<Assembly>>();
var getMethodDet = new Detour(AccessTools.Method(typeof(StackFrame), nameof(StackFrame.GetMethod)),
AccessTools.Method(typeof(StackTraceFixes), nameof(GetMethodFix)));
_origGetMethod = getMethodDet.GenerateTrampoline<Func<StackFrame, MethodBase>>();

var nat = new NativeDetour(AccessTools.Method(typeof(Assembly), nameof(Assembly.GetExecutingAssembly)),
AccessTools.Method(typeof(StackTraceFixes), nameof(GetAssemblyFix)));
_realGetAss = nat.GenerateTrampoline<Func<Assembly>>();
}
catch (Exception e)
{
Logger.LogText(Logger.LogChannel.Error, $"Failed to apply stack trace fix: ({e.GetType().FullName}) {e.Message}");
}
_applied = true;
}

Expand Down Expand Up @@ -77,4 +82,4 @@ private static void OnILChainRefresh(object self)
}
}
}
}
}
6 changes: 6 additions & 0 deletions Harmony/Public/Harmony.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Reflection;
using System.Text;
using HarmonyLib.Internal.RuntimeFixes;
using HarmonyLib.Tools;

namespace HarmonyLib
Expand All @@ -19,6 +20,11 @@ public class Harmony
// ReSharper disable once InconsistentNaming
public static bool DEBUG;

static Harmony()
{
StackTraceFixes.Install();
}

/// <summary>Creates a new Harmony instance</summary>
/// <param name="id">A unique identifier (you choose your own)</param>
/// <returns>A Harmony instance</returns>
Expand Down

0 comments on commit d1a0a90

Please sign in to comment.