-
-
Notifications
You must be signed in to change notification settings - Fork 495
Home
A library for patching .NET assemblies during runtime.
If you develop in C# and your code is loaded as a module/plugin into a host application, you can use Harmony to alter the functionality of all the available assemblies of that application. Where other patch libraries simply allow you to replace the original method, Harmony goes one step further and gives you:
- A way to keep the original method intact
- Execute your code before and/or after the original method
- No conflicts with other Harmony patches
Harmony is designed to work with .NET 2.0 and is compatible with Mono which makes it a great way to develop extensions for Unity games. It has no other dependencies than .NET 2.0 and will most likely work in other environments too. Harmony was tested on PC, Mac and Linux and support 32- and 64-bit.
Here is a very short example on how to patch the method WindowStack.Add(Window)
in a mod for the game RimWorld so that it logs the window object to the games debug window:
class Main
{
static Main()
{
var harmony = HarmonyInstance.Create("net.pardeike.rimworld.example1");
harmony.PatchAll(typeof(Main).Module);
}
}
[HarmonyPatch(typeof(WindowStack))]
[HarmonyPatch("Add")]
[HarmonyPatch(new Type[] { typeof(Window) })]
class WriteLinePatch
{
static bool Prefix(WindowStack instance, ref Window window)
{
Log.Warning("Opening window " + window);
return true;
}
}
For more details, please refer to the following sections: