Update main.cpp

This commit is contained in:
gggaa2152 2026-04-06 05:12:49 +08:00 committed by GitHub
parent f13e2488f8
commit 40f9aa97b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -45,7 +45,7 @@ uintptr_t get_module_base(const char* name) {
return base;
}
// --- 增强版 GL 状态保护 (修复闪退关键) ---
// --- 增强版 GL 状态保护 ---
struct GLStateBackup {
GLint last_program;
GLint last_texture;
@ -55,13 +55,7 @@ struct GLStateBackup {
GLint last_vertex_array;
GLint last_viewport[4];
GLint last_scissor_box[4];
GLint last_blend_src_rgb;
GLint last_blend_dst_rgb;
GLint last_blend_src_alpha;
GLint last_blend_dst_alpha;
GLint last_blend_equation_rgb;
GLint last_blend_equation_alpha;
GLint last_pixel_unpack_buffer; // 修复纹理上传冲突
GLint last_pixel_unpack_buffer;
GLboolean last_enable_blend;
GLboolean last_enable_cull_face;
GLboolean last_enable_depth_test;
@ -77,14 +71,6 @@ struct GLStateBackup {
glGetIntegerv(GL_PIXEL_UNPACK_BUFFER_BINDING, &last_pixel_unpack_buffer);
glGetIntegerv(GL_VIEWPORT, last_viewport);
glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box);
glGetIntegerv(GL_BLEND_SRC_RGB, &last_blend_src_rgb);
glGetIntegerv(GL_BLEND_DST_RGB, &last_blend_dst_rgb);
glGetIntegerv(GL_BLEND_SRC_ALPHA, &last_blend_src_alpha);
glGetIntegerv(GL_BLEND_DST_ALPHA, &last_blend_dst_alpha);
glGetIntegerv(GL_BLEND_EQUATION_RGB, &last_blend_equation_rgb);
glGetIntegerv(GL_BLEND_EQUATION_ALPHA, &last_blend_equation_alpha);
last_enable_blend = glIsEnabled(GL_BLEND);
last_enable_cull_face = glIsEnabled(GL_CULL_FACE);
last_enable_depth_test = glIsEnabled(GL_DEPTH_TEST);
@ -99,15 +85,10 @@ struct GLStateBackup {
glBindVertexArray(last_vertex_array);
glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, last_element_array_buffer);
glBlendEquationSeparate(last_blend_equation_rgb, last_blend_equation_alpha);
glBlendFuncSeparate(last_blend_src_rgb, last_blend_dst_rgb, last_blend_src_alpha, last_blend_dst_alpha);
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);
glViewport(last_viewport[0], last_viewport[1], last_viewport[2], last_viewport[3]);
glScissor(last_scissor_box[0], last_scissor_box[1], last_scissor_box[2], last_scissor_box[3]);
}
@ -120,41 +101,36 @@ void DrawImGui(int width, int height) {
io.MousePos = ImVec2(g_Touch.x, g_Touch.y);
io.MouseDown[0] = g_Touch.down;
// 严苛状态保护开始
GLStateBackup gl_state;
gl_state.Backup();
ImGui_ImplOpenGL3_NewFrame();
ImGui::NewFrame();
// 设置初始窗口位置,防止因为 io.DisplaySize 突变导致窗口飞出屏幕
ImGui::SetNextWindowPos(ImVec2(width * 0.1f, height * 0.1f), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(550, 400), ImGuiCond_FirstUseEver);
if (ImGui::Begin("JKMenu v4.0 - Android 15 Hyper-Stable", &g_ShowMenu)) {
ImGui::TextColored(ImVec4(0, 1, 0, 1), "Safe Render Pipeline: ACTIVE");
ImGui::Text("Viewport: %dx%d", width, height);
if (ImGui::Begin("JKMenu v4.1 - Android 15 Pro", &g_ShowMenu)) {
ImGui::TextColored(ImVec4(0, 1, 0, 1), "Rendering Thread: STABLE");
ImGui::Text("Resolution: %d x %d", width, height);
ImGui::Separator();
static bool esp = false;
ImGui::Checkbox("Enable ESP Overlay", &esp);
static float fov = 90.0f;
ImGui::SliderFloat("Camera FOV", &fov, 60.0f, 140.0f);
ImGui::Checkbox("Enemy ESP", &esp);
static float dist = 300.0f;
ImGui::SliderFloat("Distance", &dist, 10.0f, 1000.0f);
if (ImGui::Button("Minimize Menu")) g_ShowMenu = false;
if (ImGui::Button("Close Menu")) g_ShowMenu = false;
}
ImGui::End();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
// 严苛状态保护结束
gl_state.Restore();
}
EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) {
// 互斥锁保护,防止多线程渲染干扰
if (pthread_mutex_trylock(&g_CtxMutex) != 0) {
return old_eglSwapBuffers(dpy, surface);
}
@ -170,7 +146,11 @@ EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) {
io.IniFilename = nullptr;
io.DisplaySize = ImVec2((float)width, (float)height);
// 核心修复:强制使用可预测的 GLSL 版本
// 核心:强制构建字体纹理以防绘制空数据
unsigned char* pixels;
int fw, fh;
io.Fonts->GetTexDataAsRGBA32(&pixels, &fw, &fh);
if (ImGui_ImplOpenGL3_Init("#version 300 es")) {
g_Initialized = true;
LOGI("Safe ImGui Init Success at %dx%d", width, height);
@ -189,7 +169,6 @@ EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) {
int (*old_AInputEvent_getType)(const AInputEvent* event) = nullptr;
int hook_AInputEvent_getType(const AInputEvent* event) {
if (!event) return old_AInputEvent_getType(event);
int type = old_AInputEvent_getType(event);
if (type == AINPUT_EVENT_TYPE_MOTION) {
g_Touch.x = AMotionEvent_getRawX(event, 0);
@ -203,23 +182,18 @@ int hook_AInputEvent_getType(const AInputEvent* event) {
void* init_thread(void*) {
LOGI("Monitoring started...");
// 增加延迟,确保 libEGL 符号表完全就绪
while (!get_module_base("libEGL.so")) sleep(1);
sleep(2);
// 给系统留出符号解析时间
usleep(500000);
void* sym_egl = dlsym(RTLD_DEFAULT, "eglSwapBuffers");
if (sym_egl) {
LOGI("Hooking eglSwapBuffers at %p", sym_egl);
DobbyHook(sym_egl, (void*)hook_eglSwapBuffers, (void**)&old_eglSwapBuffers);
}
if (sym_egl) DobbyHook(sym_egl, (void*)hook_eglSwapBuffers, (void**)&old_eglSwapBuffers);
void* sym_input = dlsym(RTLD_DEFAULT, "AInputEvent_getType");
if (sym_input) {
LOGI("Hooking AInputEvent_getType at %p", sym_input);
DobbyHook(sym_input, (void*)hook_AInputEvent_getType, (void**)&old_AInputEvent_getType);
}
if (sym_input) DobbyHook(sym_input, (void*)hook_AInputEvent_getType, (void**)&old_AInputEvent_getType);
LOGI("All hooks applied.");
return nullptr;
}