Update main.cpp

This commit is contained in:
gggaa2152 2026-04-06 06:08:11 +08:00 committed by GitHub
parent 89a7d43c4e
commit b340ca7f76
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

254
main.cpp
View File

@ -1,173 +1,109 @@
#include <jni.h>
#include <pthread.h>
#include <unistd.h>
#include <android/log.h>
#include <string.h>
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <EGL/egl.h>
#include <imgui.h>
#include <imgui_impl_opengl3.h>
#include <imgui_impl_android.h>
#include <GLES3/gl3.h>
#include "dobby.h"
#include <EGL/egl.h>
#include <dobby.h>
#include <dlfcn.h>
#include "memory_utils.h"
// ImGui 核心与 OpenGL3 后端
#include "imgui.h"
#include "backends/imgui_impl_opengl3.h"
// --- 全局变量与原始函数指针 ---
using T_eglSwapBuffers = EGLBoolean (*)(EGLDisplay, EGLSurface);
T_eglSwapBuffers orig_eglSwapBuffers = nullptr;
#define TAG "JKMenu_TRACE"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)
// --- 全局变量 ---
bool g_Initialized = false;
bool g_ShowMenu = true;
int g_FrameCount = 0;
EGLBoolean (*old_eglSwapBuffers)(EGLDisplay dpy, EGLSurface surface) = nullptr;
// 获取模块基址
uintptr_t get_module_base(const char* name) {
FILE* fp = fopen("/proc/self/maps", "r");
char line[1024];
uintptr_t base = 0;
while (fp && fgets(line, sizeof(line), fp)) {
if (strstr(line, name)) {
base = (uintptr_t)strtoull(line, NULL, 16);
break;
}
}
if (fp) fclose(fp);
return base;
}
// --- 渲染保护与 UI 绘制 ---
void DrawImGui(int width, int height) {
if (!g_ShowMenu) return;
// 1. 保存当前所有可能被 ImGui 改变的 GL 状态 (全量保存)
GLint last_program, last_texture, last_array_buffer, last_vao;
glGetIntegerv(GL_CURRENT_PROGRAM, &last_program);
glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vao);
GLint last_viewport[4]; glGetIntegerv(GL_VIEWPORT, last_viewport);
GLint last_scissor_box[4]; glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box);
GLboolean last_enable_blend = glIsEnabled(GL_BLEND);
GLboolean last_enable_cull_face = glIsEnabled(GL_CULL_FACE);
GLboolean last_enable_depth_test = glIsEnabled(GL_DEPTH_TEST);
GLboolean last_enable_scissor_test = glIsEnabled(GL_SCISSOR_TEST);
// 2. ImGui 渲染帧启动
ImGui_ImplOpenGL3_NewFrame();
ImGui::NewFrame();
ImGui::SetNextWindowSize(ImVec2(400, 250), ImGuiCond_FirstUseEver);
if (ImGui::Begin("JKMenu Render Test", &g_ShowMenu)) {
ImGui::Text("Render Status: Active");
ImGui::Text("GL Version: %s", glGetString(GL_VERSION));
ImGui::Separator();
static float f = 0.5f;
ImGui::SliderFloat("Alpha Test", &f, 0.0f, 1.0f);
if (ImGui::Button("Close Menu")) {
g_ShowMenu = false;
}
}
ImGui::End();
// 3. 提交绘制
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
// 4. 恢复原始状态
glUseProgram(last_program);
glBindTexture(GL_TEXTURE_2D, last_texture);
glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer);
glBindVertexArray(last_vao);
glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]);
glScissor(last_scissor_box[0], last_scissor_box[1], (GLsizei)last_scissor_box[2], (GLsizei)last_scissor_box[3]);
if (last_enable_blend) glEnable(GL_BLEND); else glDisable(GL_BLEND);
if (last_enable_cull_face) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE);
if (last_enable_depth_test) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
if (last_enable_scissor_test) glEnable(GL_SCISSOR_TEST); else glDisable(GL_SCISSOR_TEST);
}
// --- EGL SwapBuffers Hook ---
EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) {
static bool is_rendering = false;
if (is_rendering) return old_eglSwapBuffers(dpy, surface);
is_rendering = true;
g_FrameCount++;
// 初始化逻辑
// --- 渲染状态备份 (王者荣耀专项优化:深度还原) ---
void Hook_eglSwapBuffers(EGLDisplay display, EGLSurface surface) {
if (!g_Initialized) {
EGLContext ctx = eglGetCurrentContext();
// 降低门槛:只要有 Context 且 帧数 > 10 就可以尝试初始化
if (ctx != EGL_NO_CONTEXT && g_FrameCount > 60) {
LOGI("[TRACE] Step 3: Attempting ImGui Init with Context: %p", ctx);
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.IniFilename = nullptr;
io.FontGlobalScale = 2.0f; // 适配移动端高分屏
// 1. 初始化 ImGui 上下文
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.IniFilename = nullptr; // 不保存配置文件,防止被游戏扫描到
// 2. 初始化后台渲染器 (王者荣耀使用 GLES3)
ImGui_ImplOpenGL3_Init("#version 300 es");
g_Initialized = true;
}
// 优先尝试 300 es,如果失败尝试 nullptr 让后端自动检测
if (ImGui_ImplOpenGL3_Init("#version 300 es")) {
g_Initialized = true;
LOGI("[TRACE] Step 4: Render Init SUCCESS on Frame %d", g_FrameCount);
} else {
LOGE("[TRACE] ERROR: ImGui_ImplOpenGL3_Init Failed! GL Error: %d", glGetError());
// 如果 300 失败,清理掉 Context 准备下一帧重试或报错
ImGui::DestroyContext();
if (g_ShowMenu) {
// --- [核心:全状态备份] 学习自高级 Hook 逻辑 ---
GLint last_program, last_vertex_array, last_array_buffer, last_element_array_buffer;
GLint last_viewport[4];
GLint last_scissor_box[4];
glGetIntegerv(GL_CURRENT_PROGRAM, &last_program);
glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array);
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &last_element_array_buffer);
glGetIntegerv(GL_VIEWPORT, last_viewport);
glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box);
// 备份关键能力开关
GLboolean last_enable_blend = glIsEnabled(GL_BLEND);
GLboolean last_enable_cull_face = glIsEnabled(GL_CULL_FACE);
GLboolean last_enable_depth_test = glIsEnabled(GL_DEPTH_TEST);
GLboolean last_enable_scissor_test = glIsEnabled(GL_SCISSOR_TEST);
// --- 开始 ImGui 帧 ---
ImGui_ImplOpenGL3_NewFrame();
ImGui::NewFrame();
// 绘制王者荣耀专属风格菜单
ImGui::SetNextWindowSize(ImVec2(400, 320), ImGuiCond_FirstUseEver);
if (ImGui::Begin("JKInternal - SGame (王者荣耀)", nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
ImGui::TextColored(ImVec4(1, 1, 0, 1), "渲染注入成功: libEGL.so -> eglSwapBuffers");
ImGui::Separator();
static bool esp_box = false;
static bool esp_line = false;
ImGui::Checkbox("英雄方框", &esp_box);
ImGui::SameLine();
ImGui::Checkbox("英雄射线", &esp_line);
static float draw_color[4] = { 1.0f, 0.0f, 0.0f, 1.0f };
ImGui::ColorEdit4("绘制颜色", draw_color);
ImGui::Separator();
if (ImGui::Button("隐藏菜单 (按音量键恢复-未实现)")) {
g_ShowMenu = false;
}
}
ImGui::End();
// --- 渲染 ImGui ---
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
// --- [核心:全状态还原] 让游戏管线无感知 ---
glUseProgram(last_program);
glBindVertexArray(last_vertex_array);
glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, last_element_array_buffer);
glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]);
glScissor(last_scissor_box[0], last_scissor_box[1], (GLsizei)last_scissor_box[2], (GLsizei)last_scissor_box[3]);
if (last_enable_blend) glEnable(GL_BLEND); else glDisable(GL_BLEND);
if (last_enable_cull_face) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE);
if (last_enable_depth_test) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
if (last_enable_scissor_test) glEnable(GL_SCISSOR_TEST); else glDisable(GL_SCISSOR_TEST);
}
if (g_Initialized && g_ShowMenu) {
EGLint w, h;
eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
ImGui::GetIO().DisplaySize = ImVec2((float)w, (float)h);
DrawImGui(w, h);
// 调用原始函数指针,完成屏幕交换
orig_eglSwapBuffers(display, surface);
}
// --- 注入入口 ---
__attribute__((constructor))
void MainEntry() {
// 动态获取 eglSwapBuffers 地址并进行 Dobby Hook
void* handle = dlopen("libEGL.so", RTLD_LAZY);
if (handle) {
void* target = dlsym(handle, "eglSwapBuffers");
if (target) {
DobbyHook(target, (void*)Hook_eglSwapBuffers, (void**)&orig_eglSwapBuffers);
}
}
is_rendering = false;
return old_eglSwapBuffers(dpy, surface);
}
// --- 初始化线程 ---
void* init_thread(void*) {
LOGI("[TRACE] Step 1: Plugin Thread Started");
// 等待核心图形库加载
while (!get_module_base("libEGL.so") || !get_module_base("libGLESv3.so")) {
usleep(100000);
}
// 使用 RTLD_DEFAULT 确保拿到的是进程内正在运行的 eglSwapBuffers
void* sym_egl = dlsym(RTLD_DEFAULT, "eglSwapBuffers");
if (sym_egl) {
DobbyHook(sym_egl, (void*)hook_eglSwapBuffers, (void**)&old_eglSwapBuffers);
LOGI("[TRACE] Step 2: eglSwapBuffers Hooked (Render Only)");
} else {
LOGE("[TRACE] FATAL: Could not find eglSwapBuffers symbol!");
}
return nullptr;
}
__attribute__((constructor)) void _init() {
pthread_t tid;
pthread_create(&tid, nullptr, init_thread, nullptr);
pthread_detach(tid);
}
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
return JNI_VERSION_1_6;
}