Update main.cpp

This commit is contained in:
gggaa2152 2026-04-06 10:00:22 +08:00 committed by GitHub
parent 9fdb68ad78
commit f98e31e1bc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

107
main.cpp
View File

@ -8,6 +8,7 @@
#include "dobby.h"
#include <android/log.h>
#include <android/input.h>
#include <android/native_window.h>
#define LOG_TAG "AndKitty_Menu"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
@ -17,46 +18,32 @@ static bool g_Initialized = false;
static bool g_ShowMenu = true;
static float g_Width = 0, g_Height = 0;
// 存储触摸数据
struct {
float x, y;
bool down;
} g_Touch = {0, 0, false};
// --- 原函数指针 ---
typedef int32_t (*p_getPointerId)(const AInputEvent* motion_event, size_t pointer_index);
static p_getPointerId old_getPointerId = nullptr;
typedef int (*p_AInputQueue_getEvent)(AInputQueue* queue, AInputEvent** out_event);
static p_AInputQueue_getEvent old_getEvent = nullptr;
typedef EGLBoolean (*p_eglSwapBuffers)(EGLDisplay dpy, EGLSurface surface);
static p_eglSwapBuffers old_eglSwapBuffers = nullptr;
// --- 【GitHub 常用方案】直接从 PointerId 入手 ---
int32_t hook_getPointerId(const AInputEvent* event, size_t index) {
if (g_ShowMenu && event != nullptr) {
int type = AInputEvent_getType(event);
if (type == AINPUT_EVENT_TYPE_MOTION) {
int action = AMotionEvent_getAction(event) & AMOTION_EVENT_ACTION_MASK;
// 获取手指 0 的位置
float rawX = AMotionEvent_getX(event, 0);
float rawY = AMotionEvent_getY(event, 0);
// 只有当坐标有效时才更新,防止跳动
if (rawX >= 0 && rawY >= 0) {
g_Touch.x = rawX;
g_Touch.y = rawY;
if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
g_Touch.down = true;
} else if (action == AMOTION_EVENT_ACTION_UP || action == AMOTION_EVENT_ACTION_POINTER_UP || action == AMOTION_EVENT_ACTION_CANCEL) {
g_Touch.down = false;
}
}
// 如果菜单显示,且触摸在菜单范围内,你可以选择性拦截 (此处暂不拦截以保证游戏操作)
// --- 【核心】GitHub 常用:ImGui 官方 Android 事件处理逻辑 ---
// 这种方式不需要手动写坐标,直接把 Android 原生事件丢给 ImGui
int hook_AInputQueue_getEvent(AInputQueue* queue, AInputEvent** out_event) {
int res = old_getEvent(queue, out_event);
if (res >= 0 && g_ShowMenu && out_event && *out_event) {
// 调用 ImGui 内部的 Android 处理函数 (需要保证 ImGui 已初始化)
if (g_Initialized) {
ImGui_ImplAndroid_HandleInputEvent(*out_event);
}
// 可选:如果触摸在菜单内,返回 1 消耗掉事件,让游戏收不到点击
/*
ImGuiIO& io = ImGui::GetIO();
if (io.WantCaptureMouse) {
// 注意:某些游戏拦截事件会导致摇杆卡死,初次建议不拦截
}
*/
}
return old_getPointerId(event, index);
return res;
}
// --- 渲染逻辑 ---
@ -70,39 +57,44 @@ EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) {
if (!g_Initialized) {
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
// 关键:告诉 ImGui 屏幕尺寸
io.DisplaySize = ImVec2(g_Width, g_Height);
ImGui_ImplOpenGL3_Init("#version 300 es");
// 样式适配
// 设置样式
ImGui::StyleColorsDark();
ImGui::GetStyle().ScaleAllSizes(3.0f);
g_Initialized = true;
LOGI("ImGui Context Ready. Resolution: %.fx%.f", g_Width, g_Height);
LOGI("ImGui Full Init Success: %.fx%.f", g_Width, g_Height);
}
ImGui_ImplOpenGL3_NewFrame();
ImGui::NewFrame();
if (g_ShowMenu) {
ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = ImVec2(g_Width, g_Height);
// 【核心】将 Hook 到的坐标精准同步给 ImGui
io.MousePos = ImVec2(g_Touch.x, g_Touch.y);
io.MouseDown[0] = g_Touch.down;
// 设置窗口位置和大小
ImGui::SetNextWindowPos(ImVec2(100, 100), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(600, 450), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(600, 400), ImGuiCond_FirstUseEver);
if (ImGui::Begin("AndKitty SGame Stable", &g_ShowMenu)) {
ImGui::Text("Github Strategy: PointerId Monitoring");
if (ImGui::Begin("AndKitty SGame Ultimate", &g_ShowMenu)) {
ImGui::TextColored(ImVec4(0, 1, 1, 1), "Input: AInputQueue + HandleEvent");
ImGui::Separator();
ImGui::Text("MousePos: %.1f, %.1f", io.MousePos.x, io.MousePos.y);
ImGui::Text("IsDown: %s", io.MouseDown[0] ? "True" : "False");
ImGuiIO& io = ImGui::GetIO();
ImGui::Text("Mouse Pos: %.1f, %.1f", io.MousePos.x, io.MousePos.y);
ImGui::Text("WantCapture: %s", io.WantCaptureMouse ? "YES" : "NO");
ImGui::Separator();
static float menu_alpha = 0.8f;
ImGui::SliderFloat("Menu Alpha", &menu_alpha, 0.1f, 1.0f);
static bool esp = false;
ImGui::Checkbox("Enable ESP", &esp);
static int radius = 50;
ImGui::SliderInt("Range", &radius, 10, 200);
if (ImGui::Button("Close Menu")) g_ShowMenu = false;
}
ImGui::End();
@ -114,19 +106,20 @@ EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) {
}
void* init_thread(void*) {
LOGI("Applying Stable Input Hooks...");
// 渲染
LOGI("Applying Source-Level Input Strategy...");
// 1. Hook SwapBuffers
void* egl = dlsym(RTLD_DEFAULT, "eglSwapBuffers");
if (egl) DobbyHook(egl, (void*)hook_eglSwapBuffers, (void**)&old_eglSwapBuffers);
// 输入: 参考 GitHub 方案,锁定 libandroid.so 中的 AMotionEvent_getPointerId
// 2. Hook AInputQueue_getEvent
// 这是 Android 系统的“咽喉”,所有事件都会流经这里
void* libandroid = dlopen("libandroid.so", RTLD_NOW);
if (libandroid) {
void* getPointerId = dlsym(libandroid, "AMotionEvent_getPointerId");
if (getPointerId) {
DobbyHook(getPointerId, (void*)hook_getPointerId, (void**)&old_getPointerId);
LOGI("Input Monitoring Active via PointerId");
void* getEvent = dlsym(libandroid, "AInputQueue_getEvent");
if (getEvent) {
DobbyHook(getEvent, (void*)hook_AInputQueue_getEvent, (void**)&old_getEvent);
LOGI("AInputQueue Hooked Successfully.");
}
}