Skip to main content Link Search Menu Expand Document (external link)

Advanced Modding using Harmony

Great care has been taken to design, refactor, and implement an API that gives you the flexibility to implement any number of mods that affect game code.

However there’s always limits to what you can do with what Jump King exposes, and in these cases Harmony is an option you can leverage to push the limits of your creativity!

Harmony

Harmony is an Open Source library for patching, replacing, and decorating .NET functions during the runtime.

You can use Harmony to have custom code run before, after, or instead of any methods in Jump King, and is how a lot of the early mods for the game were written.

Learn more about Harmony here: https://github.com/pardeike/Harmony

Learn more about how to use Harmony here: https://harmony.pardeike.net/articles/intro.html

Patching Etiquette

You should always assume that a player may have more than just your mod installed, so try to prevent implementing any patching logic that could stop other modder’s code from running.

For example, you could add a patch method to run before the JumpState.DoJump function. At the end of your function you can return true to allow the original DoJump (or any other patches) to continue, or false to prevent it.

Now your mod may have an appropriate reason to stop the player from jumping (maybe they are meant to be in some form of cutscene or stuck state for your mod) but keep in mind this could prevent other mods from having their code executed.

Adding Harmony to a mod

To add Harmony in your mod project, you can follow one of the following two ways.

Adding Harmony as a NuGet

  1. Open your mod project in Visual Studio.
  2. From the right-side of your Visual Studio interface, you should see the Solution Explorer with a tree view.

    Tree view

  3. Right-click on the project you want to add Harmony to and click on ‘Manage NuGet Packages…’.

    Nuget option

    If you don’t see this option, make sure you are not currently debugging your mod.

    If nothing happens, it might be because the solution file is missing, which can be easily generated by:

    • clicking the multiple floppy disks displayed as Save All or, Save sln
    • clicking Ctrl Shift S on your keyboard or,
    • by clicking File then Save All. Save sln

    After doing so, a prompt will ask you to save the SLN file, clicking Save will solve this issue. Saving SLN file

  4. Once clicked, a new page will display. You should be on the Browse tab. If not, change it to Browse. Browse tab

  5. Click on the search bar (or hold Ctrl+L to focus on it) and type Harmony. Search tab

  6. From the results, click on Lib.Harmony by Andreas Pardeike. Search found Harmony

  7. Click Install, then Apply (to apply changes). From the Output you can see its install progress.

    Details tab Agree to terms Output log

  8. Once finished, you can easily access Harmony from your file using the HarmonyLib namespace like so:

     [BeforeLevelLoad]
     public static void BeforeLevelLoad()
     {
         // initializes harmony
         Harmony harmony = new Harmony("phoenixx19.Localmod.Harmony");
    
     // configuration dependent snippet
     // if it's DEBUG, then the containing code it will be found in the Debug build
     #if DEBUG
    
         // this will enable you to use breakpoints & debug inside Visual Studio
         Debugger.Launch();
    
         // this enables harmony to log everything related to patching
         Harmony.DEBUG = true;
     #endif
    
         // your awesome code here...
     }
    

Examples of mods that use Harmony