From 2fa1f50aedb3d6ca496bf65c13e1e9df9f0ab981 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <101191390+HIllya51@users.noreply.github.com> Date: Wed, 20 Mar 2024 23:20:17 +0800 Subject: [PATCH] simplfy --- LunaHook/engines/mono/monocommon.hpp | 39 ++++------------------ LunaHook/engines/python/python.cpp | 9 ++---- LunaHook/engines/python/python2.cpp | 20 +++--------- LunaHook/engines/v8/v8.cpp | 48 ++++++++-------------------- LunaHook/stackoffset.hpp | 18 +++++++++++ 5 files changed, 44 insertions(+), 90 deletions(-) diff --git a/LunaHook/engines/mono/monocommon.hpp b/LunaHook/engines/mono/monocommon.hpp index 6db11ff..8c3c04b 100644 --- a/LunaHook/engines/mono/monocommon.hpp +++ b/LunaHook/engines/mono/monocommon.hpp @@ -33,24 +33,13 @@ void commonsolvemonostring(uintptr_t offset,uintptr_t *data, size_t*len){ } void mscorlib_system_string_hook_fun(hook_stack* stack, HookParam *hp, uintptr_t *data, uintptr_t *split, size_t*len) { - #ifdef _WIN64 - uintptr_t offset=stack->rcx; - #else - uintptr_t offset=stack->stack[1]; - #endif - commonsolvemonostring(offset,data,len); + commonsolvemonostring(stack->ARG1,data,len); } void mscorlib_system_string_InternalSubString_hook_fun(hook_stack* stack, HookParam *hp, uintptr_t *data, uintptr_t *split, size_t*len) { - #ifdef _WIN64 - uintptr_t offset=stack->rcx; - uintptr_t startIndex=stack->rdx; - uintptr_t length=stack->r8; - #else - uintptr_t offset=stack->stack[1]; - uintptr_t startIndex=stack->stack[2]; - uintptr_t length=stack->stack[3]; - #endif + uintptr_t offset=stack->ARG1; + uintptr_t startIndex=stack->ARG2; + uintptr_t length=stack->ARG3; MonoString* string = (MonoString*)offset; if(string==0)return; @@ -73,21 +62,11 @@ auto mscorlib_system_string_funcs=std::unordered_map{ }; void unity_ui_string_hook_fun(hook_stack* stack, HookParam *hp, uintptr_t *data, uintptr_t *split, size_t*len) { - #ifdef _WIN64 - uintptr_t offset=stack->rdx; - #else - uintptr_t offset=stack->stack[2]; - #endif - commonsolvemonostring(offset,data,len); + commonsolvemonostring(stack->ARG2,data,len); } void unity_ui_string_hook_after(hook_stack* stack,void* data, size_t len) { - #ifdef _WIN64 - uintptr_t offset=stack->rdx; - #else - uintptr_t offset=stack->stack[2]; - #endif - MonoString* string = (MonoString*)offset; + MonoString* string = (MonoString*)stack->ARG2; if(string==0)return; if(wcslen((wchar_t*)string->chars)!=string->length)return; @@ -96,11 +75,7 @@ void unity_ui_string_hook_after(hook_stack* stack,void* data, size_t len) memcpy(newstring,string,sizeof(MonoString)); wcscpy((wchar_t*)newstring->chars,(wchar_t*)data); newstring->length=len/2; - #ifdef _WIN64 - stack->rdx=(uintptr_t)newstring; - #else - stack->stack[2]=(uintptr_t)newstring; - #endif + stack->ARG2=(uintptr_t)newstring; } void MONO_IL2CPP_NEW_HOOK(void* text_fun,void* hook_after, uintptr_t addr,const char*name){ diff --git a/LunaHook/engines/python/python.cpp b/LunaHook/engines/python/python.cpp index 90f7e8f..35a5798 100644 --- a/LunaHook/engines/python/python.cpp +++ b/LunaHook/engines/python/python.cpp @@ -42,13 +42,8 @@ void PyRunScript(const char* script) void hook_internal_renpy_call_host(){ HookParam hp_internal; hp_internal.address=(uintptr_t)internal_renpy_call_host; - #ifndef _WIN64 - hp_internal.offset=get_stack(1); - hp_internal.split=get_stack(2); - #else - hp_internal.offset=get_reg(regs::rcx); - hp_internal.split=get_reg(regs::rdx); - #endif + hp_internal.offset=GETARG1; + hp_internal.split=GETARG2; hp_internal.type=USING_SPLIT|USING_STRING|CODEC_UTF16|EMBED_ABLE|EMBED_BEFORE_SIMPLE|EMBED_AFTER_NEW; NewHook(hp_internal, "internal_renpy_call_host"); PyRunScript(LoadResData(L"renpy_hook_text",L"PYSOURCE").c_str()); diff --git a/LunaHook/engines/python/python2.cpp b/LunaHook/engines/python/python2.cpp index bb9ffb7..ea8bf8d 100644 --- a/LunaHook/engines/python/python2.cpp +++ b/LunaHook/engines/python/python2.cpp @@ -1,6 +1,7 @@ #include"types.h" #include"python.h" #include"main.h" +#include"stackoffset.hpp" namespace { typedef wchar_t Py_UNICODE ; typedef size_t Py_ssize_t; @@ -82,11 +83,7 @@ bool InsertRenpyHook(){ hp.text_fun = [](hook_stack* stack, HookParam* hp, uintptr_t* data, uintptr_t* split, size_t* len) { - #ifndef _WIN64 - auto format=(PyObject *)stack->stack[1]; - #else - auto format=(PyObject *)stack->rcx; - #endif + auto format=(PyObject *)stack->ARG1; auto [strptr,strlen]=GetPyUnicodeString(format); *data=(uintptr_t)strptr; *len=0; @@ -101,18 +98,9 @@ bool InsertRenpyHook(){ hp.type|=EMBED_ABLE|EMBED_BEFORE_SIMPLE; hp.hook_after=[](hook_stack* stack,void* data, size_t len) { - #ifndef _WIN64 - auto format=(PyObject *)stack->stack[1]; - #else - auto format=(PyObject *)stack->rcx; - #endif + auto format=(PyObject *)stack->ARG1; if(format==NULL)return; - #ifndef _WIN64 - stack->stack[1]= - #else - stack->rcx= - #endif - (uintptr_t)PyUnicode_FromUnicode((Py_UNICODE *)data,len/2); + stack->ARG1=(uintptr_t)PyUnicode_FromUnicode((Py_UNICODE *)data,len/2); }; hookrenpy(module); } diff --git a/LunaHook/engines/v8/v8.cpp b/LunaHook/engines/v8/v8.cpp index 244c5d2..e459694 100644 --- a/LunaHook/engines/v8/v8.cpp +++ b/LunaHook/engines/v8/v8.cpp @@ -5,11 +5,7 @@ #include"main.h" #include"v8.h" #include"embed_util.h" -#ifndef _WIN64 -#define arg2 stack[2] -#else -#define arg2 rdx -#endif +#include"stackoffset.hpp" namespace{ bool hookClipboard(){ @@ -17,7 +13,7 @@ namespace{ hp.address=(uintptr_t)SetClipboardData; hp.type= USING_STRING|CODEC_UTF16|EMBED_ABLE|EMBED_BEFORE_SIMPLE; hp.text_fun=[](hook_stack* stack, HookParam *hp, uintptr_t* data, uintptr_t* split, size_t* len){ - HGLOBAL hClipboardData=(HGLOBAL)stack->arg2; + HGLOBAL hClipboardData=(HGLOBAL)stack->ARG2; *data=(uintptr_t)GlobalLock(hClipboardData); *len=wcslen((wchar_t*)*data)*2; GlobalUnlock(hClipboardData); @@ -33,7 +29,7 @@ namespace{ auto pchData = (wchar_t*)GlobalLock(hClipboardData); wcscpy(pchData, (wchar_t*)transwithfont.c_str()); GlobalUnlock(hClipboardData); - s->arg2=(uintptr_t)hClipboardData; + s->ARG2=(uintptr_t)hClipboardData; }; return NewHook(hp,"hookClipboard"); } @@ -53,12 +49,7 @@ typedef void(*RequestInterrupt_callback)(void*, void*); #define fnRunv1 "?Run@Script@v8@@QAE?AV?$Local@VValue@v8@@@2@XZ" #define fnCompilev2 "?Compile@Script@v8@@SA?AV?$MaybeLocal@VScript@v8@@@2@V?$Local@VContext@v8@@@2@V?$Local@VString@v8@@@2@PAVScriptOrigin@2@@Z" #define fnRunv2 "?Run@Script@v8@@QAE?AV?$MaybeLocal@VValue@v8@@@2@V?$Local@VContext@v8@@@2@@Z" -typedef void*(__thiscall *GetCurrentContextt)(void*, void*); -typedef void*(__thiscall*Runt1)(void*,void*); -typedef void*(__thiscall*Runt2)(void*,void*,void*); - -typedef void*(__thiscall *RequestInterruptt)(void*, RequestInterrupt_callback, void*); #else #define fnRequestInterrupt "?RequestInterrupt@Isolate@v8@@QEAAXP6AXPEAV12@PEAX@Z1@Z" #define fnNewFromUtf8v2 "?NewFromUtf8@String@v8@@SA?AV?$MaybeLocal@VString@v8@@@2@PEAVIsolate@2@PEBDW4NewStringType@2@H@Z" @@ -69,13 +60,13 @@ typedef void*(__thiscall *RequestInterruptt)(void*, RequestInterrupt_callback, v #define fnRunv1 "?Run@Script@v8@@QEAA?AV?$Local@VValue@v8@@@2@XZ" #define fnCompilev2 "?Compile@Script@v8@@SA?AV?$MaybeLocal@VScript@v8@@@2@V?$Local@VContext@v8@@@2@V?$Local@VString@v8@@@2@PEAVScriptOrigin@2@@Z" #define fnRunv2 "?Run@Script@v8@@QEAA?AV?$MaybeLocal@VValue@v8@@@2@V?$Local@VContext@v8@@@2@@Z" -typedef void*(*GetCurrentContextt)(void*, void*); -typedef void*(*Runt1)(void*,void*); -typedef void*(*Runt2)(void*,void*,void*); - -typedef void*(*RequestInterruptt)(void*, RequestInterrupt_callback, void*); #endif +typedef void*(THISCALL *GetCurrentContextt)(void*, void*); +typedef void*(THISCALL *Runt1)(void*,void*); +typedef void*(THISCALL *Runt2)(void*,void*,void*); +typedef void*(THISCALL *RequestInterruptt)(void*, RequestInterrupt_callback, void*); + typedef void*(*NewFromUtf8t)(void*, void*, const char*, int, int) ; typedef void*(*Compilet)(void*, void*, void*, void*); RequestInterruptt RequestInterrupt; @@ -151,13 +142,8 @@ void v8runscript_isolate_bypass(hook_stack* stack, HookParam* hp, uintptr_t* dat static bool runonce=false; if(runonce)return; runonce=true; -#ifndef _WIN64 -#define isolatearg stack[2] -#else -#define isolatearg rdx -#endif - - auto isolate=(void*)stack->isolatearg;//测试正确,且和v8::Isolate::GetCurrent结果相同 + + auto isolate=(void*)stack->ARG2;//测试正确,且和v8::Isolate::GetCurrent结果相同 v8runscript_isolate(isolate); } void* v8getcurrisolate(HMODULE hmod){ @@ -226,20 +212,12 @@ namespace{ [](hook_stack* stack, HookParam* hp, uintptr_t* data, uintptr_t* split, size_t* len) { - #ifndef _WIN64 - auto length=((size_t(__thiscall*)(void*))Utf8Length)((void*)stack->ecx); - #else - auto length=((size_t(*)(void*))Utf8Length)((void*)stack->rcx); - #endif + auto length=((size_t(THISCALL *)(void*))Utf8Length)((void*)stack->THISCALLTHIS); if(!length)return; auto u8str=new char[length+1]; int writen; - #ifndef _WIN64 - ((size_t(__thiscall*)(void*,char*,int,int*,int))WriteUtf8)((void*)stack->ecx,u8str,length,&writen,0); - #else - ((size_t(*)(void*,char*,int,int*,int))WriteUtf8)((void*)stack->rcx,u8str,length,&writen,0); - #endif - *data=(uintptr_t)u8str; + ((size_t(THISCALL *)(void*,char*,int,int*,int))WriteUtf8)((void*)stack->THISCALLTHIS,u8str,length,&writen,0); + *data=(uintptr_t)u8str; *len=length; }; diff --git a/LunaHook/stackoffset.hpp b/LunaHook/stackoffset.hpp index 1856586..e6e8864 100644 --- a/LunaHook/stackoffset.hpp +++ b/LunaHook/stackoffset.hpp @@ -82,3 +82,21 @@ inline uintptr_t regof(regs reg,hook_stack* stack){ } return 0; } + +#ifndef _WIN64 +#define ARG1 stack[1] +#define ARG2 stack[2] +#define ARG3 stack[3] +#define THISCALL __thiscall +#define THISCALLTHIS ecx +#define GETARG1 get_stack(1) +#define GETARG2 get_stack(2) +#else +#define ARG1 rcx +#define ARG2 rdx +#define ARG3 r8 +#define THISCALLTHIS rcx +#define THISCALL +#define GETARG1 get_reg(regs::rcx) +#define GETARG2 get_reg(regs::rdx) +#endif \ No newline at end of file