Targeting a method

Harmony Mods ›› Harmony ››
Parent Previous Next

Harmony targets methods to find which ones you want to make changes to. This uses the HarmonyPatch mechanism.


HarmonyPatch can be used in different combinations of formats to target your method.


The following guides can be used:


References a Class

[HarmonyPatch(typeof(NGuiWdwInGameHUD))]  

References a Method

[HarmonyPatch("SetTooltipText")]

References a parameter signature


[HarmonyPatch(new Type[] { typeof(string), typeof(string[]), typeof(string), typeof(ToolTipEvent) })]


If you are targeting a method that is not overloaded, then you would only need the first two: Class and Method. However, if the same method name exists with different parameters, you can add a reference to it.


In the above example, the parameter signature would be  SetTooltipText( string myString, string[] myStringarray, string anotherString, ToolTipEvent justTheTip ).


Here are some examples from the ClearUI Mod:


Vanilla:

protected override bool canShowOverlay(ItemActionData actionData)

   [HarmonyPatch(typeof(ItemActionDynamic))]

   [HarmonyPatch("canShowOverlay")]


There is only one canShowOverlay in the ItemActionDynamic class, so we only need to specify the method name.


In the next example, the SetToolTipText in the NGuiWdwInGameHUD is a bit different. There are actually 5 methods with the same name, with different parameters:


public void SetTooltipText(string _textstring _alertSound)

public void SetTooltipText(string _textstring _alertSoundToolTipEvent handler)

public void SetTooltipText(string _textstring _argstring _alertSound)

public void SetTooltipText(string _textstring _argstring _alertSoundToolTipEvent handler)

public void SetTooltipText(string _textstring[] _argsstring _alertSoundToolTipEvent eventHandler)

We want to target the last one, so we have to define the parameter signature.


Vanilla:

public void SetTooltipText(string _textstring[] _argsstring _alertSoundToolTipEvent eventHandler)

   [HarmonyPatch(typeof(NGuiWdwInGameHUD))]

   [HarmonyPatch("SetTooltipText")]

   [HarmonyPatch(new Type[] { typeof(string), typeof(string[]), typeof(string), typeof(ToolTipEvent) })]


These definition must exist on top of the class that contains your Harmony calls, such as Prefix() and Postfix(). Those classes themselves do not need to define an interface to IHarmony, and they can be called whatever you want, as long as they are unique.


   // Remove the SetLabel Text calls

   [HarmonyPatch(typeof(NGUIWindowManager))]

   [HarmonyPatch("SetLabelText")]

   [HarmonyPatch(new Type[] { typeof(EnumNGUIWindow), typeof(string), typeof(bool) })]

   public class SphereII_ClearUI_NGUIWindowManager_SetLabelText

   {

       static bool Prefix()

       {

           return false;

       }

   }



Created with the Personal Edition of HelpNDoc: Free help authoring tool