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 four finalizers only passed the exception on, so they now return void. A smoke test fails if a finalizer returns a value again. Version 1.0.1.
This commit is contained in:
Fabian @ Blax Software 2026-12-02 18:11:52 +01:00
parent 88b02ca502
commit aaa12082c4
7 changed files with 29 additions and 12 deletions

Binary file not shown.

Binary file not shown.

View File

@ -3,7 +3,7 @@
<name>Blax Castes</name> <name>Blax Castes</name>
<author>Blax Gaming</author> <author>Blax Gaming</author>
<packageId>blax.castes</packageId> <packageId>blax.castes</packageId>
<modVersion>1.0.0</modVersion> <modVersion>1.0.1</modVersion>
<supportedVersions> <supportedVersions>
<li>1.7</li> <li>1.7</li>
</supportedVersions> </supportedVersions>

View File

@ -1,5 +1,9 @@
# Changelog # Changelog
## 1.0.1
- Errors from other mods or the game in the hooked methods now keep their full stack trace, so they no longer look like Blax Castes errors.
## 1.0.0 ## 1.0.0
- Castes: a caste bundles an apparel, food, drug and reading policy, a medical care level, a schedule, work priorities and, with [1001] Weapons Tab Reborn, a weapon loadout. Putting a pawn in a caste sets every slot the caste manages in one step. Prisoners get the food policy and medical care only; quest lodgers skip apparel, drugs, reading and the schedule; babies skip food. - Castes: a caste bundles an apparel, food, drug and reading policy, a medical care level, a schedule, work priorities and, with [1001] Weapons Tab Reborn, a weapon loadout. Putting a pawn in a caste sets every slot the caste manages in one step. Prisoners get the food policy and medical care only; quest lodgers skip apparel, drugs, reading and the schedule; babies skip food.

View File

@ -312,10 +312,9 @@ namespace BlaxCastes
{ {
public static void Prefix(Pawn __instance, out LeaveState __state) => __state = JoinHooks.BeforeLeaving(__instance); public static void Prefix(Pawn __instance, out LeaveState __state) => __state = JoinHooks.BeforeLeaving(__instance);
public static Exception Finalizer(Exception __exception, LeaveState __state) public static void Finalizer(LeaveState __state)
{ {
JoinHooks.AfterLeaving(__state); JoinHooks.AfterLeaving(__state);
return __exception;
} }
} }
@ -328,10 +327,9 @@ namespace BlaxCastes
{ {
public static void Prefix(Map map, out List<LeaveState> __state) => __state = JoinHooks.BeforeMapClosed(map); public static void Prefix(Map map, out List<LeaveState> __state) => __state = JoinHooks.BeforeMapClosed(map);
public static Exception Finalizer(Exception __exception, List<LeaveState> __state) public static void Finalizer(List<LeaveState> __state)
{ {
JoinHooks.AfterMapClosed(__state); JoinHooks.AfterMapClosed(__state);
return __exception;
} }
} }
@ -348,10 +346,9 @@ namespace BlaxCastes
{ {
public static void Prefix(Map map, out List<LeaveState> __state) => __state = JoinHooks.BeforePawnsLeave(map?.mapPawns?.AllPawnsSpawned); public static void Prefix(Map map, out List<LeaveState> __state) => __state = JoinHooks.BeforePawnsLeave(map?.mapPawns?.AllPawnsSpawned);
public static Exception Finalizer(Exception __exception, List<LeaveState> __state) public static void Finalizer(List<LeaveState> __state)
{ {
JoinHooks.AfterMapClosed(__state); JoinHooks.AfterMapClosed(__state);
return __exception;
} }
} }
@ -360,7 +357,8 @@ namespace BlaxCastes
/// and Trade as Slave or Colonist only set guest.joinStatus before this call, so the final status /// and Trade as Slave or Colonist only set guest.joinStatus before this call, so the final status
/// (slave or not) picks the row. Selling: a "sold" scope keeps the slave-to-free step inside the sale /// (slave or not) picks the row. Selling: a "sold" scope keeps the slave-to-free step inside the sale
/// from queueing a join, and the finalizer writes the former-caste record with the status the pawn /// from queueing a join, and the finalizer writes the former-caste record with the status the pawn
/// had before the sale. The finalizer pops the scope even when PreTraded throws. /// had before the sale. The finalizer pops the scope even when PreTraded throws. It only reads
/// __exception and returns void, so Harmony rethrows the original exception with its stack trace.
/// </summary> /// </summary>
[HarmonyPatch(typeof(Pawn), nameof(Pawn.PreTraded))] [HarmonyPatch(typeof(Pawn), nameof(Pawn.PreTraded))]
internal static class Patch_Pawn_PreTraded internal static class Patch_Pawn_PreTraded
@ -388,7 +386,7 @@ namespace BlaxCastes
} }
} }
public static Exception Finalizer(Exception __exception, Pawn __instance, TradeState __state) public static void Finalizer(Exception __exception, Pawn __instance, TradeState __state)
{ {
try try
{ {
@ -400,7 +398,6 @@ namespace BlaxCastes
{ {
JoinHooks.LogOnce("Trade scope", e); JoinHooks.LogOnce("Trade scope", e);
} }
return __exception;
} }
} }
} }

View File

@ -39,6 +39,22 @@ namespace BlaxCastesTests
T.Check(warnings.Count == 0, "Blax Castes warnings at startup: " + string.Join(" | ", warnings.Take(3))); T.Check(warnings.Count == 0, "Blax Castes warnings at startup: " + string.Join(" | ", warnings.Take(3)));
} }
[BlaxTest("startup: every Blax Castes 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 == BlaxCastesStartup.HarmonyId)
.ToList();
T.Check(finalizers.Count >= 4, "Blax Castes finalizers found: " + finalizers.Count + ", expected at least 4");
var bad = finalizers.Where(p => p.PatchMethod.ReturnType != typeof(void)).Select(p => p.PatchMethod.DeclaringType?.Name).ToList();
T.Check(bad.Count == 0, "finalizers that return a value: " + string.Join(", ", bad));
}
[BlaxTest("startup: settings, game component and strings are loaded")] [BlaxTest("startup: settings, game component and strings are loaded")]
public static void Loaded() public static void Loaded()
{ {

View File

@ -736,7 +736,7 @@ namespace BlaxCastesTests
} }
finally finally
{ {
AccessTools.Method(patch, "Finalizer").Invoke(null, new object[] { null, prefixArgs[1] }); AccessTools.Method(patch, "Finalizer").Invoke(null, new object[] { prefixArgs[1] });
} }
T.Equal(0, JoinScopes.Depth, "open join scopes after the map closed"); T.Equal(0, JoinScopes.Depth, "open join scopes after the map closed");
T.Check(!JoinDefaults.IsQueued(p), "the lost slave was queued (" + JoinDefaults.QueuedFor(p)?.reason + ")"); T.Check(!JoinDefaults.IsQueued(p), "the lost slave was queued (" + JoinDefaults.QueuedFor(p)?.reason + ")");
@ -764,7 +764,7 @@ namespace BlaxCastesTests
} }
finally finally
{ {
AccessTools.Method(patch, "Finalizer").Invoke(null, new object[] { null, prefixArgs[1] }); AccessTools.Method(patch, "Finalizer").Invoke(null, new object[] { prefixArgs[1] });
} }
T.Equal(0, JoinScopes.Depth, "open join scopes after " + method); T.Equal(0, JoinScopes.Depth, "open join scopes after " + method);
} }