diff --git a/extension.cpp b/extension.cpp index 98066ac..7717a9d 100644 --- a/extension.cpp +++ b/extension.cpp @@ -55,14 +55,13 @@ A2SFixes g_Interface; SMEXT_LINK(&g_Interface); CDetour *g_pDetour_SendTo = NULL; -CDetour *g_pDetour_RecvFrom = NULL; - -char g_PlayerReply[1024]; -bf_write g_PlayerReplyPacket(g_PlayerReply, 1024); char *NewBuf; char *OldBuf; +char g_PlayerReply[1024]; +bf_write g_PlayerReplyPacket(g_PlayerReply, 1024); + /** * @brief */ @@ -70,11 +69,13 @@ DETOUR_DECL_STATIC6(Detour_SendTo, int, int, s, char *, buf, int, len, int, flag { if (memcmp(buf, A2S_INFO_REPLY, 5) == 0) { -// g_pSM->LogMessage(myself, "A2S_INFO_REPLY: %s", buf); - delete[] NewBuf; NewBuf = new char[len]; + + // Copy memory memcpy(NewBuf, buf, len); + + // Store Pointer OldBuf = NewBuf; // Skip header and protocol @@ -131,14 +132,12 @@ DETOUR_DECL_STATIC6(Detour_SendTo, int, int, s, char *, buf, int, len, int, flag if (memcmp(buf, A2S_PLAYER_REPLY, 5) == 0) { - //g_pSM->LogMessage(myself, "A2S_PLAYER_REPLY: %s", buf); - - g_PlayerReplyPacket.Reset(); //build up the packet as a bitbuffer so we can use the nice helper functions to do the work for us - - g_PlayerReplyPacket.WriteLong(-1);//FF FF FF FF - g_PlayerReplyPacket.WriteByte(68);//44 - - g_PlayerReplyPacket.WriteByte(playerhelpers->GetNumPlayers()); //number of players + g_PlayerReplyPacket.Reset(); // Build up the packet as a bitbuffer so we can use the nice helper functions to do the work for us + + g_PlayerReplyPacket.WriteLong(-1); // FF FF FF FF + g_PlayerReplyPacket.WriteByte(68); // 44 + + g_PlayerReplyPacket.WriteByte(playerhelpers->GetNumPlayers()); // Number of players int iPlayers = 0; @@ -146,59 +145,35 @@ DETOUR_DECL_STATIC6(Detour_SendTo, int, int, s, char *, buf, int, len, int, flag { IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(index); - if (pPlayer != NULL && pPlayer->IsConnected() && pPlayer->IsInGame() && !pPlayer->IsSourceTV()) + if (pPlayer != NULL && pPlayer->IsInGame() && !pPlayer->IsSourceTV()) { - IPlayerInfo *pInfo = pPlayer->GetPlayerInfo(); - INetChannelInfo *pNetInfo = engine->GetPlayerNetInfo(index); - g_PlayerReplyPacket.WriteByte(iPlayers); g_PlayerReplyPacket.WriteString(pPlayer->GetName()); - - if(pInfo != NULL) + + IPlayerInfo *pInfo = pPlayer->GetPlayerInfo(); + + if (pInfo != NULL) g_PlayerReplyPacket.WriteLong(pInfo->GetFragCount()); else g_PlayerReplyPacket.WriteLong(0); - - if(pNetInfo != NULL) + + INetChannelInfo *pNetInfo = engine->GetPlayerNetInfo(index); + + if (pNetInfo != NULL) g_PlayerReplyPacket.WriteFloat(pNetInfo->GetTimeConnected()); else g_PlayerReplyPacket.WriteFloat(0.0); - + iPlayers++; } } - + return DETOUR_STATIC_CALL(Detour_SendTo)(s, (char *)g_PlayerReplyPacket.GetData(), g_PlayerReplyPacket.GetNumBytesWritten(), flags, to, tolen); } return DETOUR_STATIC_CALL(Detour_SendTo)(s, buf, len, flags, to, tolen); } -/** - * @brief - */ -DETOUR_DECL_STATIC6(Detour_RecvFrom, int, int, s, char *, buf, int, len, int, flags, sockaddr *, from, socklen_t*, fromlen) -{ -// if (memcmp(buf, A2S_INFO_REQUEST, 5) == 0) -// { -// g_pSM->LogMessage(myself, "A2S_INFO_REQUEST: %s", buf); -// } - -// if (memcmp(buf, A2S_PLAYER_REQUEST, 5) == 0) -// { -// g_pSM->LogMessage(myself, "A2S_PLAYER_REQUEST: %s", buf); -// } - - return DETOUR_STATIC_CALL(Detour_RecvFrom)(s, buf, len, flags, from, fromlen); -} - -bool A2SFixes::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late) -{ - GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER); - - return true; -} - /** * @brief This is called after the initial loading sequence has been processed. * @@ -220,17 +195,7 @@ bool A2SFixes::SDK_OnLoad(char *error, size_t maxlength, bool late) return false; } - g_pDetour_RecvFrom = DETOUR_CREATE_STATIC_ADDRESS(Detour_RecvFrom, (void*)recvfrom); - - if (g_pDetour_RecvFrom == NULL) - { - snprintf(error, maxlength, "Could not create detour for 'recvfrom'"); - SDK_OnUnload(); - return false; - } - g_pDetour_SendTo->EnableDetour(); - g_pDetour_RecvFrom->EnableDetour(); return true; } @@ -239,15 +204,24 @@ bool A2SFixes::SDK_OnLoad(char *error, size_t maxlength, bool late) */ void A2SFixes::SDK_OnUnload() { - if(g_pDetour_SendTo != NULL) + if (g_pDetour_SendTo != NULL) { g_pDetour_SendTo->Destroy(); g_pDetour_SendTo = NULL; } +} - if(g_pDetour_RecvFrom != NULL) - { - g_pDetour_RecvFrom->Destroy(); - g_pDetour_RecvFrom = NULL; - } +/** + * @brief Called when Metamod is attached, before the extension version is called. + * + * @param error Error buffer. + * @param maxlength Maximum size of error buffer. + * @param late Whether or not Metamod considers this a late load. + * @return True to succeed, false to fail. + */ +bool A2SFixes::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late) +{ + GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER); + + return true; } \ No newline at end of file