From 5ed5ef949662ea4bebaf8d3f8c11906815f3bf7f Mon Sep 17 00:00:00 2001 From: wootguy Date: Mon, 16 Dec 2024 17:39:10 -0800 Subject: [PATCH] add chat util, update hook, fixes - fix console prints being tripled formatted - fix random error code for plugin load failures --- dlls/hooks/PluginHooks.h | 2 +- dlls/hooks/PluginManager.cpp | 7 +-- dlls/hooks/client_commands.cpp | 89 +--------------------------------- dlls/util/util.cpp | 55 ++++++++++++++++++++- dlls/util/util.h | 4 ++ 5 files changed, 64 insertions(+), 93 deletions(-) diff --git a/dlls/hooks/PluginHooks.h b/dlls/hooks/PluginHooks.h index 76f444fc..8eddd9be 100644 --- a/dlls/hooks/PluginHooks.h +++ b/dlls/hooks/PluginHooks.h @@ -163,7 +163,7 @@ struct HLCOOP_PLUGIN_HOOKS { HOOK_RETURN_DATA (*pfnCvarValue2)(const edict_t* pEntity, int requestID, const char* pszCvarName, const char* pszValue); // called before a chat message is sent. Update the message pointer to change the message. - HOOK_RETURN_DATA (*pfnChatMessage)(CBasePlayer* plr, const char** message); + HOOK_RETURN_DATA (*pfnChatMessage)(CBasePlayer* plr, const char** message, bool teamOnly); }; // do not call directly, use RegisterPlugin instead diff --git a/dlls/hooks/PluginManager.cpp b/dlls/hooks/PluginManager.cpp index d57043d4..154a08e8 100644 --- a/dlls/hooks/PluginManager.cpp +++ b/dlls/hooks/PluginManager.cpp @@ -37,8 +37,9 @@ int g_plugin_id = 0; #ifdef _WIN32 #include "windows.h" -#define LOADLIB_FUNC_NAME "LoadLibrary" +#define LOADLIB_FUNC_NAME "" #define PLUGIN_EXT ".dll" +#define LibLoadError() (std::string("LoadLibrary error code ") + std::to_string(GetLastError()).c_str()) #else #include #define PLUGIN_EXT ".so" @@ -47,6 +48,7 @@ int g_plugin_id = 0; #define GetLastError dlerror #define FreeLibrary dlclose #define HMODULE void* +#define LibLoadError() dlerror() #endif typedef int(*PLUGIN_INIT_FUNCTION)(void); @@ -92,8 +94,7 @@ bool PluginManager::LoadPlugin(Plugin& plugin) { #endif if (!plugin.h_module) { - ALERT(at_error, "Plugin load failed '%s' (" LOADLIB_FUNC_NAME " error code %d)\n", - plugin.fpath.c_str(), GetLastError()); + ALERT(at_error, "Plugin load failed '%s' (%s)\n", plugin.fpath.c_str(), LibLoadError()); return false; } diff --git a/dlls/hooks/client_commands.cpp b/dlls/hooks/client_commands.cpp index 242ffdea..8f9b4834 100644 --- a/dlls/hooks/client_commands.cpp +++ b/dlls/hooks/client_commands.cpp @@ -143,7 +143,6 @@ void Host_Say(edict_t* pEntity, int teamonly) CBasePlayer* client; int j; char* p; - char text[128]; char szTemp[256]; const char* cpSay = "say"; const char* cpSayTeam = "say_team"; @@ -198,95 +197,11 @@ void Host_Say(edict_t* pEntity, int teamonly) if (!p || !p[0] || !Q_UnicodeValidate(p)) return; // no character found, so say nothing -// turn on color set 2 (color on, no sound) - // turn on color set 2 (color on, no sound) - if (player->IsObserver() && (teamonly)) - snprintf(text, 128, "%c(SPEC) %s: ", 2, STRING(pEntity->v.netname)); - else if (teamonly) - snprintf(text, 128, "%c(TEAM) %s: ", 2, STRING(pEntity->v.netname)); - else - snprintf(text, 128, "%c%s: ", 2, STRING(pEntity->v.netname)); - - j = sizeof(text) - 2 - strlen(text); // -2 for /n and null terminator - if ((int)strlen(p) > j) - p[j] = 0; - - CALL_HOOKS_VOID(pfnChatMessage, player, (const char**)&p); - - strcat_safe(text, p, 128); - strcat_safe(text, "\n", 128); - + CALL_HOOKS_VOID(pfnChatMessage, player, (const char**)&p, teamonly); player->m_flNextChatTime = gpGlobals->time + CHAT_INTERVAL; - // loop through all players - // Start with the first player. - // This may return the world in single player if the client types something between levels or during spawn - // so check it, or it will infinite loop - - client = NULL; - while (((client = (CBasePlayer*)UTIL_FindEntityByClassname(client, "player")) != NULL) && (!FNullEnt(client->edict()))) - { - if (!client->pev) - continue; - - if (client->edict() == pEntity) - continue; - - if (!(client->IsNetClient())) // Not a client ? (should never be true) - continue; - - // can the receiver hear the sender? or has he muted him? - // TODO: voice muting has a buggy implementation that causes random ppl to get muted - // when you change servers. Create a new system. - //if (g_VoiceGameMgr.PlayerHasBlockedPlayer(client, player)) - // continue; - - /* - if (!player->IsObserver() && teamonly && g_pGameRules->PlayerRelationship(client, CBaseEntity::Instance(pEntity)) != GR_TEAMMATE) - continue; - - // Spectators can only talk to other specs - if (player->IsObserver() && teamonly) - if (!client->IsObserver()) - continue; - */ - - MESSAGE_BEGIN(MSG_ONE, gmsgSayText, NULL, client->pev); - WRITE_BYTE(ENTINDEX(pEntity)); - WRITE_STRING(text); - MESSAGE_END(); - - } - - if (player->tempNameActive) { - player->Rename(player->DisplayName(), true, MSG_ONE, player->edict()); - player->UpdateTeamInfo(-1, MSG_ONE, player->edict()); - } - - // print to the sending client - MESSAGE_BEGIN(MSG_ONE, gmsgSayText, NULL, &pEntity->v); - WRITE_BYTE(ENTINDEX(pEntity)); - WRITE_STRING(text); - MESSAGE_END(); - - if (player->tempNameActive) { - player->Rename(player->m_tempName, false, MSG_ONE, player->edict()); - player->UpdateTeamInfo(player->m_tempTeam, MSG_ONE, player->edict()); - } - - // echo to server console for listen servers, dedicated servers should have logs enabled - if (!IS_DEDICATED_SERVER()) - g_engfuncs.pfnServerPrint(text); - - const char* temp; - if (teamonly) - temp = "say_team"; - else - temp = "say"; - - // team match? - UTIL_LogPlayerEvent(pEntity, "%s \"%s\"\n", temp, p); + UTIL_ClientSay(player, p, NULL, NULL, teamonly); } #define ABORT_IF_CHEATS_DISABLED(cheatName) \ diff --git a/dlls/util/util.cpp b/dlls/util/util.cpp index adc80c30..da3477bf 100644 --- a/dlls/util/util.cpp +++ b/dlls/util/util.cpp @@ -1490,6 +1490,57 @@ void UTIL_ClientPrint(CBaseEntity* client, PRINT_TYPE print_type, const char * m } } +void UTIL_ClientSay(CBasePlayer* plr, const char* text, const char* customPrefix, const char* customLogPrefix, bool teamMessage) { + if (!plr || !text) { + return; + } + + if (plr->tempNameActive) { + plr->Rename(plr->DisplayName(), true, MSG_ONE, plr->edict()); + plr->UpdateTeamInfo(-1, MSG_ONE, plr->edict()); + } + + std::string textTmp = text; // in case VarArgs was used to call this func + std::string prefix = ""; + + if (customPrefix) + prefix = customPrefix; + else if (plr->IsObserver() && (teamMessage)) + prefix = UTIL_VarArgs("(SPEC) %s: ", STRING(plr->pev->netname)); + else if (teamMessage) + prefix = UTIL_VarArgs("(TEAM) %s: ", STRING(plr->pev->netname)); + else + prefix = UTIL_VarArgs("%s: ", STRING(plr->pev->netname)); + + const char* msg = UTIL_VarArgs("%c%s%s\n", 2, prefix.c_str(), textTmp.c_str()); + + // print to the sending client + MESSAGE_BEGIN(MSG_ALL, gmsgSayText); + WRITE_BYTE(plr->entindex()); + WRITE_STRING(msg); + MESSAGE_END(); + + if (plr->tempNameActive) { + plr->Rename(plr->m_tempName, false, MSG_ONE, plr->edict()); + plr->UpdateTeamInfo(plr->m_tempTeam, MSG_ONE, plr->edict()); + } + + // echo to server console for listen servers, dedicated servers should have logs enabled + if (!IS_DEDICATED_SERVER()) + g_engfuncs.pfnServerPrint(text); + + const char* temp; + if (customLogPrefix) + temp = customLogPrefix; + else if (teamMessage) + temp = "say_team"; + else + temp = "say"; + + // team match? + UTIL_LogPlayerEvent(plr->edict(), "%s \"%s\"\n", temp, text); +} + char *UTIL_dtos1( int d ) { static char buf[8]; @@ -2841,7 +2892,7 @@ void DEBUG_MSG(ALERT_TYPE target, const char* format, ...) { OutputDebugString(log_line); #endif - g_engfuncs.pfnAlertMessage(target, log_line); + g_engfuncs.pfnAlertMessage(target, "%s", log_line); if (target == at_error) { if (lastMapName != STRING(gpGlobals->mapname)) { @@ -2863,7 +2914,7 @@ void handleThreadPrints() { for (int failsafe = 0; failsafe < 128; failsafe++) { if (g_thread_prints.dequeue(msg)) { - ALERT(msg.atype, msg.msg.c_str()); + ALERT(msg.atype, "%s", msg.msg.c_str()); } else { break; diff --git a/dlls/util/util.h b/dlls/util/util.h index 535ec88d..7a368044 100644 --- a/dlls/util/util.h +++ b/dlls/util/util.h @@ -427,6 +427,10 @@ EXPORT void UTIL_PrecacheOther( const char *szClassname, std::unordered_map