From c7ab7ab4da63f237027b668fab2630e306cb8c13 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Mon, 25 Nov 2024 19:56:00 +0800 Subject: [PATCH] Fix default font --- .gitmodules | 3 +++ CMakeLists.txt | 5 +++++ dllmain.cpp | 39 +++++++++++++++++++++++++++++++++++++-- utils | 1 + 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 .gitmodules create mode 160000 utils diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..2676665 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "utils"] + path = utils + url = https://github.com/lifegpc/c-utils diff --git a/CMakeLists.txt b/CMakeLists.txt index de4b655..be8b9df 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,12 @@ include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include") set(DETOURS_LIB "${CMAKE_CURRENT_SOURCE_DIR}/lib/detours.lib") +set(ENABLE_ICONV OFF CACHE BOOL "Libiconv is not needed.") +add_subdirectory(utils) +include_directories("${CMAKE_CURRENT_SOURCE_DIR}/utils") + add_library(jewena_patch SHARED dllmain.cpp) target_link_libraries(jewena_patch "${DETOURS_LIB}") +target_link_libraries(jewena_patch utils) add_executable(jewena-chs WIN32 main.cpp winres.rc jewena-chs.exe.manifest) diff --git a/dllmain.cpp b/dllmain.cpp index 781183a..cdeb3a3 100644 --- a/dllmain.cpp +++ b/dllmain.cpp @@ -1,6 +1,10 @@ #include #include "detours.h" #include +#include "wchar_util.h" + +static HFONT(WINAPI *TrueCreateFontW)(int nHeight, int nWidth, int nEscapement, int nOrientation, int fnWeight, DWORD dwItalic, DWORD dwUnderline, DWORD dwStrikeOut, DWORD dwCharSet, DWORD dwOutPrecision, DWORD dwClipPrecision, DWORD dwQuality, DWORD dwPitchAndFamily, LPCWSTR lpFaceName) = CreateFontW; +static HFONT(WINAPI *TrueCreateFontA)(int nHeight, int nWidth, int nEscapement, int nOrientation, int fnWeight, DWORD dwItalic, DWORD dwUnderline, DWORD dwStrikeOut, DWORD dwCharSet, DWORD dwOutPrecision, DWORD dwClipPrecision, DWORD dwQuality, DWORD dwPitchAndFamily, LPCSTR lpFaceName) = CreateFontA; char* to_utf8(char* target, const char* source, UINT cp) { int count = MultiByteToWideChar(cp, MB_ERR_INVALID_CHARS, source, -1, NULL, 0); @@ -33,19 +37,50 @@ PVOID GetHandle() { return (char*)hModule + 0xf3c20; } +static PVOID h = nullptr; + +HFONT WINAPI HookedCreateFontW(int nHeight, int nWidth, int nEscapement, int nOrientation, int fnWeight, DWORD dwItalic, DWORD dwUnderline, DWORD dwStrikeOut, DWORD dwCharSet, DWORD dwOutPrecision, DWORD dwClipPrecision, DWORD dwQuality, DWORD dwPitchAndFamily, LPCWSTR lpFaceName) { + std::wstring name(lpFaceName); + if (name == L"Meiryo") { + lpFaceName = L"微软雅黑"; + } + return TrueCreateFontW(nHeight, nWidth, nEscapement, nOrientation, fnWeight, dwItalic, dwUnderline, dwStrikeOut, dwCharSet, dwOutPrecision, dwClipPrecision, dwQuality, dwPitchAndFamily, lpFaceName); +} + +HFONT WINAPI HookedCreateFontA(int nHeight, int nWidth, int nEscapement, int nOrientation, int fnWeight, DWORD dwItalic, DWORD dwUnderline, DWORD dwStrikeOut, DWORD dwCharSet, DWORD dwOutPrecision, DWORD dwClipPrecision, DWORD dwQuality, DWORD dwPitchAndFamily, LPCSTR lpFaceName) { + UINT cp[] = { CP_UTF8, CP_OEMCP, CP_ACP, 932 }; + std::wstring font; + for (int i = 0; i < 4; i++) { + if (wchar_util::str_to_wstr(font, lpFaceName, cp[i])) { + if (font == L"Meiryo") { + font = L"微软雅黑"; + } + return TrueCreateFontW(nHeight, nWidth, nEscapement, nOrientation, fnWeight, dwItalic, dwUnderline, dwStrikeOut, dwCharSet, dwOutPrecision, dwClipPrecision, dwQuality, dwPitchAndFamily, font.c_str()); + } + } + if (!strcmp(lpFaceName, "Meiryo")) { + lpFaceName = "Microsoft YaHei"; + } + return TrueCreateFontA(nHeight, nWidth, nEscapement, nOrientation, fnWeight, dwItalic, dwUnderline, dwStrikeOut, dwCharSet, dwOutPrecision, dwClipPrecision, dwQuality, dwPitchAndFamily, lpFaceName); +} + extern "C" __declspec(dllexport) void Attach() { DetourTransactionBegin(); DetourUpdateThread(GetCurrentThread()); - PVOID h = GetHandle(); + h = GetHandle(); DetourAttach(&h, (PVOID)jis_to_utf8); + DetourAttach(&TrueCreateFontW, HookedCreateFontW); + DetourAttach(&TrueCreateFontA, HookedCreateFontA); DetourTransactionCommit(); } extern "C" __declspec(dllexport) void Detach() { + if (!h) return; DetourTransactionBegin(); DetourUpdateThread(GetCurrentThread()); - PVOID h = GetHandle(); DetourDetach(&h, (PVOID)jis_to_utf8); + DetourDetach(&TrueCreateFontW, HookedCreateFontW); + DetourDetach(&TrueCreateFontA, HookedCreateFontA); DetourTransactionCommit(); } diff --git a/utils b/utils new file mode 160000 index 0000000..d4d0be7 --- /dev/null +++ b/utils @@ -0,0 +1 @@ +Subproject commit d4d0be7e7a278fc2e58f04f9ff77ac26170bbcc4