mirror of
https://github.com/ocornut/imgui.git
synced 2026-11-07 06:55:53 +01:00
Update main.cpp
This commit is contained in:
parent
28700f9849
commit
f4c3c9731c
162
main.cpp
162
main.cpp
@ -9,10 +9,9 @@
|
||||
#include <dlfcn.h>
|
||||
#include <EGL/egl.h>
|
||||
#include <GLES3/gl3.h>
|
||||
#include <android/input.h>
|
||||
#include "dobby.h"
|
||||
|
||||
// ImGui 包含
|
||||
// ImGui 核心与 OpenGL3 后端
|
||||
#include "imgui.h"
|
||||
#include "backends/imgui_impl_opengl3.h"
|
||||
|
||||
@ -24,11 +23,9 @@
|
||||
bool g_Initialized = false;
|
||||
bool g_ShowMenu = true;
|
||||
int g_FrameCount = 0;
|
||||
pthread_mutex_t g_CtxMutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
EGLBoolean (*old_eglSwapBuffers)(EGLDisplay dpy, EGLSurface surface) = nullptr;
|
||||
|
||||
struct TouchData { float x, y; bool down; } g_Touch;
|
||||
|
||||
// 获取模块基址
|
||||
uintptr_t get_module_base(const char* name) {
|
||||
FILE* fp = fopen("/proc/self/maps", "r");
|
||||
char line[1024];
|
||||
@ -43,120 +40,119 @@ uintptr_t get_module_base(const char* name) {
|
||||
return base;
|
||||
}
|
||||
|
||||
// --- 渲染保护与 UI 绘制 ---
|
||||
void DrawImGui(int width, int height) {
|
||||
if (!g_ShowMenu) return;
|
||||
|
||||
// STEP 4.1: 进入绘制逻辑
|
||||
LOGI("[TRACE] Step 4.1: Enter DrawImGui");
|
||||
|
||||
// 保存状态
|
||||
GLint last_fbo, last_vao, last_program;
|
||||
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &last_fbo);
|
||||
glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vao);
|
||||
// 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);
|
||||
|
||||
// STEP 4.2: 状态解绑
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
for (int i = 0; i < 8; i++) glDisableVertexAttribArray(i);
|
||||
LOGI("[TRACE] Step 4.2: State Unbind Success");
|
||||
|
||||
// 2. ImGui 渲染帧启动
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
ImGui::NewFrame();
|
||||
|
||||
ImGui::SetNextWindowSize(ImVec2(400, 250), ImGuiCond_FirstUseEver);
|
||||
if (ImGui::Begin("Debug Trace Menu", &g_ShowMenu)) {
|
||||
ImGui::Text("If you see this, Step 4.3 is OK");
|
||||
if (ImGui::Button("Test Crash Point")) { LOGI("Button Clicked"); }
|
||||
if (ImGui::Begin("JKMenu Render Test", &g_ShowMenu)) {
|
||||
ImGui::Text("Render Status: Active");
|
||||
ImGui::Text("Wait for Input Implementation...");
|
||||
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();
|
||||
|
||||
// STEP 4.3: 生成绘制数据
|
||||
// 3. 提交绘制
|
||||
ImGui::Render();
|
||||
LOGI("[TRACE] Step 4.3: ImGui Render Data Generated");
|
||||
|
||||
// STEP 4.4: 提交 GPU 绘制 (最容易闪退的点)
|
||||
LOGI("[TRACE] Step 4.4: Executing glDraw...");
|
||||
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
||||
LOGI("[TRACE] Step 4.5: glDraw Finished");
|
||||
|
||||
// 还原状态
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, last_fbo);
|
||||
glBindVertexArray(last_vao);
|
||||
// 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) {
|
||||
if (pthread_mutex_trylock(&g_CtxMutex) != 0) return old_eglSwapBuffers(dpy, surface);
|
||||
|
||||
// 简单的重入锁
|
||||
static bool is_rendering = false;
|
||||
if (is_rendering) return old_eglSwapBuffers(dpy, surface);
|
||||
is_rendering = true;
|
||||
|
||||
g_FrameCount++;
|
||||
|
||||
// STEP 3: 检测初始化时机
|
||||
if (!g_Initialized && g_FrameCount > 150) {
|
||||
LOGI("[TRACE] Step 3.1: Start ImGui Init");
|
||||
IMGUI_CHECKVERSION();
|
||||
ImGui::CreateContext();
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
io.IniFilename = nullptr;
|
||||
|
||||
EGLint w, h;
|
||||
eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
|
||||
eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
|
||||
io.DisplaySize = ImVec2((float)w, (float)h);
|
||||
// 初始化逻辑:等待 200 帧确保 Context 完全稳定
|
||||
if (!g_Initialized && g_FrameCount > 200) {
|
||||
if (eglGetCurrentContext() != EGL_NO_CONTEXT) {
|
||||
LOGI("[TRACE] Step 3: Starting ImGui Render Init");
|
||||
|
||||
IMGUI_CHECKVERSION();
|
||||
ImGui::CreateContext();
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
io.IniFilename = nullptr;
|
||||
|
||||
// 解决高分屏 UI 太小的问题
|
||||
io.FontGlobalScale = 2.0f;
|
||||
|
||||
if (ImGui_ImplOpenGL3_Init("#version 100")) {
|
||||
g_Initialized = true;
|
||||
LOGI("[TRACE] Step 3.2: ImGui Init Success");
|
||||
} else {
|
||||
LOGE("[TRACE] Step 3.2 ERROR: ImGui Init Failed");
|
||||
if (ImGui_ImplOpenGL3_Init("#version 300 es")) {
|
||||
g_Initialized = true;
|
||||
LOGI("[TRACE] Step 4: Render Init Success");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (g_Initialized) {
|
||||
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);
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&g_CtxMutex);
|
||||
is_rendering = false;
|
||||
return old_eglSwapBuffers(dpy, surface);
|
||||
}
|
||||
|
||||
// 输入处理 (Step 5)
|
||||
int (*old_AInputEvent_getType)(const AInputEvent* event) = nullptr;
|
||||
int hook_AInputEvent_getType(const AInputEvent* event) {
|
||||
int type = old_AInputEvent_getType(event);
|
||||
if (type == AINPUT_EVENT_TYPE_MOTION) {
|
||||
g_Touch.x = AMotionEvent_getRawX(event, 0);
|
||||
g_Touch.y = AMotionEvent_getRawY(event, 0);
|
||||
int action = AMotionEvent_getAction(event) & AMOTION_EVENT_ACTION_MASK;
|
||||
g_Touch.down = (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_MOVE);
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
// --- 初始化线程 ---
|
||||
void* init_thread(void*) {
|
||||
LOGI("[TRACE] Step 1: Thread Started");
|
||||
while (!get_module_base("libEGL.so")) usleep(100000);
|
||||
LOGI("[TRACE] Step 1: Plugin Thread Started");
|
||||
|
||||
void* egl_handle = dlopen("libEGL.so", RTLD_LAZY);
|
||||
if (egl_handle) {
|
||||
void* sym = dlsym(egl_handle, "eglSwapBuffers");
|
||||
if (sym) {
|
||||
DobbyHook(sym, (void*)hook_eglSwapBuffers, (void**)&old_eglSwapBuffers);
|
||||
LOGI("[TRACE] Step 2.1: eglSwapBuffers Hooked");
|
||||
}
|
||||
dlclose(egl_handle);
|
||||
// 等待核心图形库加载
|
||||
while (!get_module_base("libEGL.so") || !get_module_base("libGLESv3.so")) {
|
||||
usleep(100000);
|
||||
}
|
||||
|
||||
// 仅 Hook 渲染函数
|
||||
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)");
|
||||
}
|
||||
|
||||
void* sym_in = dlsym(RTLD_DEFAULT, "AInputEvent_getType");
|
||||
if (sym_in) {
|
||||
DobbyHook(sym_in, (void*)hook_AInputEvent_getType, (void**)&old_AInputEvent_getType);
|
||||
LOGI("[TRACE] Step 2.2: Input Hooked");
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@ -166,4 +162,6 @@ __attribute__((constructor)) void _init() {
|
||||
pthread_detach(tid);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) { return JNI_VERSION_1_6; }
|
||||
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
|
||||
return JNI_VERSION_1_6;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user