PatchScripts

Harmony Mods ››
Parent Previous Next

PatchScripts are C# scripts which implement the IPatcherMod, which allows direct manipulation of the Assembly-CSharp.dll at build time. These insert changes directly into the main library, and use CECIL IL code.


They can add new reference hooks, remove existing code, and change the access levels of variables and methods.


Here is an example script, which implements IPatcherMod's Patch and Link. This script does not do any linking, but instead only changes the method "FeedInventoryData" to be a public method, inside the class "ItemActionUserOther". By setting the method to Public, we can access and make calls to that script from other parts of the script.

using System;

using SDX.Compiler;

using Mono.Cecil;

using Mono.Cecil.Cil;

using System.Linq;


public class ItemActionsChange : IPatcherMod

{

   public bool Patch(ModuleDefinition module)

   {

       Console.WriteLine("==ItemActions Patcher Patcher===");

       var gm = module.Types.First(d => d.Name == "ItemActionUseOther");

       var method = gm.NestedTypes.First(d => d.Name == "FeedInventoryData");

       method.IsNestedPublic = true;

       method.IsPublic = true;

       return true;

   }


   // Called after the patching process and after scripts are compiled.

   // Used to link references between both assemblies

   // Return true if successful

   public bool Link(ModuleDefinition gameModule, ModuleDefinition modModule)

   {

       return true;

   }


   // Helper functions to allow us to access and change variables that are otherwise unavailable.

   private void SetMethodToVirtual(MethodDefinition meth)

   {

       meth.IsVirtual = true;

   }


   private void SetFieldToPublic(FieldDefinition field)

   {

       field.IsFamily = false;

       field.IsPrivate = false;

       field.IsPublic = true;


   }

   private void SetMethodToPublic(MethodDefinition field)

   {

       field.IsFamily = false;

       field.IsPrivate = false;

       field.IsPublic = true;


   }

}


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