Skip to content

Commit

Permalink
Merge pull request #543 from sfall-team/feature/fallback-language-by-…
Browse files Browse the repository at this point in the history
…line

Fallback to english by individual message in MSG files
  • Loading branch information
NovaRain authored Jun 3, 2024
2 parents 281e795 + 85d2d2e commit 657ae59
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 22 deletions.
2 changes: 1 addition & 1 deletion sfall/FalloutEngine/Functions_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
114 changes: 94 additions & 20 deletions sfall/Modules/Message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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: <MaleText^FemaleText>
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();

Expand Down Expand Up @@ -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;
}
Expand All @@ -139,26 +139,96 @@ static void __declspec(naked) scr_get_msg_str_speech_hook() {
}
}

struct MessageListInfo {
std::string FileName;
fo::MessageList* List;
};

std::unordered_map<fo::MessageList*, MessageListInfo> 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 {
push eax;
mov ecx, ebx; // message list
call MessageExitHook;
pop eax;
jmp fo::funcoffs::mem_free_;
}
}

Expand Down Expand Up @@ -235,7 +305,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);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion sfall/Modules/Scripting/Handlers/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit 657ae59

Please sign in to comment.