fix: Harmony finalizers return void, so errors from other mods keep their stack trace

An Exception-returning finalizer makes Harmony throw the exception again instead of rethrowing it, which cuts the stack trace at the patched method. All ten finalizers only passed the exception on, so they now return void. A smoke test fails if a finalizer returns a value again. Version 1.2.2.
This commit is contained in:
Fabian @ Blax Software 2026-12-02 18:11:50 +01:00
parent ef7d7f86b0
commit ccaf4ac01b
6 changed files with 31 additions and 21 deletions

Binary file not shown.

Binary file not shown.

View File

@ -3,7 +3,7 @@
<name>Blax Slaves</name> <name>Blax Slaves</name>
<author>Blax Gaming</author> <author>Blax Gaming</author>
<packageId>blax.slaves</packageId> <packageId>blax.slaves</packageId>
<modVersion>1.2.1</modVersion> <modVersion>1.2.2</modVersion>
<supportedVersions> <supportedVersions>
<li>1.7</li> <li>1.7</li>
</supportedVersions> </supportedVersions>

View File

@ -1,5 +1,9 @@
# Changelog # Changelog
## 1.2.2
- Errors from other mods or the game in the hooked methods now keep their full stack trace, so they no longer look like Blax Slaves errors.
## 1.2.1 ## 1.2.1
- Works with any DLC combination, core only included. Without Ideology the slave features stay off and their settings show greyed out with a "needs Ideology" note; every trader still buys your prisoners, and the slaver changes still apply. - Works with any DLC combination, core only included. Without Ideology the slave features stay off and their settings show greyed out with a "needs Ideology" note; every trader still buys your prisoners, and the slaver changes still apply.

View File

@ -129,10 +129,9 @@ namespace BlaxSlaves
{ {
public static void Prefix(Pawn victim, out SlaveHarm.Frame __state) => __state = SlaveHarm.Push(victim); public static void Prefix(Pawn victim, out SlaveHarm.Frame __state) => __state = SlaveHarm.Push(victim);
public static Exception Finalizer(Exception __exception, SlaveHarm.Frame __state) public static void Finalizer(SlaveHarm.Frame __state)
{ {
SlaveHarm.Pop(__state); SlaveHarm.Pop(__state);
return __exception;
} }
} }
@ -141,10 +140,9 @@ namespace BlaxSlaves
{ {
public static void Prefix(Pawn victim, out SlaveHarm.Frame __state) => __state = SlaveHarm.Push(victim); public static void Prefix(Pawn victim, out SlaveHarm.Frame __state) => __state = SlaveHarm.Push(victim);
public static Exception Finalizer(Exception __exception, SlaveHarm.Frame __state) public static void Finalizer(SlaveHarm.Frame __state)
{ {
SlaveHarm.Pop(__state); SlaveHarm.Pop(__state);
return __exception;
} }
} }
@ -161,10 +159,9 @@ namespace BlaxSlaves
__state.protectedVictim = false; __state.protectedVictim = false;
} }
public static Exception Finalizer(Exception __exception, SlaveHarm.Frame __state) public static void Finalizer(SlaveHarm.Frame __state)
{ {
SlaveHarm.Pop(__state); SlaveHarm.Pop(__state);
return __exception;
} }
} }
@ -179,10 +176,9 @@ namespace BlaxSlaves
__state.protectedVictim = false; __state.protectedVictim = false;
} }
public static Exception Finalizer(Exception __exception, SlaveHarm.Frame __state) public static void Finalizer(SlaveHarm.Frame __state)
{ {
SlaveHarm.Pop(__state); SlaveHarm.Pop(__state);
return __exception;
} }
} }
@ -192,10 +188,9 @@ namespace BlaxSlaves
{ {
public static void Prefix(Pawn patient, out SlaveHarm.Frame __state) => __state = SlaveHarm.Push(patient); public static void Prefix(Pawn patient, out SlaveHarm.Frame __state) => __state = SlaveHarm.Push(patient);
public static Exception Finalizer(Exception __exception, SlaveHarm.Frame __state) public static void Finalizer(SlaveHarm.Frame __state)
{ {
SlaveHarm.Pop(__state); SlaveHarm.Pop(__state);
return __exception;
} }
} }
@ -218,10 +213,9 @@ namespace BlaxSlaves
} }
} }
public static Exception Finalizer(Exception __exception, SlaveHarm.Frame __state) public static void Finalizer(SlaveHarm.Frame __state)
{ {
SlaveHarm.Pop(__state); SlaveHarm.Pop(__state);
return __exception;
} }
} }
@ -256,10 +250,9 @@ namespace BlaxSlaves
soldInGift = queue; soldInGift = queue;
} }
public static Exception Finalizer(Exception __exception, Queue<Pawn> __state) public static void Finalizer(Queue<Pawn> __state)
{ {
soldInGift = __state; soldInGift = __state;
return __exception;
} }
} }
@ -302,10 +295,9 @@ namespace BlaxSlaves
} }
} }
public static Exception Finalizer(Exception __exception, SlaveHarm.Frame __state) public static void Finalizer(SlaveHarm.Frame __state)
{ {
SlaveHarm.Pop(__state); SlaveHarm.Pop(__state);
return __exception;
} }
} }
@ -343,10 +335,9 @@ namespace BlaxSlaves
public static void Prefix() => depth++; public static void Prefix() => depth++;
public static Exception Finalizer(Exception __exception) public static void Finalizer()
{ {
depth--; depth--;
return __exception;
} }
} }
@ -431,10 +422,9 @@ namespace BlaxSlaves
} }
} }
public static Exception FrameFinalizer(Exception __exception, SlaveHarm.Frame __state) public static void FrameFinalizer(SlaveHarm.Frame __state)
{ {
SlaveHarm.Pop(__state); SlaveHarm.Pop(__state);
return __exception;
} }
public static void NotPendingPostfix(Pawn pawn, ref bool __result) public static void NotPendingPostfix(Pawn pawn, ref bool __result)

View File

@ -28,6 +28,22 @@ namespace BlaxSlavesTests
T.Check(warnings.Count == 0, "warnings mentioning Blax Slaves at startup: " + string.Join(" | ", warnings.Take(3).Select(e => e.text))); T.Check(warnings.Count == 0, "warnings mentioning Blax Slaves at startup: " + string.Join(" | ", warnings.Take(3).Select(e => e.text)));
} }
[BlaxTest("startup: every Blax Slaves finalizer returns void, so Harmony rethrows exceptions with their stack trace")]
public static void FinalizersReturnVoid()
{
// One finalizer returning Exception on a method makes Harmony "throw ex" instead of "rethrow"
// (0Harmony EmitFinalizers), which cuts the inner frames off every exception passing through it.
var finalizers = Harmony.GetAllPatchedMethods()
.Select(Harmony.GetPatchInfo)
.Where(info => info != null)
.SelectMany(info => info.Finalizers)
.Where(p => p.owner == "blax.slaves")
.ToList();
T.Check(finalizers.Count >= 9, "Blax Slaves finalizers found: " + finalizers.Count + ", expected at least 9");
var bad = finalizers.Where(p => p.PatchMethod.ReturnType != typeof(void)).Select(p => p.PatchMethod.DeclaringType?.Name + "." + p.PatchMethod.Name).ToList();
T.Check(bad.Count == 0, "finalizers that return a value: " + string.Join(", ", bad));
}
[BlaxTest("startup: settings and defs loaded")] [BlaxTest("startup: settings and defs loaded")]
public static void DefsLoaded() public static void DefsLoaded()
{ {