Skip to content

Commit 94c5186

Browse files
authored
refactor: PreLoader and GameLoader for CMake integration (#2)
1 parent a2317cb commit 94c5186

File tree

3 files changed

+159
-47
lines changed

3 files changed

+159
-47
lines changed

CMakeLists.txt

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
cmake_minimum_required(VERSION 3.22.1)
2+
project(preloader)
3+
4+
5+
file(GLOB_RECURSE PRELOADER_SRC
6+
"src/**/*.cpp"
7+
)
8+
9+
10+
11+
add_library(preloader SHARED ${PRELOADER_SRC})
12+
13+
14+
target_include_directories(preloader
15+
PRIVATE src
16+
)
17+
18+
target_compile_definitions(preloader
19+
PRIVATE PRELOADER_EXPORT UNICODE
20+
)
21+
22+
23+
include(FetchContent)
24+
25+
FetchContent_Declare(
26+
nlohmann_json
27+
GIT_REPOSITORY https://github.com/nlohmann/json.git
28+
GIT_TAG v3.11.3
29+
)
30+
FetchContent_MakeAvailable(nlohmann_json)
31+
32+
FetchContent_Declare(
33+
fmt
34+
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
35+
GIT_TAG 10.2.1
36+
)
37+
FetchContent_MakeAvailable(fmt)
38+
39+
find_library(log-lib log)
40+
find_library(android-lib android)
41+
find_library(egl-lib EGL)
42+
find_library(gles-lib GLESv3)
43+
44+
45+
if(CMAKE_ANDROID_ARCH_ABI STREQUAL "armeabi-v7a")
46+
set(GLOSSHOOK_PATH "${CMAKE_CURRENT_SOURCE_DIR}/lib/ARM/libGlossHook.a")
47+
elseif(CMAKE_ANDROID_ARCH_ABI STREQUAL "arm64-v8a")
48+
set(GLOSSHOOK_PATH "${CMAKE_CURRENT_SOURCE_DIR}/lib/ARM64/libGlossHook.a")
49+
else()
50+
message(FATAL_ERROR "Unsupported ABI: ${CMAKE_ANDROID_ARCH_ABI}")
51+
endif()
52+
53+
54+
target_link_libraries(preloader
55+
PRIVATE
56+
${GLOSSHOOK_PATH}
57+
${log-lib}
58+
${android-lib}
59+
${egl-lib}
60+
${gles-lib}
61+
nlohmann_json::nlohmann_json
62+
fmt::fmt
63+
)

src/pl/PreLoader.cpp

Lines changed: 96 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,108 @@
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

617
JavaVM *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
}

xmake.lua

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)