diff --git a/CMakeLists.txt b/CMakeLists.txt index c5a5290..0494f8c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,14 +1,7 @@ -cmake_minimum_required(VERSION 3.24) +cmake_minimum_required(VERSION 3.28) project(gta4_chs) -find_package(utf8cpp CONFIG REQUIRED) -find_package(range-v3 CONFIG REQUIRED) -find_package(fmt CONFIG REQUIRED) -find_package(spdlog CONFIG REQUIRED) -find_package(RapidJSON CONFIG REQUIRED) -find_package(ZLIB REQUIRED) - add_subdirectory(plugin) add_subdirectory(whm_io) diff --git a/CMakePresets.json b/CMakePresets.json index cbe5ebb..705b1b1 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -2,9 +2,8 @@ "version": 3, "configurePresets": [ { - "name": "windows-base", + "name": "windows-x86-base", "description": "面向具有 Visual Studio 开发环境的 Windows。", - "hidden": true, "generator": "Ninja Multi-Config", "binaryDir": "${sourceDir}/out/build/${presetName}", "installDir": "${sourceDir}/out/install/${presetName}", @@ -24,22 +23,20 @@ "VCPKG_TARGET_TRIPLET": "x86-windows-static", "CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" } - }, + } + ], + "buildPresets": [ { "name": "x86-debug", "displayName": "x86 Debug", - "inherits": "windows-base", - "cacheVariables": { - "CMAKE_BUILD_TYPE": "Debug" - } + "configurePreset": "windows-x86-base", + "configuration": "Debug" }, { "name": "x86-release", "displayName": "x86 Release", - "inherits": "windows-base", - "cacheVariables": { - "CMAKE_BUILD_TYPE": "Release" - } + "configurePreset": "windows-x86-base", + "configuration": "Release" } ] } diff --git a/Note.txt b/Note.txt index b007e23..6f95a23 100644 --- a/Note.txt +++ b/Note.txt @@ -1,5 +1,8 @@ -----------todo----------- json自定义翻译功能 +91C777 call CFont::ProcessString +922BA4 call CFont::ProcessString + 某些邮件回复时崩溃 *craplist.net邮件排版问题(最低优先) diff --git a/common/stdinc.h b/common/stdinc.h index 426008c..c70ce54 100644 --- a/common/stdinc.h +++ b/common/stdinc.h @@ -31,6 +31,7 @@ #include "injector/calling.hpp" #include "injector/hooking.hpp" #include "injector/injector.hpp" +#include "injector/utility.hpp" // vcpkg #include diff --git a/plugin/CMakeLists.txt b/plugin/CMakeLists.txt index c33344b..484e188 100644 --- a/plugin/CMakeLists.txt +++ b/plugin/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.24) +cmake_minimum_required(VERSION 3.28) project(plugin) diff --git a/plugin/font.cpp b/plugin/font.cpp index 3cd56fc..74d6fde 100644 --- a/plugin/font.cpp +++ b/plugin/font.cpp @@ -11,21 +11,21 @@ static const float fTextureRowsCount = fTextureResolution / fSpriteHeight; static const float fTextureColumnsCount = fTextureResolution / fSpriteWidth; static const float fRatio = 4.0f; -void *CNFont; +void* CNFont; -void *__fastcall CFont::LoadTextureCB(void *pDictionary, int, uint hash) +void* __fastcall CFont::LoadTextureCB(void* pDictionary, int, uint hash) { auto result = plugin.game.Dictionary_grcTexturePC_GetElementByKey(pDictionary, hash); CNFont = plugin.game.Dictionary_grcTexturePC_GetElementByKey(pDictionary, - plugin.game.Hash_HashStringFromSeediCase("font_chs")); + plugin.game.Hash_HashStringFromSeediCase("font_chs")); return result; } -const GTAChar *CFont::SkipWord_Prolog(std::uintptr_t address) +const GTAChar* CFont::SkipWord_Prolog(std::uintptr_t address) { - auto ptr = reinterpret_cast(address & 0x7FFFFFFF); + auto ptr = reinterpret_cast(address & 0x7FFFFFFF); if ((address & 0x80000000) == 0) { @@ -57,7 +57,7 @@ bool CFont::IsSpecialPunctuationMark(GTAChar chr) #endif } -void CFont::SkipSpecialPunctuationMarks(const GTAChar *&str) +void CFont::SkipSpecialPunctuationMarks(const GTAChar*& str) { while (IsSpecialPunctuationMark(*str)) { @@ -65,7 +65,7 @@ void CFont::SkipSpecialPunctuationMarks(const GTAChar *&str) } } -void CFont::AddSpecialPunctuationMarksWidth(const GTAChar *&str, float *width) +void CFont::AddSpecialPunctuationMarksWidth(const GTAChar*& str, float* width) { while (IsSpecialPunctuationMark(*str)) { @@ -74,7 +74,7 @@ void CFont::AddSpecialPunctuationMarksWidth(const GTAChar *&str, float *width) } } -const GTAChar *CFont::SkipWord(const GTAChar *str) +const GTAChar* CFont::SkipWord(const GTAChar* str) { if (str == nullptr) { @@ -113,7 +113,7 @@ const GTAChar *CFont::SkipWord(const GTAChar *str) return current; } -const GTAChar *CFont::SkipSpaces(const GTAChar *text) +const GTAChar* CFont::SkipSpaces(const GTAChar* text) { if (text == nullptr) return text; @@ -126,7 +126,7 @@ const GTAChar *CFont::SkipSpaces(const GTAChar *text) return text; } -float CFont::GetMaxWordWidth(const GTAChar *text) +float CFont::GetMaxWordWidth(const GTAChar* text) { if (text == nullptr) return 0.0f; @@ -146,12 +146,12 @@ float CFont::GetMaxWordWidth(const GTAChar *text) return max_word_width; } -float CFont::GetStringWidthRemake(const GTAChar *str, bool get_all) +float CFont::GetStringWidthRemake(const GTAChar* str, bool get_all) { float current_width = 0.0f, max_width = 0.0f; bool had_word = false; auto render_index = plugin.game.Font_GetRenderIndex(); - auto &using_details = plugin.game.game_addr.pFont_Details[render_index]; + auto& using_details = plugin.game.game_addr.pFont_Details[render_index]; TokenStruct token_data; GTAChar token_string[64]; @@ -225,7 +225,7 @@ float CFont::GetStringWidthRemake(const GTAChar *str, bool get_all) case 1: { // 91BF26 current_width += plugin.game.game_addr.pFont_ButtonWidths[token_data.f0[token_index] - 255] * - using_details.fBlipScaleX; + using_details.fBlipScaleX; had_word = true; break; @@ -305,17 +305,22 @@ float CFont::GetStringWidthRemake(const GTAChar *str, bool get_all) return std::max(current_width, max_width); } +void CFont::ProcessStringRoutine(float x, float y, const GTAChar* str, void* a4) +{ + plugin.game.Font_ProcessString(x, y, plugin.string_table.GetString(str), a4); +} + float CFont::GetCHSCharacterSizeNormal() { auto render_index = plugin.game.Font_GetRenderIndex(); - auto &using_details = plugin.game.game_addr.pFont_Details[render_index]; - auto &using_font_data = plugin.game.game_addr.pFont_Datas[using_details.nFont]; + auto& using_details = plugin.game.game_addr.pFont_Details[render_index]; + auto& using_font_data = plugin.game.game_addr.pFont_Datas[using_details.nFont]; float extra_width = using_font_data.fWidthOfSpaceBetweenChars[using_details.nExtraWidthIndex]; return (((fChsWidth + fChsWidthFix + extra_width) / *plugin.game.game_addr.pFont_ResolutionX + - using_details.fEdgeSize2) * - using_details.fScaleX); + using_details.fEdgeSize2) * + using_details.fScaleX); } float CFont::GetCharacterSizeNormalDispatch(GTAChar chr) @@ -335,7 +340,7 @@ float CFont::GetCHSCharacterSizeDrawing(bool use_extra_width) float extra_width = 0.0f; auto render_state = plugin.game.game_addr.pFont_RenderState; - auto &using_font_data = plugin.game.game_addr.pFont_Datas[render_state->nFont]; + auto& using_font_data = plugin.game.game_addr.pFont_Datas[render_state->nFont]; if (use_extra_width) { @@ -343,8 +348,8 @@ float CFont::GetCHSCharacterSizeDrawing(bool use_extra_width) } return ((fChsWidth + fChsWidthFix + extra_width) / *plugin.game.game_addr.pFont_ResolutionX + - render_state->fEdgeSize) * - render_state->fScaleX; + render_state->fEdgeSize) * + render_state->fScaleX; } float CFont::GetCharacterSizeDrawingDispatch(GTAChar chr, bool use_extra_width) @@ -406,23 +411,23 @@ void CFont::PrintCHSChar(float x, float y, GTAChar chr) auto new_range = new_ub - new_lb; return old_diff / old_range * new_range + new_lb; - }; + }; // 将virtual_char_rect投影到old_screen_rect中,得到real_screen_rect CRect real_screen_rect; // 计算y的第二个参数越小,字的y长度越大 real_screen_rect.top_right.x = flt_proj(0.0f, 64.0f, relative_char_rect.top_right.x, old_screen_rect.bottom_left.x, - old_screen_rect.top_right.x); + old_screen_rect.top_right.x); real_screen_rect.top_right.y = flt_proj(0.0f, 74.4912f, relative_char_rect.top_right.y, old_screen_rect.top_right.y, - old_screen_rect.bottom_left.y); + old_screen_rect.bottom_left.y); real_screen_rect.bottom_left.x = flt_proj(0.0f, 64.0f, relative_char_rect.bottom_left.x, - old_screen_rect.bottom_left.x, old_screen_rect.top_right.x); + old_screen_rect.bottom_left.x, old_screen_rect.top_right.x); real_screen_rect.bottom_left.y = flt_proj(0.0f, 74.4912f, relative_char_rect.bottom_left.y, - old_screen_rect.top_right.y, old_screen_rect.bottom_left.y); + old_screen_rect.top_right.y, old_screen_rect.bottom_left.y); switch (render_state->nFont) { diff --git a/plugin/font.h b/plugin/font.h index 4912e5b..1af3067 100644 --- a/plugin/font.h +++ b/plugin/font.h @@ -12,7 +12,7 @@ enum eTextAlignment class CFontDetails { - public: +public: uint field_0; float fScaleX; float fScaleY; @@ -46,7 +46,7 @@ VALIDATE_SIZE(CFontDetails, 0x48); class CFontInfo { - public: +public: uchar iPropValues[255]; uchar iTextureMap[255]; char pad1[2]; @@ -62,7 +62,7 @@ class CFontInfo int iSubFont2End; int iCommonFontStart; int iCommonFontEnd; - void *pTexture; + void* pTexture; float fTextureWidth; float fTextureHeight; float fSpriteSize; @@ -73,7 +73,7 @@ VALIDATE_SIZE(CFontInfo, 0x258); class CFontRenderState { - public: +public: uint magic; float field_4; float field_8; @@ -99,7 +99,7 @@ VALIDATE_SIZE(CFontRenderState, 0x38); class CFontBuffer { - public: +public: CFontRenderState render_state; GTAChar buffer[996]; }; @@ -119,8 +119,8 @@ VALIDATE_SIZE(TokenStruct, 0x114); class CFont { - public: - static void *__fastcall LoadTextureCB(void *, int, uint); +public: + static void* __fastcall LoadTextureCB(void*, int, uint); static float GetCharacterSizeNormalDispatch(GTAChar chr); static float GetCHSCharacterSizeNormal(); @@ -134,14 +134,16 @@ class CFont // 判断字符是否为不能放在行首的标点符号 static bool IsSpecialPunctuationMark(GTAChar chr); // 将特殊字符看作单词的一部分 - static void SkipSpecialPunctuationMarks(const GTAChar *&str); - static void AddSpecialPunctuationMarksWidth(const GTAChar *&str, float *width); + static void SkipSpecialPunctuationMarks(const GTAChar*& str); + static void AddSpecialPunctuationMarksWidth(const GTAChar*& str, float* width); - static const GTAChar *SkipWord(const GTAChar *str); - static const GTAChar *SkipWord_Prolog(std::uintptr_t address); - static const GTAChar *SkipSpaces(const GTAChar *text); + static const GTAChar* SkipWord(const GTAChar* str); + static const GTAChar* SkipWord_Prolog(std::uintptr_t address); + static const GTAChar* SkipSpaces(const GTAChar* text); - static float GetMaxWordWidth(const GTAChar *text); + static float GetMaxWordWidth(const GTAChar* text); - static float GetStringWidthRemake(const GTAChar *str, bool get_all); + static float GetStringWidthRemake(const GTAChar* str, bool get_all); + + static void ProcessStringRoutine(float x, float y, const GTAChar* str, void* a4); }; diff --git a/plugin/game.cpp b/plugin/game.cpp index 130eb58..32b80a5 100644 --- a/plugin/game.cpp +++ b/plugin/game.cpp @@ -1,9 +1,9 @@ #include "game.h" #include "plugin.h" -void CGame::Graphics_SetRenderState(void *texture, int arg4) +void CGame::Graphics_SetRenderState(void* texture, int arg4) { - injector::cstd::call(game_addr.fnGraphics_SetRenderState, texture, arg4); + injector::cstd::call(game_addr.fnGraphics_SetRenderState, texture, arg4); } void* CGame::Dictionary_grcTexturePC_GetElementByKey(void* dictionary, uint hash) @@ -31,9 +31,9 @@ float CGame::Font_GetCharacterSizeDrawing(GTAChar chr, bool use_extra_width) return injector::cstd::call(game_addr.fnFont_GetCharacterSizeDrawing, chr, use_extra_width); } -void CGame::Font_Render2DPrimitive(const CRect *screen_rect, const CRect *texture_rect, uint color, bool buffered) +void CGame::Font_Render2DPrimitive(const CRect* screen_rect, const CRect* texture_rect, uint color, bool buffered) { - injector::cstd::call(game_addr.fnFont_Render2DPrimitive, screen_rect, texture_rect, color, buffered); + injector::cstd::call(game_addr.fnFont_Render2DPrimitive, screen_rect, texture_rect, color, buffered); } void CGame::Font_PrintChar(float posx, float posy, GTAChar chr, bool buffered) @@ -41,26 +41,31 @@ void CGame::Font_PrintChar(float posx, float posy, GTAChar chr, bool buffered) injector::cstd::call(game_addr.fnFont_PrintChar, posx, posy, chr, buffered); } -int CGame::Font_ParseToken(const GTAChar *text, GTAChar *text_to_show, TokenStruct *token_data) +void CGame::Font_ProcessString(float posx, float posy, const GTAChar* str, void* a4) { - return injector::cstd::call( + injector::cstd::call(game_addr.fnFont_ProcessString, posx, posy, str, a4); +} + +int CGame::Font_ParseToken(const GTAChar* text, GTAChar* text_to_show, TokenStruct* token_data) +{ + return injector::cstd::call( game_addr.fnFont_ParseToken, text, text_to_show, token_data); } -const GTAChar *CGame::Font_ProcessToken(const GTAChar *text, uint *color, bool get_color, uchar *color_code, - int *key_number, bool *is_new_line_token, GTAChar *text_to_show, TokenStruct *token_data) +const GTAChar* CGame::Font_ProcessToken(const GTAChar* text, uint* color, bool get_color, uchar* color_code, + int* key_number, bool* is_new_line_token, GTAChar* text_to_show, TokenStruct* token_data) { - return injector::cstd::call( + return injector::cstd::call( game_addr.fnFont_ProcessToken, text, color, get_color, color_code, key_number, is_new_line_token, text_to_show, token_data); } -void CGame::Font_AddTokenStringWidth(const GTAChar *text, float *width, int render_index) +void CGame::Font_AddTokenStringWidth(const GTAChar* text, float* width, int render_index) { - return injector::cstd::call(game_addr.fnFont_AddTokenStringWidth, text, width, render_index); + return injector::cstd::call(game_addr.fnFont_AddTokenStringWidth, text, width, render_index); } -void CGame::MailAppendByteString(int id, const uchar *str) +void CGame::MailAppendByteString(int id, const uchar* str) { - injector::stdcall::call(game_addr.fnMailAppendByteString, id, str); + injector::stdcall::call(game_addr.fnMailAppendByteString, id, str); } diff --git a/plugin/game.h b/plugin/game.h index 8020a77..903688e 100644 --- a/plugin/game.h +++ b/plugin/game.h @@ -4,61 +4,63 @@ class CGame { - public: +public: struct { - IDirect3DDevice9 **ppD3DDevice; - void *pTexturePCVirtualTable; - void *fnGraphics_SetRenderState; - void *fnDictionary_GetElementByKey; - void *fnHash_HashStringFromSeediCase; - int *pGameEpisodeID; + IDirect3DDevice9** ppD3DDevice; + void* pTexturePCVirtualTable; + void* fnGraphics_SetRenderState; + void* fnDictionary_GetElementByKey; + void* fnHash_HashStringFromSeediCase; + int* pGameEpisodeID; - CFontInfo *pFont_Datas; - CFontDetails *pFont_Details; - CFontRenderState *pFont_RenderState; + CFontInfo* pFont_Datas; + CFontDetails* pFont_Details; + CFontRenderState* pFont_RenderState; - float *pFont_BlipWidth; - float *pFont_ResolutionX; - float *pFont_ButtonWidths; + float* pFont_BlipWidth; + float* pFont_ResolutionX; + float* pFont_ButtonWidths; - void *fnFont_GetRenderIndex; - void *fnFont_PrintChar; - void *fnFont_GetCharacterSizeNormal; - void *fnFont_GetCharacterSizeDrawing; - void *fnFont_Render2DPrimitive; - void *fnFont_AddTokenStringWidth; - void *fnFont_ParseToken; - void *fnFont_ProcessToken; + void* fnFont_GetRenderIndex; + void* fnFont_PrintChar; + void* fnFont_GetCharacterSizeNormal; + void* fnFont_GetCharacterSizeDrawing; + void* fnFont_Render2DPrimitive; + void* fnFont_AddTokenStringWidth; + void* fnFont_ParseToken; + void* fnFont_ProcessToken; + void* fnFont_ProcessString; - void *fnMailAppendByteString; - char *pDLCTruncateBuffer; + void* fnMailAppendByteString; + char* pDLCTruncateBuffer; } game_addr; - void Graphics_SetRenderState(void *texture, int = 1); + void Graphics_SetRenderState(void* texture, int = 1); - void *Dictionary_grcTexturePC_GetElementByKey(void *dictionary, uint hash); + void* Dictionary_grcTexturePC_GetElementByKey(void* dictionary, uint hash); - uint Hash_HashStringFromSeediCase(const char *str, uint hash = 0); + uint Hash_HashStringFromSeediCase(const char* str, uint hash = 0); uchar Font_GetRenderIndex(); float Font_GetCharacterSizeNormal(GTAChar chr); float Font_GetCharacterSizeDrawing(GTAChar chr, bool use_extra_width); - void Font_Render2DPrimitive(const CRect *screen_rect, const CRect *texture_rect, uint color, bool buffered); + void Font_Render2DPrimitive(const CRect* screen_rect, const CRect* texture_rect, uint color, bool buffered); void Font_PrintChar(float posx, float posy, GTAChar chr, bool buffered); + void Font_ProcessString(float posx, float posy, const GTAChar* str, void* a4); // ParseToken逻辑 // 1000+ BLIP // 256-300 按键类 // 全部strcmp都不通过则当成单字符token,返回此字符 - int Font_ParseToken(const GTAChar *text, GTAChar *text_to_show, TokenStruct *token_data); + int Font_ParseToken(const GTAChar* text, GTAChar* text_to_show, TokenStruct* token_data); - const GTAChar *Font_ProcessToken(const GTAChar *text, uint *color, bool get_color, uchar *color_code, - int *key_number, bool *is_new_line_token, GTAChar *text_to_show, - TokenStruct *token_data); + const GTAChar* Font_ProcessToken(const GTAChar* text, uint* color, bool get_color, uchar* color_code, + int* key_number, bool* is_new_line_token, GTAChar* text_to_show, + TokenStruct* token_data); - void Font_AddTokenStringWidth(const GTAChar *text, float *width, int render_index); + void Font_AddTokenStringWidth(const GTAChar* text, float* width, int render_index); - void MailAppendByteString(int id, const uchar *str); + void MailAppendByteString(int id, const uchar* str); }; diff --git a/plugin/gta_font.cpp b/plugin/gta_font.cpp index 530a909..cf7192f 100644 --- a/plugin/gta_font.cpp +++ b/plugin/gta_font.cpp @@ -4,148 +4,129 @@ namespace gta_font { -static const char *__stdcall GetTextFileName(int) -{ - return "chinese.gxt"; -} - -// 922095 -__declspec(naked) void ProcessToken_Epilog() -{ - __asm + // 922095 + __declspec(naked) void ProcessToken_Epilog() { - pop edi; - call plugin.game.game_addr.fnFont_ProcessToken; - or eax, 0x80000000; - jmp edi; + __asm + { + pop edi; + call plugin.game.game_addr.fnFont_ProcessToken; + or eax, 0x80000000; + jmp edi; + } } -} -// 922120 -__declspec(naked) void ProcessToken_Epilog2() -{ - __asm + // 922120 + __declspec(naked) void ProcessToken_Epilog2() { - and edi,0x7FFFFFFF; - movzx eax,word ptr[edi-4]; - sub edi,4; - ret; + __asm + { + and edi, 0x7FFFFFFF; + movzx eax, word ptr[edi - 4]; + sub edi, 4; + ret; + } } -} -// 922147 -__declspec(naked) void ProcessToken_Epilog3() -{ - __asm + // 922147 + __declspec(naked) void ProcessToken_Epilog3() { - and edi,0x7FFFFFFF; - mov [esp+0x16], cl; - test cl,cl; - ret; + __asm + { + and edi, 0x7FFFFFFF; + mov[esp + 0x16], cl; + test cl, cl; + ret; + } } -} -static char *strncpy_for_datfile_section(char *dst, char *src, uint count) -{ - return std::strncpy(dst, "content_chs.dat", count); -} + void register_patches(batch_matching& batch_matcher) + { + // 搜索"~%c~"找到CFont::ProcessString -void register_patches(batch_matching &batch_matcher) -{ - // 搜索"~%c~"找到CFont::ProcessString + //储存CFont::ProcessString地址 + batch_matcher.register_step("81 EC 8C 0A 00 00", 1, [](const byte_pattern::result_type& addresses) { + plugin.game.game_addr.fnFont_ProcessString = addresses[0].p(); + }); - // ProcessToken epilog - batch_matcher.register_step("89 AC 24 80 00 00 00 E8", 1, [](const byte_pattern::result_type &addresses) { - plugin.game.game_addr.fnFont_ProcessToken = injector::GetBranchDestination(addresses[0].i(7)).get(); - injector::MakeCALL(addresses[0].i(7), ProcessToken_Epilog); - }); + //劫持菜单项的CFont::ProcessString + batch_matcher.register_step("F3 0F 10 44 24 18 8B C1 50", 1, [](const byte_pattern::result_type& addresses) { + injector::MakeCALL(addresses[0].i(33), CFont::ProcessStringRoutine); + }); - // ProcessString token branch2 - batch_matcher.register_step("0F B7 47 FC 83 EF 04", 1, [](const std::vector &addresses) { - injector::MakeCALL(addresses[0].i(), ProcessToken_Epilog2); - injector::MakeNOP(addresses[0].i(5), 2); - }); + // ProcessToken epilog + batch_matcher.register_step("89 AC 24 80 00 00 00 E8", 1, [](const byte_pattern::result_type& addresses) { + plugin.game.game_addr.fnFont_ProcessToken = injector::GetBranchDestination(addresses[0].i(7)).get(); + injector::MakeCALL(addresses[0].i(7), ProcessToken_Epilog); + }); - // ProcessString token branch3 - batch_matcher.register_step("88 4C 24 12 84 C9", 1, [](const std::vector &addresses) { - injector::MakeCALL(addresses[0].i(), ProcessToken_Epilog3); - injector::MakeNOP(addresses[0].i(5), 1); - }); + // ProcessString token branch2 + batch_matcher.register_step("0F B7 47 FC 83 EF 04", 1, [](const std::vector& addresses) { + injector::MakeCALL(addresses[0].i(), ProcessToken_Epilog2); + injector::MakeNOP(addresses[0].i(5), 2); + }); - // CFont::ProcessString使用了 - // 跳过单词 - batch_matcher.register_step("57 8B 7C 24 08 85 FF 75 04 33 C0 5F C3 56", 1, - [](const byte_pattern::result_type &addresses) { - injector::MakeJMP(addresses[0].i(), CFont::SkipWord_Prolog); - }); + // ProcessString token branch3 + batch_matcher.register_step("88 4C 24 12 84 C9", 1, [](const std::vector& addresses) { + injector::MakeCALL(addresses[0].i(), ProcessToken_Epilog3); + injector::MakeNOP(addresses[0].i(5), 1); + }); - // ProcessString使用了 - // 获取字符宽度 - batch_matcher.register_step("83 C0 E0 50 E8 ? ? ? ? D9 5C 24", 2, [](const byte_pattern::result_type &addresses) { - injector::MakeCALL(addresses[0].i(4), CFont::GetCharacterSizeNormalDispatch); - injector::MakeCALL(addresses[1].i(4), CFont::GetCharacterSizeNormalDispatch); - }); + // CFont::ProcessString使用了 + // 跳过单词 + batch_matcher.register_step("57 8B 7C 24 08 85 FF 75 04 33 C0 5F C3 56", 1, + [](const byte_pattern::result_type& addresses) { + injector::MakeJMP(addresses[0].i(), CFont::SkipWord_Prolog); + }); - // 查找GetCharacterSizeNormal的引用获得 - batch_matcher.register_step("6A 01 57 E8 ? ? ? ? D9 5C 24 30", 1, [](const byte_pattern::result_type &addresses) { - injector::MakeCALL(addresses[0].i(3), CFont::GetCharacterSizeDrawingDispatch); - }); + // ProcessString使用了 + // 获取字符宽度 + batch_matcher.register_step("83 C0 E0 50 E8 ? ? ? ? D9 5C 24", 2, [](const byte_pattern::result_type& addresses) { + injector::MakeCALL(addresses[0].i(4), CFont::GetCharacterSizeNormalDispatch); + injector::MakeCALL(addresses[1].i(4), CFont::GetCharacterSizeNormalDispatch); + }); - // 查找GetCharacterSizeNormal的引用获得 - batch_matcher.register_step("EB 12 6A 01 57 E8 ? ? ? ? D9 5C 24 24", 1, - [](const byte_pattern::result_type &addresses) { - injector::MakeCALL(addresses[0].i(5), CFont::GetCharacterSizeDrawingDispatch); - }); + // 查找GetCharacterSizeNormal的引用获得 + batch_matcher.register_step("6A 01 57 E8 ? ? ? ? D9 5C 24 30", 1, [](const byte_pattern::result_type& addresses) { + injector::MakeCALL(addresses[0].i(3), CFont::GetCharacterSizeDrawingDispatch); + }); - // RenderSingleBuffer使用了 - // 绘制字符 - batch_matcher.register_step("F3 0F 11 0C 24 E8 ? ? ? ? 8B 35", 1, [](const byte_pattern::result_type &addresses) { - injector::MakeCALL(addresses[0].i(5), CFont::PrintCharDispatch); - }); + // 查找GetCharacterSizeNormal的引用获得 + batch_matcher.register_step("EB 12 6A 01 57 E8 ? ? ? ? D9 5C 24 24", 1, + [](const byte_pattern::result_type& addresses) { + injector::MakeCALL(addresses[0].i(5), CFont::GetCharacterSizeDrawingDispatch); + }); - // 另一个使用PrintChar的函数里面 - batch_matcher.register_step("E8 ? ? ? ? 6A 01 57 E8 ? ? ? ? D9 5C 24 30", 1, - [](const byte_pattern::result_type &addresses) { - injector::MakeCALL(addresses[0].i(), CFont::PrintCharDispatch); - }); + // RenderSingleBuffer使用了 + // 绘制字符 + batch_matcher.register_step("F3 0F 11 0C 24 E8 ? ? ? ? 8B 35", 1, [](const byte_pattern::result_type& addresses) { + injector::MakeCALL(addresses[0].i(5), CFont::PrintCharDispatch); + }); - // 搜索"font3" - // 修改本体字体文件名为fonts_chs - batch_matcher.register_step("80 7C 24 13 00 0F 85 6E 01 00 00", 2, [](const byte_pattern::result_type &addresses) { - injector::WriteMemory(addresses[0].i(12), "platform:/textures/fonts_chs", true); - injector::WriteMemory(addresses[1].i(12), "platform:/textures/fonts_chs", true); - }); + // 另一个使用PrintChar的函数里面 + batch_matcher.register_step("E8 ? ? ? ? 6A 01 57 E8 ? ? ? ? D9 5C 24 30", 1, + [](const byte_pattern::result_type& addresses) { + injector::MakeCALL(addresses[0].i(), CFont::PrintCharDispatch); + }); - // 修改资料片字体文件名 - // 935093 修改setup2.xml解析的结果为content_chs.dat,在content_chs.dat中指定FONTTXD - batch_matcher.register_step("6A 20 50 8D 44 24 6C 50 E8", 1, [](const byte_pattern::result_type &addresses) { - injector::MakeCALL(addresses[0].i(8), strncpy_for_datfile_section); - }); + // 加载fonts.wtd中的font_chs + batch_matcher.register_step("8B CE 50 E8 ? ? ? ? 80 3D ? ? ? ? 6A", 2, + [](const byte_pattern::result_type& addresses) { + injector::MakeCALL(addresses[0].i(3), CFont::LoadTextureCB); + injector::MakeCALL(addresses[1].i(3), CFont::LoadTextureCB); + }); - // 加载fonts_chs.wtd中的font_chs - batch_matcher.register_step("8B CE 50 E8 ? ? ? ? 80 3D ? ? ? ? 6A", 2, - [](const byte_pattern::result_type &addresses) { - injector::MakeCALL(addresses[0].i(3), CFont::LoadTextureCB); - injector::MakeCALL(addresses[1].i(3), CFont::LoadTextureCB); - }); + // ProcessString使用了 + // GetStringWidth + batch_matcher.register_step("B8 B4 10 00 00", 1, [](const std::vector& addresses) { + injector::MakeJMP(addresses[0].i(-6), CFont::GetStringWidthRemake); + }); - // 搜索"american.gxt" - // 改变GXT的文件名 - batch_matcher.register_step("80 7C 24 04 00 56 8B F1 74 07", 1, [](const byte_pattern::result_type &addresses) { - injector::MakeJMP(addresses[0].i(), GetTextFileName); - }); - - // ProcessString使用了 - // GetStringWidth - batch_matcher.register_step("B8 B4 10 00 00", 1, [](const std::vector &addresses) { - injector::MakeJMP(addresses[0].i(-6), CFont::GetStringWidthRemake); - }); - - // 使用了GetStringWidth - // GetMaxWordWidth - batch_matcher.register_step("51 56 8B 74 24 0C 85 F6 75 05 D9 EE 5E 59 C3 66", 1, - [](const std::vector &addresses) { - injector::MakeJMP(addresses[0].i(), CFont::GetMaxWordWidth); - }); -} + // 使用了GetStringWidth + // GetMaxWordWidth + batch_matcher.register_step("51 56 8B 74 24 0C 85 F6 75 05 D9 EE 5E 59 C3 66", 1, + [](const std::vector& addresses) { + injector::MakeJMP(addresses[0].i(), CFont::GetMaxWordWidth); + }); + } } // namespace gta_font diff --git a/plugin/gta_string.h b/plugin/gta_string.h index 8a7459f..590f9b3 100644 --- a/plugin/gta_string.h +++ b/plugin/gta_string.h @@ -1,4 +1,4 @@ -#pragma once +#pragma once #include "../common/stdinc.h" #include "../common/common.h" @@ -6,20 +6,20 @@ namespace gta_string { unsigned gtaWcslen(const GTAChar* str); - //´浵±êÌâ + //存档标题 uchar* gtaTruncateString(uchar* dst, const GTAChar* src, unsigned size); void gtaExpandString(const uchar* src, GTAChar* dst); - //ÏÔʾ´浵¸²¸ÇÌáʾʱÓÃÓڸñʽ»¯µĴ浵Ãû³Æ + //显示存档覆盖提示时用于格式化的存档名称 void gtaExpandString2(GTAChar* dst, const uchar* src, int a8); - //TBoGTÖØÍæÈÎÎñѡÔñ½çÃæ + //TBoGT重玩任务选择界面 void gtaExpandString3(GTAChar* dst, const uchar* src, int a8); - //Óʼþ/Íøҳ + //邮件/网页 void gtaTruncateString2(const GTAChar* src, uchar* dst); void gtaExpandStringGxt(const uchar* src, GTAChar* dst); - //½ÓÊտí×ַû´®£¬µ«ת»»³Éutf8 + //接收宽字符串,但转换成utf8 void __stdcall gtaMailAppendWideStringAsUtf8(int id, const GTAChar* str); uchar* gtaUTF8strncpy(uchar* dest, const uchar* source, unsigned size); diff --git a/plugin/plugin.cpp b/plugin/plugin.cpp index f594630..6d34d8d 100644 --- a/plugin/plugin.cpp +++ b/plugin/plugin.cpp @@ -71,6 +71,7 @@ bool CPlugin::Init(HMODULE module) char_table.LoadTable(GetPluginAsset("char_table.dat")); whm_table.LoadTable(GetPluginAsset("whm_table.dat")); + string_table.LoadTable(GetPluginAsset("custom_translations.json")); return true; } diff --git a/plugin/plugin.h b/plugin/plugin.h index 7ba51cd..72eac3a 100644 --- a/plugin/plugin.h +++ b/plugin/plugin.h @@ -5,6 +5,7 @@ #include "char_table.h" #include "whm_table.h" #include "game.h" +#include "string_table.h" #include "../common/module_path.h" class CPlugin @@ -14,16 +15,17 @@ public: HANDLE GetGameModule() const; HANDLE GetPluginModule() const; std::filesystem::path GetGameRoot() const; - std::filesystem::path GetPluginAsset(const std::filesystem::path &rest_path) const; + std::filesystem::path GetPluginAsset(const std::filesystem::path& rest_path) const; //保证进入构造函数之后这些对象已经初始化了 ModulePath game_module_path, plugin_module_path; CCharTable char_table; CWhmTable whm_table; + CStringTable string_table; CGame game; private: - static void RegisterPatchSteps(batch_matching &batch_matcher); + static void RegisterPatchSteps(batch_matching& batch_matcher); }; extern CPlugin plugin; diff --git a/plugin/string_table.cpp b/plugin/string_table.cpp index 096c5e8..58a0068 100644 --- a/plugin/string_table.cpp +++ b/plugin/string_table.cpp @@ -1,15 +1,10 @@ #include "string_table.h" #include "../common/fnv_hash.h" -CStringTable& CStringTable::GetInstance() -{ - static CStringTable instance; - - return instance; -} - void CStringTable::LoadTable(const std::filesystem::path& filename) { + auto logger = spdlog::get("gta4.chs"); + std::ifstream ifs(filename); if (!ifs) @@ -25,7 +20,8 @@ void CStringTable::LoadTable(const std::filesystem::path& filename) { auto origin = it.key(); auto translated = it.value().get(); - std::vector utf16; + std::vector worigin; + std::vector wtranslated; if (translated.empty()) { continue; @@ -33,18 +29,23 @@ void CStringTable::LoadTable(const std::filesystem::path& filename) try { - utf8::utf8to16(translated.begin(), translated.end(), std::back_inserter(utf16)); - utf16.push_back(0); - m_Table.emplace(fnv_hash::hash_string(origin.c_str(), false), std::move(utf16)); + utf8::utf8to16(origin.begin(), origin.end(), std::back_inserter(worigin)); + worigin.push_back(0); + utf8::utf8to16(translated.begin(), translated.end(), std::back_inserter(wtranslated)); + wtranslated.push_back(0); + m_Table.emplace(fnv_hash::hash_string(worigin.data(), false), std::move(wtranslated)); } catch (utf8::exception&) { + logger->error("Utf8 convert failed."); continue; } } } catch (nlohmann::json::exception&) { + logger->error("Json processing failed."); + return; } } diff --git a/plugin/string_table.h b/plugin/string_table.h index 4b73b0b..cb4af54 100644 --- a/plugin/string_table.h +++ b/plugin/string_table.h @@ -5,14 +5,12 @@ class CStringTable { public: - static CStringTable& GetInstance(); + CStringTable() = default; + ~CStringTable() = default; void LoadTable(const std::filesystem::path& filename); const GTAChar* GetString(const GTAChar* origin) const; private: - CStringTable() = default; - ~CStringTable() = default; - - std::unordered_map> m_Table; + std::unordered_map> m_Table; }; diff --git a/whm_io/CMakeLists.txt b/whm_io/CMakeLists.txt index 27c4d3e..3f6c017 100644 --- a/whm_io/CMakeLists.txt +++ b/whm_io/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.24) +cmake_minimum_required(VERSION 3.28) project(whm_io)