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

282 lines
9.2 KiB
C++
Raw Normal View History

#include"python.h"
#include"embed_util.h"
#include"main.h"
#include"stackoffset.hpp"
2024-03-13 11:14:01 +08:00
#include <windows.h>
#include <shlobj.h>
2024-03-18 21:47:46 +08:00
#include <dwrite.h>
#include <windowsx.h>
2024-03-13 11:14:01 +08:00
extern "C" __declspec(dllexport) const wchar_t* internal_renpy_call_host(const wchar_t* text,int split){
return text;
}
2024-05-05 02:33:58 +08:00
bool Luna_checkisusingembed(uint64_t address, uint64_t ctx1, uint64_t ctx2)
{
auto sm = embedsharedmem;
if (!sm)
return false;
for (int i = 0; i < 10; i++)
{
if (sm->use[i])
{
if ((sm->addr[i] == address) && (sm->ctx1[i] == ctx1) && (sm->ctx2[i] == ctx2))
return true;
}
}
return false;
}
extern "C" __declspec(dllexport) bool internal_renpy_call_is_embed_using(int split){
return Luna_checkisusingembed((uint64_t)internal_renpy_call_host,0,split);
}
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;
2024-05-04 13:08:39 +08:00
bool 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-05-04 13:08:39 +08:00
return PyGILState_Ensure&&PyGILState_Release&&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;
2024-03-20 23:20:17 +08:00
hp_internal.offset=GETARG1;
hp_internal.split=GETARG2;
2024-05-05 02:33:58 +08:00
hp_internal.type=USING_SPLIT|USING_STRING|CODEC_UTF16|EMBED_ABLE|EMBED_BEFORE_SIMPLE|EMBED_AFTER_NEW|NO_CONTEXT;
2024-03-08 23:49:48 +08:00
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
}
2024-03-13 11:14:01 +08:00
typedef BOOL(WINAPI* PGFRI)(LPCWSTR, LPDWORD, LPVOID, DWORD);
#define QFR_LOGFONT (2)
#define LOADFONTTHREADNUM 4
std::unordered_map<std::wstring, std::wstring> loadfontfiles() {
PWSTR localAppDataPath;
HRESULT result = SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &localAppDataPath);
std::unordered_map<std::wstring, std::wstring> fnts;
2024-03-08 23:49:48 +08:00
2024-03-13 11:14:01 +08:00
std::vector< std::wstring>collectfile;
for (auto fontdir : { std::wstring(LR"(C:\Windows\Fonts)"),std::wstring(localAppDataPath) + LR"(\Microsoft\Windows\Fonts)" }) {
2024-03-16 18:30:21 +08:00
if (!std::filesystem::exists(fontdir))continue;
2024-03-13 11:14:01 +08:00
for (auto entry : std::filesystem::directory_iterator(fontdir)) {
collectfile.emplace_back(entry.path());
}
}
std::vector<std::thread>ts;
std::vector<decltype(fnts)> fntss(LOADFONTTHREADNUM);
auto singletask = [&](int i) {
HINSTANCE hGdi32 = GetModuleHandleA("gdi32.dll");
if (hGdi32 == 0)return;
PGFRI GetFontResourceInfo = (PGFRI)GetProcAddress(hGdi32, "GetFontResourceInfoW");
for (auto j = i; j < collectfile.size(); j += LOADFONTTHREADNUM) {
auto fontfile = collectfile[j];
DWORD dwFontsLoaded = AddFontResourceExW(fontfile.c_str(), FR_PRIVATE, 0);
if (dwFontsLoaded == 0) {
continue;
}
auto lpLogfonts = std::make_unique<LOGFONTW[]>(dwFontsLoaded);
DWORD cbBuffer = dwFontsLoaded * sizeof(LOGFONTW);
2024-03-13 11:30:21 +08:00
auto succ=GetFontResourceInfo(fontfile.c_str(), &cbBuffer, lpLogfonts.get(), QFR_LOGFONT);
RemoveFontResourceExW(fontfile.c_str(), FR_PRIVATE, 0);
if (!succ)continue;
2024-03-13 11:14:01 +08:00
for (int k = 0; k < dwFontsLoaded; k++)
fntss[i].insert(std::make_pair(lpLogfonts[k].lfFaceName, fontfile));
}
2024-03-13 11:30:21 +08:00
};
2024-03-13 11:14:01 +08:00
for (int i = 0; i < LOADFONTTHREADNUM; i++) {
ts.emplace_back(std::thread(singletask,i));
}
for (int i = 0; i < LOADFONTTHREADNUM; i++)
ts[i].join();
for (int i = 0; i < LOADFONTTHREADNUM; i++) {
for (auto p : fntss[i])
fnts.insert(std::move(p));
}
return fnts;
2024-03-08 23:49:48 +08:00
}
2024-03-18 21:47:46 +08:00
//https://stackoverflow.com/questions/16769758/get-a-font-filename-based-on-the-font-handle-hfont
HRESULT (*fnDWriteCreateFactory)(
_In_ DWRITE_FACTORY_TYPE factoryType,
_In_ REFIID iid,
_COM_Outptr_ IUnknown **factory
);
std::list<WCHAR*> get_fonts_path(LPCWSTR family_name, BOOL is_bold, BOOL is_italic, BYTE charset)
{
std::list<WCHAR*> fonts_filename_list;
HRESULT hr;
IDWriteFactory* dwrite_factory;
hr = fnDWriteCreateFactory(DWRITE_FACTORY_TYPE_ISOLATED, __uuidof(IDWriteFactory), reinterpret_cast<IUnknown**>(&dwrite_factory));
if (FAILED(hr))
{
return fonts_filename_list;
}
IDWriteGdiInterop* gdi_interop;
hr = dwrite_factory->GetGdiInterop(&gdi_interop);
if (FAILED(hr))
{
dwrite_factory->Release();
return fonts_filename_list;
}
LOGFONT lf;
memset(&lf, 0, sizeof(lf));
wcscpy_s(lf.lfFaceName, LF_FACESIZE, family_name);
lf.lfWeight = is_bold ? FW_BOLD : FW_REGULAR; // TODO Change with the real ass weight
lf.lfItalic = is_italic;
lf.lfCharSet = charset;
lf.lfOutPrecision = OUT_TT_PRECIS;
lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
lf.lfQuality = ANTIALIASED_QUALITY;
lf.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
HFONT hFont = CreateFontIndirect(&lf);
HDC hdc = CreateCompatibleDC(NULL);
HFONT hOldFont = SelectFont(hdc, hFont);
IDWriteFontFace* font_face;
hr = gdi_interop->CreateFontFaceFromHdc(hdc, &font_face);
if (FAILED(hr))
{
gdi_interop->Release();
dwrite_factory->Release();
return fonts_filename_list;
}
UINT file_count;
hr = font_face->GetFiles(&file_count, NULL);
if (FAILED(hr))
{
font_face->Release();
gdi_interop->Release();
dwrite_factory->Release();
return fonts_filename_list;
}
IDWriteFontFile** font_files = new IDWriteFontFile * [file_count];
hr = font_face->GetFiles(&file_count, font_files);
if (FAILED(hr))
{
font_face->Release();
gdi_interop->Release();
dwrite_factory->Release();
return fonts_filename_list;
}
for (int i = 0; i < file_count; i++)
{
LPCVOID font_file_reference_key;
UINT font_file_reference_key_size;
hr = font_files[i]->GetReferenceKey(&font_file_reference_key, &font_file_reference_key_size);
if (FAILED(hr))
{
font_files[i]->Release();
continue;
}
IDWriteFontFileLoader* loader;
hr = font_files[i]->GetLoader(&loader);
if (FAILED(hr))
{
font_files[i]->Release();
continue;
}
IDWriteLocalFontFileLoader* local_loader;
hr = loader->QueryInterface(__uuidof(IDWriteLocalFontFileLoader), (void**)&local_loader);
if (FAILED(hr))
{
loader->Release();
font_files[i]->Release();
continue;
}
UINT32 path_length;
hr = local_loader->GetFilePathLengthFromKey(font_file_reference_key, font_file_reference_key_size, &path_length);
if (FAILED(hr))
{
local_loader->Release();
loader->Release();
font_files[i]->Release();
continue;
}
WCHAR* path = new WCHAR[path_length + 1];
hr = local_loader->GetFilePathFromKey(font_file_reference_key, font_file_reference_key_size, path, path_length + 1);
if (FAILED(hr))
{
local_loader->Release();
loader->Release();
font_files[i]->Release();
continue;
}
fonts_filename_list.push_back(path);
local_loader->Release();
loader->Release();
font_files[i]->Release();
}
font_face->Release();
gdi_interop->Release();
SelectObject(hdc, hOldFont);
ReleaseDC(NULL, hdc);
DeleteObject(hFont);
dwrite_factory->Release();
return fonts_filename_list;
}
2024-03-08 23:49:48 +08:00
}
2024-03-13 11:14:01 +08:00
extern "C" __declspec(dllexport) const wchar_t* internal_renpy_get_font(){
if(wcslen(embedsharedmem->fontFamily)==0)return NULL;
2024-03-18 21:47:46 +08:00
fnDWriteCreateFactory=(decltype(fnDWriteCreateFactory))GetProcAddress(LoadLibrary(L"Dwrite.dll"),"DWriteCreateFactory");
if(fnDWriteCreateFactory){
auto fonts_filename_list = get_fonts_path(embedsharedmem->fontFamily, false, false, DEFAULT_CHARSET);
if(fonts_filename_list.size()==0)return NULL;
return *fonts_filename_list.begin();
}
else{
static auto fontname2fontfile=std::move(loadfontfiles());
if(fontname2fontfile.find(embedsharedmem->fontFamily)==fontname2fontfile.end())return NULL;
else return fontname2fontfile.at(embedsharedmem->fontFamily).c_str();
}
2024-03-08 23:49:48 +08:00
}
2024-05-04 13:08:39 +08:00
bool hookrenpy(HMODULE module){
if(!LoadPyRun(module))return false;
2024-03-13 11:14:01 +08:00
patch_fun=[](){
PyRunScript(LoadResData(L"renpy_hook_font",L"PYSOURCE").c_str());
};
2024-03-08 23:49:48 +08:00
hook_internal_renpy_call_host();
2024-05-04 13:08:39 +08:00
dont_detach=true;
return true;
}