Update main.cpp

This commit is contained in:
gggaa2152 2026-04-06 08:51:48 +08:00 committed by GitHub
parent 3ed773db31
commit 086413a3d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

119
main.cpp
View File

@ -5,6 +5,7 @@
#include <EGL/egl.h>
#include <GLES3/gl3.h>
#include <dlfcn.h>
#include <string>
#include "dobby.h"
#include "imgui.h"
#include "imgui_impl_android.h"
@ -19,49 +20,57 @@ static bool g_ShowMenu = true;
typedef EGLBoolean (*p_eglSwapBuffers)(EGLDisplay dpy, EGLSurface surface);
static p_eglSwapBuffers old_eglSwapBuffers = nullptr;
// --- 触摸处理逻辑 ---
// 处理 Android 原始输入事件并同步给 ImGui
bool HandleAndroidInput(AInputEvent* event, ImGuiIO& io) {
// --- 触摸同步逻辑 ---
void SyncImGuiInput(AInputEvent* event) {
if (!g_Initialized || !g_ShowMenu) return;
ImGuiIO& io = ImGui::GetIO();
int32_t type = AInputEvent_getType(event);
if (type == AINPUT_EVENT_TYPE_MOTION) {
int32_t action = AMotionEvent_getAction(event) & AMOTION_EVENT_ACTION_MASK;
int32_t action = AMotionEvent_getAction(event);
int32_t action_code = action & AMOTION_EVENT_ACTION_MASK;
float x = AMotionEvent_getX(event, 0);
float y = AMotionEvent_getY(event, 0);
io.MousePos = ImVec2(x, y);
if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
if (action_code == AMOTION_EVENT_ACTION_DOWN || action_code == AMOTION_EVENT_ACTION_POINTER_DOWN) {
io.MouseDown[0] = true;
} else if (action == AMOTION_EVENT_ACTION_UP || action == AMOTION_EVENT_ACTION_POINTER_UP) {
} else if (action_code == AMOTION_EVENT_ACTION_UP || action_code == AMOTION_EVENT_ACTION_POINTER_UP || action_code == AMOTION_EVENT_ACTION_CANCEL) {
io.MouseDown[0] = false;
}
// 如果点击在 ImGui 窗口上,返回 true 以拦截事件,不传给游戏
return ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow) || ImGui::IsAnyItemHovered();
}
return false;
}
// Hook 目标:AInputQueue_getEvent (这是 Android Native 游戏获取触摸的最上层接口)
// --- Hook 1: AInputQueue (通用入口) ---
typedef int32_t (*p_AInputQueue_getEvent)(AInputQueue* queue, AInputEvent** outEvent);
static p_AInputQueue_getEvent old_getEvent = nullptr;
static p_AInputQueue_getEvent old_AInputQueue_getEvent = nullptr;
int32_t hook_AInputQueue_getEvent(AInputQueue* queue, AInputEvent** outEvent) {
int32_t res = old_getEvent(queue, outEvent);
if (res >= 0 && g_ShowMenu && g_Initialized) {
// 将事件发给 ImGui 处理
if (HandleAndroidInput(*outEvent, ImGui::GetIO())) {
// 如果 ImGui 消耗了此事件,我们可以在此处对 event 进行 finish 处理
// 但为了稳定性,通常我们只同步坐标而不完全拦截,或者根据需要返回
}
int32_t res = old_AInputQueue_getEvent(queue, outEvent);
if (res >= 0 && outEvent && *outEvent) {
SyncImGuiInput(*outEvent);
}
return res;
}
// --- Hook 2: 直接 Hook AMotionEvent 获取函数 (防止 libunity.so 绕过 Queue) ---
typedef float (*p_AMotionEvent_getX)(const AInputEvent* motion_event, size_t pointer_index);
static p_AMotionEvent_getX old_getX = nullptr;
float hook_AMotionEvent_getX(const AInputEvent* motion_event, size_t pointer_index) {
float x = old_getX(motion_event, pointer_index);
if (g_ShowMenu && g_Initialized && pointer_index == 0) {
ImGui::GetIO().MousePos.x = x;
}
return x;
}
// 渲染钩子
EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) {
if (!g_Initialized) {
LOGI("Initializing ImGui for SGame...");
EGLint width, height;
eglQuerySurface(dpy, surface, EGL_WIDTH, &width);
eglQuerySurface(dpy, surface, EGL_HEIGHT, &height);
@ -70,13 +79,12 @@ EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) {
ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = ImVec2((float)width, (float)height);
// 针对王者高分辨率屏优化缩放
float scale = (float)width / 1920.0f;
float scale = (float)width / 1920.0f;
ImGui::GetStyle().ScaleAllSizes(scale > 1.2f ? scale : 1.2f);
ImGui_ImplOpenGL3_Init("#version 300 es");
g_Initialized = true;
LOGI("ImGui Render Initialized: %d x %d", width, height);
LOGI("ImGui Initialized for Unity SGame: %d x %d", width, height);
}
if (g_ShowMenu) {
@ -84,21 +92,17 @@ EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) {
ImGui::NewFrame();
ImGui::SetNextWindowPos(ImVec2(100, 100), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(550, 380), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(550, 400), ImGuiCond_FirstUseEver);
if (ImGui::Begin("AndKitty Menu (SGame)", &g_ShowMenu)) {
ImGui::TextColored(ImVec4(0, 1, 0, 1), "Touch Status: Hooked");
// 确保没有任何禁止移动的 Flag
if (ImGui::Begin("AndKitty SGame Unity", &g_ShowMenu)) {
ImGui::TextColored(ImVec4(0, 1, 1, 1), "Module: libunity.so Context");
ImGui::Separator();
static bool esp_line = false;
ImGui::Checkbox("ESP Line", &esp_line);
static bool esp = false;
ImGui::Checkbox("ESP Enable", &esp);
static float f_val = 0.5f;
ImGui::SliderFloat("FOV", &f_val, 0.0f, 1.0f);
if (ImGui::Button("Hide Menu")) {
g_ShowMenu = false;
}
if (ImGui::Button("Close Menu")) g_ShowMenu = false;
}
ImGui::End();
@ -110,33 +114,36 @@ EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) {
}
void* init_thread(void*) {
LOGI("Plugin thread: Waiting 10s for SGame environment...");
sleep(10);
LOGI("Waiting for libunity.so and libandroid.so...");
// 循环等待 libunity.so 加载完成,这对 Unity 游戏很重要
while (true) {
if (dlsym(RTLD_DEFAULT, "eglSwapBuffers") && dlopen("libunity.so", RTLD_NOLOAD)) {
break;
}
usleep(500000);
}
sleep(5); // 额外等待确保引擎初始化
// 1. Hook 渲染
void* swap_addr = dlsym(RTLD_DEFAULT, "eglSwapBuffers");
if (swap_addr) {
LOGI("Hooking eglSwapBuffers...");
DobbyHook(swap_addr, (void*)hook_eglSwapBuffers, (void**)&old_eglSwapBuffers);
DobbyHook(swap_addr, (void*)hook_eglSwapBuffers, (void**)&old_eglSwapBuffers);
// 2. Hook 系统输入队列 (通用)
void* input_addr = dlsym(RTLD_DEFAULT, "AInputQueue_getEvent");
if (input_addr) {
DobbyHook(input_addr, (void*)hook_AInputQueue_getEvent, (void**)&old_AInputQueue_getEvent);
}
// 2. Hook 输入 (针对王者荣耀这种 Native 架构)
void* getEvent_addr = dlsym(RTLD_DEFAULT, "AInputQueue_getEvent");
if (getEvent_addr) {
LOGI("Hooking AInputQueue_getEvent for Touch...");
DobbyHook(getEvent_addr, (void*)hook_AInputQueue_getEvent, (void**)&old_getEvent);
} else {
// 备选方案:尝试从 libandroid.so 加载
void* h_android = dlopen("libandroid.so", RTLD_NOW);
if (h_android) {
getEvent_addr = dlsym(h_android, "AInputQueue_getEvent");
if (getEvent_addr) {
DobbyHook(getEvent_addr, (void*)hook_AInputQueue_getEvent, (void**)&old_getEvent);
}
}
// 3. 针对 Unity 的“强制同步”:Hook AMotionEvent_getX
// 很多版本的 Unity 直接调用这个函数获取坐标,Hook 这里可以强行同步 ImGui 坐标
void* getx_addr = dlsym(RTLD_DEFAULT, "AMotionEvent_getX");
if (getx_addr) {
LOGI("Hooking AMotionEvent_getX for Unity focus...");
DobbyHook(getx_addr, (void*)hook_AMotionEvent_getX, (void**)&old_getX);
}
LOGI("Hooking Process Completed. Ready for SGame.");
LOGI("Unity SGame Hooks Ready.");
return nullptr;
}