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

AttachIl2Cpp

Open solomode0001 opened this issue 1 year ago • 5 comments

Hi,

I noticed that the AttachIl2Cpp feature was removed in BNM 2.0.4. I really liked using this feature to call methods outside the Il2cpp thread. Is there any chance you could bring it back in a future update? Thanks!

solomode0001 avatar Aug 05 '24 06:08 solomode0001

Even using it, calling unity/il2cpp methods outside main thread is dangerous. This method don't syncs threads. You can call some math unity methods safely, some other will crash after call.

BNM-Dev avatar Aug 05 '24 11:08 BNM-Dev

If i don't use AttachIl2Cpp or il2cpp_thread_attach on the current thread, the app will crash. However, if i use it on a thread before calling a method, the app runs smoothly. For example, in my code below, I call a method on the ImGui thread. When I don't use AttachIl2Cpp, the app crashes, but it works fine when I do

ImGui::Spacing();
auto currencyManager = SDK::CurrencyManager::instance<SDK::CurrencyManager*>().Get();
if (currencyManager && BNM::IsUnityObjectAlive(currencyManager)) {
	ImGui::Checkbox(OBFUSCATE("Debug"), &config.profile.debug);
	if (config.profile.debug) {
		ImGui::Spacing();
		ImGui::Separator();
		ImGui::Spacing();
		ImGui::Text(OBFUSCATE("Current Money: "));
		if (ImGui::InputInt(OBFUSCATE("##currentMoney"), currencyManager->currentGold<int>().GetPointer())) {
			if (BNM::AttachIl2Cpp()) {
				auto OnGoldChanged = currencyManager->OnGoldChanged<Action<int>*>().Get();
				if (OnGoldChanged) OnGoldChanged->Invoke(currencyManager->currentGold<int>().Get());
				BNM::DetachIl2Cpp();
			}
		}
	}
}
ImGui::Spacing();
ImGui::End();

solomode0001 avatar Aug 05 '24 11:08 solomode0001

You have own thread for imgui?

BNM-Dev avatar Aug 05 '24 11:08 BNM-Dev

i am using GLSurfaceView.Renderer for IMGUI

solomode0001 avatar Aug 05 '24 11:08 solomode0001

After reanalyzing il2cpp src, I see that code attaching to. I will return methods it in next patch if it really useful.

BNM-Dev avatar Aug 05 '24 16:08 BNM-Dev