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

BNM Initialization error (and how I fixed it)

Open Pubert-CS opened this issue 7 months ago • 1 comments

When initializing BNM, this line crashes for me:

auto UnityEngineCoreModule = Image(BNM_OBFUSCATE_TMP("UnityEngine.CoreModule.dll"));

The crash happens inside Loading.cpp during initialization.

After looking into it, the issue seems to come from Internal::TryGetImage when it calls Assembly$$GetAllAssemblies().

Normally this works fine, as I haven't had this issue on earlier unity versions, but in this game, that seems to break early during startup and causes the crash before anything else can continue.

I wasn't getting any crash logs about this, so I don't really have anything to show (sorry about that)

As a test, I replaced Assembly$$GetAllAssemblies() with this version:

inline std::vector<IL2CPP::Il2CppAssembly*>* Assembly$$GetAllAssemblies() {
    auto domain = il2cpp_domain_get();

    auto il2cpp_domain_get_assemblies_fn =
        (IL2CPP::Il2CppAssembly**(*)(IL2CPP::Il2CppDomain*, size_t*))
        dlsym(Internal::il2cppLibraryHandle, BNM_IL2CPP_API_il2cpp_domain_get_assemblies);

    size_t count = 0;
    auto assemblies = il2cpp_domain_get_assemblies_fn(domain, &count);

    if (!assemblies || count == 0) {
        return {};
    }

    return new std::vector<IL2CPP::Il2CppAssembly*>(assemblies, assemblies + count);
}

With this change, BNM starts correctly and Image() no longer crashes.

I know this isn’t the safest fix, but it works consistently for this game's version (6000.2.6f2).

I actually saw another issue that seemed to be erroring at the same time, too https://github.com/ByNameModding/BNM-Android/issues/176

Pubert-CS avatar May 02 '26 00:05 Pubert-CS

I actually had a very similar issue (#175), which I had crash logs. This looks like it would probably fix it, so later I'll probably use this. Thank you!

crimsonlmaoo avatar May 02 '26 15:05 crimsonlmaoo

do you know if this works on games newer then 6000.2.6f2?

leon2277 avatar Jun 23 '26 22:06 leon2277

I've only had issues with BNM/ByNameModding with most of it, the issue being when BNM tries to access structs on il2cppclass and whatever, it tends to crash and I have debugged quite a lot and it does seem to crash when accessing structures.

And of course when I use il2cpp_ functions, it actually doesn't crash at the Assembly$$GetAllAssemblies() after fixing that, but happens later in the loading.

(might not be relevant to this conversation just talking about other stuff)

ThatOneGuyHeh avatar Jun 24 '26 01:06 ThatOneGuyHeh

Now of course this could just be a skill issue for me, but it only gets farther when I switch to using il2cpp functions/methods.

ThatOneGuyHeh avatar Jun 24 '26 01:06 ThatOneGuyHeh

Now of course this could just be a skill issue for me, but it only gets farther when I switch to using il2cpp functions/methods.

yes, same for me

leon2277 avatar Jun 29 '26 18:06 leon2277

I've only had issues with BNM/ByNameModding with most of it, the issue being when BNM tries to access structs on il2cppclass and whatever, it tends to crash and I have debugged quite a lot and it does seem to crash when accessing structures.

And of course when I use il2cpp_ functions, it actually doesn't crash at the Assembly$$GetAllAssemblies() after fixing that, but happens later in the loading.

(might not be relevant to this conversation just talking about other stuff)

I've had multiple issues with these things, and I have ruled out fixes, but do not remember what I did. I had this with specific games, but fixed it later on (it seemed to be parts of class creation have null parts resolving me into having a crash). I had to add specific checks to multiple places or just purposely remove them to prevent these crashes.

crimsonlmaoo avatar Jun 30 '26 15:06 crimsonlmaoo