LunaHook-mirror/LunaHook/engines/python/python.cpp

63 lines
2.0 KiB
C++
Raw Normal View History

#include"python.h"
#include"embed_util.h"
#include"main.h"
#include"stackoffset.hpp"
#include"types.h"
#include"defs.h"
2024-03-08 23:49:48 +08:00
namespace{
typedef enum {PyGILState_LOCKED, PyGILState_UNLOCKED}
PyGILState_STATE;
typedef PyGILState_STATE (*PyGILState_Ensure_t)(void);
typedef void (*PyGILState_Release_t)(PyGILState_STATE);
typedef int (*PyRun_SimpleString_t)(const char *command);
PyRun_SimpleString_t PyRun_SimpleString;
PyGILState_Release_t PyGILState_Release;
PyGILState_Ensure_t PyGILState_Ensure;
void LoadPyRun(HMODULE module)
{
PyGILState_Ensure=(PyGILState_Ensure_t)GetProcAddress(module, "PyGILState_Ensure");
PyGILState_Release=(PyGILState_Release_t)GetProcAddress(module, "PyGILState_Release");
PyRun_SimpleString=(PyRun_SimpleString_t)GetProcAddress(module, "PyRun_SimpleString");
}
2024-03-08 23:49:48 +08:00
void PyRunScript(const char* script)
{
if(!(PyGILState_Ensure&&PyGILState_Release&&PyRun_SimpleString))return;
auto state=PyGILState_Ensure();
PyRun_SimpleString(script);
PyGILState_Release(state);
2024-03-08 23:49:48 +08:00
}
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.type=USING_SPLIT|USING_STRING|CODEC_UTF16|EMBED_ABLE|EMBED_BEFORE_SIMPLE|EMBED_AFTER_NEW;
NewHook(hp_internal, "internal_renpy_call_host");
2024-03-08 23:54:27 +08:00
PyRunScript(LoadResData(L"renpy_hook_text",L"PYSOURCE").c_str());
2024-03-08 23:49:48 +08:00
}
void patchfontfunction(){
//由于不知道怎么从字体名映射到ttc/ttf文件名所以暂时写死arial/msyh
if(wcslen(embedsharedmem->fontFamily)==0)return;
2024-03-08 23:54:27 +08:00
PyRunScript(LoadResData(L"renpy_hook_font",L"PYSOURCE").c_str());
2024-03-08 23:49:48 +08:00
}
}
const wchar_t* internal_renpy_call_host(const wchar_t* text,int split){
return text;
}
void hookrenpy(HMODULE module){
LoadPyRun(module);
patch_fun=patchfontfunction;
hook_internal_renpy_call_host();
}