Skip to content

Commit

Permalink
Patch TextMessageParse to support "titles.txt" with UTF-16 encoding.
Browse files Browse the repository at this point in the history
  • Loading branch information
hzqst committed Jan 22, 2024
1 parent e24a7be commit 3ab92f8
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 18 deletions.
42 changes: 42 additions & 0 deletions Plugins/CaptionMod/exportfuncs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,48 @@ char * NewV_strncpy(char *a1, const char *a2, size_t a3)
return gPrivateFuncs.V_strncpy(a1, a2, a3);
}

void TextMessageParse(byte* pMemFile, int fileSize)
{
if (fileSize > 2 && pMemFile[0] == 0xFF && pMemFile[1] == 0xFE)
{
if (IsTextUnicode(pMemFile + 2, fileSize - 2, NULL))
{
auto wszBuf = (const wchar_t*)(pMemFile + 2);
char* szBuf = (char*)malloc(fileSize);
if (szBuf)
{
memset(szBuf, 0, fileSize);

int szBufLen = vgui::localize()->ConvertUnicodeToANSI(wszBuf, szBuf, fileSize);

szBuf[szBufLen] = 0;

gPrivateFuncs.TextMessageParse((byte *)szBuf, szBufLen);

free(szBuf);
return;
}
}
}

return gPrivateFuncs.TextMessageParse(pMemFile, fileSize);
}

client_textmessage_t* pfnTextMessageGet(const char* pName)
{
if (g_pViewPort)
{
CDictionary* dict = g_pViewPort->FindDictionary(pName);

if (dict)
{
return NULL;
}
}

return gPrivateFuncs.pfnTextMessageGet(pName);
}

void MessageMode_f(void)
{
if (!m_iIntermission && gEngfuncs.Cmd_Argc() == 1 && cap_newchat->value)
Expand Down
1 change: 1 addition & 0 deletions Plugins/CaptionMod/exportfuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void IN_Accumulate(void);
void CL_CreateMove(float frametime, struct usercmd_s *cmd, int active);

client_textmessage_t *pfnTextMessageGet(const char *pName);
void TextMessageParse(byte* pMemFile, int fileSize);

void *NewClientFactory(void);

Expand Down
15 changes: 0 additions & 15 deletions Plugins/CaptionMod/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1947,21 +1947,6 @@ void CHudMessage::MessageAdd(client_textmessage_t *newMessage, bool bIsDynamicMe
m_DynamicTextMessages.emplace_back(newMessage);
}

client_textmessage_t *pfnTextMessageGet(const char *pName)
{
if (g_pViewPort)
{
CDictionary *dict = g_pViewPort->FindDictionary(pName);

if (dict)
{
return NULL;
}
}

return gPrivateFuncs.pfnTextMessageGet(pName);
}

int __MsgFunc_ShowMenu(const char* pszName, int iSize, void* pbuf)
{
if (m_HudMenu.MsgFunc_ShowMenu(pszName, iSize, pbuf) != 0)
Expand Down
50 changes: 48 additions & 2 deletions Plugins/CaptionMod/privatefuncs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ static hook_t *g_phook_S_FindName = NULL;
static hook_t *g_phook_S_StartDynamicSound = NULL;
static hook_t *g_phook_S_StartStaticSound = NULL;
static hook_t *g_phook_pfnTextMessageGet = NULL;
static hook_t *g_phook_TextMessageParse = NULL;
static hook_t *g_phook_WeaponsResource_SelectSlot = NULL;
static hook_t* g_phook_ScClient_SoundEngine_PlayFMODSound = NULL;
static hook_t* g_phook_FMOD_System_playSound = NULL;
static hook_t *g_phook_ScClient_SoundEngine_PlayFMODSound = NULL;
static hook_t *g_phook_FMOD_System_playSound = NULL;
//static hook_t *g_phook_FileSystem_SetGameDirectory = NULL;

static HMODULE g_hFMODEx = NULL;
Expand Down Expand Up @@ -487,6 +488,49 @@ void Engine_FillAddress(void)
}
Sig_FuncNotFound(S_LoadSound);

if (1)
{
/*
.text:10226B8A 68 44 7F 2C 10 push offset aTmessageTextme ; "tmessage::TextMessageParse : messageCou"...
.text:10226B8F E8 9C 8C FF FF call Sys_Error
.text:10226B94 ; ---------------------------------------------------------------------------
.text:10226B94 83 C4 04 add esp, 4
*/
const char sigs[] = "tmessage::TextMessageParse";
auto TextMessageParse_String = Search_Pattern_Data(sigs);
if (!TextMessageParse_String)
TextMessageParse_String = Search_Pattern_Rdata(sigs);
if (TextMessageParse_String)
{
char pattern[] = "\x68\x2A\x2A\x2A\x2A\xE8\x2A\x2A\x2A\x2A\x83\xC4";
*(DWORD*)(pattern + 1) = (DWORD)TextMessageParse_String;
auto TextMessageParse_PushString = (PUCHAR)Search_Pattern(pattern);
if (TextMessageParse_PushString)
{
gPrivateFuncs.TextMessageParse = (decltype(gPrivateFuncs.TextMessageParse))g_pMetaHookAPI->ReverseSearchFunctionBeginEx(TextMessageParse_PushString, 0x500, [](PUCHAR Candidate) {

if (Candidate[0] == 0x55 &&
Candidate[1] == 0x8B &&
Candidate[2] == 0xEC)
{
return TRUE;
}

//.text:01DC1250 B8 F8 F1 00 00 mov eax, 0F1F8h
//.text : 01DC1255 E8 F6 25 0A 00 call __alloca_probe
if (Candidate[0] == 0xB8 &&
Candidate[5] == 0xE8)
{
return TRUE;
}

return FALSE;
});
}
}
}
Sig_FuncNotFound(TextMessageParse);

if (1)
{
const char pattern[] = "\x50\xFF\x15\x2A\x2A\x2A\x2A\x50\xE8\x2A\x2A\x2A\x2A\x83\xC4\x08\x85\xC0";
Expand Down Expand Up @@ -1664,13 +1708,15 @@ void Engine_InstallHooks(void)
Install_InlineHook(S_StartDynamicSound);
Install_InlineHook(S_StartStaticSound);
Install_InlineHook(pfnTextMessageGet);
Install_InlineHook(TextMessageParse);
}

void Engine_UninstallHooks(void)
{
Uninstall_Hook(S_StartDynamicSound);
Uninstall_Hook(S_StartStaticSound);
Uninstall_Hook(pfnTextMessageGet);
Uninstall_Hook(TextMessageParse);
}

void Client_InstallHooks(void)
Expand Down
3 changes: 2 additions & 1 deletion Plugins/CaptionMod/privatefuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ typedef struct
//VGUI2
char* (*V_strncpy)(char* a1, const char* a2, size_t a3);

//EngineFuncs
//Engine TextMessage
client_textmessage_t *(*pfnTextMessageGet)(const char *pName);
void (*TextMessageParse)(byte* pMemFile, int fileSize);

//Commands
void(*MessageMode_f)(void);
Expand Down

0 comments on commit 3ab92f8

Please sign in to comment.