Native Hooking

Native Hooking is a C‍+‍+ exclusive feature of SML that allows you to attach a custom function body to an existing C‍+‍+ function.

All C/C‍+‍+ functioning hooking stuff can be found in SML’s Patching/NativeHookManager.h.

Hooking of Blueprint-implemented functions is covered on the Blueprint Hooking System page.

Background Information

The hooking system SML is using is just C / C‍+‍+ function hooking, as Unreal Engine is C‍+‍+. This system can theoretically be used on any executable, but you need to know the location of functions in memory, which is difficult. For general C‍+‍+ executables, and most UE games, you only have a .exe, which provides no information about where functions are located, so to figure that out you need very good reverse engineering skills, and it needs to be done on an exe-by-exe basis, so if the game updates, everything breaks.

What’s different about Satisfactory is that it provides PDBs, which normally are used to determine the call stack when a crash happens, but they can also be used in the other direction: to find a function address based on the name.

Finding a function address based on the name is the technique that SML v1 used to implement hooking. The trouble there is that many functions may be stripped, or inlined, which makes C‍+‍+ modding in general harder, but also complicates hooking, as methods might suddenly disappear and be embedded into the place they are called from.

At our request, Coffee Stain enabled Modular Build in Update 4, which means each Unreal Engine module is a dll on its own. This means that all functions are exported, so their address can be natively found by the linker, and also that no function can be inlined. Modular builds also enable the native Unreal Engine mod support (i.e., loading mod plugins from the Mods folder).

C‍+‍+ Function Hooks

C‍+‍+ hooks, also known as native hooks, allow changing the behavior of game and engine functions implemented in C‍+‍+ without modifying their source code.

SML’s hooking interface provides distinct 3 ways of hooking functions, each of which have two types of call order.

If multiple hooks are attached to the same function, these hooks will then get called in the order they were registered.

There is a normal hook that gets called before the actual function gets called. Through this hook, you are able to prevent the final function call and you are also able to overwrite the return value. If you cancel the execution of the final function, you can prevent the following hooks from being called. Keep in mind that this means that another hook can prevent your hooks from being called by Satisfactory. The normal hook’s signature is void(TCallScope<HookFuncSignature>&, hookFuncParams). If you hook a member function, the this pointer is handled like a parameter and is the first parameter used. As long as you don’t cancel the final function execution, or do it yourself by calling the scope object, the final function will be implicitly called after your hook function returns.

The call scope object allows you to:

  • Cancel the final function execution (if the hook function returns void).

    void hook(TCallScope<...>& Scope, class* exampleClass, int exampleArg) {
     Scope.Cancel();
    }
  • Call the next hooks and the final function within your body. Calling the scope need to have all the same parameters as the func hook signature.

    void hook(TCallScope<int(int)>& Scope, class* exampleClass, int exampleArg) {
    	// stuff before final function call
    	int result = Scope(exampleClass, exampleArg); // call following hooks (final function might get called as long as following hooks don't cancel/overwrite it)
    	// stuff after final function call
    }
  • You can also override the return value before the final call (causes the final call to not occur)

    void hook(TCallScope<int(int)>& Scope, class* exampleClass, int exampleArg) {
    	// final function might get called
    	Scope.Override(customReturnValue);
    	// final function wont get called anymore
    }

Since you still want to make sure your hook gets called, no care about whether the final function got called or not we introduce the "after" hooks. These hooks get all called after the normal hook calls and only allow you to read the parameters as well as the resulting return value. That means you can’t influence the final function call. Also, don’t use the TCallScope object, instead the first parameter of your hooks signature is the return value followed by the function call parameters.

void hook(int returnValue, int exampleArg) {
	// do some stuff
}

The 2 Types of Hooks

By 'hook types' we mean the different ways of attaching a hook to a function. Each attachment method works differently under the hood, and it’s important to pay attention to the key differences between the different types of hooks.

Be aware that the type of return values and parameters etc has nothing to do with each other or if it is a member function, you can use them in any way. Note that the Hook function is a std::function, which means that it can be any type a std::function can accept, such as function pointers, function pointers with bound placeholders, or even lambdas.

The behavior of hooks at editor time is highly unpredictable and can cause crashes. As such, you should ensure that your hooks are not applied at editor time, else you could potentially be unable to open the Unreal Editor until you edit the hook code externally.

The most convenient way is to put the SUBSCRIBE_ macros in your code inside an if (!WITH_EDITOR) { …​ } block:

if (!WITH_EDITOR) {
	SUBSCRIBE_METHOD(SomeClass::SomeMethod, &Hook_SomeMethod);
}

Using #if !WITH_EDITOR and #endif directives are also an option, but it is not recommended: they hide errors until building shipping and confuse IDEs, making development and debugging slightly more annoying with no benefit.

Type: SUBSCRIBE_METHOD

The SUBSCRIBE_METHOD-Macro attaches a hook such that the code you pass will be called before the function executes.

If multiple mods have subscribed to the same function, the hooks will be called in the order they were registered.

Usage goes as follows:

// in target class SomeClass
public:
	int MemberFunction(int arg1);
	static void StaticFunction();

// in your code
#include "Patching/NativeHookManager.h"

void registerHooks() {
	SUBSCRIBE_METHOD(SomeClass::MemberFunction, [](auto& scope, SomeClass* self, int arg1) {
		// do some nice stuff there
	});

	SUBSCRIBE_METHOD(SomeClass::StaticFunction, [](auto& scope) {
		// do some nice stuff there
	});
}

Hooking an overloaded function might not work as intended since the compiler has no clue what exact symbol you now want to hook. For that, you should have a look at the SUBSCRIBE_METHOD_MANUAL-Macro which allows you to explicitly set the symbol you want to hook.

Type: SUBSCRIBE_METHOD_VIRTUAL

The SUBSCRIBE_METHOD_VIRTUAL macro attaches the given hook to the function passed by pointer for the given class.

This hook only modifies the function that the virtual table for the given class points to. Functions in subclasses overriding the virtual function of the given class won’t be modified, but the hook will still run if the hooked function is called by the overriding implementation (i.e. "calls super"). If the overriding implementation of a subclass does not "call super", you have to hook said subclass separately. Because pure virtual functions do not have a proper function body, they cannot possibly be hooked.

Usage goes as follows:

// in target parent class SomeClass
public:
	virtual int MemberFunction(int arg1);

// in child class SomeChild that we don't want to hook
// class SomeChild : public SomeClass
public:
	virtual int MemberFunction(int arg1) override;

// in your code
#include "Patching/NativeHookManager.h"

void registerHooks() {
	SomeClass* SampleObject = GetMutableDefault<SomeClass>(); // For UObject derived classes, use SUBSCRIBE_UOBJECT_METHOD instead
	SUBSCRIBE_METHOD_VIRTUAL(SomeClass::MemberFunction, SampleObject, [](auto& scope, SomeClass* self, int arg1) {
		// do some nice stuff there
	});

	SomeClass parent;
	parent->MemberFunction(0); // hook gets called
	SomeChild c;
	c->MemberFunction(1); // hook does not get called
}

Special Cases

Depending on the type of function you are attempting to hook and what you want to do with it, you may need to make some adjustments.

Const Functions

When hooking a const function you will need to prefix the "self" pointer with const.

Is Const? Format

Non-Const

(auto& scope, SomeClass* self)

Const

(auto& scope, const SomeClass* self)

Hooking AFTER

For "after" hooks, add the _AFTER postfix to the macro names.

Be aware that the hook function signature changes accordingly and no longer needs the "scope".

The below examples are for non-virtual functions. For virtual functions, use SUBSCRIBE_METHOD_VIRTUAL_AFTER instead of SUBSCRIBE_METHOD_AFTER.

Return? Parameters? Format

SUBSCRIBE_METHOD_AFTER(SomeClass::MemberFunction, [](SomeClass* self))

✔️

SUBSCRIBE_METHOD_AFTER(SomeClass::MemberFunction, [](auto returnValue, SomeClass* self))

✔️

SUBSCRIBE_METHOD_AFTER(SomeClass::MemberFunction, [](SomeClass* self, int arg1, int arg2))

✔️

✔️

SUBSCRIBE_METHOD_AFTER(SomeClass::MemberFunction, [](auto returnValue, SomeClass* self, int arg1, int arg2))

FORCEINLINE Functions

Functions that are FORCEINLINE cannot be hooked.

UFUNCTIONs

A function being a UFUNCTION or not makes no difference on whether it can be hooked.

Unhooking

Unhooking functionality has not been extensively tested. Please report issues you encounter on the Discord.

Macros will return a delegate that can be used with the UNSUBSCRIBE_METHOD or UNSUBSCRIBE_UOBJECT_METHOD macro respectively in order to unsubscribe from the function.

Blueprint Function Hooks

Hooking Blueprint-implemented functions from C‍+‍+ is no longer supported as of SML 3.11.0 for the reasons described here. Use the Blueprint Hooking System instead.

If you’d like to implement the code your hooks run in C‍+‍+, write the code in a blueprint function library or similar and call the functions from the hook blueprint.

Protected/Private Function Hooking

If the function you are attempting to hook is protected or private to that specific class, you must use the friend declaration.

This also means that you can only hook this function from a class, not global scope.

For example, let’s assume you have a class called MyWatcher in a namespace called MyMod, and you wish to hook the function EnterChatMessage from AFGPlayerController class.

The suggested method of doing this is with Access Transformers. In your AccessTransformers.ini file you would create the entry:

Friend=(Class="AFGPlayerController", FriendClass="MyWatcher")

Alternatively, you can edit the header files directly. This is not advisable for reasons described in more detail on the Access Transformers page. You must first edit the FGPlayerController.h header and add the following block of code to it:

namespace MyMod
{
	class MyWatcher;
}

Then you have to add the friend declaration to the class itself, in result, it should look like this:

...

class FACTORYGAME_API AFGPlayerController : public AFGPlayerControllerBase
{
	GENERATED_BODY()
public:
	friend MyMod::MyWatcher;

...
}