Skip to content

Commit

Permalink
Moved MonoMod details to the Harmony interface
Browse files Browse the repository at this point in the history
Original will be null when patches are not present
BLSE data was added when not needed
Added Prism for highlighting
  • Loading branch information
Aragas committed Apr 7, 2024
1 parent ab1005b commit 7db7c4d
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 169 deletions.
2 changes: 1 addition & 1 deletion build/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<!--Development Variables-->
<PropertyGroup>
<GITHUB_RUN_NUMBER Condition="$(GITHUB_RUN_NUMBER) == ''">0</GITHUB_RUN_NUMBER>
<GITHUB_RUN_NUMBER Condition="$(GITHUB_RUN_NUMBER) == ''">7</GITHUB_RUN_NUMBER>
<Version>13.0.0.$(GITHUB_RUN_NUMBER)</Version>
<LangVersion>11.0</LangVersion>
<Nullable>enable</Nullable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,21 @@

namespace BUTR.CrashReport.Bannerlord
{
using global::Bannerlord.BUTR.Shared.Extensions;
using global::Bannerlord.BUTR.Shared.Helpers;
using global::Bannerlord.ModuleManager;

using global::BUTR.CrashReport.Extensions;
using global::BUTR.CrashReport.Interfaces;
using global::BUTR.CrashReport.Models;
using global::BUTR.CrashReport.Utils;

using global::HarmonyLib;
using global::HarmonyLib.BUTR.Extensions;

using global::System;
using global::System.Collections.Generic;
using global::System.Diagnostics;
using global::System.Globalization;
using global::System.IO;
using global::System.Linq;
using global::System.Reflection;
using global::System.Security.Cryptography;

internal class CrashReportInfoHelper :
IAssemblyUtilities,
Expand All @@ -88,9 +83,13 @@ private static List<ILoaderPluginInfo> GetPlugins()
.Select(x => x.Substring("Bannerlord.BLSE.Features.".Length))
.Select(x => x.Substring(0, x.IndexOf('.') is var idx and not -1 ? idx : x.Length))
.Distinct()
.Select(x => $"BLSE.{x}");
.Select(x => $"BLSE.{x}")
.ToList();

return featurePatches.Concat(new[] { "BLSE.AssemblyResolver"}).Select(x => new LoaderPluginInfo
if (featurePatches.Count > 0)
featurePatches.Add("BLSE.AssemblyResolver");

return featurePatches.Select(x => new LoaderPluginInfo
{
Id = x,
Version = null,
Expand Down
81 changes: 70 additions & 11 deletions src/BUTR.CrashReport.Bannerlord.Source/HarmonyProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,84 @@

namespace BUTR.CrashReport.Bannerlord
{
using global::Bannerlord.BUTR.Shared.Extensions;
using global::Bannerlord.BUTR.Shared.Helpers;
using global::Bannerlord.ModuleManager;

using global::BUTR.CrashReport.Extensions;
using global::BUTR.CrashReport.Interfaces;
using global::BUTR.CrashReport.Models;
using global::BUTR.CrashReport.Utils;

using global::HarmonyLib;
using global::HarmonyLib.BUTR.Extensions;

using global::System;
using global::System.Collections.Generic;
using global::System.Diagnostics;
using global::System.Globalization;
using global::System.IO;
using global::System.Linq;
using global::System.Reflection;
using global::System.Security.Cryptography;

using static global::HarmonyLib.BUTR.Extensions.AccessTools2;

public class HarmonyProvider : IHarmonyProvider
{
private static class MonoModUtils
{
private delegate object GetCurrentRuntimeDelegate();
private static readonly GetCurrentRuntimeDelegate? CurrentRuntimeMethod = GetPropertyGetterDelegate<GetCurrentRuntimeDelegate>(
"MonoMod.RuntimeDetour.DetourHelper:Runtime", logErrorInTrace: false);

private delegate MethodBase GetIdentifiableOldDelegate(object instance, MethodBase method);
private static readonly GetIdentifiableOldDelegate? GetIdentifiableOldMethod = GetDelegate<GetIdentifiableOldDelegate>(
"MonoMod.RuntimeDetour.IDetourRuntimePlatform:GetIdentifiable", logErrorInTrace: false);

private delegate IntPtr GetNativeStartDelegate(object instance, MethodBase method);
private static readonly GetNativeStartDelegate? GetNativeStartMethod = GetDelegate<GetNativeStartDelegate>(
"MonoMod.RuntimeDetour.IDetourRuntimePlatform:GetNativeStart", logErrorInTrace: false);


private delegate object GetCurrentPlatformTripleDelegate();
private static readonly GetCurrentPlatformTripleDelegate? CurrentPlatformTripleMethod = GetPropertyGetterDelegate<GetCurrentPlatformTripleDelegate>(
"MonoMod.Core.Platforms.PlatformTriple:Current", logErrorInTrace: false);

private delegate MethodBase GetIdentifiableDelegate(object instance, MethodBase method);
private static readonly GetIdentifiableDelegate? GetIdentifiableMethod = GetDelegate<GetIdentifiableDelegate>(
"MonoMod.Core.Platforms.PlatformTriple:GetIdentifiable", logErrorInTrace: false);

private delegate IntPtr GetNativeMethodBodyDelegate(object instance, MethodBase method);
private static readonly GetNativeMethodBodyDelegate? GetNativeMethodBodyMethod = GetDelegate<GetNativeMethodBodyDelegate>(
"MonoMod.Core.Platforms.PlatformTriple:GetNativeMethodBody", logErrorInTrace: false);

public static MethodBase? GetIdentifiable(MethodBase method)
{
try
{
if (CurrentRuntimeMethod?.Invoke() is { } runtime)
return GetIdentifiableOldMethod?.Invoke(runtime, method);

if (CurrentPlatformTripleMethod?.Invoke() is { } platformTriple)
return GetIdentifiableMethod?.Invoke(platformTriple, method);
}
catch (Exception e)
{
Trace.TraceError(e.ToString());
}

return null;
}

public static IntPtr GetNativeMethodBody(MethodBase method)
{
try
{
if (CurrentRuntimeMethod?.Invoke() is { } runtine)
return GetNativeStartMethod?.Invoke(runtine, method) ?? IntPtr.Zero;

if (CurrentPlatformTripleMethod?.Invoke() is { } platformTriple)
return GetNativeMethodBodyMethod?.Invoke(platformTriple, method) ?? IntPtr.Zero;
}
catch (Exception e)
{
Trace.TraceError(e.ToString());
}

return IntPtr.Zero;
}
}

public virtual IEnumerable<MethodBase> GetAllPatchedMethods() => Harmony.GetAllPatchedMethods();

public virtual global::BUTR.CrashReport.Models.HarmonyPatches? GetPatchInfo(MethodBase originalMethod)
Expand Down Expand Up @@ -118,6 +173,10 @@ public class HarmonyProvider : IHarmonyProvider
return null;
}
}

public MethodBase? GetIdentifiable(MethodBase method) => MonoModUtils.GetIdentifiable(method);

public IntPtr GetNativeMethodBody(MethodBase method) => MonoModUtils.GetNativeMethodBody(method);
}
}

Expand Down
5 changes: 0 additions & 5 deletions src/BUTR.CrashReport.Bannerlord.Source/LoaderPluginInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,8 @@

namespace BUTR.CrashReport.Bannerlord
{
using global::Bannerlord.BUTR.Shared.Helpers;

using global::BUTR.CrashReport.Models;

using global::System.Collections.Generic;
using global::System.Linq;

internal class LoaderPluginInfo : ILoaderPluginInfo
{
/// <inheritdoc />
Expand Down
1 change: 1 addition & 0 deletions src/BUTR.CrashReport.Bannerlord.Source/ModuleInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
namespace BUTR.CrashReport.Bannerlord
{
using global::Bannerlord.BUTR.Shared.Helpers;

using global::BUTR.CrashReport.Models;

using global::System.Collections.Generic;
Expand Down
19 changes: 6 additions & 13 deletions src/BUTR.CrashReport.Decompilers/Utils/MethodDecompiler.Iced.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;

using static BUTR.CrashReport.Decompilers.Utils.MonoModUtils;

using Decoder = iced::Iced.Intel.Decoder;

namespace BUTR.CrashReport.Decompilers.Utils;
Expand All @@ -21,13 +18,10 @@ partial class MethodDecompiler
/// <summary>
/// Gets the Native representation of the methods
/// </summary>
public static string[] DecompileNativeCode(MethodBase? method, int nativeILOffset)
public static string[] DecompileNativeCode(IntPtr nativeCodePtr, int nativeILOffset)
{
static IEnumerable<string> GetLines(MethodBase method, int nativeILOffset)
static IEnumerable<string> GetLines(IntPtr nativeCodePtr, int nativeILOffset)
{
var nativeCodePtr = GetNativeMethodBody(method);
if (nativeCodePtr == IntPtr.Zero) yield break;

var length = (uint) nativeILOffset + 16;
var bytecode = new byte[length];

Expand All @@ -43,27 +37,26 @@ static IEnumerable<string> GetLines(MethodBase method, int nativeILOffset)
{
Options =
{
DigitSeparator = "`",
FirstOperandCharIndex = 10
FirstOperandCharIndex = 10,
}
};

while (decoder.IP < length)
{
var instr = decoder.Decode();
formatter.Format(instr, output); // Don't use instr.ToString(), it allocates more, uses masm syntax and default options
sb.Append(instr.IP.ToString("X4")).Append(" ").Append(output.ToStringAndReset());
sb.Append(instr.IP.ToString("X4")).Append(' ').Append(output.ToStringAndReset());
yield return sb.ToString();
sb.Clear();
}
}

if (method is null) return Array.Empty<string>();
if (nativeCodePtr == IntPtr.Zero) return Array.Empty<string>();
if (nativeILOffset == StackFrame.OFFSET_UNKNOWN) return Array.Empty<string>();

try
{
return GetLines(method, nativeILOffset).ToArray();
return GetLines(nativeCodePtr, nativeILOffset).ToArray();
}
catch (Exception e)
{
Expand Down
82 changes: 0 additions & 82 deletions src/BUTR.CrashReport.Decompilers/Utils/MonoModUtils.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public sealed record EnhancedStacktraceFrameModel
public required MethodExecuting ExecutingMethod { get; set; }

/// <summary>
/// The original method that might be patched.
/// The original method that is being patched. Is null when no patches exists. Use <see cref="ExecutingMethod"/> instead.
/// </summary>
public required MethodSimple? OriginalMethod { get; set; }

Expand Down
15 changes: 10 additions & 5 deletions src/BUTR.CrashReport.Renderer.Html/CrashReportHtml.Html.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ private static string GetBase(CrashReportModel crashReport, IEnumerable<LogSourc
padding: 5px;
}
</style>
<![if !IE]>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/themes/prism.min.css">
<![endif]>
</head>
<body style='background-color: #ececec;'>
<table style='width: 100%;'>
Expand Down Expand Up @@ -127,7 +130,9 @@ Most likely this error was caused by a custom installed module.
{{Container("enhanced-stacktrace", "Enhanced Stacktrace", GetEnhancedStacktraceHtml(crashReport))}}
{{Container("involved", "Involved Modules and Plugins", GetInvolvedHtml(crashReport))}}
{{Container("installed-modules", "Installed Modules", GetInstalledModulesHtml(crashReport))}}
{{Container("installed-plugins", $"Loaded {crashReport.Metadata.LoaderPluginProviderName} Plugins", GetLoadedBLSEPluginsHtml(crashReport))}}
{{(crashReport.Metadata.LoaderPluginProviderName is not null
? Container("installed-plugins", $"Loaded {crashReport.Metadata.LoaderPluginProviderName} Plugins", GetLoadedBLSEPluginsHtml(crashReport))
: string.Empty)}}
{{Container("assemblies", "Assemblies", $"""
<label>Hide: </label>
<label><input type='checkbox' onclick='showHideByClassName(this, "sys_assembly")' /> System</label>
Expand All @@ -152,6 +157,8 @@ Most likely this error was caused by a custom installed module.

<![if !IE]>
<script src="https://cdn.jsdelivr.net/pako/1.0.3/pako_inflate.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/prism.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/autoloader/prism-autoloader.min.js"></script>
<![endif]>
{{Scripts}}
</body>
Expand Down Expand Up @@ -306,13 +313,11 @@ private static string Container(string id, string name, string content, bool hid
</div>
""";

private static string ContainerCode(string id, string name, string content, bool hide = false) => $"""
private static string ContainerCode(string id, string name, string content, string language, bool hide = false) => $"""
<div>
<a href="javascript:;" class="headers" onclick="showHideById(this, '{id}')">+ {name}</a>
<div id="{id}" class="headers-container" style="display: none;">
<pre>
{content}
</pre>
<pre><code class="language-{language}">{content}</code></pre>
</div>
</div>
""";
Expand Down
Loading

0 comments on commit 7db7c4d

Please sign in to comment.