From ecb6dd2b785522dcd90a98be58c50a91fc6ecb66 Mon Sep 17 00:00:00 2001 From: phobos2077 Date: Fri, 31 May 2024 19:34:32 +0200 Subject: [PATCH 1/2] Fallback to english my individual message in MSG files (closes #542) --- sfall/FalloutEngine/Functions_def.h | 2 +- sfall/Modules/Message.cpp | 112 +++++++++++++++++---- sfall/Modules/Scripting/Handlers/Utils.cpp | 2 +- 3 files changed, 94 insertions(+), 22 deletions(-) diff --git a/sfall/FalloutEngine/Functions_def.h b/sfall/FalloutEngine/Functions_def.h index 2861088f8..9d25d90f9 100644 --- a/sfall/FalloutEngine/Functions_def.h +++ b/sfall/FalloutEngine/Functions_def.h @@ -44,7 +44,7 @@ WRAP_WATCOM_FFUNC3(long, item_w_mp_cost, fo::GameObject*, source, long, hitMode, // Calculates path and returns it's length WRAP_WATCOM_FFUNC6(long, make_path_func, fo::GameObject*, objectFrom, long, tileFrom, long, tileTo, char*, pathDataBuffer, long, checkTileTo, void*, blockingFunc) WRAP_WATCOM_FFUNC7(long, make_straight_path_func, fo::GameObject*, objFrom, DWORD, tileFrom, DWORD, tileTo, void*, arrayPtr, DWORD*, outObject, long, flags, void*, blockingFunc) -WRAP_WATCOM_FFUNC3(long, message_find, DWORD*, msgFile, long, msgNumber, DWORD*, outBuf) +WRAP_WATCOM_FFUNC3(long, message_find, fo::MessageList*, msgFile, long, msgNumber, DWORD*, outBuf) WRAP_WATCOM_FFUNC4(long, mouse_click_in, long, x, long, y, long, x_offs, long, y_offs) WRAP_WATCOM_FFUNC4(long, mouse_in, long, x, long, y, long, x_offs, long, y_offs) WRAP_WATCOM_FFUNC3(fo::GameObject*, obj_blocking_at, fo::GameObject*, object, long, tile, long, elevation) diff --git a/sfall/Modules/Message.cpp b/sfall/Modules/Message.cpp index b42a51f35..96d78030f 100644 --- a/sfall/Modules/Message.cpp +++ b/sfall/Modules/Message.cpp @@ -72,7 +72,7 @@ static long heroIsFemale = -1; // Searches the special character in the text and removes the text depending on the player's gender // example: -static long __fastcall ReplaceGenderWord(fo::MessageNode* msgData, DWORD* msgFile) { +static long __fastcall ReplaceGenderWord(fo::MessageNode* msgData, fo::MessageList* msgFile) { if (!InDialog() || msgData->flags & MSG_GENDER_CHECK_FLG) return 1; if (heroIsFemale < 0) heroIsFemale = fo::util::HeroIsFemale(); @@ -119,7 +119,7 @@ static long __fastcall ReplaceGenderWord(fo::MessageNode* msgData, DWORD* msgFil // set flag unsigned long outValue; fo::func::message_find(msgFile, msgData->number, &outValue); - ((fo::MessageNode*)(msgFile[1] + (outValue * 16)))->flags |= MSG_GENDER_CHECK_FLG; + msgFile->nodes[outValue].flags |= MSG_GENDER_CHECK_FLG; return 1; } @@ -139,26 +139,94 @@ static void __declspec(naked) scr_get_msg_str_speech_hook() { } } +struct MessageListInfo { + std::string FileName; + fo::MessageList* List; +}; + +std::unordered_map messageListMap; + +static bool messageLoadForceEnglish; + +static fo::DbFile* __fastcall MessageLoadHook(const char* fullPath, const char* mode, const char* msgFile, fo::MessageList* messageList) { + fo::DbFile* file = !messageLoadForceEnglish ? fo::func::db_fopen(fullPath, mode) : nullptr; + if (file == nullptr) { + messageLoadForceEnglish = false; + char buf[MAX_PATH]; + sprintf(buf, "text\\english\\%s", msgFile); + return fo::func::db_fopen(buf, mode); + } + + // Remember loaded message list name. + messageListMap.emplace(messageList, MessageListInfo{msgFile, nullptr}); + return file; +} + +static long __fastcall MessageFindHook(fo::MessageList** list, long num, DWORD* outIndex) { + if (fo::func::message_find(*list, num, outIndex)) { + return true; + } + // If message not found in original list, try to find in fallback list. + auto& listIt = messageListMap.find(*list); + if (listIt == messageListMap.end()) { + return false; + } + MessageListInfo& listInfo = listIt->second; + if (listInfo.List == nullptr) { + // Load fallback message list. + messageLoadForceEnglish = true; + listInfo.List = new fo::MessageList(); + fo::func::message_load(listInfo.List, listInfo.FileName.c_str()); + // TODO: some error message if fallback file wasn't loaded? + } + *list = listInfo.List; + return fo::func::message_find(*list, num, outIndex); +} + + +static void __fastcall MessageExitHook(fo::MessageList* list) { + // Delete fallback message file. + auto& listIt = messageListMap.find(list); + if (listIt == messageListMap.end()) { + return; + } + MessageListInfo& listInfo = listIt->second; + if (listInfo.List != nullptr) { + fo::func::message_exit(listInfo.List); + delete listInfo.List; + } + messageListMap.erase(listIt); +} + // Loads the msg file from the 'english' folder if it does not exist in the current language directory -static void __declspec(naked) message_load_hook() { +static void __declspec(naked) message_load__db_fopen_hook() { + __asm { + push esi; // message list + push ebp; // relative path + // edx - mode + mov ecx, eax; // full path + call MessageLoadHook; + retn; + } +} + +static void __declspec(naked) message_search__message_find_hook() { __asm { - mov ebx, edx; // keep mode - mov ecx, eax; // keep buf - call fo::funcoffs::db_fopen_; - test eax, eax; - jz noFile; + push ecx; // save msg list + push ebx; // out index + // edx - msg num + lea ecx, [esp + 4]; // msg list ** + call MessageFindHook; + pop ecx; // restore, possibly modified list ptr retn; -noFile: - push ebp; // file - push 0x500208; // "english" - push 0x50B7D0; // "text" - push 0x50B7D8; // "%s\%s\%s" - push ecx; // buf - call fo::funcoffs::sprintf_; - add esp, 20; - mov edx, ebx; - mov eax, ecx; - jmp fo::funcoffs::db_fopen_; + } +} + +static void __declspec(naked) message_exit__mem_free_hook() { + __asm { + mov ecx, ebx; // message list + call MessageExitHook; + jmp fo::funcoffs::mem_free_; } } @@ -235,7 +303,11 @@ static void FallbackEnglishLoadMsgFiles() { const char* lang; if (fo::func::get_game_config_string(&lang, "system", "language")) { strncpy_s(gameLanguage, lang, _TRUNCATE); - if (_stricmp(lang, "english") != 0) HookCall(0x484B18, message_load_hook); + if (_stricmp(lang, "english") != 0) { + HookCall(0x484B18, message_load__db_fopen_hook); + HookCall(0x484C4B, message_search__message_find_hook); + HookCall(0x4849B9, message_exit__mem_free_hook); + } } } diff --git a/sfall/Modules/Scripting/Handlers/Utils.cpp b/sfall/Modules/Scripting/Handlers/Utils.cpp index f53080a31..759b84f73 100644 --- a/sfall/Modules/Scripting/Handlers/Utils.cpp +++ b/sfall/Modules/Scripting/Handlers/Utils.cpp @@ -319,7 +319,7 @@ void op_message_str_game(OpcodeContext& ctx) { } else if (fileId >= 0x2000) { // Extra game message files. ExtraGameMessageListsMap::iterator it = Message::gExtraGameMsgLists.find(fileId); if (it != Message::gExtraGameMsgLists.end()) { - msg = fo::util::GetMsg(it->second.get(), msgId, 2); + msg = fo::util::GetMessageStr(it->second.get(), msgId); } } } From 85d2d2e24d47bd91c1c1973e2e4dca5598eac612 Mon Sep 17 00:00:00 2001 From: phobos2077 Date: Sun, 2 Jun 2024 10:25:18 +0200 Subject: [PATCH 2/2] message_exit__mem_free_hook: save eax register - Otherwise ReleaseXP build messes it up in MessageExitHook --- sfall/Modules/Message.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sfall/Modules/Message.cpp b/sfall/Modules/Message.cpp index 96d78030f..6e60420df 100644 --- a/sfall/Modules/Message.cpp +++ b/sfall/Modules/Message.cpp @@ -224,9 +224,11 @@ static void __declspec(naked) message_search__message_find_hook() { static void __declspec(naked) message_exit__mem_free_hook() { __asm { - mov ecx, ebx; // message list + push eax; + mov ecx, ebx; // message list call MessageExitHook; - jmp fo::funcoffs::mem_free_; + pop eax; + jmp fo::funcoffs::mem_free_; } }