Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to patch methods using harmony

Tags:

c#

class

patch

How do you replace a method with parameters at runtime using Harmony in c#?

like image 586
jj5 Avatar asked May 24 '26 16:05

jj5


1 Answers

There are multiple ways of replacing a method with Harmony. The most common one is adding a prefix that returns false and therefore skips the original.

Example:

// this is the original method you want to replace
class TheClass {
   string TheOriginal(int foo, List<string> bar) { … }
}

// I will skip the basic setup of Harmony and only show you the prefix
static bool Prefix(int foo, List<string> bar, ref string __result, TheClass __instance)
{
   // access “this” in the original
  __instance.SomeOtherMethod();

   // use originals input parameters
   Log(bar);

   // return your own result
   __result = ”…”;

   // return false to skip the original
   return false;
}
like image 96
Andreas Pardeike Avatar answered May 27 '26 05:05

Andreas Pardeike



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!