Skip to content

Commit

Permalink
add chat util, update hook, fixes
Browse files Browse the repository at this point in the history
- fix console prints being tripled formatted
- fix random error code for plugin load failures
  • Loading branch information
wootguy committed Dec 17, 2024
1 parent 3ef13aa commit 5ed5ef9
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 93 deletions.
2 changes: 1 addition & 1 deletion dlls/hooks/PluginHooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions dlls/hooks/PluginManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <dlfcn.h>
#define PLUGIN_EXT ".so"
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down
89 changes: 2 additions & 87 deletions dlls/hooks/client_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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) \
Expand Down
55 changes: 53 additions & 2 deletions dlls/util/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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)) {
Expand All @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions dlls/util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,10 @@ EXPORT void UTIL_PrecacheOther( const char *szClassname, std::unordered_map<st
EXPORT void UTIL_ClientPrint(CBaseEntity* client, PRINT_TYPE print_type, const char* msg);
EXPORT void UTIL_ClientPrintAll(PRINT_TYPE print_type, const char *msg);

// handles coloring and logs to the server console and file
// don't include the newline
EXPORT void UTIL_ClientSay(CBasePlayer* plr, const char* text, const char* customPrefix=NULL, const char* customLogPrefix=NULL, bool teamMessage=false);

class CBasePlayerItem;
class CBasePlayer;
EXPORT BOOL UTIL_GetNextBestWeapon( CBasePlayer *pPlayer, CBasePlayerItem *pCurrentWeapon );
Expand Down

0 comments on commit 5ed5ef9

Please sign in to comment.