Update main.cpp

This commit is contained in:
gggaa2152 2026-04-06 07:48:41 +08:00 committed by GitHub
parent ebd753b7a1
commit d29916daa7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

154
main.cpp
View File

@ -1,91 +1,117 @@
#include <jni.h>
#include <string>
#include <thread>
#include <chrono>
#include <dlfcn.h>
#include <android/native_window.h>
#include <android/native_window_jni.h> // 必须添加,提供 ANativeWindow_fromSurface
#include <unistd.h>
#include <android/log.h>
#include <EGL/egl.h>
#include <GLES3/gl3.h>
#include <android/log.h>
#include "dobby.h"
#include "imgui.h"
#include "backends/imgui_impl_android.h"
#include "backends/imgui_impl_opengl3.h"
#include "imgui_impl_android.h"
#include "imgui_impl_opengl3.h"
#include "dobby.h"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, "ModMenu", __VA_ARGS__)
// 日志配置
#define LOG_TAG "AndKitty"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
static ANativeWindow* g_AndroidWindow = nullptr;
static bool g_ImGuiReady = false;
static int g_GlWidth = 0, g_GlHeight = 0;
// --- 全局状态 ---
bool g_Initialized = false;
bool g_ShowMenu = true;
int g_Width = 0;
int g_Height = 0;
typedef EGLBoolean (*eglSwapBuffers_t)(EGLDisplay dpy, EGLSurface surface);
eglSwapBuffers_t original_eglSwapBuffers = nullptr;
// --- Hook 目标定义 ---
typedef EGLBoolean (*eglSwapBuffers_t)(EGLDisplay dpy, EGLSurface surf);
eglSwapBuffers_t orig_eglSwapBuffers = nullptr;
void RenderImGui() {
if (!g_ImGuiReady) return;
// --- 渲染 UI 逻辑 ---
void DrawImGuiMenu() {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplAndroid_NewFrame();
ImGui::NewFrame();
ImGui::Begin("Mod Menu");
ImGui::Text("Injected via eglSwapBuffers Hook");
ImGui::Text("FPS: %.1f", ImGui::GetIO().Framerate);
if (ImGui::Button("Click me")) {
LOGI("Button clicked!");
if (g_ShowMenu) {
// 设置菜单初始大小
ImGui::SetNextWindowSize(ImVec2(g_Width / 3.5f, g_Height / 2.5f), ImGuiCond_FirstUseEver);
if (ImGui::Begin("AndKitty Mod Menu", &g_ShowMenu)) {
ImGui::Text("分辨率: %d x %d", g_Width, g_Height);
ImGui::Separator();
static bool aim = false;
ImGui::Checkbox("自动瞄准 (Aimbot)", &aim);
if (ImGui::Button("隐藏菜单 (Hide)")) {
g_ShowMenu = false;
}
}
ImGui::End();
}
ImGui::End();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
EGLBoolean hooked_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) {
static bool initialized = false;
if (!initialized && g_AndroidWindow != nullptr) {
eglQuerySurface(dpy, surface, EGL_WIDTH, &g_GlWidth);
eglQuerySurface(dpy, surface, EGL_HEIGHT, &g_GlHeight);
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui_ImplAndroid_Init(g_AndroidWindow);
ImGui_ImplOpenGL3_Init("#version 300 es");
ImGui::StyleColorsDark();
initialized = true;
g_ImGuiReady = true;
LOGI("ImGui initialized! %dx%d", g_GlWidth, g_GlHeight);
}
if (g_ImGuiReady) RenderImGui();
return original_eglSwapBuffers(dpy, surface);
}
// --- Hook 后的 eglSwapBuffers ---
EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surf) {
if (!g_Initialized) {
// 动态获取真实屏幕宽高
eglQuerySurface(dpy, surf, EGL_WIDTH, &g_Width);
eglQuerySurface(dpy, surf, EGL_HEIGHT, &g_Height);
void InstallHook() {
void* libegl = dlopen("libEGL.so", RTLD_LAZY);
if (!libegl) { LOGI("Failed to load libEGL.so"); return; }
void* func = dlsym(libegl, "eglSwapBuffers");
if (func) {
DobbyHook(func, (void*)hooked_eglSwapBuffers, (void**)&original_eglSwapBuffers);
LOGI("Hook installed");
}
dlclose(libegl);
}
if (g_Width > 0 && g_Height > 0) {
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
// 将真实分辨率赋给 ImGui
io.DisplaySize = ImVec2((float)g_Width, (float)g_Height);
void SetupHooksThread() {
while (true) {
if (dlopen("libEGL.so", RTLD_LAZY | RTLD_NOLOAD)) {
InstallHook();
break;
// 初始化后端
ImGui_ImplAndroid_Init(nullptr);
ImGui_ImplOpenGL3_Init("#version 300 es");
ImGui::StyleColorsDark();
// 调整 UI 缩放以适配高分屏
ImGui::GetStyle().ScaleAllSizes(g_Height / 1080.0f);
g_Initialized = true;
LOGI("ImGui 适配完成: %d x %d", g_Width, g_Height);
}
}
DrawImGuiMenu();
return orig_eglSwapBuffers(dpy, surf);
}
// --- 渲染 Hook 查找 ---
void setup_render_hook() {
void* egl_handle = dlopen("libEGL.so", RTLD_LAZY);
if (egl_handle) {
void* target = dlsym(egl_handle, "eglSwapBuffers");
if (target) {
DobbyHook(target, (dobby_dummy_func_t)hook_eglSwapBuffers, (dobby_dummy_func_t*)&orig_eglSwapBuffers);
LOGI("成功挂载 eglSwapBuffers");
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
__attribute__((constructor))
void Init() {
std::thread(SetupHooksThread).detach();
}
extern "C" JNIEXPORT void JNICALL
Java_com_yourpackage_ModMenu_setNativeWindow(JNIEnv* env, jobject thiz, jobject surface) {
if (surface != nullptr) {
g_AndroidWindow = ANativeWindow_fromSurface(env, surface);
// --- AndKittyInjector 入口 (JNI_OnLoad) ---
extern "C" jint JNIEXPORT JNI_OnLoad(JavaVM* vm, void *key) {
// 校验 AndKittyInjector 的特有 Key (1337)
if (key != (void*)1337) {
return JNI_VERSION_1_6;
}
LOGI("AndKittyInjector 授权成功,准备注入...");
std::thread([]() {
// 等待游戏渲染环境完全启动
sleep(5);
setup_render_hook();
}).detach();
return JNI_VERSION_1_6;
}