添加配置类以加载默认字体设置,并更新字体替换逻辑

This commit is contained in:
2025-03-01 10:17:25 +08:00
parent fa20655bed
commit 842295b6b1
4 changed files with 69 additions and 3 deletions

View File

@@ -14,7 +14,7 @@ set(ENABLE_ICONV OFF CACHE BOOL "Libiconv is not needed.")
add_subdirectory(utils) add_subdirectory(utils)
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/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 "${DETOURS_LIB}")
target_link_libraries(jewena_patch utils) target_link_libraries(jewena_patch utils)

48
config.cpp Normal file
View File

@@ -0,0 +1,48 @@
#include "config.hpp"
#include "fileop.h"
#include "file_reader.h"
#include "malloc.h"
#include <fcntl.h>
#if _WIN32
#include <Windows.h>
#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;
}

11
config.hpp Normal file
View File

@@ -0,0 +1,11 @@
#include <unordered_map>
#include <string>
class Config {
public:
std::unordered_map<std::string, std::string> configs;
Config() {
configs["defaultFont"] = "微软雅黑";
}
bool Load(std::string path);
};

View File

@@ -1,10 +1,13 @@
#include <Windows.h> #include <Windows.h>
#include "config.hpp"
#include "detours.h" #include "detours.h"
#include <stdio.h> #include <stdio.h>
#include "wchar_util.h" #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 *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 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) { char* to_utf8(char* target, const char* source, UINT cp) {
int count = MultiByteToWideChar(cp, MB_ERR_INVALID_CHARS, source, -1, NULL, 0); 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) { 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); std::wstring name(lpFaceName);
if (name == L"Meiryo") { 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); 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++) { for (int i = 0; i < 4; i++) {
if (wchar_util::str_to_wstr(font, lpFaceName, cp[i])) { if (wchar_util::str_to_wstr(font, lpFaceName, cp[i])) {
if (font == L"Meiryo") { 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()); 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() { 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(); DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread()); DetourUpdateThread(GetCurrentThread());
h = GetHandle(); h = GetHandle();