diff --git a/main.cpp b/main.cpp index 2178a834b..db1677887 100644 --- a/main.cpp +++ b/main.cpp @@ -1,15 +1,7 @@ -#include -#include -#include #include -#include -#include -#include #include - #include #include - #include "imgui.h" #include "imgui_impl_android.h" #include "imgui_impl_opengl3.h" @@ -19,76 +11,54 @@ #define LOG_TAG "AndKitty_Menu" #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) -#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) -// --- 状态与坐标 --- +// --- 全局状态 --- static bool g_Initialized = false; static bool g_ShowMenu = true; -static float g_ScreenWidth = 0.0f; -static float g_ScreenHeight = 0.0f; +static float g_Width = 0, g_Height = 0; -// 记录触摸状态 struct { - float x; - float y; + float x, y; bool down; -} g_TouchState = {0.0f, 0.0f, false}; +} g_Touch = {0, 0, false}; // --- 原函数指针 --- -typedef float (*p_getRawX)(const AInputEvent* motion_event, size_t pointer_index); -static p_getRawX old_getRawX = nullptr; - -typedef float (*p_getRawY)(const AInputEvent* motion_event, size_t pointer_index); -static p_getRawY old_getRawY = nullptr; - -typedef int32_t (*p_getAction)(const AInputEvent* motion_event); -static p_getAction old_getAction = nullptr; +typedef int32_t (*p_getPointerId)(const AInputEvent* motion_event, size_t pointer_index); +static p_getPointerId old_getPointerId = nullptr; typedef EGLBoolean (*p_eglSwapBuffers)(EGLDisplay dpy, EGLSurface surface); static p_eglSwapBuffers old_eglSwapBuffers = nullptr; -// --- 【核心】直接拦截系统坐标读取 --- -// 无论游戏怎么绕,它最终必须调用这些函数来获取坐标 -float hook_getRawX(const AInputEvent* event, size_t index) { - float x = old_getRawX(event, index); - if (index == 0) g_TouchState.x = x; - return x; -} +// --- 【核心】极简拦截器 --- +// 参考主流做法:Hook getPointerId 是获取 InputEvent 最稳妥的时机 +int32_t hook_getPointerId(const AInputEvent* event, size_t index) { + if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) { + int32_t action = AMotionEvent_getAction(event) & AMOTION_EVENT_ACTION_MASK; + + // 记录坐标和状态 + g_Touch.x = AMotionEvent_getX(event, 0); + g_Touch.y = AMotionEvent_getY(event, 0); -float hook_getRawY(const AInputEvent* event, size_t index) { - float y = old_getRawY(event, index); - if (index == 0) g_TouchState.y = y; - return y; -} - -int32_t hook_getAction(const AInputEvent* event) { - int32_t action = old_getAction(event); - int32_t code = action & AMOTION_EVENT_ACTION_MASK; - - if (code == AMOTION_EVENT_ACTION_DOWN || code == AMOTION_EVENT_ACTION_POINTER_DOWN) { - g_TouchState.down = true; - } else if (code == AMOTION_EVENT_ACTION_UP || code == AMOTION_EVENT_ACTION_POINTER_UP || code == AMOTION_EVENT_ACTION_CANCEL) { - g_TouchState.down = false; + if (action == AMOTION_EVENT_ACTION_DOWN) g_Touch.down = true; + else if (action == AMOTION_EVENT_ACTION_UP || action == AMOTION_EVENT_ACTION_CANCEL) g_Touch.down = false; } - return action; + return old_getPointerId(event, index); } // --- 渲染逻辑 --- EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) { - if (g_ScreenWidth <= 0) { + if (g_Width <= 0) { EGLint w, h; eglQuerySurface(dpy, surface, EGL_WIDTH, &w); eglQuerySurface(dpy, surface, EGL_HEIGHT, &h); - g_ScreenWidth = (float)w; - g_ScreenHeight = (float)h; + g_Width = (float)w; g_Height = (float)h; } if (!g_Initialized) { ImGui::CreateContext(); ImGui_ImplOpenGL3_Init("#version 300 es"); - ImGui::GetStyle().ScaleAllSizes(3.0f); + ImGui::GetStyle().ScaleAllSizes(3.0f); // 适配高分屏 g_Initialized = true; - LOGI("ImGui Render Started: %.fx%.f", g_ScreenWidth, g_ScreenHeight); } ImGui_ImplOpenGL3_NewFrame(); @@ -96,26 +66,20 @@ EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) { if (g_ShowMenu) { ImGuiIO& io = ImGui::GetIO(); - io.DisplaySize = ImVec2(g_ScreenWidth, g_ScreenHeight); - - // 关键注入 - io.MousePos = ImVec2(g_TouchState.x, g_TouchState.y); - io.MouseDown[0] = g_TouchState.down; + io.DisplaySize = ImVec2(g_Width, g_Height); + io.MousePos = ImVec2(g_Touch.x, g_Touch.y); + io.MouseDown[0] = g_Touch.down; - ImGui::SetNextWindowSize(ImVec2(500, 450), ImGuiCond_FirstUseEver); - if (ImGui::Begin("AndKitty SGame Final", &g_ShowMenu)) { - ImGui::TextColored(ImVec4(0, 1, 1, 1), "Input Mode: Raw System Hook"); + ImGui::SetNextWindowSize(ImVec2(550, 400), ImGuiCond_FirstUseEver); + if (ImGui::Begin("AndKitty SGame Lite", &g_ShowMenu)) { + ImGui::Text("Simple Input Mode Active"); ImGui::Separator(); + ImGui::Text("Touch: %.1f, %.1f (%s)", g_Touch.x, g_Touch.y, g_Touch.down ? "DOWN" : "UP"); - ImGui::Text("Screen: %.0f x %.0f", g_ScreenWidth, g_ScreenHeight); - ImGui::Text("Touch Pos: %.1f, %.1f", g_TouchState.x, g_TouchState.y); - ImGui::Text("Touch Down: %s", g_TouchState.down ? "YES" : "NO"); + static float f = 0.5f; + ImGui::SliderFloat("Value Test", &f, 0.0f, 1.0f); - ImGui::Separator(); - static bool esp = false; - ImGui::Checkbox("Enable Visuals", &esp); - - if (ImGui::Button("Close Menu")) g_ShowMenu = false; + if (ImGui::Button("Hide Menu")) g_ShowMenu = false; } ImGui::End(); } @@ -125,30 +89,17 @@ EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) { return old_eglSwapBuffers(dpy, surface); } -// --- 初始化线程 --- void* init_thread(void*) { - LOGI("New Input Strategy: Hooking AMotionEvent_getRaw..."); - - // 1. Hook 渲染 + // 渲染 Hook void* egl = dlsym(RTLD_DEFAULT, "eglSwapBuffers"); if (egl) DobbyHook(egl, (void*)hook_eglSwapBuffers, (void**)&old_eglSwapBuffers); - // 2. Hook libandroid.so 的底层坐标获取函数 - void* libandroid = dlopen("libandroid.so", RTLD_NOW); - if (libandroid) { - void* getX = dlsym(libandroid, "AMotionEvent_getRawX"); - void* getY = dlsym(libandroid, "AMotionEvent_getRawY"); - void* getAct = dlsym(libandroid, "AMotionEvent_getAction"); - - // 如果没有 getRawX (旧版本安卓),尝试 getX - if (!getX) getX = dlsym(libandroid, "AMotionEvent_getX"); - if (!getY) getY = dlsym(libandroid, "AMotionEvent_getY"); - - if (getX) DobbyHook(getX, (void*)hook_getRawX, (void**)&old_getRawX); - if (getY) DobbyHook(getY, (void*)hook_getRawY, (void**)&old_getRawY); - if (getAct) DobbyHook(getAct, (void*)hook_getAction, (void**)&old_getAction); - - LOGI("Raw Hooks Result: X:%p Y:%p Act:%p", getX, getY, getAct); + // 输入 Hook: 锁定 libandroid.so 或直接从 RTLD_DEFAULT 找 + // 绝大多数 Mod 使用 AMotionEvent_getPointerId 作为切入点 + void* getPointerId = dlsym(RTLD_DEFAULT, "AMotionEvent_getPointerId"); + if (getPointerId) { + DobbyHook(getPointerId, (void*)hook_getPointerId, (void**)&old_getPointerId); + LOGI("Simple Hook Applied: getPointerId"); } return nullptr;