Finding & Creating a simple hook

OwnProx

Moderator
Plugin Developer
Messages
700
Likes
351
Points
63
What is a Hook? a hook is a way intercepting a function when it gets called by the server it will execute your hook before returning back to the original function giving you the ability to modify the return or manipulate the arguments or do your own task

Automatic Hook Generator Tool (Easy Method)
https://arkserverapi.com/threads/arkapi-c-hook-generator-tool.761/





Manual Hooks (Hard Method)

These are the header files of where you can find such functions to hook:
https://github.com/Michidu/ARK-Server-API/tree/master/version/Core/Public/API/ARK



Finding A Hook

in Actor.h you notice this structure is called APrimalCharacter and you want to hook damage function so ctrl + f search: APrimalCharacter.TakeDamage

you will find this
float TakeDamage(float Damage, FDamageEvent * DamageEvent, AController * EventInstigator, AActor * DamageCauser) { return NativeCall<float, float, FDamageEvent *, AController *, AActor *>(this, "APrimalCharacter.TakeDamage", Damage, DamageEvent, EventInstigator, DamageCauser); }


Declaring A Hook

now this part is pretty simple you declare your hook at the top of your page just below the #include's

DECLARE_HOOK(APrimalCharacter_TakeDamage, float, APrimalCharacter*, float, FDamageEvent*, AController*, AActor*);

lets explain the above declare hook:

Argument 0: (APrimalCharacter_TakeDamage): is the Declaration name this can be anything but your hook function must relate to it
Argument 1: The return type of the function you want to hook (float TakeDamage) as you see here it starts with float so its return type is a float
Argument 2: The Pointer to the class we found the function in, notice the TakeDamage(float Damage, FDamageEvent * DamageEvent, AController * EventInstigator, AActor * DamageCauser) { return NativeCall<float, float, FDamageEvent *, AController *, AActor *>(this, "APrimalCharacter.TakeDamage", Damage, DamageEvent, EventInstigator, DamageCauser); }
if you look "APrimalCharacter.TakeDamage", so you know it will be APrimalCharacter* (you could also scroll up to the top of the struct to see it)
Argument 3-6: these are the arguments that are in TakeDamage Function, TakeDamage(float Damage, FDamageEvent * DamageEvent, AController * EventInstigator, AActor * DamageCauser)

so notice here ^^ float Damage so thats a float then, FDamageEvent*, then AController*, then AActor* ^^ all arguments from up there


Creating the function for the hook to call (2 Examples Below)

Creating our function (with No Damage)
float _cdecl Hook_APrimalCharacter_TakeDamage(APrimalCharacter* _this, float Damage, FDamageEvent* DamageEvent, AController* EventInstigator
, AActor* DamageCauser)
{
return 0;
}

Creating our function (Normal Damage)
float _cdecl Hook_APrimalCharacter_TakeDamage(APrimalCharacter* _this, float Damage, FDamageEvent* DamageEvent, AController* EventInstigator
, AActor* DamageCauser)
{
return APrimalCharacter_TakeDamage_original(_this, Damage, DamageEvent, EventInstigator, DamageCauser);
}


Adding the hook

Setting the hook (notice in the TakeDamage function we want to hook ("APrimalCharacter.TakeDamage")) you need this as the first argument for below and the second argument is your custom function:
ArkApi::GetHooks().SetHook("APrimalCharacter.TakeDamage", &Hook_APrimalCharacter_TakeDamage, &APrimalCharacter_TakeDamage_original);

Removing the Hook
Removing the hook (useful for plugin unloading):
ArkApi::GetHooks().DisableHook("APrimalCharacter.TakeDamage", &Hook_APrimalCharacter_TakeDamage);
 
Last edited:
Advertisement

Monetise Your Game Server

Want to monetise your ARK Server? Create your own webstore at tebex.io

Top