2024-05-30 17:32:03 +08:00
|
|
|
#include "python.h"
|
2024-03-18 21:47:46 +08:00
|
|
|
#include <dwrite.h>
|
2024-05-30 17:32:03 +08:00
|
|
|
extern "C" __declspec(dllexport) const wchar_t *internal_renpy_call_host(const wchar_t *text, int split)
|
|
|
|
{
|
2024-03-13 11:14:01 +08:00
|
|
|
return text;
|
|
|
|
}
|
2024-05-30 17:32:03 +08:00
|
|
|
bool Luna_checkisusingembed(uint64_t address, uint64_t ctx2, bool usingsplit)
|
2024-05-05 02:33:58 +08:00
|
|
|
{
|
|
|
|
auto sm = embedsharedmem;
|
|
|
|
if (!sm)
|
|
|
|
return false;
|
|
|
|
for (int i = 0; i < 10; i++)
|
|
|
|
{
|
|
|
|
if (sm->use[i])
|
|
|
|
{
|
2024-05-30 17:32:03 +08:00
|
|
|
if (!usingsplit)
|
|
|
|
return true;
|
|
|
|
if ((sm->addr[i] == address) && (sm->ctx2[i] == ctx2))
|
2024-05-05 02:33:58 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2024-05-30 17:32:03 +08:00
|
|
|
extern "C" __declspec(dllexport) bool internal_renpy_call_is_embed_using(int split, bool usingsplit)
|
2024-03-08 21:23:57 +08:00
|
|
|
{
|
2024-05-30 17:32:03 +08:00
|
|
|
return Luna_checkisusingembed((uint64_t)internal_renpy_call_host, split, usingsplit);
|
2024-03-08 21:23:57 +08:00
|
|
|
}
|
2024-05-30 17:32:03 +08:00
|
|
|
namespace
|
2024-03-08 21:23:57 +08:00
|
|
|
{
|
2024-03-13 11:14:01 +08:00
|
|
|
|
2024-05-30 17:32:03 +08:00
|
|
|
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);
|
2024-03-18 21:47:46 +08:00
|
|
|
|
2024-05-30 17:32:03 +08:00
|
|
|
PyRun_SimpleString_t PyRun_SimpleString;
|
|
|
|
PyGILState_Release_t PyGILState_Release;
|
|
|
|
PyGILState_Ensure_t PyGILState_Ensure;
|
2024-03-18 21:47:46 +08:00
|
|
|
|
2024-05-30 17:32:03 +08:00
|
|
|
bool LoadPyRun(HMODULE module)
|
2024-03-18 21:47:46 +08:00
|
|
|
{
|
2024-05-30 17:32:03 +08:00
|
|
|
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");
|
|
|
|
return PyGILState_Ensure && PyGILState_Release && PyRun_SimpleString;
|
2024-03-18 21:47:46 +08:00
|
|
|
}
|
|
|
|
|
2024-05-30 17:32:03 +08:00
|
|
|
void PyRunScript(const char *script)
|
2024-03-18 21:47:46 +08:00
|
|
|
{
|
2024-05-30 17:32:03 +08:00
|
|
|
if (!(PyGILState_Ensure && PyGILState_Release && PyRun_SimpleString))
|
|
|
|
return;
|
2024-03-18 21:47:46 +08:00
|
|
|
|
2024-05-30 17:32:03 +08:00
|
|
|
auto state = PyGILState_Ensure();
|
|
|
|
PyRun_SimpleString(script);
|
|
|
|
PyGILState_Release(state);
|
|
|
|
}
|
2024-03-18 21:47:46 +08:00
|
|
|
|
2024-05-30 17:32:03 +08:00
|
|
|
void hook_internal_renpy_call_host()
|
2024-03-18 21:47:46 +08:00
|
|
|
{
|
2024-05-30 17:32:03 +08:00
|
|
|
HookParam hp_internal;
|
|
|
|
hp_internal.address = (uintptr_t)internal_renpy_call_host;
|
|
|
|
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 | NO_CONTEXT;
|
|
|
|
NewHook(hp_internal, "internal_renpy_call_host");
|
|
|
|
PyRunScript(LoadResData(L"renpy_hook_text", L"PYSOURCE").c_str());
|
2024-03-18 21:47:46 +08:00
|
|
|
}
|
|
|
|
|
2024-05-30 17:32:03 +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()
|
2024-03-18 21:47:46 +08:00
|
|
|
{
|
|
|
|
|
2024-05-30 17:32:03 +08:00
|
|
|
PWSTR localAppDataPath;
|
|
|
|
HRESULT result = SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &localAppDataPath);
|
|
|
|
std::unordered_map<std::wstring, std::wstring> fnts;
|
2024-03-18 21:47:46 +08:00
|
|
|
|
2024-05-30 17:32:03 +08:00
|
|
|
std::vector<std::wstring> collectfile;
|
|
|
|
for (auto fontdir : {std::wstring(LR"(C:\Windows\Fonts)"), std::wstring(localAppDataPath) + LR"(\Microsoft\Windows\Fonts)"})
|
|
|
|
{
|
|
|
|
if (!std::filesystem::exists(fontdir))
|
|
|
|
continue;
|
|
|
|
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);
|
|
|
|
auto succ = GetFontResourceInfo(fontfile.c_str(), &cbBuffer, lpLogfonts.get(), QFR_LOGFONT);
|
|
|
|
RemoveFontResourceExW(fontfile.c_str(), FR_PRIVATE, 0);
|
|
|
|
if (!succ)
|
|
|
|
continue;
|
|
|
|
for (int k = 0; k < dwFontsLoaded; k++)
|
|
|
|
fntss[i].insert(std::make_pair(lpLogfonts[k].lfFaceName, fontfile));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
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-18 21:47:46 +08:00
|
|
|
}
|
|
|
|
|
2024-05-30 17:32:03 +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)
|
2024-03-18 21:47:46 +08:00
|
|
|
{
|
2024-05-30 17:32:03 +08:00
|
|
|
std::list<WCHAR *> fonts_filename_list;
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
IDWriteFactory *dwrite_factory;
|
|
|
|
hr = fnDWriteCreateFactory(DWRITE_FACTORY_TYPE_ISOLATED, __uuidof(IDWriteFactory), reinterpret_cast<IUnknown **>(&dwrite_factory));
|
2024-03-18 21:47:46 +08:00
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2024-05-30 17:32:03 +08:00
|
|
|
return fonts_filename_list;
|
2024-03-18 21:47:46 +08:00
|
|
|
}
|
|
|
|
|
2024-05-30 17:32:03 +08:00
|
|
|
IDWriteGdiInterop *gdi_interop;
|
|
|
|
hr = dwrite_factory->GetGdiInterop(&gdi_interop);
|
2024-03-18 21:47:46 +08:00
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2024-05-30 17:32:03 +08:00
|
|
|
dwrite_factory->Release();
|
|
|
|
return fonts_filename_list;
|
2024-03-18 21:47:46 +08:00
|
|
|
}
|
|
|
|
|
2024-05-30 17:32:03 +08:00
|
|
|
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);
|
2024-03-18 21:47:46 +08:00
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2024-05-30 17:32:03 +08:00
|
|
|
gdi_interop->Release();
|
|
|
|
dwrite_factory->Release();
|
|
|
|
return fonts_filename_list;
|
2024-03-18 21:47:46 +08:00
|
|
|
}
|
|
|
|
|
2024-05-30 17:32:03 +08:00
|
|
|
UINT file_count;
|
|
|
|
hr = font_face->GetFiles(&file_count, NULL);
|
2024-03-18 21:47:46 +08:00
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2024-05-30 17:32:03 +08:00
|
|
|
font_face->Release();
|
|
|
|
gdi_interop->Release();
|
|
|
|
dwrite_factory->Release();
|
|
|
|
return fonts_filename_list;
|
2024-03-18 21:47:46 +08:00
|
|
|
}
|
|
|
|
|
2024-05-30 17:32:03 +08:00
|
|
|
IDWriteFontFile **font_files = new IDWriteFontFile *[file_count];
|
|
|
|
hr = font_face->GetFiles(&file_count, font_files);
|
2024-03-18 21:47:46 +08:00
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2024-05-30 17:32:03 +08:00
|
|
|
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);
|
|
|
|
|
2024-03-18 21:47:46 +08:00
|
|
|
local_loader->Release();
|
|
|
|
loader->Release();
|
|
|
|
font_files[i]->Release();
|
|
|
|
}
|
|
|
|
|
2024-05-30 17:32:03 +08:00
|
|
|
font_face->Release();
|
|
|
|
gdi_interop->Release();
|
|
|
|
SelectObject(hdc, hOldFont);
|
|
|
|
ReleaseDC(NULL, hdc);
|
|
|
|
DeleteObject(hFont);
|
2024-03-18 21:47:46 +08:00
|
|
|
|
2024-05-30 17:32:03 +08:00
|
|
|
dwrite_factory->Release();
|
2024-03-18 21:47:46 +08:00
|
|
|
|
2024-05-30 17:32:03 +08:00
|
|
|
return fonts_filename_list;
|
2024-03-18 21:47:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2024-05-30 17:32:03 +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
|
|
|
|
2024-05-30 17:32:03 +08:00
|
|
|
fnDWriteCreateFactory = (decltype(fnDWriteCreateFactory))GetProcAddress(LoadLibrary(L"Dwrite.dll"), "DWriteCreateFactory");
|
|
|
|
if (fnDWriteCreateFactory)
|
|
|
|
{
|
2024-03-18 21:47:46 +08:00
|
|
|
auto fonts_filename_list = get_fonts_path(embedsharedmem->fontFamily, false, false, DEFAULT_CHARSET);
|
2024-05-30 17:32:03 +08:00
|
|
|
if (fonts_filename_list.size() == 0)
|
|
|
|
return NULL;
|
2024-03-18 21:47:46 +08:00
|
|
|
return *fonts_filename_list.begin();
|
|
|
|
}
|
2024-05-30 17:32:03 +08:00
|
|
|
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-18 21:47:46 +08:00
|
|
|
}
|
2024-03-08 23:49:48 +08:00
|
|
|
}
|
2024-05-30 17:32:03 +08:00
|
|
|
bool hookrenpy(HMODULE module)
|
|
|
|
{
|
|
|
|
if (!LoadPyRun(module))
|
|
|
|
return false;
|
|
|
|
patch_fun = []()
|
|
|
|
{
|
|
|
|
PyRunScript(LoadResData(L"renpy_hook_font", L"PYSOURCE").c_str());
|
2024-03-13 11:14:01 +08:00
|
|
|
};
|
2024-03-08 23:49:48 +08:00
|
|
|
hook_internal_renpy_call_host();
|
2024-05-30 17:32:03 +08:00
|
|
|
dont_detach = true;
|
2024-05-04 13:08:39 +08:00
|
|
|
return true;
|
2024-03-08 21:23:57 +08:00
|
|
|
}
|