BNM-Android icon indicating copy to clipboard operation
BNM-Android copied to clipboard

I need help.

Open trangocuong opened this issue 1 year ago • 3 comments

Hello. I'm new to hooking on Android and I want to translate a game that I'm currently playing. However, when I try to hook it, I can't figure out why my code isn't running. my code:

#include <EGL/egl.h> // need to make a common.h that contains all these headers cuz this is nasty

#include <GLES2/gl2.h> #include <GLES2/gl2ext.h> #include <GLES2/gl2platform.h> #include #include #include "nlohmann/json.hpp" #include "http/cpr/cpr.h" #include "Misc/Logging.h" #include "ImGui/imgui.h" #include "ImGui/imgui_impl_android.h" #include "ImGui/imgui_impl_opengl3.h" #include "Obfuscation/Obfuscate.h" #include #include <android/native_window_jni.h> #include <jni.h> #include #include <unistd.h> #include <pthread.h> #include <dlfcn.h> #include "Misc/JNIStuff.h" #include "Misc/FileWrapper.h" #include "Misc/Utils.h" #include <BNM/Utils.hpp> #include "ByNameModding/src/private/Internals.hpp" #include "Memory/KittyScanner.hpp" #include "Obfuscation/Custom_Obfuscate.h" #include "Unity/Unity.h" #include "Misc/FunctionPointers.h" #include "Hooking/Hooks.h" #include "Misc/ImGuiStuff.h" #include "Menu.h" #include "Hooking/JNIHooks.h" #include "Unity/Screen.h" #include "Memory/KittyMemory.hpp" #include "Unity/Input.h" // the private version held by certain polarmodders has image loading and a lot more

#include <BNM/UserSettings/GlobalSettings.hpp>

#include <BNM/Class.hpp> #include <BNM/Field.hpp> #include <BNM/Method.hpp> #include <BNM/Property.hpp> #include <BNM/Defaults.hpp> #include <BNM/Operators.hpp> #include <BNM/BasicMonoStructures.hpp>

using namespace BNM::Operators;

BNM::Class ObjectClass{}; // Variable for storing property BNM::Property<BNM::Structures::Mono::String *> ObjectName{}; // Variable for storing method BNM::Method<BNM::Structures::Mono::String *> ObjectToString{};

BNM::Class AbyssBuffButtonClass{}; BNM::Class TextMeshProUGUIClass{}; // Variable for storing property

BNM::Field field_sold{}; BNM::Field<void *> field_description{};

// Methods for TextMeshProUGUI BNM::Method<BNM::Structures::Mono::String *> GetTextMethod{}; BNM::Method SetTextMethod{};

KittyScanner::ElfScanner g_il2cppELF{};

void *KittyMemoryFinder(const char *name, void *data) { auto &scanner = *(KittyScanner::ElfScanner *) data; // auto &scanner = g_il2cppELF; return (void *) scanner.findSymbol(name); }

void Load_BNM_Finder() { BNM::Loading::SetMethodFinder(KittyMemoryFinder, (void *) &g_il2cppELF);

//BNM::Loading::AllowedLateInitHook();

g_il2cppELF = KittyScanner::ElfScanner::createWithPath("libil2cpp.so");

auto result = BNM::Loading::TryLoadByUsersFinder();
BNM_LOG_DEBUG("07: %d", (int) result);

}

EGLBoolean (*old_eglSwapBuffers)(...); EGLBoolean new_eglSwapBuffers(EGLDisplay _display, EGLSurface _surface) { SetupImGui(); Menu::DrawImGui();

return old_eglSwapBuffers(_display, _surface);

}

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void * reserved) { jvm = vm; JNIEnv *env; vm->GetEnv((void **) &env, JNI_VERSION_1_6);

BNM::Loading::TryLoadByJNI(env);
BNM_LOG_INFO("test");
return JNI_VERSION_1_6;

} void (*old_SetType)(void *instance); void SetType_Hook(void *instance) { old_SetType(instance);

if (!instance) {
    BNM_LOG_ERR("AbyssBuffButton instance is null!");
    return;
}

bool isSold = field_sold[instance];
void *descObj = field_description[instance];
if (!descObj) {
    BNM_LOG_ERR("Description object is null!");
    return;
}

BNM::Structures::Mono::String *originalText = GetTextMethod(descObj);
if (!originalText) {
    BNM_LOG_ERR("Failed to get description text!");
    return;
}

if (!isSold) {
    std::string text = originalText->str();
    std::string::size_type pos = text.find(':');
    std::string afterColon = (pos != std::string::npos) ? text.substr(pos + 1) : text;
    afterColon.erase(0, afterColon.find_first_not_of(' '));
    SetTextMethod(descObj, BNM::CreateMonoString(afterColon));
} else {
    SetTextMethod(descObj, BNM::CreateMonoString(BNM_OBFUSCATE("Sold out")));
}

}

void run(){ using namespace BNM; // To avoid writing BNM:: in this method BNM_LOG_INFO("Starting BNM initialization"); // Get the UnityEngine class.Object

AbyssBuffButtonClass = Class("", "AbyssBuffButton");
if (!AbyssBuffButtonClass) {
    BNM_LOG_ERR("Failed to find AbyssBuffButton in default namespace, trying alternative");
    AbyssBuffButtonClass = Class("GameObjects.ButtonObjects", "AbyssBuffButton");
    if (!AbyssBuffButtonClass) {
        BNM_LOG_ERR("Failed to find AbyssBuffButton in GameObjects.ButtonObjects");
    }
}
BNM_LOG_INFO("Found AbyssBuffButton class");

// Get TMPro.TextMeshProUGUI class
TextMeshProUGUIClass = Class("TMPro", "TextMeshProUGUI");
if (!TextMeshProUGUIClass) {
    BNM_LOG_ERR("Failed to find TMPro.TextMeshProUGUI class!");
}

// Get fields
field_sold = AbyssBuffButtonClass.GetField("sold");
if (!field_sold) {
    BNM_LOG_ERR("Failed to find sold field!");
}
field_description = AbyssBuffButtonClass.GetField("description");
if (!field_description) {
    BNM_LOG_ERR("Failed to find description field!");
}

// Get TextMeshProUGUI methods
GetTextMethod = TextMeshProUGUIClass.GetMethod("get_text", 0);
SetTextMethod = TextMeshProUGUIClass.GetMethod("set_text", 1);

// Get and hook SetType method
auto SetTypeMethod = AbyssBuffButtonClass.GetMethod("SetType");

InvokeHook(SetTypeMethod, SetType_Hook, old_SetType);

}

void *hack_thread(void *) { using namespace BNM;

do {
    sleep(1);
} while (!IsLoaded());

Load_BNM_Finder();

AttachIl2Cpp(); // this is required when you use bynamemodding functions
Unity::Screen::Setup();

Unity::Input::Setup();

Pointers::LoadPointers();
BNM::Loading::AddOnLoadedEvent(run);
DetachIl2Cpp(); // remember to detach when you are done using bynamemodding functions
return nullptr;

} attribute((constructor)) void lib_main() { auto eglhandle = dlopen(OBFUSCATE("libEGL.so"), RTLD_LAZY); const char *dlopen_error = dlerror(); if (dlopen_error) { eglhandle = dlopen(OBFUSCATE("libunity.so"), RTLD_LAZY); // I have no idea if this works it was just to me that it would fix crashes so I did it really quickly } auto eglSwapBuffers = dlsym(eglhandle, OBFUSCATE("eglSwapBuffers")); const char *dlsym_error = dlerror(); if (dlsym_error) { LOGE(OBFUSCATE("Cannot load symbol 'eglSwapBuffers': %s"), dlsym_error); } else { hook(eglSwapBuffers, (void *) new_eglSwapBuffers, (void **) &old_eglSwapBuffers); }

pthread_t ptid;
pthread_create(&ptid, nullptr, hack_thread, nullptr);

}

log:

05-07 13:42:43.116 D/ByNameModding(25855): [SetupBNM] il2cpp::vm::Class::Init in lib: 0x13412e4. 05-07 13:42:43.116 D/ByNameModding(25855): [SetupBNM] code has il2cpp_image_get_class. BNM will use it. 05-07 13:42:43.116 D/ByNameModding(25855): [SetupBNM] il2cpp::vm::Class::FromIl2CppType in lib: 0x1340e3c. 05-07 13:42:43.116 D/ByNameModding(25855): [SetupBNM] il2cpp::vm::Type::GetClassOrElementClass in lib: 0x1335b5c. 05-07 13:42:43.116 D/ByNameModding(25855): [SetupBNM] il2cpp::vm::Image::FromName in lib: 0x13323a4. 05-07 13:42:43.116 D/ByNameModding(25855): [SetupBNM] il2cpp::vm::Assembly::GetAllAssemblies in lib: 0x135cd80. 05-07 13:42:43.123 D/ByNameModding(25855): [CreateClass] Target: [BNM.Coroutine]::[IEnumerator]::[Assembly-CSharp.dll] 05-07 13:42:43.124 D/ByNameModding(25855): [CreateClass] Target: [BNM.Coroutine]::[CustomWait]::[Assembly-CSharp.dll]

It doesn't throw any errors, but it also doesn't run, so I have no idea how to fix it.

trangocuong avatar May 07 '25 06:05 trangocuong

How have useless thing and wrong calls. Fixed version:

#include <EGL/egl.h> // need to make a common.h that contains all these headers cuz this is nasty

#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <GLES2/gl2platform.h>
#include <fstream>
#include <sstream>
#include "nlohmann/json.hpp"
#include "http/cpr/cpr.h"
#include "Misc/Logging.h"
#include "ImGui/imgui.h"
#include "ImGui/imgui_impl_android.h"
#include "ImGui/imgui_impl_opengl3.h"
#include "Obfuscation/Obfuscate.h"
#include <cstdio>
#include <android/native_window_jni.h>
#include <jni.h>
#include <string>
#include <unistd.h>
#include <pthread.h>
#include <dlfcn.h>
#include "Misc/JNIStuff.h"
#include "Misc/FileWrapper.h"
#include "Misc/Utils.h"
#include <BNM/Utils.hpp>
#include "ByNameModding/src/private/Internals.hpp"
#include "Memory/KittyScanner.hpp"
#include "Obfuscation/Custom_Obfuscate.h"
#include "Unity/Unity.h"
#include "Misc/FunctionPointers.h"
#include "Hooking/Hooks.h"
#include "Misc/ImGuiStuff.h"
#include "Menu.h"
#include "Hooking/JNIHooks.h"
#include "Unity/Screen.h"
#include "Memory/KittyMemory.hpp"
#include "Unity/Input.h"
// the private version held by certain polarmodders has image loading and a lot more

#include <BNM/UserSettings/GlobalSettings.hpp>

#include <BNM/Class.hpp>
#include <BNM/Field.hpp>
#include <BNM/Method.hpp>
#include <BNM/Property.hpp>
#include <BNM/Defaults.hpp>
#include <BNM/Operators.hpp>
#include <BNM/BasicMonoStructures.hpp>

using namespace BNM::Operators;

BNM::Class ObjectClass{};
// Variable for storing property
BNM::Property<BNM::Structures::Mono::String *ObjectName{};
// Variable for storing method
BNM::Method<BNM::Structures::Mono::String *ObjectToString{};

BNM::Class AbyssBuffButtonClass{};
BNM::Class TextMeshProUGUIClass{};
// Variable for storing property

BNM::Field<boolfield_sold{};
BNM::Field<void *field_description{};

// Methods for TextMeshProUGUI
BNM::Method<BNM::Structures::Mono::String *GetTextMethod{};
BNM::Method<voidSetTextMethod{};

EGLBoolean (*old_eglSwapBuffers)(...);
EGLBoolean new_eglSwapBuffers(EGLDisplay _display, EGLSurface _surface) {
    SetupImGui();
    Menu::DrawImGui();

    return old_eglSwapBuffers(_display, _surface);
}

void run();

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void * reserved) {
    jvm = vm;
    JNIEnv *env;
    vm->GetEnv((void **) &env, JNI_VERSION_1_6);

    BNM::Loading::TryLoadByJNI(env);
    BNM::Loading::AddOnLoadedEvent(run);
	    
    BNM_LOG_INFO("test");
    return JNI_VERSION_1_6;
}

void (*old_SetType)(void *instance);
void SetType_Hook(void *instance) {
    old_SetType(instance);

    if (!instance) {
        BNM_LOG_ERR("AbyssBuffButton instance is null!");
        return;
    }

    bool isSold = field_sold[instance];
    void *descObj = field_description[instance];
    if (!descObj) {
        BNM_LOG_ERR("Description object is null!");
        return;
    }

    BNM::Structures::Mono::String *originalText = GetTextMethod(descObj);
    if (!originalText) {
        BNM_LOG_ERR("Failed to get description text!");
        return;
    }

    if (!isSold) {
        std::string text = originalText->str();
        std::string::size_type pos = text.find(':');
        std::string afterColon = (pos != std::string::npos) ? text.substr(pos + 1) : text;
        afterColon.erase(0, afterColon.find_first_not_of(' '));
        SetTextMethod(descObj, BNM::CreateMonoString(afterColon));
    } else {
        SetTextMethod(descObj, BNM::CreateMonoString(BNM_OBFUSCATE("Sold out")));
    }
}

void run() {
    using namespace BNM; // To avoid writing BNM:: in this method
	
    Unity::Screen::Setup();

    Unity::Input::Setup();

    Pointers::LoadPointers();
	
    BNM_LOG_INFO("Starting BNM initialization");
    // Get the UnityEngine class.Object


    AbyssBuffButtonClass = Class("", "AbyssBuffButton");
    if (!AbyssBuffButtonClass) {
        BNM_LOG_ERR("Failed to find AbyssBuffButton in default namespace, trying alternative");
        AbyssBuffButtonClass = Class("GameObjects.ButtonObjects", "AbyssBuffButton");
        if (!AbyssBuffButtonClass) {
            BNM_LOG_ERR("Failed to find AbyssBuffButton in GameObjects.ButtonObjects");
        }
    }
    BNM_LOG_INFO("Found AbyssBuffButton class");

    // Get TMPro.TextMeshProUGUI class
    TextMeshProUGUIClass = Class("TMPro", "TextMeshProUGUI");
    if (!TextMeshProUGUIClass) {
        BNM_LOG_ERR("Failed to find TMPro.TextMeshProUGUI class!");
    }

    // Get fields
    field_sold = AbyssBuffButtonClass.GetField("sold");
    if (!field_sold) {
        BNM_LOG_ERR("Failed to find sold field!");
    }
    field_description = AbyssBuffButtonClass.GetField("description");
    if (!field_description) {
        BNM_LOG_ERR("Failed to find description field!");
    }

    // Get TextMeshProUGUI methods
    GetTextMethod = TextMeshProUGUIClass.GetMethod("get_text", 0);
    SetTextMethod = TextMeshProUGUIClass.GetMethod("set_text", 1);

    // Get and hook SetType method
    auto SetTypeMethod = AbyssBuffButtonClass.GetMethod("SetType");

    InvokeHook(SetTypeMethod, SetType_Hook, old_SetType);
}


__attribute__((constructor))
void lib_main() {
    auto eglhandle = dlopen(OBFUSCATE("libEGL.so"), RTLD_LAZY);
    const char *dlopen_error = dlerror();
    if (dlopen_error) {
        eglhandle = dlopen(OBFUSCATE("libunity.so"), RTLD_LAZY); // I have no idea if this works it was just to me that it would fix crashes so I did it really quickly
    }
    auto eglSwapBuffers = dlsym(eglhandle, OBFUSCATE("eglSwapBuffers"));
    const char *dlsym_error = dlerror();
    if (dlsym_error) {
        LOGE(OBFUSCATE("Cannot load symbol 'eglSwapBuffers': %s"), dlsym_error);
    } else {
        hook(eglSwapBuffers, (void *) new_eglSwapBuffers, (void **) &old_eglSwapBuffers);
    }
}

BNM-Dev avatar May 07 '25 10:05 BNM-Dev

Thank you very much, the code is working now. However, I can't retrieve the text from the description. log:

05-08 08:28:48.679 I/ByNameModding(16534): test
05-08 08:28:49.003 D/ByNameModding(16534): [SetupBNM] il2cpp::vm::Class::Init in lib: 0x13412e4.
05-08 08:28:49.003 D/ByNameModding(16534): [SetupBNM] code has il2cpp_image_get_class. BNM will use it.
05-08 08:28:49.003 D/ByNameModding(16534): [SetupBNM] il2cpp::vm::Class::FromIl2CppType in lib: 0x1340e3c.
05-08 08:28:49.003 D/ByNameModding(16534): [SetupBNM] il2cpp::vm::Type::GetClassOrElementClass in lib: 0x1335b5c.
05-08 08:28:49.003 D/ByNameModding(16534): [SetupBNM] il2cpp::vm::Image::FromName in lib: 0x13323a4.
05-08 08:28:49.004 D/ByNameModding(16534): [SetupBNM] il2cpp::vm::Assembly::GetAllAssemblies in lib: 0x135cd80.
05-08 08:28:49.011 D/ByNameModding(16534): [CreateClass] Target: [BNM.Coroutine]::[IEnumerator]::[Assembly-CSharp.dll]
05-08 08:28:49.011 D/ByNameModding(16534): 	Added method Finalize 0.
05-08 08:28:49.012 D/ByNameModding(16534): 		Overridden method - [mscorlib.dll]::[System]::[Void] [mscorlib.dll]::[System]::[Object].(Finalize){arguments count: 0}.
05-08 08:28:49.012 D/ByNameModding(16534): 	Added method MoveNext 0.
05-08 08:28:49.012 D/ByNameModding(16534): 		Overridden method - [mscorlib.dll]::[System]::[Boolean] [mscorlib.dll]::[System.Collections]::[IEnumerator].(MoveNext){arguments count: 0}.
05-08 08:28:49.012 D/ByNameModding(16534): 	Added method Reset 0.
05-08 08:28:49.012 D/ByNameModding(16534): 		Overridden method - [mscorlib.dll]::[System]::[Void] [mscorlib.dll]::[System.Collections]::[IEnumerator].(Reset){arguments count: 0}.
05-08 08:28:49.012 D/ByNameModding(16534): 	Added method get_Current 0.
05-08 08:28:49.012 D/ByNameModding(16534): 		Overridden method - [mscorlib.dll]::[System]::[Object] [mscorlib.dll]::[System.Collections]::[IEnumerator].(get_Current){arguments count: 0}.
05-08 08:28:49.012 D/ByNameModding(16534): [CreateClass] Target: [BNM.Coroutine]::[CustomWait]::[Assembly-CSharp.dll]
05-08 08:28:49.012 D/ByNameModding(16534): 	Added method Finalize 0.
05-08 08:28:49.012 D/ByNameModding(16534): 		Overridden method - [mscorlib.dll]::[System]::[Void] [mscorlib.dll]::[System]::[Object].(Finalize){arguments count: 0}.
05-08 08:28:49.012 D/ByNameModding(16534): 	Added method MoveNext 0.
05-08 08:28:49.012 D/ByNameModding(16534): 		Overridden method - [mscorlib.dll]::[System]::[Boolean] [mscorlib.dll]::[System.Collections]::[IEnumerator].(MoveNext){arguments count: 0}.
05-08 08:28:49.012 D/ByNameModding(16534): 	Added method Reset 0.
05-08 08:28:49.012 D/ByNameModding(16534): 		Overridden method - [mscorlib.dll]::[System]::[Void] [mscorlib.dll]::[System.Collections]::[IEnumerator].(Reset){arguments count: 0}.
05-08 08:28:49.012 D/ByNameModding(16534): 	Added method get_Current 0.
05-08 08:28:49.012 D/ByNameModding(16534): 		Overridden method - [mscorlib.dll]::[System]::[Object] [mscorlib.dll]::[System.Collections]::[IEnumerator].(get_Current){arguments count: 0}.
05-08 08:28:49.014 I/ByNameModding(16534): Starting BNM initialization
05-08 08:28:49.014 I/ByNameModding(16534): Found AbyssBuffButton class
05-08 08:28:49.014 I/ByNameModding(16534): ok
05-08 08:28:49.015 I/ByNameModding(16534): ok
05-08 08:29:01.833 I/ByNameModding(16534): run
05-08 08:29:01.833 W/ByNameModding(16534): An attempt to call [mscorlib.dll]::[System]::[String] [Unity.TextMeshPro.dll]::[TMPro]::[TMP_Text].(get_text){arguments count: 0} with an incorrect number of arguments... I hope you know what you're doing. BNM cannot add MethodInfo to arguments :(. Please fix it.
05-08 08:29:01.833 E/ByNameModding(16534): You cannot call the instance [mscorlib.dll]::[System]::[String] [Unity.TextMeshPro.dll]::[TMPro]::[TMP_Text].(get_text){arguments count: 0} method without instance! Please set instance before calling the method.
05-08 08:29:01.833 E/ByNameModding(16534): Failed to get description text!
05-08 08:29:01.833 I/ByNameModding(16534): run
05-08 08:29:01.833 W/ByNameModding(16534): An attempt to call [mscorlib.dll]::[System]::[String] [Unity.TextMeshPro.dll]::[TMPro]::[TMP_Text].(get_text){arguments count: 0} with an incorrect number of arguments... I hope you know what you're doing. BNM cannot add MethodInfo to arguments :(. Please fix it.
05-08 08:29:01.833 E/ByNameModding(16534): You cannot call the instance [mscorlib.dll]::[System]::[String] [Unity.TextMeshPro.dll]::[TMPro]::[TMP_Text].(get_text){arguments count: 0} method without instance! Please set instance before calling the method.
05-08 08:29:01.833 E/ByNameModding(16534): Failed to get description text!
05-08 08:29:01.833 I/ByNameModding(16534): run
05-08 08:29:01.833 W/ByNameModding(16534): An attempt to call [mscorlib.dll]::[System]::[String] [Unity.TextMeshPro.dll]::[TMPro]::[TMP_Text].(get_text){arguments count: 0} with an incorrect number of arguments... I hope you know what you're doing. BNM cannot add MethodInfo to arguments :(. Please fix it.
05-08 08:29:01.833 E/ByNameModding(16534): You cannot call the instance [mscorlib.dll]::[System]::[String] [Unity.TextMeshPro.dll]::[TMPro]::[TMP_Text].(get_text){arguments count: 0} method without instance! Please set instance before calling the method.
05-08 08:29:01.833 E/ByNameModding(16534): Failed to get description text!

dump:

// Namespace: 
public class AbyssBuffButton : UIButton // TypeDefIndex: 3379
{
	// Fields
	public AbyssBuff theBuffType; // 0x5C
	public int cost; // 0x60
	public TextMeshProUGUI description; // 0x68
	public bool sold; // 0x70
	public int index; // 0x74

	// Methods

	// RVA: 0x14C84B0 Offset: 0x14C74B0 VA: 0x14C84B0
	public void SetType(AbyssBuff theBuffType, bool sold) { }

	// RVA: 0x14C8788 Offset: 0x14C7788 VA: 0x14C8788
	public void SetSold() { }

	// RVA: 0x14C87EC Offset: 0x14C77EC VA: 0x14C87EC
	public void .ctor() { }
}

This is a Harmony patch running on PC:

using HarmonyLib;
using Il2Cpp;
using Il2CppTMPro;
using PvZ_Fusion_Translator.AssetStore;
using UnityEngine;

namespace PvZ_Fusion_Translator.Patches.GameObjects.ButtonObjects
{
    [HarmonyPatch(typeof(AbyssBuffButton))]
    public static class AbyssBuffButton_Patch
    {
        [HarmonyPatch(nameof(AbyssBuffButton.SetType))]
        [HarmonyPostfix]
        private static void SetType_Postfix(AbyssBuffButton __instance)
        {
            // Handle translation for both normal and sold states
            if (!__instance.sold)
            {
                // Rebuild the text with translations
                __instance.description.text = __instance.description.text.Split('\n')[1];
            }
            else
            {
                __instance.description.text = StringStore.TranslateText("当前词条已出售");
            }
        }
    }
}

my code:

#include <EGL/egl.h> // need to make a common.h that contains all these headers cuz this is nasty
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <GLES2/gl2platform.h>
#include <fstream>
#include <sstream>
#include "nlohmann/json.hpp"
#include "http/cpr/cpr.h"
#include "Misc/Logging.h"
#include "ImGui/imgui.h"
#include "ImGui/imgui_impl_android.h"
#include "ImGui/imgui_impl_opengl3.h"
#include "Obfuscation/Obfuscate.h"
#include <cstdio>
#include <android/native_window_jni.h>
#include <jni.h>
#include <string>
#include <unistd.h>
#include <pthread.h>
#include <dlfcn.h>
#include "Misc/JNIStuff.h"
#include "Misc/FileWrapper.h"
#include "Misc/Utils.h"
#include <BNM/Utils.hpp>
#include "ByNameModding/src/private/Internals.hpp"
#include "Memory/KittyScanner.hpp"
#include "Obfuscation/Custom_Obfuscate.h"
#include "Unity/Unity.h"
#include "Misc/FunctionPointers.h"
#include "Hooking/Hooks.h"
#include "Misc/ImGuiStuff.h"
#include "Menu.h"
#include "Hooking/JNIHooks.h"
#include "Unity/Screen.h"
#include "Memory/KittyMemory.hpp"
#include "Unity/Input.h"
// the private version held by certain polarmodders has image loading and a lot more
#include <BNM/UserSettings/GlobalSettings.hpp>
#include <BNM/Class.hpp>
#include <BNM/Field.hpp>
#include <BNM/Method.hpp>
#include <BNM/Property.hpp>
#include <BNM/Defaults.hpp>
#include <BNM/Operators.hpp>
#include <BNM/BasicMonoStructures.hpp>
using namespace BNM::Operators;
BNM::Class ObjectClass{};
// Variable for storing property
BNM::Property<BNM::Structures::Mono::String *>ObjectName{};
// Variable for storing method
BNM::Method<BNM::Structures::Mono::String *>ObjectToString{};
BNM::Class AbyssBuffButtonClass{};
BNM::Class TextMeshProUGUIClass{};
// Variable for storing property
BNM::Field<bool>field_sold{};
BNM::Field<void *>field_description{};
// Methods for TextMeshProUGUI
BNM::Method<BNM::Structures::Mono::String *>GetTextMethod{};
BNM::Method<void>SetTextMethod{};
EGLBoolean (*old_eglSwapBuffers)(...);
EGLBoolean new_eglSwapBuffers(EGLDisplay _display, EGLSurface _surface) {
    SetupImGui();
    Menu::DrawImGui();
    return old_eglSwapBuffers(_display, _surface);
}
void run();
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void * reserved) {
    jvm = vm;
    JNIEnv *env;
    vm->GetEnv((void **) &env, JNI_VERSION_1_6);
    BNM::Loading::TryLoadByJNI(env);
    BNM::Loading::AddOnLoadedEvent(run);
    BNM_LOG_INFO("test");
    return JNI_VERSION_1_6;
}
void (*old_SetType)(void *instance);
void SetType_Hook(void *instance) {
    old_SetType(instance);
    if (!instance) {
        BNM_LOG_ERR("AbyssBuffButton instance is null!");
        return;
    }
    BNM_LOG_INFO("run");
    bool isSold = field_sold[instance];
    void *descObj = field_description[instance];
	if (!descObj) {
        BNM_LOG_ERR("Description object is null!");
        return;
    }
    BNM::Structures::Mono::String *originalText = GetTextMethod(descObj);
    if (!originalText) {
        BNM_LOG_ERR("Failed to get description text!");
        return;
    }
    if (!isSold) {
        std::string text = originalText->str();
        std::string::size_type pos = text.find(':');
        std::string afterColon = (pos != std::string::npos) ? text.substr(pos + 1) : text;
        afterColon.erase(0, afterColon.find_first_not_of(' '));
        SetTextMethod(descObj, BNM::CreateMonoString(afterColon));
    } else {
        SetTextMethod(descObj, BNM::CreateMonoString(BNM_OBFUSCATE("Sold out")));
    }
}
void run() {
    using namespace BNM; // To avoid writing BNM:: in this method
    Unity::Screen::Setup();
    Unity::Input::Setup();
    Pointers::LoadPointers();
    BNM_LOG_INFO("Starting BNM initialization");
    // Get the UnityEngine class.Object
    AbyssBuffButtonClass = Class("", "AbyssBuffButton");
    if (!AbyssBuffButtonClass) {
        BNM_LOG_ERR("Failed to find AbyssBuffButton in default namespace, trying alternative");
        AbyssBuffButtonClass = Class("GameObjects.ButtonObjects", "AbyssBuffButton");
        if (!AbyssBuffButtonClass) {
            BNM_LOG_ERR("Failed to find AbyssBuffButton in GameObjects.ButtonObjects");
        }
    }
    BNM_LOG_INFO("Found AbyssBuffButton class");
    field_sold = AbyssBuffButtonClass.GetField("sold");
    field_description =AbyssBuffButtonClass.GetField("description");
    TextMeshProUGUIClass = Class("TMPro", "TextMeshProUGUI");
    if (!TextMeshProUGUIClass) {
        BNM_LOG_ERR("Failed to find TMPro.TextMeshProUGUI class!");
    }
    ExampleClass_Constructor = AbyssBuffButtonClass.GetMethod(".ctor", 0);
    if (ExampleClass_Constructor.Initialized()) {
        BNM_LOG_INFO("ok");
    }
    // Get TextMeshProUGUI methods
    GetTextMethod = TextMeshProUGUIClass.GetMethod("get_text", 0);
    SetTextMethod = TextMeshProUGUIClass.GetMethod("set_text", 1);
    // Get and hook SetType method
    auto SetTypeMethod = AbyssBuffButtonClass.GetMethod("SetType");
    BNM_LOG_INFO("ok");
    HOOK(SetTypeMethod, SetType_Hook, old_SetType);
}
__attribute__((constructor))
void lib_main() {
    auto eglhandle = dlopen(OBFUSCATE("libEGL.so"), RTLD_LAZY);
    const char *dlopen_error = dlerror();
    if (dlopen_error) {
        eglhandle = dlopen(OBFUSCATE("libunity.so"), RTLD_LAZY); // I have no idea if this works it was just to me that it would fix crashes so I did it really quickly
    }
    auto eglSwapBuffers = dlsym(eglhandle, OBFUSCATE("eglSwapBuffers"));
    const char *dlsym_error = dlerror();
    if (dlsym_error) {
        LOGE(OBFUSCATE("Cannot load symbol 'eglSwapBuffers': %s"), dlsym_error);
    } else {
        hook(eglSwapBuffers, (void *) new_eglSwapBuffers, (void **) &old_eglSwapBuffers);
    }
}

trangocuong avatar May 08 '25 01:05 trangocuong

BNM::Structures::Mono::String *originalText = GetTextMethod(descObj);

Here you trying to call method incorrectly. To call instance method, instance should be passed in [], not as an argument.

BNM::Structures::Mono::String *originalText = GetTextMethod[descObj]();

BNM uses polymorphism in API, so almost every API class use same rules.

BNM-Dev avatar May 08 '25 07:05 BNM-Dev