Skip to content

Commit

Permalink
Merge pull request #318 from BUTR/dev
Browse files Browse the repository at this point in the history
v2.7.2
  • Loading branch information
Aragas authored Mar 16, 2023
2 parents 1320a00 + 3e81917 commit 899c9ff
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 30 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "src/Ben.Demystifier"]
path = src/Ben.Demystifier
url = https://github.com/BUTR/Ben.Demystifier
branch = main
2 changes: 1 addition & 1 deletion build/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<!--Development Variables-->
<PropertyGroup>
<!--Module Version-->
<Version>2.7.1</Version>
<Version>2.7.2</Version>
<!--Harmony Version-->
<HarmonyVersion>2.10.1</HarmonyVersion>
<HarmonyExtensionsVersion>3.2.0.77</HarmonyExtensionsVersion>
Expand Down
5 changes: 5 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
---------------------------------------------------------------------------------------------------
Version: 2.7.2
Game Versions: v1.0.0,v1.0.1,v1.0.2,v1.0.3,v1.1.0,v1.1.1
* Added back Ben.Demystifier
* Updated Crash report to contain more valuable data
---------------------------------------------------------------------------------------------------
Version: 2.7.1
Game Versions: v1.0.0,v1.0.1,v1.0.2,v1.0.3,v1.1.0,v1.1.1
* Game Pass PC was missing implementation files. Thanks dzpliu for noticing!
Expand Down
11 changes: 11 additions & 0 deletions src/Bannerlord.ButterLib.sln
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Development", "Development"
..\docs\articles\Development\Overview.md = ..\docs\articles\Development\Overview.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ben.Demystifier", "Ben.Demystifier\src\Ben.Demystifier\Ben.Demystifier.csproj", "{0F069603-5A4C-4397-A741-0E4D9D5A9635}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Beta_Debug|x64 = Beta_Debug|x64
Expand Down Expand Up @@ -168,6 +170,14 @@ Global
{5C1705E2-CC31-4215-A4CE-7A8AFF6D9BC3}.Stable_Debug|x64.Build.0 = Stable_Debug|x64
{5C1705E2-CC31-4215-A4CE-7A8AFF6D9BC3}.Stable_Release|x64.ActiveCfg = Stable_Release|x64
{5C1705E2-CC31-4215-A4CE-7A8AFF6D9BC3}.Stable_Release|x64.Build.0 = Stable_Release|x64
{0F069603-5A4C-4397-A741-0E4D9D5A9635}.Beta_Debug|x64.ActiveCfg = Debug|Any CPU
{0F069603-5A4C-4397-A741-0E4D9D5A9635}.Beta_Debug|x64.Build.0 = Debug|Any CPU
{0F069603-5A4C-4397-A741-0E4D9D5A9635}.Beta_Release|x64.ActiveCfg = Debug|Any CPU
{0F069603-5A4C-4397-A741-0E4D9D5A9635}.Beta_Release|x64.Build.0 = Debug|Any CPU
{0F069603-5A4C-4397-A741-0E4D9D5A9635}.Stable_Debug|x64.ActiveCfg = Debug|Any CPU
{0F069603-5A4C-4397-A741-0E4D9D5A9635}.Stable_Debug|x64.Build.0 = Debug|Any CPU
{0F069603-5A4C-4397-A741-0E4D9D5A9635}.Stable_Release|x64.ActiveCfg = Debug|Any CPU
{0F069603-5A4C-4397-A741-0E4D9D5A9635}.Stable_Release|x64.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -199,6 +209,7 @@ Global
{43B4A632-B25F-4424-8810-3D60FD54E203} = {AAC60DC8-6952-44D5-B9BD-789902E27273}
{5C1705E2-CC31-4215-A4CE-7A8AFF6D9BC3} = {43B4A632-B25F-4424-8810-3D60FD54E203}
{4C1C912A-C043-4BA3-A3A9-95989F1061B2} = {98D79DD7-266F-4EB7-915A-31BB67D2A9B8}
{0F069603-5A4C-4397-A741-0E4D9D5A9635} = {1392C229-D932-4592-9B4C-EF8E29E83D16}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {368BCFA9-8202-4785-AAD4-E8D3D1913736}
Expand Down
7 changes: 2 additions & 5 deletions src/Bannerlord.ButterLib/Bannerlord.ButterLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,9 @@
<PackageReference Include="IsExternalInit" Version="1.0.3" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
<PackageReference Include="Nullable" Version="1.3.1" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
</ItemGroup>
<!--
<ItemGroup Condition="$(TargetFramework) == 'net472'">
<PackageReference Include="Ben.Demystifier" Version="0.4.1" PrivateAssets="all" />
<PackageReference Update="System.Reflection.Metadata" Version="5.0.0" PrivateAssets="all" />
<ItemGroup>
<ProjectReference Include="..\Ben.Demystifier\src\Ben.Demystifier\Ben.Demystifier.csproj" PrivateAssets="all" />
</ItemGroup>
-->
<!--NuGet References-->

<!--Windows Forms-->
Expand Down
41 changes: 25 additions & 16 deletions src/Bannerlord.ButterLib/ExceptionHandler/CrashReport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

namespace Bannerlord.ButterLib.ExceptionHandler
{
internal record StacktraceEntry(MethodBase Method, bool HarmonyIssue, ModuleInfoExtended? ModuleInfo, string StackFrameDescription);
internal record StacktraceEntry(MethodBase Method, bool MethodFromStackframeIssue, ModuleInfoExtended? ModuleInfo, string StackFrameDescription);

internal class CrashReport
{
public Guid Id { get; } = Guid.NewGuid();
public Exception Exception { get; }
public List<StacktraceEntry> Stacktrace { get; }
public List<ModuleInfoExtendedWithMetadata> LoadedModules { get; } = ModuleInfoHelper.GetLoadedModules().OfType<ModuleInfoExtendedWithMetadata>().ToList();
public List<ModuleInfoExtendedWithMetadata> LoadedModules { get; } = ModuleInfoHelper.GetLoadedModules().ToList();
public List<Assembly> ModuleLoadedAssemblies { get; } = new();
public List<Assembly> ExternalLoadedAssemblies { get; } = new();
public Dictionary<MethodBase, Patches> LoadedHarmonyPatches { get; } = new();
Expand Down Expand Up @@ -70,6 +70,10 @@ private static IEnumerable<StacktraceEntry> GetAllInvolvedModules(Exception ex)
static IEnumerable<(MethodBase, ModuleInfoExtended)> GetFinalizers(Patches? info) => info is null
? Enumerable.Empty<(MethodBase, ModuleInfoExtended)>()
: AddMetadata(info.Finalizers.OrderBy(t => t.priority).Select(t => t.PatchMethod));

static IEnumerable<(MethodBase, ModuleInfoExtended)> GetILManipulators(Patches? info) => info is null
? Enumerable.Empty<(MethodBase, ModuleInfoExtended)>()
: AddMetadata(info.ILManipulators.OrderBy(t => t.priority).Select(t => t.PatchMethod));

static IEnumerable<(MethodBase, ModuleInfoExtended)> AddMetadata(IEnumerable<MethodInfo> methods)
{
Expand All @@ -96,27 +100,28 @@ private static IEnumerable<StacktraceEntry> GetAllInvolvedModules(Exception ex)
}
}

#if _NET472
var trace = new EnhancedStackTrace(ex);
#else
var trace = new StackTrace(ex);
#endif
foreach (var frame in trace.GetFrames() ?? Array.Empty<StackFrame>())
foreach (var frame in trace.GetFrames())
{
if (!frame.HasMethod()) continue;

MethodBase? method;
var harmonyIssue = false;
var methodFromStackframeIssue = false;
try
{
method = Harmony.GetMethodFromStackframe(frame);
}
// NullReferenceException means the method was not found. Harmony doesn't handle this case gracefully
catch (NullReferenceException)
{
method = frame.GetMethod();
}
// The given generic instantiation was invalid.
// From what I understand, this will occur with generic methods
// Also when static constructors throw errors, Harmony resolution will fail
catch (Exception)
catch (Exception e)
{
harmonyIssue = true;
methodFromStackframeIssue = true;
method = frame.GetMethod();
}

Expand All @@ -125,27 +130,31 @@ private static IEnumerable<StacktraceEntry> GetAllInvolvedModules(Exception ex)
var patches = FindPatches(method);
foreach (var (methodBase, extendedModuleInfo) in GetFinalizers(patches))
{
yield return new(methodBase, harmonyIssue, extendedModuleInfo, frameDesc);
yield return new(methodBase, methodFromStackframeIssue, extendedModuleInfo, frameDesc);
}
foreach (var (methodBase, extendedModuleInfo) in GetPostfixes(patches))
{
yield return new(methodBase, harmonyIssue, extendedModuleInfo, frameDesc);
yield return new(methodBase, methodFromStackframeIssue, extendedModuleInfo, frameDesc);
}
foreach (var (methodBase, extendedModuleInfo) in GetPrefixes(patches))
{
yield return new(methodBase, harmonyIssue, extendedModuleInfo, frameDesc);
yield return new(methodBase, methodFromStackframeIssue, extendedModuleInfo, frameDesc);
}
foreach (var (methodBase, extendedModuleInfo) in GetTranspilers(patches))
{
yield return new(methodBase, harmonyIssue, extendedModuleInfo, frameDesc);
yield return new(methodBase, methodFromStackframeIssue, extendedModuleInfo, frameDesc);
}
foreach (var (methodBase, extendedModuleInfo) in GetILManipulators(patches))
{
yield return new(methodBase, methodFromStackframeIssue, extendedModuleInfo, frameDesc);
}

var moduleInfo = GetModuleInfoIfMod(method);

yield return new(method, harmonyIssue, moduleInfo, frameDesc);
yield return new(method, methodFromStackframeIssue, moduleInfo, frameDesc);

if (method is MethodInfo methodInfo && Harmony.GetOriginalMethod(methodInfo) is { } original)
yield return new(original, harmonyIssue, moduleInfo, frameDesc);
yield return new(original, methodFromStackframeIssue, moduleInfo, frameDesc);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ public static void Show(Exception exception)
return;
}

#if _NET472
HtmlBuilder.BuildAndShow(new CrashReport(exception.Demystify()));
#else
HtmlBuilder.BuildAndShow(new CrashReport(exception));
#endif
}
}
}
30 changes: 26 additions & 4 deletions src/Bannerlord.ButterLib/ExceptionHandler/HtmlBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace Bannerlord.ButterLib.ExceptionHandler
{
internal static class HtmlBuilder
{
private static readonly int Version = 8;
private static readonly int Version = 9;
private static readonly string NL = Environment.NewLine;

public static readonly string MiniDumpTag = "<!-- MINI DUMP -->";
Expand All @@ -48,15 +48,23 @@ public static string Build(CrashReport crashReport)
{
var launcherType = GetLauncherType();
var launcherVersion = GetLauncherVersion();

var butrLoaderVersion = GetBUTRLoaderVersion();
var blseVersion = GetBLSEVersion();
var launcherExVersion = GetLauncherExVersion();

var runtime = System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription;
return @$"
<html>
<head>
<title>Bannerlord Crash Report</title>
<meta charset='utf-8'>
<game version='{ApplicationVersionHelper.GameVersionStr()}'>
<launcher type='{launcherType}' version='{launcherVersion}'>
<runtime value='{runtime}'>
{(string.IsNullOrEmpty(butrLoaderVersion) ? "" : $"<butrloader version='{butrLoaderVersion}'>")}
{(string.IsNullOrEmpty(blseVersion) ? "" : $"<blse version='{blseVersion}'>")}
{(string.IsNullOrEmpty(launcherExVersion) ? "" : $"<launcherex version='{launcherExVersion}'>")}
<report id='{crashReport.Id}' version='{Version}'>
<style>
.headers {{
Expand Down Expand Up @@ -136,7 +144,10 @@ Most likely this error was caused by a custom installed module.
<br/>
Launcher: {launcherType} ({launcherVersion})
<br/>
{(string.IsNullOrEmpty(butrLoaderVersion) ? "" : $"<butrloader version='{butrLoaderVersion}'>")}
Runtime: {runtime}
{(string.IsNullOrEmpty(blseVersion) ? "" : $"<br/>BLSE Version: {blseVersion}")}
{(string.IsNullOrEmpty(launcherExVersion) ? "" : $"<br/>LauncherEx Version: {launcherExVersion}")}
<br/>
</div>
</td>
<td>
Expand Down Expand Up @@ -310,6 +321,16 @@ private static string GetBUTRLoaderVersion()

return string.Empty;
}
private static string GetBLSEVersion()
{
var blseMetadata = AccessTools2.TypeByName("Bannerlord.BLSE.BLSEInterceptorAttribute")?.Assembly.GetCustomAttributes<AssemblyMetadataAttribute>();
return blseMetadata?.FirstOrDefault(x => x.Key == "BLSEVersion")?.Value ?? string.Empty;
}
private static string GetLauncherExVersion()
{
var launcherExMetadata = AccessTools2.TypeByName("Bannerlord.LauncherEx.Mixins.LauncherVMMixin")?.Assembly.GetCustomAttributes<AssemblyMetadataAttribute>();
return launcherExMetadata?.FirstOrDefault(x => x.Key == "LauncherExVersion")?.Value ?? string.Empty;
}

private static string GetLauncherType()
{
Expand Down Expand Up @@ -366,12 +387,12 @@ private static string GetEnhancedStacktraceHtml(CrashReport crashReport)
sb.Append("<li>")
.Append($"Frame: {stacktrace.Key}</br>")
.Append("<ul>");
foreach (var (method, harmonyIssue, module, _) in stacktrace)
foreach (var (method, methodFromStackframeIssue, module, _) in stacktrace)
{
sb.Append("<li>")
.Append($"Module: {(module is null ? "UNKNOWN" : module.Id)}</br>")
.Append($"Method: {method.FullDescription()}</br>")
.Append($"HarmonyIssue: {harmonyIssue}</br>")
.Append($"Method From Stackframe Issue: {methodFromStackframeIssue}</br>")
.Append("</li>");
}
sb.AppendLine("</ul></li>");
Expand Down Expand Up @@ -719,6 +740,7 @@ void AppendPatches(string name, IEnumerable<Patch> patches)
AppendPatches(nameof(patches.Postfixes), patches.Postfixes);
AppendPatches(nameof(patches.Finalizers), patches.Finalizers);
AppendPatches(nameof(patches.Transpilers), patches.Transpilers);
AppendPatches(nameof(patches.ILManipulators), patches.ILManipulators);

if (patchesBuilder.Length > 0)
{
Expand Down
1 change: 1 addition & 0 deletions src/Ben.Demystifier
Submodule Ben.Demystifier added at 461d4b

0 comments on commit 899c9ff

Please sign in to comment.