DMT Harmony Format

Harmony Mods ›› Harmony ››
Parent Previous Next

DMT defines how an IHarmony script is detected and loaded into memory. Harmony scripts must use the interface IHarmony and contains a Start() to initialize.


Here is an example of the NerdPole Harmony script. The highlighted yellow parts are required for each Harmony script you write. The other content will change depending on what the script will do.


using Harmony;

using System.Reflection;

using UnityEngine;

using DMT;


[HarmonyPatch(typeof(Block))]

[HarmonyPatch("PlaceBlock")]

public class SphereII_NerdPoll : IHarmony

{

   public void Start()

   {

       Debug.Log(" Loading Patch: " + GetType().ToString());

       var harmony = HarmonyInstance.Create(GetType().ToString());

       harmony.PatchAll(Assembly.GetExecutingAssembly());

   }


   // Returns true for the default PlaceBlock code to execute. If it returns false, it won't execute it at all.

   static bool Prefix(EntityAlive _ea)

   {

       Debug.Log("Prefix PlaceBox");

       EntityPlayerLocal player = _ea as EntityPlayerLocal;

       if(player == null)

           return true;


       if(player.IsGodMode == true)

           return true;


       if(player.IsFlyMode == true)

           return true;


       if(player.IsInElevator())

           return true;


       if(player.IsInWater())

           return true;


       // If you aren't on the ground, don't place the block.

       if(!player.onGround)

           return false;


       return true;

   }

}


The above example contains a single class that inherits from the IHarmony interface, it has a Start() to initialize, and it has a single Prefix(). The class itself specifies the HarmonyPatch targets, which will be explained in the next section.


DMT Harmony also supports sub-classes to allow more flexibility.


In the below example, we have a top level ClearUI class. Inside of that, we have a sub-class that inherits from IHarmony and contains our Start() logic. Since the ClearUI mod targets many different methods, we can make subclasses for each method. The single Start() in the ClearUI_Init will initialize all the classes in the class ClearUI. This lets us avoid having to define a Start() for each method we want to target.


public class ClearUI

{

   public class ClearUI_Init : IHarmony

   {

       public void Start()

       {

           Debug.Log(" Loading Patch: " + GetType().ToString());

           var harmony = HarmonyInstance.Create(GetType().ToString());

           harmony.PatchAll(Assembly.GetExecutingAssembly());

       }

   }


   // Sneak Damage pop up

   [HarmonyPatch(typeof(EntityPlayerLocal))]

   [HarmonyPatch("NotifySneakDamage")]

   public class SphereII_ClearUI_NotifySneakDamage

   {

       static bool Prefix()

       {

           return false;

       }

   }

}

Created with the Personal Edition of HelpNDoc: Create iPhone web-based documentation