Skip to content

Commit

Permalink
Added ClientCommand, ClientSay Events to Log API
Browse files Browse the repository at this point in the history
  • Loading branch information
SmileYzn committed Aug 17, 2023
1 parent e459d3a commit f983cca
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 7 deletions.
5 changes: 4 additions & 1 deletion LogApi/LogApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CLogApi gLogApi;
void CLogApi::ServerActivate()
{
// Enable Log API (0 Disable, 1 Enable)
this->m_log_api_on = gLogUtil.CvarRegister("log_api_enable", "1");
this->m_log_api_on = gLogUtil.CvarRegister("log_api_enable", "0");

// Set Log API Address (API HTTP/s Address Ie. https://api.yoursite.com/)
this->m_log_api_address = gLogUtil.CvarRegister("log_api_address", "");
Expand All @@ -17,6 +17,9 @@ void CLogApi::ServerActivate()
// Set Log API Bearer Token (Authentication Token or leave empty to disable)
this->m_log_api_bearer = gLogUtil.CvarRegister("log_api_bearer", "");

// Execute Settings File
g_engfuncs.pfnServerCommand("exec addons/logapi/logapi.cfg\n");

// Clear events
this->m_Events.clear();

Expand Down
94 changes: 89 additions & 5 deletions LogApi/LogEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void CLogEvent::ClientConnect(edict_t* pEntity, const char* pszName, const char*
{
this->m_Event.clear();

if (pEntity)
if (!FNullEnt(pEntity))
{
this->m_Event["Event"] = __func__;

Expand All @@ -78,7 +78,7 @@ void CLogEvent::ClientPutInServer(edict_t* pEntity)
{
this->m_Event.clear();

if (pEntity)
if (!FNullEnt(pEntity))
{
this->m_Event["Event"] = __func__;

Expand All @@ -99,7 +99,7 @@ void CLogEvent::ClientDisconnect(edict_t* pEntity)
{
this->m_Event.clear();

if (pEntity)
if (!FNullEnt(pEntity))
{
this->m_Event["Event"] = __func__;

Expand All @@ -120,7 +120,7 @@ void CLogEvent::ClientKill(edict_t* pEntity)
{
this->m_Event.clear();

if (pEntity)
if (!FNullEnt(pEntity))
{
this->m_Event["Event"] = __func__;

Expand All @@ -141,7 +141,7 @@ void CLogEvent::ClientUserInfoChanged(edict_t* pEntity, char* InfoBuffer)
{
this->m_Event.clear();

if (pEntity)
if (!FNullEnt(pEntity))
{
this->m_Event["Event"] = __func__;

Expand All @@ -157,3 +157,87 @@ void CLogEvent::ClientUserInfoChanged(edict_t* pEntity, char* InfoBuffer)
gLogApi.SendEvent(LogApi::Events::ClientUserInfoChanged, this->m_Event);
}
}

void CLogEvent::ClientCommand(edict_t* pEntity)
{
if (gLogApi.EventEnabled(__func__))
{
this->m_Event.clear();

if (!FNullEnt(pEntity))
{
auto Command = g_engfuncs.pfnCmd_Argv(0);

if (Command)
{
if (Command[0u] != '\0')
{
this->m_Event["Event"] = __func__;

this->m_Event["UserId"] = g_engfuncs.pfnGetPlayerUserId(pEntity);

this->m_Event["Name"] = STRING(pEntity->v.netname);

this->m_Event["AuthId"] = g_engfuncs.pfnGetPlayerAuthId(pEntity);

this->m_Event["Command"] = Command;

this->m_Event["Args"] = "";

auto Args = g_engfuncs.pfnCmd_Args();

if (Args)
{
if (Args[0u] != '\0')
{
this->m_Event["Args"] = Args;
}
}
}
}
}

gLogApi.SendEvent(LogApi::Events::ClientCommand, this->m_Event);
}
}

void CLogEvent::ClientSay(edict_t* pEntity)
{
if (gLogApi.EventEnabled(__func__))
{
this->m_Event.clear();

if (!FNullEnt(pEntity))
{
auto Type = g_engfuncs.pfnCmd_Argv(0);

if (Type)
{
if (Type[0u] != '\0')
{
auto Message = g_engfuncs.pfnCmd_Args();

if (Message[0u] != '\0')
{
if (!Q_stricmp(Type, "say") || !Q_stricmp(Type, "say_team"))
{
this->m_Event["Event"] = __func__;

this->m_Event["UserId"] = g_engfuncs.pfnGetPlayerUserId(pEntity);

this->m_Event["Name"] = STRING(pEntity->v.netname);

this->m_Event["AuthId"] = g_engfuncs.pfnGetPlayerAuthId(pEntity);

this->m_Event["Type"] = Type;

this->m_Event["Message"] = Message;
}
}
}
}
}

gLogApi.SendEvent(LogApi::Events::ClientSay, this->m_Event);
}
}
4 changes: 4 additions & 0 deletions LogApi/LogEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace LogApi
ClientDisconnect,
ClientKill,
ClientUserInfoChanged,
ClientCommand,
ClientSay,
};
}

Expand All @@ -26,6 +28,8 @@ class CLogEvent
void ClientDisconnect(edict_t* pEntity);
void ClientKill(edict_t* pEntity);
void ClientUserInfoChanged(edict_t* pEntity, char* InfoBuffer);
void ClientCommand(edict_t* pEntity);
void ClientSay(edict_t* pEntity);
private:
// Current event data
nlohmann::ordered_json m_Event;
Expand Down
44 changes: 44 additions & 0 deletions LogApi/LogUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,47 @@ void CLogUtil::ServerExecute(std::string CommandData)
g_engfuncs.pfnServerCommand(ExecuteCommand);
}
}

void CLogUtil::ClientPrint(edict_t* pEntity, int msg_dest, const char* Format, ...)
{
va_list argList;

va_start(argList, Format);

char Buffer[188] = { 0 };

int Length = vsnprintf(Buffer, sizeof(Buffer), Format, argList);

va_end(argList);

if (msg_dest == PRINT_CONSOLE)
{
if (Length > 125)
{
Length = 125;
}

Buffer[Length++] = '\n';
Buffer[Length++] = '\n';
Buffer[Length] = 0;
}

static int iMsgTextMsg;

if (iMsgTextMsg || (iMsgTextMsg = gpMetaUtilFuncs->pfnGetUserMsgID(PLID, "TextMsg", NULL)))
{
if (pEntity)
{
g_engfuncs.pfnMessageBegin(MSG_ONE, iMsgTextMsg, NULL, pEntity);
}
else
{
g_engfuncs.pfnMessageBegin(MSG_BROADCAST, iMsgTextMsg, NULL, NULL);
}

g_engfuncs.pfnWriteByte(msg_dest);
g_engfuncs.pfnWriteString("%s");
g_engfuncs.pfnWriteString(Buffer);
g_engfuncs.pfnMessageEnd();
}
}
8 changes: 8 additions & 0 deletions LogApi/LogUtil.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
#pragma once

// Client Print
constexpr auto PRINT_NOTIFY = 1;
constexpr auto PRINT_CONSOLE = 2;
constexpr auto PRINT_CHAT = 3;
constexpr auto PRINT_CENTER = 4;
constexpr auto PRINT_RADIO = 5;

class CLogUtil
{
public:
cvar_t* CvarRegister(const char* Name, const char* Value);
void ServerExecute(std::string Command);
void ClientPrint(edict_t* pEntity, int msg_dest, const char* Format, ...);

private:
std::map<std::string, cvar_t> m_Cvar;
Expand Down
11 changes: 11 additions & 0 deletions LogApi/MetaDLL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ C_DLLEXPORT int GetEntityAPI2_Post(DLL_FUNCTIONS* pFunctionTable, int* interface

gDLL_FunctionTable_Post.pfnClientUserInfoChanged = DLL_POST_ClientUserInfoChanged;

gDLL_FunctionTable_Post.pfnClientCommand = DLL_POST_ClientCommand;

memcpy(pFunctionTable, &gDLL_FunctionTable_Post, sizeof(DLL_FUNCTIONS));

return 1;
Expand Down Expand Up @@ -104,4 +106,13 @@ void DLL_POST_ClientUserInfoChanged(edict_t* pEntity, char* InfoBuffer)

RETURN_META(MRES_IGNORED);
}

void DLL_POST_ClientCommand(edict_t* pEntity)
{
gLogEvent.ClientCommand(pEntity);

gLogEvent.ClientSay(pEntity);

RETURN_META(MRES_IGNORED);
}
#pragma endregion
1 change: 1 addition & 0 deletions LogApi/MetaDLL.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ void DLL_POST_ClientPutInServer(edict_t* pEntity);
void DLL_POST_ClientDisconnect(edict_t* pEntity);
void DLL_POST_ClientKill(edict_t* pEntity);
void DLL_POST_ClientUserInfoChanged(edict_t* pEntity, char* InfoBuffer);
void DLL_POST_ClientCommand(edict_t* pEntity);
#pragma endregion
4 changes: 3 additions & 1 deletion cstrike/addons/logapi/events.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
"ClientPutInServer": false,
"ClientDisconnect": false,
"ClientKill": false,
"ClientUserInfoChanged": false
"ClientUserInfoChanged": false,
"ClientCommand": false,
"ClientSay": false
}

0 comments on commit f983cca

Please sign in to comment.