Skip to content

Commit

Permalink
v2.3.1 adds tweak values for turning off stacktrace caching/enhancing
Browse files Browse the repository at this point in the history
  • Loading branch information
pardeike committed Apr 10, 2024
1 parent d02ec38 commit f73531d
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 54 deletions.
2 changes: 1 addition & 1 deletion About/About.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<name>Harmony</name>
<author>Andreas Pardeike</author>
<packageId>brrainz.harmony</packageId>
<modVersion>2.3.0.0</modVersion>
<modVersion>2.3.1.0</modVersion>
<url>https://github.com/pardeike/HarmonyRimWorld</url>
<supportedVersions>
<li>1.2</li>
Expand Down
2 changes: 1 addition & 1 deletion About/Manifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Manifest>
<identifier>net.pardeike.rimworld.mod.harmony</identifier>
<version>2.3.0.0</version>
<version>2.3.1.0</version>
<targetVersions>
<li>1.2.0</li>
<li>1.3.0</li>
Expand Down
Binary file modified Current/Assemblies/HarmonyMod.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<ModName>HarmonyMod</ModName>
<ModFileName>HarmonyMod</ModFileName>
<Repository>https://github.com/pardeike/HarmonyRimworld</Repository>
<ModVersion>2.3.0.0</ModVersion>
<ModVersion>2.3.1.0</ModVersion>
<ProjectGuid>{C0C9BB35-9100-47A8-886E-32932EF301ED}</ProjectGuid>
</PropertyGroup>

Expand Down
61 changes: 21 additions & 40 deletions Source/ExceptionAnalyser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,34 @@
using System.Linq;
using System.Reflection;
using System.Text;
using LudeonTK;
using UnityEngine;
using JetBrains.Annotations;

namespace HarmonyMod
{
public static class ExceptionTools
static class ExceptionTools
{
[TweakValue("_Harmony")]
[UsedImplicitly]
public static bool DisableStackTraceCaching;
static readonly AccessTools.FieldRef<StackTrace, StackTrace[]> captured_traces = AccessTools.FieldRefAccess<StackTrace, StackTrace[]>("captured_traces");
static readonly AccessTools.FieldRef<StackFrame, string> internalMethodName = AccessTools.FieldRefAccess<StackFrame, string>("internalMethodName");
static readonly AccessTools.FieldRef<StackFrame, long> methodAddress = AccessTools.FieldRefAccess<StackFrame, long>("methodAddress");

public static readonly AccessTools.FieldRef<StackTrace, StackTrace[]> captured_traces = AccessTools.FieldRefAccess<StackTrace, StackTrace[]>("captured_traces");
public static readonly AccessTools.FieldRef<StackFrame, string> internalMethodName = AccessTools.FieldRefAccess<StackFrame, string>("internalMethodName");
public static readonly AccessTools.FieldRef<StackFrame, long> methodAddress = AccessTools.FieldRefAccess<StackFrame, long>("methodAddress");
delegate void GetFullNameForStackTraceDelegate(StackTrace instance, StringBuilder sb, MethodBase mi);
static readonly MethodInfo m_GetFullNameForStackTrace = AccessTools.Method(typeof(StackTrace), "GetFullNameForStackTrace");
static readonly GetFullNameForStackTraceDelegate GetFullNameForStackTrace = AccessTools.MethodDelegate<GetFullNameForStackTraceDelegate>(m_GetFullNameForStackTrace);

public delegate void GetFullNameForStackTraceDelegate(StackTrace instance, StringBuilder sb, MethodBase mi);
private static readonly MethodInfo m_GetFullNameForStackTrace = AccessTools.Method(typeof(StackTrace), "GetFullNameForStackTrace");
public static readonly GetFullNameForStackTraceDelegate GetFullNameForStackTrace = AccessTools.MethodDelegate<GetFullNameForStackTraceDelegate>(m_GetFullNameForStackTrace);
delegate uint GetMethodIndexDelegate(StackFrame instance);
static readonly MethodInfo m_GetMethodIndex = AccessTools.Method(typeof(StackFrame), "GetMethodIndex");
static readonly GetMethodIndexDelegate GetMethodIndex = AccessTools.MethodDelegate<GetMethodIndexDelegate>(m_GetMethodIndex);

public delegate uint GetMethodIndexDelegate(StackFrame instance);
private static readonly MethodInfo m_GetMethodIndex = AccessTools.Method(typeof(StackFrame), "GetMethodIndex");
public static readonly GetMethodIndexDelegate GetMethodIndex = AccessTools.MethodDelegate<GetMethodIndexDelegate>(m_GetMethodIndex);
delegate string GetSecureFileNameDelegate(StackFrame instance);
static readonly MethodInfo m_GetSecureFileName = AccessTools.Method(typeof(StackFrame), "GetSecureFileName");
static readonly GetSecureFileNameDelegate GetSecureFileName = AccessTools.MethodDelegate<GetSecureFileNameDelegate>(m_GetSecureFileName);

public delegate string GetSecureFileNameDelegate(StackFrame instance);
private static readonly MethodInfo m_GetSecureFileName = AccessTools.Method(typeof(StackFrame), "GetSecureFileName");
public static readonly GetSecureFileNameDelegate GetSecureFileName = AccessTools.MethodDelegate<GetSecureFileNameDelegate>(m_GetSecureFileName);
delegate string GetAotIdDelegate();
static readonly MethodInfo m_GetAotId = AccessTools.Method(typeof(StackTrace), "GetAotId");
static readonly GetAotIdDelegate GetAotId = AccessTools.MethodDelegate<GetAotIdDelegate>(m_GetAotId);

public delegate string GetAotIdDelegate();
private static readonly MethodInfo m_GetAotId = AccessTools.Method(typeof(StackTrace), "GetAotId");
public static readonly GetAotIdDelegate GetAotId = AccessTools.MethodDelegate<GetAotIdDelegate>(m_GetAotId);
internal static readonly ConcurrentDictionary<int, int> seenStacktraces = [];

public static readonly ConcurrentDictionary<int, int> seenStacktraces = [];

public static string ExtractHarmonyEnhancedStackTrace()
{
try
{
return ExtractHarmonyEnhancedStackTrace(new StackTrace(3, true), false, out _);
}
catch (System.Exception)
{
return StackTraceUtility.ExtractStackTrace();
}
}

public static string ExtractHarmonyEnhancedStackTrace(StackTrace trace, bool forceRefresh, out int hash)
internal static string ExtractHarmonyEnhancedStackTrace(StackTrace trace, bool forceRefresh, out int hash)
{
var sb = new StringBuilder();
var traces = captured_traces(trace);
Expand All @@ -62,7 +43,7 @@ public static string ExtractHarmonyEnhancedStackTrace(StackTrace trace, bool for
_ = sb.AddHarmonyFrames(trace);
var stacktrace = sb.ToString();
hash = stacktrace.GetHashCode();
if (DisableStackTraceCaching)
if (HarmonyMain.noStacktraceCaching)
return stacktrace;
var hashRef = $"[Ref {hash:X}]";
if (forceRefresh)
Expand All @@ -73,7 +54,7 @@ public static string ExtractHarmonyEnhancedStackTrace(StackTrace trace, bool for
return $"{hashRef}\n{stacktrace}";
}

public static bool AddHarmonyFrames(this StringBuilder sb, StackTrace trace)
static bool AddHarmonyFrames(this StringBuilder sb, StackTrace trace)
{
if (trace.FrameCount == 0)
return false;
Expand Down Expand Up @@ -131,7 +112,7 @@ public static bool AddHarmonyFrames(this StringBuilder sb, StackTrace trace)
return true;
}

public static void AppendPatch(this StringBuilder sb, MethodBase method, IEnumerable<Patch> fixes, string name)
static void AppendPatch(this StringBuilder sb, MethodBase method, IEnumerable<Patch> fixes, string name)
{
foreach (var patch in PatchProcessor.GetSortedPatchMethods(method, fixes.ToArray()))
{
Expand Down
8 changes: 8 additions & 0 deletions Source/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.

using System.Diagnostics.CodeAnalysis;

[assembly: SuppressMessage("CodeQuality", "IDE0051:Remove unused private members", Justification = "Used by Harmony", Scope = "member", Target = "~M:HarmonyMod.Environment_GetStackTrace_Patch.Prefix(System.Exception,System.Boolean,System.String@)~System.Boolean")]
4 changes: 2 additions & 2 deletions Source/HarmonyRimWorld.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Krafs.Rimworld.Ref" Version="1.5.4048-beta" />
<PackageReference Include="Lib.Harmony" Version="2.3.3.0" />
<PackageReference Include="Krafs.Rimworld.Ref" Version="1.5.4060-beta" />
<PackageReference Include="Lib.Harmony" Version="2.3.3" />
<PackageReference Include="Microsoft.NETCore.Platforms" Version="7.0.4" />
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies.net472" Version="1.0.3">
<PrivateAssets>all</PrivateAssets>
Expand Down
41 changes: 32 additions & 9 deletions Source/Main.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using HarmonyLib;
using LudeonTK;
using RimWorld;
using System;
using System.Diagnostics;
using System.Reflection;
using UnityEngine;
using Verse;

Expand All @@ -10,8 +12,18 @@ namespace HarmonyMod
[StaticConstructorOnStartup]
public class HarmonyMain(ModContentPack content) : Mod(content)
{
[TweakValue("Harmony")]
public static bool noStacktraceCaching;
[TweakValue("Harmony")]
public static bool noStacktraceEnhancing;

public static Version harmonyVersion = default;

public static string modVersion = ((AssemblyFileVersionAttribute)Attribute.GetCustomAttribute(
Assembly.GetExecutingAssembly(),
typeof(AssemblyFileVersionAttribute), false)
).Version;

static HarmonyMain()
{
_ = Harmony.VersionInfo(out harmonyVersion);
Expand All @@ -21,23 +33,34 @@ static HarmonyMain()
}

[HarmonyPatch(typeof(VersionControl), nameof(VersionControl.DrawInfoInCorner))]
public static class VersionControl_DrawInfoInCorner_Patch
static class VersionControl_DrawInfoInCorner_Patch
{
public static void Postfix()
static void Postfix()
{
var str = $"Harmony v{HarmonyMain.harmonyVersion}";
Text.Font = GameFont.Small;
GUI.color = new Color(1f, 1f, 1f, 0.5f);
var rect = new Rect(10f, 58f, 330f, 20f);
Widgets.Label(rect, $"Harmony v{HarmonyMain.harmonyVersion}");
GUI.color = Color.white.ToTransparent(0.5f);
var size = Text.CalcSize(str);
var rect = new Rect(10f, 58f, size.x, size.y);
Widgets.Label(rect, str);
GUI.color = Color.white;
if (Mouse.IsOver(rect))
{
var tipSignal = new TipSignal($"Harmony Mod v{HarmonyMain.modVersion}");
TooltipHandler.TipRegion(rect, tipSignal);
Widgets.DrawHighlight(rect);
}
}
}

[HarmonyPatch(typeof(Environment), "GetStackTrace")]
public static class Environment_GetStackTrace_Patch
static class Environment_GetStackTrace_Patch
{
public static bool Prefix(Exception e, bool needFileInfo, ref string __result)
static bool Prefix(Exception e, bool needFileInfo, ref string __result)
{
if (HarmonyMain.noStacktraceEnhancing)
return true;

try
{
var stackTrace = e == null ? new StackTrace(needFileInfo) : new StackTrace(e, needFileInfo);
Expand All @@ -52,8 +75,8 @@ public static bool Prefix(Exception e, bool needFileInfo, ref string __result)
}

[HarmonyPatch(typeof(Log), nameof(Log.ResetMessageCount))]
public static class Log_ResetMessageCount_Patch
static class Log_ResetMessageCount_Patch
{
public static void Postfix() => ExceptionTools.seenStacktraces.Clear();
static void Postfix() => ExceptionTools.seenStacktraces.Clear();
}
}

0 comments on commit f73531d

Please sign in to comment.