Skip to content

Commit

Permalink
Fixed Harmony Patch Type metadata
Browse files Browse the repository at this point in the history
Replaced array with collection expression
  • Loading branch information
Aragas committed Apr 10, 2024
1 parent 7841da8 commit f8b577d
Show file tree
Hide file tree
Showing 20 changed files with 80 additions and 139 deletions.
22 changes: 11 additions & 11 deletions src/BUTR.CrashReport.Bannerlord.Parser/CrashReportParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private static IList<EnhancedStacktraceFrameModel> GetEnhancedStacktrace(ReadOnl
node = enhancedStacktraceDoc.DocumentNode;
}

return node.SelectSingleNode("descendant::div[@id=\"enhanced-stacktrace\"]/ul")?.ChildNodes.Where(cn => cn.Name == "li").Select(ParseEnhancedStacktrace).ToArray() ?? Array.Empty<EnhancedStacktraceFrameModel>();
return node.SelectSingleNode("descendant::div[@id=\"enhanced-stacktrace\"]/ul")?.ChildNodes.Where(cn => cn.Name == "li").Select(ParseEnhancedStacktrace).ToArray() ?? [];
}

private static HtmlDocument Create(ref string content)
Expand Down Expand Up @@ -220,12 +220,12 @@ private static CrashReportModel ParseLegacyHtmlInternal(byte version, HtmlDocume
var node = document.DocumentNode;
var id = node.SelectSingleNode("descendant::report")?.Attributes?["id"]?.Value ?? string.Empty;
var gameVersion = node.SelectSingleNode("descendant::game")?.Attributes?["version"]?.Value ?? string.Empty;
var installedModules = node.SelectSingleNode("descendant::div[@id=\"installed-modules\"]/ul")?.ChildNodes.Where(cn => cn.Name == "li").Select(x => ParseModule(version, x)).DistinctBy(x => x.Id).ToArray() ?? Array.Empty<ModuleModel>();
var installedModules = node.SelectSingleNode("descendant::div[@id=\"installed-modules\"]/ul")?.ChildNodes.Where(cn => cn.Name == "li").Select(x => ParseModule(version, x)).DistinctBy(x => x.Id).ToArray() ?? [];
var exception = ParseExceptions(node.SelectSingleNode("descendant::div[@id=\"exception\"]"), installedModules);
var involvedModules = node.SelectSingleNode("descendant::div[@id=\"involved-modules\"]/ul")?.ChildNodes.Where(cn => cn.Name == "li").SelectMany(ParseInvolvedModule).ToArray() ?? Array.Empty<InvolvedModuleOrPluginModel>();
var involvedModules = node.SelectSingleNode("descendant::div[@id=\"involved-modules\"]/ul")?.ChildNodes.Where(cn => cn.Name == "li").SelectMany(ParseInvolvedModule).ToArray() ?? [];
var enhancedStacktrace = GetEnhancedStacktrace(content.AsSpan(), version, node);

var assemblies = node.SelectSingleNode("descendant::div[@id=\"assemblies\"]/ul")?.ChildNodes.Where(cn => cn.Name == "li").Select(x => ParseAssembly(x, installedModules)).ToArray() ?? Array.Empty<AssemblyModel>();
var assemblies = node.SelectSingleNode("descendant::div[@id=\"assemblies\"]/ul")?.ChildNodes.Where(cn => cn.Name == "li").Select(x => ParseAssembly(x, installedModules)).ToArray() ?? [];
var harmonyPatches = node.SelectSingleNode("descendant::div[@id=\"harmony-patches\"]/ul").ChildNodes.Where(cn => cn.Name == "li").Select(ParseHarmonyPatch).ToArray();
var launcherType = node.SelectSingleNode("descendant::launcher")?.Attributes?["type"]?.Value ?? string.Empty;
var launcherVersion = node.SelectSingleNode("descendant::launcher")?.Attributes?["version"]?.Value ?? string.Empty;
Expand Down Expand Up @@ -452,7 +452,7 @@ private static EnhancedStacktraceFrameModel ParseEnhancedStacktrace(HtmlNode nod
AdditionalMetadata = executingMethod.AdditionalMetadata,
},
OriginalMethod = null,
PatchMethods = methods.Count == 1 ? Array.Empty<MethodSimple>() : methods.Take(methods.Count - 1).ToArray(),
PatchMethods = methods.Count == 1 ? [] : methods.Take(methods.Count - 1).ToArray(),
MethodFromStackframeIssue = false,
AdditionalMetadata = Array.Empty<MetadataModel>(),
};
Expand Down Expand Up @@ -517,17 +517,17 @@ static HarmonyPatchModel ParsePatch(HtmlNode node, HarmonyPatchType type)
Namespace = split.FirstOrDefault(x => x.StartsWith("Namespace: "))?.Split(':')[1] ?? string.Empty,
Index = split.FirstOrDefault(x => x.StartsWith("Index: "))?.Split(':')[1] is { } strIndex && int.TryParse(strIndex, out var index) ? index : 0,
Priority = split.FirstOrDefault(x => x.StartsWith("Priority: "))?.Split(':')[1] is { } strPriority && int.TryParse(strPriority, out var piority) ? piority : 400,
Before = split.FirstOrDefault(x => x.StartsWith("Before: "))?.Split(':')[1].Split(',') ?? Array.Empty<string>(),
After = split.FirstOrDefault(x => x.StartsWith("After: "))?.Split(':')[1].Split(',') ?? Array.Empty<string>(),
Before = split.FirstOrDefault(x => x.StartsWith("Before: "))?.Split(':')[1].Split(',') ?? [],
After = split.FirstOrDefault(x => x.StartsWith("After: "))?.Split(':')[1].Split(',') ?? [],
AdditionalMetadata = Array.Empty<MetadataModel>(),
};
}

var originalMethodFullName = node.ChildNodes.Skip(0).First().InnerText.Trim('\n');
var prefixes = node.ChildNodes.FirstOrDefault(x => x.InnerText?.Contains("Prefixes") == true)?.SelectSingleNode("descendant::ul/li")?.ChildNodes.Select(x => ParsePatch(x, HarmonyPatchType.Prefix)).ToArray() ?? Array.Empty<HarmonyPatchModel>();
var postfixes = node.ChildNodes.FirstOrDefault(x => x.InnerText?.Contains("Postfixes") == true)?.SelectSingleNode("descendant::ul/li")?.ChildNodes.Select(x => ParsePatch(x, HarmonyPatchType.Postfix)).ToArray() ?? Array.Empty<HarmonyPatchModel>();
var transpilers = node.ChildNodes.FirstOrDefault(x => x.InnerText?.Contains("Transpilers") == true)?.SelectSingleNode("descendant::ul/li")?.ChildNodes.Select(x => ParsePatch(x, HarmonyPatchType.Transpiler)).ToArray() ?? Array.Empty<HarmonyPatchModel>();
var finalizers = node.ChildNodes.FirstOrDefault(x => x.InnerText?.Contains("Finalizers") == true)?.SelectSingleNode("descendant::ul/li")?.ChildNodes.Select(x => ParsePatch(x, HarmonyPatchType.Finalizer)).ToArray() ?? Array.Empty<HarmonyPatchModel>();
var prefixes = node.ChildNodes.FirstOrDefault(x => x.InnerText?.Contains("Prefixes") == true)?.SelectSingleNode("descendant::ul/li")?.ChildNodes.Select(x => ParsePatch(x, HarmonyPatchType.Prefix)).ToArray() ?? [];
var postfixes = node.ChildNodes.FirstOrDefault(x => x.InnerText?.Contains("Postfixes") == true)?.SelectSingleNode("descendant::ul/li")?.ChildNodes.Select(x => ParsePatch(x, HarmonyPatchType.Postfix)).ToArray() ?? [];
var transpilers = node.ChildNodes.FirstOrDefault(x => x.InnerText?.Contains("Transpilers") == true)?.SelectSingleNode("descendant::ul/li")?.ChildNodes.Select(x => ParsePatch(x, HarmonyPatchType.Transpiler)).ToArray() ?? [];
var finalizers = node.ChildNodes.FirstOrDefault(x => x.InnerText?.Contains("Finalizers") == true)?.SelectSingleNode("descendant::ul/li")?.ChildNodes.Select(x => ParsePatch(x, HarmonyPatchType.Finalizer)).ToArray() ?? [];
var harmonyPatchModel = new HarmonyPatchesModel
{
OriginalMethodName = originalMethodFullName.Split('.').Last(),
Expand Down
2 changes: 1 addition & 1 deletion src/BUTR.CrashReport.Bannerlord.Tool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static async Task<int> Main(string[] args)
{
Converters = { new JsonStringEnumConverter() }
})!;
var logs = logsStream is not null ? JsonSerializer.Deserialize<LogSource[]>(logsStream)! : Array.Empty<LogSource>();
var logs = logsStream is not null ? JsonSerializer.Deserialize<LogSource[]>(logsStream)! : [];

var html = CrashReportHtml.AddData(CrashReportHtml.Build(crashReport, logs), crashReportJson, minidump, saveFile, screenshot);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ private static bool TryGetMethodDefinition(MethodBase method, [NotNullWhen(true)
/// </summary>
public static string[] DecompileILCode(MethodBase? method)
{
static string[] ToLines(CilInstructionCollection? instructions) => instructions?.Select(x => x.ToString()).ToArray() ?? Array.Empty<string>();
static string[] ToLines(CilInstructionCollection? instructions) => instructions?.Select(x => x.ToString()).ToArray() ?? [];

if (method is null) return Array.Empty<string>();
if (method is null) return [];

try
{
if (!TryCopyMethod(method, out var stream, out var methodHandle)) return Array.Empty<string>();
if (!TryCopyMethod(method, out var stream, out var methodHandle)) return [];

using var _ = stream;
using var ms = stream as MemoryStream ?? new MemoryStream();
Expand All @@ -70,7 +70,7 @@ public static string[] DecompileILCode(MethodBase? method)

try
{
if (!TryGetMethodDefinition(method, out _, out var methodDefinition)) return Array.Empty<string>();
if (!TryGetMethodDefinition(method, out _, out var methodDefinition)) return [];

return ToLines(methodDefinition.CilMethodBody?.Instructions);
}
Expand All @@ -79,6 +79,6 @@ public static string[] DecompileILCode(MethodBase? method)
Trace.TraceError(e.ToString());
}

return Array.Empty<string>();
return [];
}
}
18 changes: 9 additions & 9 deletions src/BUTR.CrashReport.Decompilers/Utils/MethodDecompiler.ILSpy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ partial class MethodDecompiler
/// </summary>
public static string[] DecompileILCodeExtended(MethodBase? method)
{
if (method is null) return Array.Empty<string>();
if (method is null) return [];

try
{
if (!TryCopyMethod(method, out var stream, out var methodHandle)) return Array.Empty<string>();
if (!TryCopyMethod(method, out var stream, out var methodHandle)) return [];

using var _ = stream;
using var peFile = new PEFile("Assembly", stream);
Expand All @@ -42,19 +42,19 @@ public static string[] DecompileILCodeExtended(MethodBase? method)
Trace.TraceError(e.ToString());
}

return Array.Empty<string>();
return [];
}

/// <summary>
/// Gets the C# + IL representation of the methods
/// </summary>
public static string[] DecompileILWithCSharpCode(MethodBase? method)
{
if (method is null) return Array.Empty<string>();
if (method is null) return [];

try
{
if (!TryCopyMethod(method, out var stream, out var methodHandle)) return Array.Empty<string>();
if (!TryCopyMethod(method, out var stream, out var methodHandle)) return [];

using var _ = stream;
using var peFile = new PEFile("Assembly", stream);
Expand All @@ -70,19 +70,19 @@ public static string[] DecompileILWithCSharpCode(MethodBase? method)
Trace.TraceError(e.ToString());
}

return Array.Empty<string>();
return [];
}

/// <summary>
/// Gets the C# representation of the methods
/// </summary>
public static string[] DecompileCSharpCode(MethodBase? method)
{
if (method is null) return Array.Empty<string>();
if (method is null) return [];

try
{
if (!TryCopyMethod(method, out var stream, out var methodHandle)) return Array.Empty<string>();
if (!TryCopyMethod(method, out var stream, out var methodHandle)) return [];

using var _ = stream;
using var peFile = new PEFile("Assembly", stream);
Expand All @@ -108,6 +108,6 @@ public static string[] DecompileCSharpCode(MethodBase? method)
Trace.TraceError(e.ToString());
}

return Array.Empty<string>();
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ static IEnumerable<string> GetLines(IntPtr nativeCodePtr, int nativeILOffset)
}
}

if (nativeCodePtr == IntPtr.Zero) return Array.Empty<string>();
if (nativeILOffset == StackFrame.OFFSET_UNKNOWN) return Array.Empty<string>();
if (nativeCodePtr == IntPtr.Zero) return [];
if (nativeILOffset == StackFrame.OFFSET_UNKNOWN) return [];

try
{
Expand All @@ -63,6 +63,6 @@ static IEnumerable<string> GetLines(IntPtr nativeCodePtr, int nativeILOffset)
Trace.TraceError(e.ToString());
}

return Array.Empty<string>();
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ public static Dictionary<AssemblyName, AssemblyTypeReferenceInternal[]> GetImpor
Trace.TraceError(e.ToString());
}

return Array.Empty<AssemblyTypeReferenceInternal>();
return [];
});
}
56 changes: 0 additions & 56 deletions src/BUTR.CrashReport.Models/MethodHarmonyPatch.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/BUTR.CrashReport.Models/Utils/AssemblyUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ public static class AssemblyUtils
/// <param name="publicKeyToken">The public key token.</param>
/// <returns> The public key token as string.</returns>
public static string PublicKeyAsString(byte[]? publicKeyToken) =>
string.Join(string.Empty, Array.ConvertAll(publicKeyToken ?? new byte[0], x => x.ToString("x2", CultureInfo.InvariantCulture)));
string.Join(string.Empty, Array.ConvertAll(publicKeyToken ?? [], x => x.ToString("x2", CultureInfo.InvariantCulture)));
}
2 changes: 1 addition & 1 deletion src/BUTR.CrashReport.Renderer.Html/CrashReportHtml.Html.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,4 +322,4 @@ private static string ContainerCode(string id, string name, string content, stri
</div>
""";
#pragma warning disable format // @formatter:on
}
}
16 changes: 8 additions & 8 deletions src/BUTR.CrashReport.Renderer.Html/CrashReportHtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ private static string GetEnhancedStacktraceHtml(CrashReportModel crashReport)
var id03 = random.Next();
var moduleId = method.ModuleId ?? "UNKNOWN";
var pluginId = method.LoaderPluginId ?? "UNKNOWN";
var harmonyPatch = method as MethodHarmonyPatch;
var harmonyPatchType = method.AdditionalMetadata.FirstOrDefault(x => x.Key == "HarmonyPatchType");
sbMain.Append("<li>")
.Append("Type: ").Append(harmonyPatch is not null ? "Harmony" : "UNKNOWN").Append("<br/>")
.AppendIf(harmonyPatch is not null, sb => sb.Append("Patch Type: ").Append(harmonyPatch!.PatchType.ToString()).Append("<br/>"))
.Append("Type: ").Append(harmonyPatchType is not null ? "Harmony" : "UNKNOWN").Append("<br/>")
.AppendIf(harmonyPatchType is not null, sb => sb.Append("Patch Type: ").Append(harmonyPatchType!.Value).Append("<br/>"))
.AppendIf(moduleId != "UNKNOWN", sb => sb.Append("Module Id: ").Append("<b><a href='javascript:;' onclick='scrollToElement(\"").Append(moduleId).Append("\")'>").Append(moduleId).Append("</a></b>").Append("<br/>"))
.AppendIf(pluginId != "UNKNOWN", sb => sb.Append("Plugin Id: ").Append("<b><a href='javascript:;' onclick='scrollToElement(\"").Append(pluginId).Append("\")'>").Append(pluginId).Append("</a></b>").Append("<br/>"))
.Append("Method: ").Append(method.MethodFullDescription.EscapeGenerics()).Append("<br/>")
Expand Down Expand Up @@ -239,7 +239,7 @@ private static void AddInvolvedModules(CrashReportModel crashReport, StringBuild
.Append("<ul>");
foreach (var method in stacktrace.PatchMethods)
{
var harmonyPatch = method as MethodHarmonyPatch;
var harmonyPatchType = method.AdditionalMetadata.FirstOrDefault(x => x.Key == "HarmonyPatchType");

// Ignore blank transpilers used to force the jitter to skip inlining
if (method.MethodName == "BlankTranspiler") continue;
Expand All @@ -248,7 +248,7 @@ private static void AddInvolvedModules(CrashReportModel crashReport, StringBuild
.AppendIf(moduleId2 == "UNKNOWN", sb => sb.Append("Module Id: ").Append(moduleId2).Append("<br/>"))
.AppendIf(moduleId2 != "UNKNOWN", sb => sb.Append("Module Id: ").Append("<b><a href='javascript:;' onclick='scrollToElement(\"").Append(moduleId2).Append("\")'>").Append(moduleId2).Append("</a></b>").Append("<br/>"))
.Append("Method: ").Append(method.MethodFullDescription.EscapeGenerics()).Append("<br/>")
.AppendIf(harmonyPatch is not null, sb => sb.Append("Harmony Patch Type: ").Append(harmonyPatch!.PatchType).Append("<br/>"))
.AppendIf(harmonyPatchType is not null, sb => sb.Append("Harmony Patch Type: ").Append(harmonyPatchType!.Value).Append("<br/>"))
.Append("</li>");
}
sbMain.Append("</ul>");
Expand Down Expand Up @@ -283,7 +283,7 @@ private static void AddInvolvedPlugins(CrashReportModel crashReport, StringBuild
.Append("<ul>");
foreach (var method in stacktrace.PatchMethods)
{
var harmonyPatch = method as MethodHarmonyPatch;
var harmonyPatchType = method.AdditionalMetadata.FirstOrDefault(x => x.Key == "HarmonyPatchType");

// Ignore blank transpilers used to force the jitter to skip inlining
if (method.MethodName == "BlankTranspiler") continue;
Expand All @@ -292,7 +292,7 @@ private static void AddInvolvedPlugins(CrashReportModel crashReport, StringBuild
.AppendIf(pluginId2 == "UNKNOWN", sb => sb.Append("Plugin Id: ").Append(pluginId2).Append("<br/>"))
.AppendIf(pluginId2 != "UNKNOWN", sb => sb.Append("Plugin Id: ").Append("<b><a href='javascript:;' onclick='scrollToElement(\"").Append(pluginId2).Append("\")'>").Append(pluginId2).Append("</a></b>").Append("<br/>"))
.Append("Method: ").Append(method.MethodFullDescription.EscapeGenerics()).Append("<br/>")
.AppendIf(harmonyPatch is not null, sb => sb.Append("Harmony Patch Type: ").Append(harmonyPatch!.PatchType).Append("<br/>"))
.AppendIf(harmonyPatchType is not null, sb => sb.Append("Harmony Patch Type: ").Append(harmonyPatchType!.Value).Append("<br/>"))
.Append("</li>");
}
sbMain.Append("</ul>");
Expand Down Expand Up @@ -653,4 +653,4 @@ private static string GetLogFilesHtml(IEnumerable<LogSource> files)
sb.Append("</ul>");
return sb.ToString();
}
}
}
Loading

0 comments on commit f8b577d

Please sign in to comment.