Update main.cpp

This commit is contained in:
gggaa2152 2026-04-06 04:06:23 +08:00 committed by GitHub
parent 84f4267ced
commit d08c2e4fcf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

105
main.cpp
View File

@ -13,52 +13,117 @@
#define TAG "JKMenu_SGame"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)
static bool g_Initialized = false;
static int g_Width = 0;
static int g_Height = 0;
typedef EGLBoolean (*eglSwapBuffers_t)(EGLDisplay, EGLSurface);
static eglSwapBuffers_t old_eglSwapBuffers = nullptr;
// 绘制菜单
void DrawJKMenu() {
if (!ImGui::GetCurrentContext()) return;
ImGui::Begin("JKMenu for SGame", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
// 设置默认位置和大小(仅第一次)
ImGui::SetNextWindowPos(ImVec2(100, 100), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(400, 300), ImGuiCond_FirstUseEver);
ImGui::Begin("JKMenu for SGame", nullptr, ImGuiWindowFlags_None);
ImGui::Text("Build Date: 2026-04-06");
ImGui::Text("Viewport: %dx%d", g_Width, g_Height);
ImGui::Separator();
static bool hack_enabled = false;
ImGui::Checkbox("功能开关示例", &hack_enabled);
if (ImGui::Button("功能开关示例")) {
hack_enabled = !hack_enabled;
}
ImGui::Text("状态: %s", hack_enabled ? "已开启" : "已关闭");
if (ImGui::Button("卸载插件")) {
// 可以在这里写卸载逻辑
}
ImGui::End();
}
EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surf) {
// 获取屏幕分辨率
eglQuerySurface(dpy, surf, EGL_WIDTH, &g_Width);
eglQuerySurface(dpy, surf, EGL_HEIGHT, &g_Height);
if (!g_Initialized) {
ImGui::CreateContext();
ImGui_ImplAndroid_Init(nullptr);
ImGui_ImplOpenGL3_Init("#version 300 es");
g_Initialized = true;
LOGI("ImGui Initialized in SGame process.");
if (g_Width > 0 && g_Height > 0) {
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
// 字体缩放自适应手机高分屏
io.FontGlobalScale = 2.0f;
// 初始化渲染后端
// 注意:在 Hook 环境下不需要调用 ImGui_ImplAndroid_Init
// 因为我们没有 NativeWindow,通常只用 OpenGL3 后端
ImGui_ImplOpenGL3_Init("#version 300 es");
g_Initialized = true;
LOGI("ImGui Initialized! Screen: %dx%d", g_Width, g_Height);
}
}
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplAndroid_NewFrame();
ImGui::NewFrame();
DrawJKMenu();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
if (g_Initialized) {
ImGui_ImplOpenGL3_NewFrame();
ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = ImVec2((float)g_Width, (float)g_Height);
io.DeltaTime = 1.0f / 60.0f;
ImGui::NewFrame();
DrawJKMenu();
ImGui::Render();
// 渲染 ImGui
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
return old_eglSwapBuffers(dpy, surf);
}
void* init_thread(void*) {
// 王者荣耀启动较慢,建议延迟 10 秒等待渲染库加载完毕
sleep(10);
LOGI("Thread started, waiting for game load...");
// 增加延迟确保 libEGL.so 已经完全加载到内存
sleep(15);
void* egl_addr = DobbySymbolResolver("libEGL.so", "eglSwapBuffers");
if (egl_addr) {
DobbyHook(egl_addr, (void*)hook_eglSwapBuffers, (void**)&old_eglSwapBuffers);
LOGI("Hook eglSwapBuffers Success!");
// 尝试寻找 eglSwapBuffers 地址
void* egl_addr = dlsym(RTLD_DEFAULT, "eglSwapBuffers");
if (!egl_addr) {
egl_addr = DobbySymbolResolver("libEGL.so", "eglSwapBuffers");
}
if (egl_addr) {
LOGI("Found eglSwapBuffers at %p, hooking...", egl_addr);
DobbyHook(egl_addr, (void*)hook_eglSwapBuffers, (void**)&old_eglSwapBuffers);
if (old_eglSwapBuffers) {
LOGI("Hook eglSwapBuffers Success!");
} else {
LOGE("DobbyHook failed to return original function.");
}
} else {
LOGE("Failed to find eglSwapBuffers!");
}
return nullptr;
}
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
// 当 SO 被 dlopen 加载时自动调用
__attribute__((constructor))
void _init() {
pthread_t tid;
pthread_create(&tid, nullptr, init_thread, nullptr);
}
// 也可以保留 JNI_OnLoad 作为双重保险
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
return JNI_VERSION_1_6;
}