-
-
Notifications
You must be signed in to change notification settings - Fork 515
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 system method Console.WriteLine
in a mod for the game RimWorld so that its output also goes to the games debug window:
class Main
{
static Main()
{
var harmony = HarmonyInstance.Create("net.pardeike.rimworld.example1");
harmony.PatchAll(typeof(Main).Module);
}
}
[HarmonyPatch(typeof(Console))]
[HarmonyPatch("WriteLine")]
class WriteLinePatch
{
static void Postfix(ref string msg)
{
Log.Warning(msg); // log to RimWorlds debug window
}
}
For more details, please refer to the following sections: