diff --git a/CMakeLists.txt b/CMakeLists.txt index be8b9df..44ec7a2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,7 @@ 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) +add_library(jewena_patch SHARED dllmain.cpp config.hpp config.cpp) target_link_libraries(jewena_patch "${DETOURS_LIB}") target_link_libraries(jewena_patch utils) diff --git a/config.cpp b/config.cpp new file mode 100644 index 0000000..fc84d6b --- /dev/null +++ b/config.cpp @@ -0,0 +1,48 @@ +#include "config.hpp" +#include "fileop.h" +#include "file_reader.h" +#include "malloc.h" + +#include +#if _WIN32 +#include +#endif + +bool Config::Load(std::string path) { + if (!fileop::exists(path)) { + return false; + } + int fd = 0; + int err = fileop::open(path, fd, O_RDONLY, _SH_DENYRW, _S_IREAD); + if (err < 0) { + return false; + } + FILE* f = fileop::fdopen(fd, "rb"); + if (!f) { + fileop::close(fd); + return false; + } + auto reader = create_file_reader(f, 0); + char* line = nullptr; + size_t line_size = 0; + while (!file_reader_read_line(reader, &line, &line_size)) { + std::string l(line, line_size); + free(line); + line = nullptr; + line_size = 0; + size_t comment_pos = l.find_first_of('#'); + if (comment_pos != std::string::npos) { + l = l.substr(0, comment_pos); + } + size_t eq_pos = l.find_first_of('='); + if (eq_pos == std::string::npos) { + continue; + } + std::string key = l.substr(0, eq_pos); + std::string value = l.substr(eq_pos + 1); + configs[key] = value; + } + free_file_reader(reader); + fileop::fclose(f); + return true; +} diff --git a/config.hpp b/config.hpp new file mode 100644 index 0000000..1cb4681 --- /dev/null +++ b/config.hpp @@ -0,0 +1,11 @@ +#include +#include + +class Config { + public: + std::unordered_map configs; + Config() { + configs["defaultFont"] = "微软雅黑"; + } + bool Load(std::string path); +}; diff --git a/dllmain.cpp b/dllmain.cpp index 19c40f5..b4d71b9 100644 --- a/dllmain.cpp +++ b/dllmain.cpp @@ -1,10 +1,13 @@ #include +#include "config.hpp" #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; +static Config config; +static std::wstring defaultFont; char* to_utf8(char* target, const char* source, UINT cp) { int count = MultiByteToWideChar(cp, MB_ERR_INVALID_CHARS, source, -1, NULL, 0); @@ -42,7 +45,7 @@ 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"微软雅黑"; + lpFaceName = defaultFont.c_str(); } return TrueCreateFontW(nHeight, nWidth, nEscapement, nOrientation, fnWeight, dwItalic, dwUnderline, dwStrikeOut, dwCharSet, dwOutPrecision, dwClipPrecision, dwQuality, dwPitchAndFamily, lpFaceName); } @@ -53,7 +56,7 @@ HFONT WINAPI HookedCreateFontA(int nHeight, int nWidth, int nEscapement, int nOr for (int i = 0; i < 4; i++) { if (wchar_util::str_to_wstr(font, lpFaceName, cp[i])) { if (font == L"Meiryo") { - font = L"微软雅黑"; + font = defaultFont; } return TrueCreateFontW(nHeight, nWidth, nEscapement, nOrientation, fnWeight, dwItalic, dwUnderline, dwStrikeOut, dwCharSet, dwOutPrecision, dwClipPrecision, dwQuality, dwPitchAndFamily, font.c_str()); } @@ -65,6 +68,10 @@ HFONT WINAPI HookedCreateFontA(int nHeight, int nWidth, int nEscapement, int nOr } extern "C" __declspec(dllexport) void Attach() { + config.Load("config.txt"); + if (!wchar_util::str_to_wstr(defaultFont, config.configs["defaultFont"], CP_UTF8)) { + defaultFont = L"微软雅黑"; + } DetourTransactionBegin(); DetourUpdateThread(GetCurrentThread()); h = GetHandle();