Help! How to hook bool method

C++:
bool (*old_AddItem)(void *instance, monoString *item_id, int item_value);
bool AddItem((*old_AddItem)(void *instance, monoString *item_id, int item_value) {
    if(instance!=nullptr) {
        return true;
    }
    return old_AddItem(instance, item_id, item_value);
}
 
  • Like
Reactions: BrokeBroky
C++:
bool (*old_AddItem)(void *instance, monoString *item_id, int item_value);
bool AddItem((*old_AddItem)(void *instance, monoString *item_id, int item_value) {
    if(instance!=nullptr) {
        return true;
    }
    return old_AddItem(instance, item_id, item_value);
}
How to fix?
 

Attachments

  • Screenshot_20240903-114242.jpg
    Screenshot_20240903-114242.jpg
    121.5 KB · Views: 8
  • Screenshot_20240903-114210.jpg
    Screenshot_20240903-114210.jpg
    52.2 KB · Views: 7
How to fix?
You need a monoString struct. Add a new file in your Includes folder called mono.h then paste this code inside...

C++:
typedef struct _monoString
{
    void *klass;
    void *monitor;
    int length;
    char chars[1];

    int getLength()
    {
        return length;
    }

    char *getChars()
    {
        return chars;
    }
} monoString;

monoString *CreateString(const char *str)
{
    monoString *(*CreateString)(void *instance, const char *str, int start, int length) = (monoString * (*)(void *, const char *, int, int)) getAbsoluteAddress(0xOFFSET); //offset here
    int length = (int)strlen(str);
    return CreateString(NULL, str, 0, length);
}

Replace where it says 0xOFFSET with the correct offset from your dump...

private string CreateString(sbyte* value, int startIndex, int length) {}

Then add this line to main.cpp at top...
C++:
#include "Includes/Mono.h"
 
  • Like
Reactions: BrokeBroky
You need a monoString struct. Add a new file in your Includes folder called mono.h then paste this code inside...

C++:
typedef struct _monoString
{
    void *klass;
    void *monitor;
    int length;
    char chars[1];

    int getLength()
    {
        return length;
    }

    char *getChars()
    {
        return chars;
    }
} monoString;

monoString *CreateString(const char *str)
{
    monoString *(*CreateString)(void *instance, const char *str, int start, int length) = (monoString * (*)(void *, const char *, int, int)) getAbsoluteAddress(0xOFFSET); //offset here
    int length = (int)strlen(str);
    return CreateString(NULL, str, 0, length);
}

Replace where it says 0xOFFSET with the correct offset from your dump...

private string CreateString(sbyte* value, int startIndex, int length) {}

Then add this line to main.cpp at top...
C++:
#include "Includes/Mono.h"
Where to download Mono.h, in LGL Mod Menu 3.2 there is no Mono.h
 

Attachments

  • Screenshot_20240903-120839.jpg
    Screenshot_20240903-120839.jpg
    33.6 KB · Views: 5
Also I made typo in the hook, here fixed...

C++:
bool (*old_AddItem)(void *instance, monoString *item_id, int item_value);
bool AddItem(void *instance, monoString *item_id, int item_value) {
    if(instance!=nullptr) {
        return true;
    }
    return old_AddItem(instance, item_id, item_value);
}
 
  • Like
Reactions: BrokeBroky
Help, sorry to bother you
I made a typo in the monoString struct, replace it with this...

C++:
typedef struct _monoString
{
    void *klass;
    void *monitor;
    int length;
    char chars[1];

    int getLength()
    {
        return length;
    }

    char *getChars()
    {
        return chars;
    }
} monoString;

monoString *CreateString(const char *str)
{
    monoString *(*CreateString)(void *instance, const char *str, int start, int length) = (monoString * (*)(void *, const char *, int, int)) getAbsoluteAddress("libil2cpp.so", 0xOFFSET); //offset here
    int length = (int)strlen(str);
    return CreateString(NULL, str, 0, length);
}

Remember to replace 0xOFFSET with correct offset
 
  • Like
Reactions: BrokeBroky
Also forget the bool hook that I made previously, I thought you just wanted to return true or false value.
You need to hook AddItem method like this...

C++:
bool (*AddItem)(void *instance, monoString *item_id, int item_value);

Then find an update method (preferably from the same class as AddItem method, this can be Update, FixedUdate, or LateUpdate...

C++:
void (*old_Update)(void *instance);
void Update(void *instance) {
 if(instance!=nullptr) {
  if(IncludeItem) {
   AddItem(instance, (CreateString("faith")), 50);
   IncludeItem=false;
  }
 }
 old_Update(instance);
}

Then under hackthread...
C++:
AddItem = (bool(*)(void*, monoString*, int)) getAbsoluteAddress(targetLibName, 0x61B174);

HOOK("0xOFFSET", Update, old_Update);
 
  • Like
Reactions: BrokeBroky
Also forget the bool hook that I made previously, I thought you just wanted to return true or false value.
You need to hook AddItem method like this...

C++:
bool (*AddItem)(void *instance, monoString *item_id, int item_value);

Then find an update method (preferably from the same class as AddItem method, this can be Update, FixedUdate, or LateUpdate...

C++:
void (*old_Update)(void *instance);
void Update(void *instance) {
 if(instance!=nullptr) {
  if(IncludeItem) {
   AddItem(instance, (CreateString("faith")), 50);
   IncludeItem=false;
  }
 }
 old_Update(instance);
}

Then under hackthread...
C++:
AddItem = (bool(*)(void*, monoString*, int)) getAbsoluteAddress(targetLibName, 0x61B174);

HOOK("0xOFFSET", Update, old_Update);
It's okay, I was wrong for not telling you what I wanted to do at the beginning. and thank you for helping.
then how to fix this, error in mono.h
 

Attachments

  • Screenshot_20240903-135858.jpg
    Screenshot_20240903-135858.jpg
    28 KB · Views: 10
  • Screenshot_20240903-135931.jpg
    Screenshot_20240903-135931.jpg
    13.4 KB · Views: 8
I also want to ask something, what if I use the update in another class. what code should be changed?
What do you mean? Like an update from a different class than the class AddItem is in?
You don't need to change any code just the update offset, you can technically use update from any class when hooking regular methods but sometimes they don't all work which is why it's best to use update from the same class as the method you're hooking