Modding:Harmony

From Caves of Qud Wiki
Revision as of 20:38, 13 February 2021 by Egocarib (talk | contribs)
Jump to navigation Jump to search
This article is a stub. You can help Caves of Qud Wiki by expanding it.
This article is a stub. You can help Caves of Qud Wiki by expanding it.

For best results, it's recommended to have read the following topics before this one:

Harmony is a library for dynamically patching C# code. In the context of Caves of Qud modding, it's useful for changing the behavior of things that are not explicitly exposed through the game's modding interface. Harmony is included in the base game, and as such doesn't require any external mods to use.

For a primer on using Harmony to patch game methods, see this article.

Example: Chaotic-Colored Message Text

Chaotic-colored message text created by the example harmony patch.

Create somefile.cs in your .../Mods/ModName/ directory, with the following code:

using HarmonyLib;

namespace YourMod.HarmonyPatches
{
    [HarmonyPatch(typeof(XRL.Messages.MessageQueue))]
    class YourPatch1
    {
        [HarmonyPrefix]
        [HarmonyPatch("Add")]
        static void Prefix(ref string Message)
        {
            Message = "{{chaotic|" + Message + "}}";
        }
    }
}