Update main.cpp

This commit is contained in:
gggaa2152 2026-04-06 09:19:09 +08:00 committed by GitHub
parent 98d2362cde
commit e5df7d016f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

217
main.cpp
View File

@ -4,9 +4,6 @@
#include <pthread.h>
#include <thread>
#include <chrono>
#include <iostream>
#include <fstream>
#include <string>
#include <unistd.h>
#include <dlfcn.h>
@ -18,89 +15,81 @@
#include "imgui_impl_opengl3.h"
#include "dobby.h"
#include <android/log.h>
#include <android/input.h>
#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_MenuX = 100.0f;
static float g_MenuY = 100.0f;
static float g_ScreenWidth = 0.0f;
static float g_ScreenHeight = 0.0f;
// 触摸状态全局快照 (用于底层拦截同步)
// 记录触摸状态
struct {
float x;
float y;
bool down;
} g_RealTimeTouch = {0.0f, 0.0f, false};
} g_TouchState = {0.0f, 0.0f, false};
// --- Unity 函数指针 ---
typedef bool (*p_GetMouseButton)(int button);
static p_GetMouseButton old_GetMouseButton = nullptr;
typedef void* (*p_get_mousePosition)(void* outPos);
static p_get_mousePosition old_get_mousePosition = nullptr;
// --- 原函数指针声明 ---
typedef EGLBoolean (*p_eglSwapBuffers)(EGLDisplay dpy, EGLSurface surface);
static p_eglSwapBuffers old_eglSwapBuffers = nullptr;
typedef float (*p_AMotionEvent_getX)(const AInputEvent* motion_event, size_t pointer_index);
static p_AMotionEvent_getX old_getX = nullptr;
typedef float (*p_AMotionEvent_getY)(const AInputEvent* motion_event, size_t pointer_index);
static p_AMotionEvent_getY old_getY = nullptr;
typedef int32_t (*p_AMotionEvent_getAction)(const AInputEvent* motion_event);
static p_AMotionEvent_getAction old_getAction = nullptr;
// --- 触摸 Hook 回调 ---
// 拦截系统底层坐标换算,确保拿到最真实的点击位置
float hook_AMotionEvent_getX(const AInputEvent* event, size_t index) {
float x = old_getX(event, index);
if (index == 0) g_RealTimeTouch.x = x;
return x;
// --- Hook 回调 ---
bool hook_GetMouseButton(int button) {
if (old_GetMouseButton) {
bool res = old_GetMouseButton(button);
if (button == 0) g_TouchState.down = res;
return res;
}
return false;
}
float hook_AMotionEvent_getY(const AInputEvent* event, size_t index) {
float y = old_getY(event, index);
if (index == 0) g_RealTimeTouch.y = y;
return y;
}
int32_t hook_AMotionEvent_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_RealTimeTouch.down = true;
else if (code == AMOTION_EVENT_ACTION_UP || code == AMOTION_EVENT_ACTION_POINTER_UP || code == AMOTION_EVENT_ACTION_CANCEL)
g_RealTimeTouch.down = false;
return action;
void* hook_get_mousePosition(void* outPos) {
if (old_get_mousePosition) {
void* res = old_get_mousePosition(outPos);
if (outPos) {
float* pos = (float*)outPos;
g_TouchState.x = pos[0];
// 只有拿到屏幕高度后才进行 Y 轴翻转,否则先存原始值
g_TouchState.y = (g_ScreenHeight > 0) ? (g_ScreenHeight - pos[1]) : pos[1];
}
return res;
}
return outPos;
}
// --- 渲染逻辑 ---
void SetupImGui(int width, int height) {
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
// 设置字体大小等 UI 风格
ImGui::StyleColorsDark();
ImGui_ImplOpenGL3_Init("#version 300 es");
g_Initialized = true;
LOGI("ImGui Initialized: %dx%d", width, height);
}
EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) {
static int width = 0, height = 0;
if (width == 0 || height == 0) {
if (g_ScreenWidth <= 0) {
EGLint w, h;
eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
width = w; height = h;
g_ScreenWidth = (float)w;
g_ScreenHeight = (float)h;
}
if (!g_Initialized) {
SetupImGui(width, height);
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
// 针对移动端的优化设置
io.IniFilename = nullptr;
ImGui::StyleColorsDark();
ImGui_ImplOpenGL3_Init("#version 300 es");
// 字体缩放适配高分屏
ImGui::GetStyle().ScaleAllSizes(3.0f);
g_Initialized = true;
LOGI("ImGui Render Init Success: %.fx%.f", g_ScreenWidth, g_ScreenHeight);
}
ImGui_ImplOpenGL3_NewFrame();
@ -108,77 +97,99 @@ EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) {
if (g_ShowMenu) {
ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = ImVec2(g_ScreenWidth, g_ScreenHeight);
// 【核心注入】:将底层 Hook 拿到的坐标强制塞给 ImGui
io.MousePos = ImVec2(g_RealTimeTouch.x, g_RealTimeTouch.y);
io.MouseDown[0] = g_RealTimeTouch.down;
io.DisplaySize = ImVec2((float)width, (float)height);
// 关键:强制注入 Unity 捕获的坐标
io.MousePos = ImVec2(g_TouchState.x, g_TouchState.y);
io.MouseDown[0] = g_TouchState.down;
// 绘制菜单界面
ImGui::SetNextWindowPos(ImVec2(g_MenuX, g_MenuY), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(400, 300), ImGuiCond_FirstUseEver);
if (ImGui::Begin("AndKitty Mod Menu", &g_ShowMenu)) {
ImGui::Text("Status: Running");
ImGui::SetNextWindowSize(ImVec2(550, 400), ImGuiCond_FirstUseEver);
if (ImGui::Begin("AndKitty SGame Debugger", &g_ShowMenu)) {
ImGui::TextColored(ImVec4(0, 1, 0, 1), "Input Mode: Unity Global Hook");
ImGui::Separator();
static bool esp = false;
ImGui::Checkbox("Enable ESP", &esp);
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 Status: %s", g_TouchState.down ? "DOWN" : "UP");
static float aim_speed = 5.0f;
ImGui::SliderFloat("Aim Speed", &aim_speed, 1.0f, 20.0f);
ImGui::Separator();
static bool esp = false;
ImGui::Checkbox("Enable ESP Line", &esp);
if (ImGui::Button("Hide Menu")) {
g_ShowMenu = false;
if (ImGui::Button("Reset Touch State")) {
g_TouchState.down = false;
}
// 调试信息:显示实时捕获的坐标
ImGui::Text("Touch: X:%.1f Y:%.1f (%s)",
g_RealTimeTouch.x, g_RealTimeTouch.y,
g_RealTimeTouch.down ? "DOWN" : "UP");
if (ImGui::Button("Close Menu")) g_ShowMenu = false;
}
ImGui::End();
}
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
return old_eglSwapBuffers(dpy, surface);
}
// --- 初始化线程 ---
// --- 自动搜索符号并 Hook ---
void* init_thread(void*) {
LOGI("Plugin thread started, waiting for game initialization...");
sleep(15); // 等待游戏过掉检测和 Logo 加载
LOGI("Plugin thread started. Waiting for libunity.so...");
void* handle = nullptr;
while (!handle) {
handle = dlopen("libunity.so", RTLD_NOW);
if (!handle) usleep(500000);
}
LOGI("libunity.so found at %p", handle);
// 1. Hook 渲染函数
void* egl_addr = dlsym(RTLD_DEFAULT, "eglSwapBuffers");
if (egl_addr) {
DobbyHook(egl_addr, (void*)hook_eglSwapBuffers, (void**)&old_eglSwapBuffers);
LOGI("Hooked eglSwapBuffers at %p", egl_addr);
// 1. Hook SwapBuffers (渲染)
void* egl = dlsym(RTLD_DEFAULT, "eglSwapBuffers");
if (egl) DobbyHook(egl, (void*)hook_eglSwapBuffers, (void**)&old_eglSwapBuffers);
// 2. 尝试多种可能的 Unity 输入符号 (SGame 特化)
// 列表包含常见混淆名和标准名
const char* btn_syms[] = {
"_ZN5Unity5Input14GetMouseButtonEi",
"UnityInputGetMouseButton",
"Input_GetMouseButton",
"_ZN11UnityEngine5Input14GetMouseButtonEi"
};
const char* pos_syms[] = {
"_ZN5Unity5Input17get_mousePositionEv",
"UnityInputGetMousePosition",
"Input_get_mousePosition",
"_ZN11UnityEngine5Input17get_mousePositionEv"
};
void* get_btn = nullptr;
void* get_pos = nullptr;
for (const char* s : btn_syms) {
get_btn = dlsym(handle, s);
if (get_btn) {
LOGI("Found MouseButton Sym: %s", s);
break;
}
}
// 2. Hook 底层触摸 (libandroid.so)
// 即使游戏屏蔽了上层的 InputQueue,底层的这些函数依然会被调用
void* h_android = dlopen("libandroid.so", RTLD_NOW);
if (h_android) {
void* getX = dlsym(h_android, "AMotionEvent_getX");
void* getY = dlsym(h_android, "AMotionEvent_getY");
void* getAction = dlsym(h_android, "AMotionEvent_getAction");
if (getX) DobbyHook(getX, (void*)hook_AMotionEvent_getX, (void**)&old_getX);
if (getY) DobbyHook(getY, (void*)hook_AMotionEvent_getY, (void**)&old_getY);
if (getAction) DobbyHook(getAction, (void*)hook_AMotionEvent_getAction, (void**)&old_getAction);
LOGI("Touch Force-Sync Hooks Applied!");
for (const char* s : pos_syms) {
get_pos = dlsym(handle, s);
if (get_pos) {
LOGI("Found MousePosition Sym: %s", s);
break;
}
}
if (get_btn) DobbyHook(get_btn, (void*)hook_GetMouseButton, (void**)&old_GetMouseButton);
if (get_pos) DobbyHook(get_pos, (void*)hook_get_mousePosition, (void**)&old_get_mousePosition);
LOGI("Hooks Final Status - Render: %p, BTN: %p, POS: %p", egl, get_btn, get_pos);
return nullptr;
}
// .so 加载入口
__attribute__((constructor))
void main_entry() {
void entry() {
pthread_t t;
pthread_create(&t, nullptr, init_thread, nullptr);
}