Skip to content

Instantly share code, notes, and snippets.

@pardeike
Created March 29, 2024 14:15
Show Gist options
  • Save pardeike/6294f0f9d7b601c8860a8a2af1702969 to your computer and use it in GitHub Desktop.
Save pardeike/6294f0f9d7b601c8860a8a2af1702969 to your computer and use it in GitHub Desktop.
Changes in Harmony 2.3.3
void Main()
{
Test.Run();
var harmony = new Harmony("test");
harmony.PatchCategory("category1");
Test.Run();
harmony.PatchCategory("category2");
Test.Run();
harmony.PatchCategory("category3");
Test.Run();
harmony.UnpatchCategory("category1");
Test.Run();
harmony.UnpatchCategory("category2");
Test.Run();
harmony.UnpatchCategory("category3");
Test.Run();
}
public class Test
{
public static void Run()
{
try
{
new Test().Method();
}
catch (Exception ex)
{
Console.WriteLine($"Result: {ex.Message}");
}
Console.WriteLine("");
}
public void Method()
{
Console.WriteLine("Test");
throw new Exception("Test Exception");
}
}
[HarmonyPatchCategory("category1")]
[HarmonyPatch(typeof(Test), nameof(Test.Method))]
static class Patch1
{
static void Finalizer(Exception __exception)
{
Console.WriteLine($"Finalizer got {__exception.Message}");
}
}
[HarmonyPatchCategory("category2")]
[HarmonyPatch(typeof(Test), nameof(Test.Method))]
static class Patch2
{
static void Prefix()
{
Console.WriteLine($"Prefix");
}
static void Postfix()
{
Console.WriteLine($"Postfix");
}
}
[HarmonyPatchCategory("category3")]
[HarmonyPatch(typeof(Test), nameof(Test.Method))]
[HarmonyPriority(Priority.High)]
static class Patch3
{
static Exception Finalizer(Exception __exception)
{
Console.WriteLine($"High Prio Finalizer prevented {__exception.Message}");
return null;
}
}
/* Log:
Test
Result: Test Exception
Test
Finalizer got Test Exception
Result: Test Exception
Prefix
Test
Finalizer got Test Exception
Result: Test Exception
Prefix
Test
High Prio Finalizer prevented Test Exception
Prefix
Test
High Prio Finalizer prevented Test Exception
Test
High Prio Finalizer prevented Test Exception
Test
Result: Test Exception
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment