Skip to content

Commit

Permalink
Added TypesFromAssembly
Browse files Browse the repository at this point in the history
Removed Harmony dependency
  • Loading branch information
Aragas committed Oct 11, 2024
1 parent 44b216f commit 6e540f5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ private OperatingSystemType GetOperatingSystemType()

public virtual IEnumerable<Assembly> Assemblies() => AccessTools2.AllAssemblies();

public IEnumerable<Type> TypesFromAssembly(Assembly assembly) => AccessTools.GetTypesFromAssembly(assembly);

public virtual IModuleInfo? GetAssemblyModule(CrashReportInfo crashReport, Assembly assembly)
{
try
Expand Down
2 changes: 1 addition & 1 deletion src/BUTR.CrashReport/CrashReportInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private CrashReportInfo(Exception exception, Dictionary<string, string> addition
FullName = y.FullName
}).ToArray());

Stacktrace = CrashReportUtils.GetAllInvolvedModules(Exception, assemblies, moduleProvider, loaderPluginProvider, harmonyProvider).ToArray();
Stacktrace = CrashReportUtils.GetAllInvolvedModules(Exception, assemblies, assemblyUtilities, moduleProvider, loaderPluginProvider, harmonyProvider).ToArray();
FilteredStacktrace = stacktraceFilter.Filter(Stacktrace).ToArray();

foreach (var originalMethod in harmonyProvider.GetAllPatchedMethods())
Expand Down
8 changes: 7 additions & 1 deletion src/BUTR.CrashReport/Interfaces/IAssemblyUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BUTR.CrashReport.Models;
using System;
using BUTR.CrashReport.Models;

using System.Collections.Generic;
using System.Reflection;
Expand All @@ -15,6 +16,11 @@ public interface IAssemblyUtilities
/// </summary>
IEnumerable<Assembly> Assemblies();

/// <summary>
/// Provides the implementation for getting the types present in the assembly.
/// </summary>
IEnumerable<Type> TypesFromAssembly(Assembly assembly);

/// <summary>
/// Gets the module for the assembly if there is one
/// </summary>
Expand Down
22 changes: 10 additions & 12 deletions src/BUTR.CrashReport/Utils/CrashReportUtils.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using BUTR.CrashReport.Interfaces;
using BUTR.CrashReport.Models;

using HarmonyLib;

using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand Down Expand Up @@ -89,7 +87,7 @@ public void Deconstruct(out MethodBase? original, out MethodInfo? replacement, o
/// <summary>
/// Gets the module info if the method is from a mod.
/// </summary>
public static IModuleInfo? GetModuleInfoIfMod(MethodBase? method, IEnumerable<Assembly> assemblies, IModuleProvider moduleProvider)
public static IModuleInfo? GetModuleInfoIfMod(MethodBase? method, IEnumerable<Assembly> assemblies, IAssemblyUtilities assemblyUtilities, IModuleProvider moduleProvider)
{
if (method is null)
return null;
Expand All @@ -113,7 +111,7 @@ public void Deconstruct(out MethodBase? original, out MethodInfo? replacement, o

var fullMethodName = string.Join("", patchPostfix.Take(patchPostfix.Length - 1));
var foundMethod = assemblies.Where(x => !x.IsDynamic)
.SelectMany(AccessTools.GetTypesFromAssembly)
.SelectMany(assemblyUtilities.TypesFromAssembly)
.Where(x => !x.IsAbstract)
.Where(x => !string.IsNullOrEmpty(x.DeclaringType?.FullName) && fullMethodName.StartsWith(x.DeclaringType!.FullName))
.SelectMany(x => x.GetMethods())
Expand All @@ -132,7 +130,7 @@ public void Deconstruct(out MethodBase? original, out MethodInfo? replacement, o
/// <summary>
/// Gets the loader plugin if the method is from a mod.
/// </summary>
public static ILoaderPluginInfo? GetLoaderPluginIfMod(MethodBase? method, IEnumerable<Assembly> assemblies, ILoaderPluginProvider loaderPluginProvider)
public static ILoaderPluginInfo? GetLoaderPluginIfMod(MethodBase? method, IEnumerable<Assembly> assemblies, IAssemblyUtilities assemblyUtilities, ILoaderPluginProvider loaderPluginProvider)
{
if (method is null)
return null;
Expand All @@ -156,7 +154,7 @@ public void Deconstruct(out MethodBase? original, out MethodInfo? replacement, o

var fullMethodName = string.Join("", patchPostfix.Take(patchPostfix.Length - 1));
var foundMethod = assemblies.Where(x => !x.IsDynamic)
.SelectMany(AccessTools.GetTypesFromAssembly)
.SelectMany(assemblyUtilities.TypesFromAssembly)
.Where(x => !x.IsAbstract)
.Where(x => !string.IsNullOrEmpty(x.DeclaringType?.FullName) && fullMethodName.StartsWith(x.DeclaringType!.FullName))
.SelectMany(x => x.GetMethods())
Expand Down Expand Up @@ -229,12 +227,12 @@ public static StackframePatchData GetHarmonyData(StackFrame frame, IHarmonyProvi
/// <summary>
/// Gets all involved modules in the exception stacktrace.
/// </summary>
public static IEnumerable<StacktraceEntry> GetAllInvolvedModules(Exception ex, ICollection<Assembly> assemblies, IModuleProvider moduleProvider, ILoaderPluginProvider loaderPluginProvider, IHarmonyProvider harmonyProvider)
public static IEnumerable<StacktraceEntry> GetAllInvolvedModules(Exception ex, ICollection<Assembly> assemblies, IAssemblyUtilities assemblyUtilities, IModuleProvider moduleProvider, ILoaderPluginProvider loaderPluginProvider, IHarmonyProvider harmonyProvider)
{
var inner = ex.InnerException;
if (inner is not null)
{
foreach (var modInfo in GetAllInvolvedModules(inner, assemblies, moduleProvider, loaderPluginProvider, harmonyProvider))
foreach (var modInfo in GetAllInvolvedModules(inner, assemblies, assemblyUtilities, moduleProvider, loaderPluginProvider, harmonyProvider))
yield return modInfo;
}

Expand All @@ -254,15 +252,15 @@ public static IEnumerable<StacktraceEntry> GetAllInvolvedModules(Exception ex, I
OriginalMethod = originalMethod is not null && originalMethod != executingMethod ? new()
{
Method = originalMethod,
ModuleInfo = GetModuleInfoIfMod(originalMethod, assemblies, moduleProvider),
LoaderPluginInfo = GetLoaderPluginIfMod(originalMethod, assemblies, loaderPluginProvider),
ModuleInfo = GetModuleInfoIfMod(originalMethod, assemblies, assemblyUtilities, moduleProvider),
LoaderPluginInfo = GetLoaderPluginIfMod(originalMethod, assemblies, assemblyUtilities, loaderPluginProvider),
ILInstructions = DecompileILCode(originalMethod),
CSharpILMixedInstructions = DecompileILWithCSharpCode(originalMethod),
CSharpInstructions = DecompileCSharpCode(originalMethod),
} : null,
MethodFromStackframeIssue = methodFromStackframeIssue,
ModuleInfo = GetModuleInfoIfMod(executingMethod, assemblies, moduleProvider),
LoaderPluginInfo = GetLoaderPluginIfMod(executingMethod, assemblies, loaderPluginProvider),
ModuleInfo = GetModuleInfoIfMod(executingMethod, assemblies, assemblyUtilities, moduleProvider),
LoaderPluginInfo = GetLoaderPluginIfMod(executingMethod, assemblies, assemblyUtilities, loaderPluginProvider),
ILOffset = ilOffset != StackFrame.OFFSET_UNKNOWN ? ilOffset : null,
NativeOffset = nativeILOffset != StackFrame.OFFSET_UNKNOWN ? nativeILOffset : null,
StackFrameDescription = frame.ToString(),
Expand Down

0 comments on commit 6e540f5

Please sign in to comment.