1- #include " internal/AndroidUtils.h"
2- #include " internal/ModManager.h"
1+ //
2+ // Created by mrjar on 10/5/2025.
3+ //
4+
5+ #include < jni.h>
36#include < android/native_activity.h>
47#include < dlfcn.h>
8+ #include < android/log.h>
9+ #include " internal/AndroidUtils.h"
10+ #include " internal/ModManager.h"
11+
12+ #define LOG_TAG " NativeLoader"
13+ #define LOGD (...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
14+ #define LOGE (...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
15+
516
617JavaVM *g_vm = nullptr ;
718
8- static void (*ANativeActivity_onCreate_minecraft)(ANativeActivity *, void *,
9- size_t ) = nullptr;
19+ static std::string g_modsDir;
20+ static std::string g_cacheDir;
21+ static bool g_modsInitialized = false ;
1022
11- extern " C" void ANativeActivity_onCreate (ANativeActivity *activity,
12- void *savedState,
13- size_t savedStateSize) {
14- if (ANativeActivity_onCreate_minecraft) {
15- ANativeActivity_onCreate_minecraft (activity, savedState, savedStateSize);
16- }
23+ static void (*onCreate)(ANativeActivity*, void *, size_t ) = nullptr;
24+ static void (*onFinish)(ANativeActivity*) = nullptr;
25+ static void (*androidMain)(struct android_app *) = nullptr;
26+
27+ extern " C" {
28+
29+
30+ JNIEXPORT void ANativeActivity_onCreate (ANativeActivity* activity, void * savedState, size_t savedStateSize) {
31+ if (onCreate) {
32+ onCreate (activity, savedState, savedStateSize);
33+ } else {
34+ LOGE (" ANativeActivity_onCreate function not loaded" );
35+ }
1736}
1837
19- JNIEXPORT jint JNI_OnLoad (JavaVM *vm, void *) {
20- g_vm = vm;
21- JNIEnv *env = nullptr ;
22- if (vm->GetEnv (reinterpret_cast <void **>(&env), JNI_VERSION_1_4) != JNI_OK)
38+ JNIEXPORT void ANativeActivity_finish (ANativeActivity* activity) {
39+ if (onFinish) {
40+ onFinish (activity);
41+ }
42+ }
43+
44+ JNIEXPORT void android_main (struct android_app * state) {
45+ if (androidMain) {
46+ androidMain (state);
47+ } else {
48+ LOGE (" android_main function not loaded" );
49+ }
50+ }
51+
52+ JNIEXPORT jint JNI_OnLoad (JavaVM* vm, void * reserved) {
53+ g_vm = vm;
54+ JNIEnv* env = nullptr ;
55+ if (vm->GetEnv (reinterpret_cast <void **>(&env), JNI_VERSION_1_4) != JNI_OK)
56+ return JNI_VERSION_1_4;
57+
58+ auto paths = AndroidUtils::FetchContextPaths (env);
59+ g_modsDir = paths.modsDir ;
60+ g_cacheDir = paths.cacheDir ;
61+
2362 return JNI_VERSION_1_4;
63+ }
64+
65+ JNIEXPORT void JNICALL
66+ Java_org_levimc_launcher_core_minecraft_MinecraftActivity_nativeOnLauncherLoaded (
67+ JNIEnv* env,
68+ jobject thiz,
69+ jstring libPath
70+ ) {
71+ const char * path = env->GetStringUTFChars (libPath, nullptr );
72+
73+ void * handle = dlopen (path, RTLD_NOW | RTLD_GLOBAL);
74+ if (!handle) {
75+ LOGE (" Failed to load library: %s" , dlerror ());
76+ env->ReleaseStringUTFChars (libPath, path);
77+ return ;
78+ }
2479
25- auto paths = AndroidUtils::FetchContextPaths (env);
26- ModManager::LoadAndInitializeEnabledMods (paths.modsDir , paths.cacheDir , g_vm);
80+ onCreate = reinterpret_cast <decltype (onCreate)>(dlsym (handle, " ANativeActivity_onCreate" ));
81+ onFinish = reinterpret_cast <decltype (onFinish)>(dlsym (handle, " ANativeActivity_finish" ));
82+ androidMain = reinterpret_cast <decltype (androidMain)>(dlsym (handle, " android_main" ));
83+
84+ if (!onCreate || !androidMain) {
85+ LOGE (" Failed to resolve required symbols" );
86+ } else {
87+ LOGD (" Successfully loaded Minecraft native functions" );
88+ }
89+ env->ReleaseStringUTFChars (libPath, path);
90+ if (!g_modsInitialized && !g_modsDir.empty ()) {
91+ ModManager::LoadAndInitializeEnabledMods (g_modsDir, g_cacheDir, g_vm);
92+ g_modsInitialized = true ;
93+ LOGD (" Mods initialized successfully" );
94+ }
95+ }
96+
97+ JNIEXPORT void JNICALL
98+ Java_org_levimc_launcher_core_minecraft_MinecraftLauncher_nativeOnLauncherLoaded (
99+ JNIEnv* env,
100+ jobject thiz,
101+ jstring libPath
102+ ) {
103+
104+
105+ Java_org_levimc_launcher_core_minecraft_MinecraftActivity_nativeOnLauncherLoaded (env, thiz, libPath);
106+ }
27107
28- void *handle = dlopen (" libminecraftpe.so" , RTLD_LAZY);
29- if (handle) {
30- ANativeActivity_onCreate_minecraft =
31- (void (*)(ANativeActivity *, void *, size_t ))dlsym (
32- handle, " ANativeActivity_onCreate" );
33- }
34- return JNI_VERSION_1_4;
35108}
0 commit comments