Skip to content

Pre & Post Patching with Attributes (Advanced)

PrimeSonic edited this page Aug 18, 2021 · 6 revisions

WARNING - The following patching features are not intended to be used by all mods.

Please check this list of guidelines to see if you should be using these features or if you only need Basic Mod Loading Setup.

Consider using PrePatching or PostPatching only if....

- Your mod is a library or API that handles requests from other mods
- Your mod intercepts or overrides requests from other mods
- Your mod must wait until other mods have initialized before running its own initialization
- Your mod must perform some initialization before other mods can start theirs

If your mod does not qualify with one or more of the above criteria, do not use this feature.

When does PrePatching and PostPatching happen?

Remember that mods are sorted before initialization.
So mods that act as libraries for other mods, like SMLHelper, will sink to the bottom of the load order, while mods that have no dependencies will bubble to the top.

After mods are sorted, all PrePatch methods are invoked first, followed by all normal Patch methods, and then all PostPatch methods are invoked last.
The order is not changed for this. So, for example, if Mod A is sorted to load before Mod B, then any PrePatch method from Mod A will be invoked before PrePatch methods of Mod B;
The same will be true of Patch methods and PostPatch methods.

This order is fixed once mods are sorted. So remember, all PrePatch and PostPatch methods are invoked in the same order that mods are sorted to load in.
Most mods won't be using PrePatch and PostPatch, so you should only need to consider load order relative to other mod libraries.

How do I set up [QModPrePatch] and [QModPostPatch]?

Just like the [QModPatch] attribute, there are two more attributes that you can use [QModPrePatch] and [QModPostPatch]to decorate any methods inside the class you decorated with [QModCore].

See this example

// Include this namespace to simplify the code below
using QModManager.API.ModLoading;

namespace MyMod
{
    // Your main patching class must have the QModCore attribute
    [QModCore]
    public static class MyInitializerClass
    {
        // Your patching method must have the QModPatch attribute
        [QModPrePatch]
        public static void MyPreInitializationMethod()
        {
            // Add any setup or precondition checks here
        }

        // Your patching method must have the QModPatch attribute
        [QModPatch]
        public static void MyMainInitializationMethod()
        {
            // Add your main patching code here
        }

        // Your patching method must have the QModPatch attribute
        [QModPostPatch]
        public static void MyPostInitializationMethod()
        {
            // Add any cleanup or priority patching code here
            // Use this only as a last resort if you need it to resolve cross-mod features or conflicts
        }
    }
}

Remember: you are allowed only one (1) patch method of each type.
So only one PrePatch, Patch, and PostPatch method.

  翻译: