Update veh_hook.cpp
This commit is contained in:
恍兮惚兮 2024-04-01 13:42:59 +08:00
parent 7cde15f054
commit 28c141e29f
5 changed files with 75 additions and 54 deletions

View File

@ -171,23 +171,31 @@ bool yuzusuyu::attach_function()
auto descriptor = *argidx(stack,idxDescriptor); // r8
auto entrypoint = *argidx(stack,idxEntrypoint); // r9
auto em_address = *(uintptr_t*)descriptor;
if(!entrypoint)return;
jitaddraddr(em_address,entrypoint,JITTYPE::YUZU);
auto em_address_off=em_address- 0x80004000;
if(emfunctionhooks.find(em_address_off)==emfunctionhooks.end() || !entrypoint)return;
auto op=emfunctionhooks.at(em_address_off);
if(!(checkiscurrentgame(op)))return;
HookParam hpinternal;
hpinternal.address=entrypoint;
hpinternal.emu_addr=em_address;//用于生成hcode
hpinternal.type=USING_STRING|NO_CONTEXT|BREAK_POINT|op.type;
hpinternal.text_fun=(decltype(hpinternal.text_fun))op.hookfunc;
hpinternal.filter_fun=(decltype(hpinternal.filter_fun))op.filterfun;
hpinternal.argidx=op.argidx;
hpinternal.padding=op.padding;
hpinternal.jittype=JITTYPE::YUZU;
NewHook(hpinternal,op.hookname);
[&](){
auto em_address_off=em_address- 0x80004000;
if(emfunctionhooks.find(em_address_off)==emfunctionhooks.end())return;
auto op=emfunctionhooks.at(em_address_off);
if(!(checkiscurrentgame(op)))return;
HookParam hpinternal;
hpinternal.address=entrypoint;
hpinternal.emu_addr=em_address;//用于生成hcode
hpinternal.type=USING_STRING|NO_CONTEXT|BREAK_POINT|op.type;
hpinternal.text_fun=(decltype(hpinternal.text_fun))op.hookfunc;
hpinternal.filter_fun=(decltype(hpinternal.filter_fun))op.filterfun;
hpinternal.argidx=op.argidx;
hpinternal.padding=op.padding;
hpinternal.jittype=JITTYPE::YUZU;
NewHook(hpinternal,op.hookname);
}();
[&](){
if(delayinserthook.find(em_address)==delayinserthook.end())return;
auto h=delayinserthook[em_address];
delayinserthook.erase(em_address);
NewHook(h.second,h.first.c_str());
}();
};
return NewHook(hp,"YuzuDoJit");
}

View File

@ -293,46 +293,52 @@ bool hookPPSSPPDoJit(){
hpinternal.user_value=hp->user_value;
hpinternal.address=stack->retaddr;
hpinternal.text_fun=[](hook_stack* stack, HookParam* hp, uintptr_t* data, uintptr_t* split, size_t* len){
auto ret=stack->RETADDR;
if(breakpoints.find(ret)!=breakpoints.end())return;
breakpoints.insert(ret);
auto em_address=*(uintptr_t*)(hp->user_value);
[&](){
auto ret=stack->RETADDR;
if(breakpoints.find(ret)!=breakpoints.end())return;
breakpoints.insert(ret);
jitaddraddr(em_address,ret,JITTYPE::PPSSPP);
jitaddraddr(em_address,ret,JITTYPE::PPSSPP);
if(emfunctionhooks.find(em_address)==emfunctionhooks.end())return;
if(!(checkiscurrentgame(emfunctionhooks.at(em_address))))return;
if(emfunctionhooks.find(em_address)==emfunctionhooks.end())return;
if(!(checkiscurrentgame(emfunctionhooks.at(em_address))))return;
auto op=emfunctionhooks.at(em_address);
ConsoleOutput("jit function addr %p",ret);
#ifndef _WIN64
BYTE sig[]={
0x8b,XX2,//mov reg,[ebp-off]
0x8b,0xc6,//mov eax,esi
0x25,0xff,0xff,0xff,0x3f,//and eax,0x3fffffff
0x89,XX,XX4,//mov [eax+base+off],reg
auto op=emfunctionhooks.at(em_address);
ConsoleOutput("jit function addr %p",ret);
#ifndef _WIN64
BYTE sig[]={
0x8b,XX2,//mov reg,[ebp-off]
0x8b,0xc6,//mov eax,esi
0x25,0xff,0xff,0xff,0x3f,//and eax,0x3fffffff
0x89,XX,XX4,//mov [eax+base+off],reg
};
auto findbase=MemDbg::findBytes(sig,sizeof(sig),ret,ret+0x20);
if(!findbase)
findbase=MemDbg::findBytes(sig,sizeof(sig),ret-0x1000,ret+0x1000);
if(!findbase)
ConsoleOutput("can't find emu_baseaddr");
PPSSPP::x86_baseaddr=(*(DWORD*)(findbase+12))&0xffff0000;
ConsoleOutput("x86 base addr %p",PPSSPP::x86_baseaddr);
#endif
HookParam hpinternal;
hpinternal.address=ret;
hpinternal.emu_addr=em_address;//用于生成hcode
hpinternal.type=USING_STRING|NO_CONTEXT|BREAK_POINT|op.type;
hpinternal.text_fun=(decltype(hpinternal.text_fun))op.hookfunc;
hpinternal.filter_fun=(decltype(hpinternal.filter_fun))op.filterfun;
hpinternal.argidx=op.argidx;
hpinternal.padding=op.padding;
hpinternal.jittype=JITTYPE::PPSSPP;
NewHook(hpinternal,op.hookname);
};
auto findbase=MemDbg::findBytes(sig,sizeof(sig),ret,ret+0x20);
if(!findbase)
findbase=MemDbg::findBytes(sig,sizeof(sig),ret-0x1000,ret+0x1000);
if(!findbase)
ConsoleOutput("can't find emu_baseaddr");
PPSSPP::x86_baseaddr=(*(DWORD*)(findbase+12))&0xffff0000;
ConsoleOutput("x86 base addr %p",PPSSPP::x86_baseaddr);
#endif
HookParam hpinternal;
hpinternal.address=ret;
hpinternal.emu_addr=em_address;//用于生成hcode
hpinternal.type=USING_STRING|NO_CONTEXT|BREAK_POINT|op.type;
hpinternal.text_fun=(decltype(hpinternal.text_fun))op.hookfunc;
hpinternal.filter_fun=(decltype(hpinternal.filter_fun))op.filterfun;
hpinternal.argidx=op.argidx;
hpinternal.padding=op.padding;
hpinternal.jittype=JITTYPE::PPSSPP;
NewHook(hpinternal,op.hookname);
}();
[&](){
if(delayinserthook.find(em_address)==delayinserthook.end())return;
auto h=delayinserthook[em_address];
delayinserthook.erase(em_address);
NewHook(h.second,h.first.c_str());
}();
};
static auto once=NewHook(hpinternal,"DoJitPtrRet");
};

View File

@ -207,7 +207,11 @@ bool NewHook_1(HookParam& hp, LPCSTR lpname)
bool NewHook(HookParam hp, LPCSTR name){
if(hp.address)
return NewHook_1(hp,name);
if(emuaddr2jitaddr.find(hp.emu_addr)==emuaddr2jitaddr.end())return false;
//下面的是手动插入
if(emuaddr2jitaddr.find(hp.emu_addr)==emuaddr2jitaddr.end()){
delayinserthook[hp.emu_addr]={name,hp};
return false;
}
strcpy(hp.function,"");
wcscpy(hp.module,L"");
hp.type &= ~MODULE_OFFSET;

View File

@ -25,3 +25,5 @@ void jitaddraddr(uintptr_t em_addr,uintptr_t jitaddr,JITTYPE);
void context_get(hook_stack*,PCONTEXT);
void context_set(hook_stack*,PCONTEXT);
inline std::map<uintptr_t,std::pair<std::string,HookParam>>delayinserthook;

View File

@ -38,7 +38,8 @@ bool add_veh_hook(void* origFunc, newFuncType newFunc, DWORD hook_type)
}
void repair_origin(veh_node_t *node){
DWORD _p;
VirtualProtect(node->origFunc, sizeof(int), PAGE_EXECUTE_READWRITE, &_p);
if(!VirtualProtect(node->origFunc, sizeof(int), PAGE_EXECUTE_READWRITE, &_p))
return;
memcpy((void*)node->origFunc, (const void*)(&node->origBaseByte), sizeof(char));
VirtualProtect(node->origFunc, sizeof(int), node->OldProtect, &_p);
}