Solved Implementing Hooking in LGL mod menu

Status
Not open for further replies.

GODS

Solid & Active Platinian
Original poster
Aug 20, 2019
52
787
83
25
Isekai
Before open this thread I already observe and analyze hooking examples in LGL mod menu. I also read some tutorial here


Below is the result of my understanding from those tutorial

C++:
    // Token: 0x06000A27 RID: 2599 RVA: 0x00002050 File Offset: 0x00000250
    [Token(Token = "0x60007CB")]
    [Address(RVA = "0x4D7B38", Offset = "0x4D7B38", VA = "0x4D7B38")]
    public void AddCoin(int _amount)
    {
    }

C++:
int (*old_AddCoin)(void *instance);
int AddCoin(void *instance)
{
    if(instance != NULL)
    {
        return 100100100;
    }
    return old_AddCoin(instance);
}
MsHookFunction((void*)getAbsoluteAddress(0x4D7B38), (void*)AddCoin, (void**)&old_AddCoin);
Before I ask the real question, I wanna let you know that I'm kinda new to LGL mod menu but I'm not a complete noob. Why? Because I only work with hexpatching before
but some function require me to use hooking instead of patching.

Now come to the question. Where to put my code in main.cpp?

Here I can see there are some hooking example in main.cpp

C++:
bool feature1, feature2, featureHookToggle, Health;
int sliderValue = 1, level = 0;
void *instanceBtn;


void (*AddMoneyExample)(void *instance, int amount);


bool (*old_get_BoolExample)(void *instance);
bool get_BoolExample(void *instance) {
    if (instance != NULL && featureHookToggle) {
        return true;
    }
    return old_get_BoolExample(instance);
}

float (*old_get_FloatExample)(void *instance);
float get_FloatExample(void *instance) {
    if (instance != NULL && sliderValue > 1) {
        return (float) sliderValue;
    }
    return old_get_FloatExample(instance);
}

int (*old_Level)(void *instance);
int Level(void *instance) {
    if (instance != NULL && level) {
        return (int) level;
    }
    return old_Level(instance);
}

void (*old_Update)(void *instance);
void Update(void *instance) {
    instanceBtn = instance;
    return old_Update(instance);
}

//Field offset hooking
void (*old_HealthUpdate)(void *instance);
void HealthUpdate(void *instance) {
    if (instance != NULL) {
        if (Health) {
            *(int *) ((uint64_t) instance + 0x48) = 999;
        }
    }
    return old_HealthUpdate(instance);
}
I can just put my code there right? but I don't think I should put Mshookfunction there too
C++:
MsHookFunction((void*)getAbsoluteAddress(0x4D7B38), (void*)AddCoin, (void**)&old_AddCoin);
And how to make the case for my code?
 

Numark

Awesome Active Platinian
May 23, 2017
116
929
193
Your AddCoin type data is a void, not an int. Returning the value of a void will do nothing, you need to make a function pointer of AddCoin. What you did there is wrong.

There should be a method called Update, we will use this because it calls 60 fps in our hack, which is good and fast.

Just make an Update Hook, make AddCoins a function pointer and place your AddCoins in update. Figure out the rest since there's many tutorials threads here.
 

HizroMxDz

1/3 Games Approved
Dec 25, 2019
92
98
53
x____x
Don't use return for void data types, they don't return anything. Also, if your function has additional parameters which you want to modify, make sure to include them, so this is how you would do it for your function:
C++:
void (*old_AddCoin)(void *instance, int _amount); // Your function is void, so use void, not int
void AddCoin(void *instance, int _amount)
{
    if(instance != NULL)
    {
        old_AddCoin(instance, 9999); /* If you want to just modify parameters of a function, just use the old_FunctionName and change whatever parameter to whatever you like.
        Notice how I don't use return, and just call the function, that's because it's a void data type */
    }
    old_AddCoin(instance, _amount); // If instance is NULL or something else wrong, call the original function with the **Unedited** parameters
}
MsHookFunction((void*)getAbsoluteAddress(targetLibName, 0x4D7B38), (void*) AddCoin, (void**) &old_AddCoin); // MSHOOKFunction
Also, since you wanna use this with a toggle, just create a bool inside main.cpp below the Memory Patches:
C++:
bool coins = false;
make a toggle feature for it:
C++:
OBFUSCATE("Toggle_Unlimited Coins"),
And inside of the hook, make sure to check for the hack like this using your bool:
C++:
void (*old_AddCoin)(void *instance, int _amount); // Your function is void, so use void, not int
void AddCoin(void *instance, int _amount)
{
    if(instance != NULL && coins) // Only execute the code inside the if statement when the user turns on the toggle (Use the bool you created)
    {
        old_AddCoin(instance, 9999); /* If you want to just modify parameters of a function, just use the old_FunctionName and change whatever parameter to whatever you like.
        Notice how I don't use return, and just call the function, that's because it's a void data type */
    }
    old_AddCoin(instance, _amount); // If instance is NULL or something else wrong, call the original function with the **Unedited** parameters
}
MsHookFunction((void*)getAbsoluteAddress(targetLibName, 0x4D7B38), (void*) AddCoin, (void**) &old_AddCoin); // MSHOOKFunction
And finally, add case like this:
C++:
case 0: // The case number is based off the feature number, in this case this is our first toggle so it's 0, The category above doesn't count
coins = !coins;
break;
 

GODS

Solid & Active Platinian
Original poster
Aug 20, 2019
52
787
83
25
Isekai
Your AddCoin type data is a void, not an int. Returning the value of a void will do nothing, you need to make a function pointer of AddCoin. What you did there is wrong.

There should be a method called Update, we will use this because it calls 60 fps in our hack, which is good and fast.

Just make an Update Hook, make AddCoins a function pointer and place your AddCoins in update. Figure out the rest since there's many tutorials threads here.
yeah I found Update and late update function. Anyway thank you for helping me
 
  • Like
Reactions: HizroMxDz

GODS

Solid & Active Platinian
Original poster
Aug 20, 2019
52
787
83
25
Isekai
Don't use return for void data types, they don't return anything. Also, if your function has additional parameters which you want to modify, make sure to include them, so this is how you would do it for your function:
C++:
void (*old_AddCoin)(void *instance, int _amount); // Your function is void, so use void, not int
void AddCoin(void *instance, int _amount)
{
    if(instance != NULL)
    {
        old_AddCoin(instance, 9999); /* If you want to just modify parameters of a function, just use the old_FunctionName and change whatever parameter to whatever you like.
        Notice how I don't use return, and just call the function, that's because it's a void data type */
    }
    old_AddCoin(instance, _amount); // If instance is NULL or something else wrong, call the original function with the **Unedited** parameters
}
MsHookFunction((void*)getAbsoluteAddress(targetLibName, 0x4D7B38), (void*) AddCoin, (void**) &old_AddCoin); // MSHOOKFunction
Also, since you wanna use this with a toggle, just create a bool inside main.cpp below the Memory Patches:
C++:
bool coins = false;
make a toggle feature for it:
C++:
OBFUSCATE("Toggle_Unlimited Coins"),
And inside of the hook, make sure to check for the hack like this using your bool:
C++:
void (*old_AddCoin)(void *instance, int _amount); // Your function is void, so use void, not int
void AddCoin(void *instance, int _amount)
{
    if(instance != NULL && coins) // Only execute the code inside the if statement when the user turns on the toggle (Use the bool you created)
    {
        old_AddCoin(instance, 9999); /* If you want to just modify parameters of a function, just use the old_FunctionName and change whatever parameter to whatever you like.
        Notice how I don't use return, and just call the function, that's because it's a void data type */
    }
    old_AddCoin(instance, _amount); // If instance is NULL or something else wrong, call the original function with the **Unedited** parameters
}
MsHookFunction((void*)getAbsoluteAddress(targetLibName, 0x4D7B38), (void*) AddCoin, (void**) &old_AddCoin); // MSHOOKFunction
And finally, add case like this:
C++:
case 0: // The case number is based off the feature number, in this case this is our first toggle so it's 0, The category above doesn't count
coins = !coins;
break;
Woah thanks man, Now I understand the concept already. Very informative and helpful. :face03:
 
  • Like
Reactions: HizroMxDz

GODS

Solid & Active Platinian
Original poster
Aug 20, 2019
52
787
83
25
Isekai
how if the class is not exist in dump.cs or csharp assembly ? but it exist in il2cpp ? how to hook it ?
I guess you only need the function and it offset. Doesn't matter from which file you found it as long as it generated from your dumper it is fine. Right now I stop on hooking and learn c++ from w3school.
 
  • Like
Reactions: HizroMxDz
Status
Not open for further replies.