Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
3.8 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include "voice_banmgr.h"
  11. #include "filesystem.h"
  12. #include "cdll_client_int.h"
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. #define BANMGR_FILEVERSION 1
  16. const char *g_pBanMgrFilename = "voice_ban.dt";
  17. // Hash a player ID to a byte.
  18. unsigned char HashPlayerID(char const playerID[SIGNED_GUID_LEN])
  19. {
  20. unsigned char curHash = 0;
  21. for(int i=0; i < SIGNED_GUID_LEN; i++)
  22. curHash = (unsigned char)(curHash + playerID[i]);
  23. return curHash;
  24. }
  25. CVoiceBanMgr::CVoiceBanMgr()
  26. {
  27. Clear();
  28. }
  29. CVoiceBanMgr::~CVoiceBanMgr()
  30. {
  31. Term();
  32. }
  33. bool CVoiceBanMgr::Init(const char *pGameDir)
  34. {
  35. Term();
  36. // Load in the squelch file.
  37. FileHandle_t fh = filesystem->Open(g_pBanMgrFilename, "rb");
  38. if (fh)
  39. {
  40. int version;
  41. filesystem->Read(&version, sizeof(version), fh);
  42. if(version == BANMGR_FILEVERSION)
  43. {
  44. filesystem->Seek(fh, 0, FILESYSTEM_SEEK_TAIL);
  45. int nIDs = (filesystem->Tell(fh) - sizeof(version)) / SIGNED_GUID_LEN;
  46. filesystem->Seek(fh, sizeof(version), FILESYSTEM_SEEK_CURRENT);
  47. for(int i=0; i < nIDs; i++)
  48. {
  49. char playerID[SIGNED_GUID_LEN];
  50. filesystem->Read(playerID, SIGNED_GUID_LEN, fh);
  51. AddBannedPlayer(playerID);
  52. }
  53. }
  54. filesystem->Close(fh);
  55. }
  56. return true;
  57. }
  58. void CVoiceBanMgr::Term()
  59. {
  60. // Free all the player structures.
  61. for(int i=0; i < 256; i++)
  62. {
  63. BannedPlayer *pListHead = &m_PlayerHash[i];
  64. BannedPlayer *pNext;
  65. for(BannedPlayer *pCur=pListHead->m_pNext; pCur != pListHead; pCur=pNext)
  66. {
  67. pNext = pCur->m_pNext;
  68. delete pCur;
  69. }
  70. }
  71. Clear();
  72. }
  73. void CVoiceBanMgr::SaveState(const char *pGameDir)
  74. {
  75. // Save the file out.
  76. FileHandle_t fh = filesystem->Open(g_pBanMgrFilename, "wb");
  77. if(fh)
  78. {
  79. int version = BANMGR_FILEVERSION;
  80. filesystem->Write(&version, sizeof(version), fh);
  81. for(int i=0; i < 256; i++)
  82. {
  83. BannedPlayer *pListHead = &m_PlayerHash[i];
  84. for(BannedPlayer *pCur=pListHead->m_pNext; pCur != pListHead; pCur=pCur->m_pNext)
  85. {
  86. filesystem->Write(pCur->m_PlayerID, SIGNED_GUID_LEN, fh);
  87. }
  88. }
  89. filesystem->Close(fh);
  90. }
  91. }
  92. bool CVoiceBanMgr::GetPlayerBan(char const playerID[SIGNED_GUID_LEN])
  93. {
  94. return !!InternalFindPlayerSquelch(playerID);
  95. }
  96. void CVoiceBanMgr::SetPlayerBan(char const playerID[SIGNED_GUID_LEN], bool bSquelch)
  97. {
  98. if(bSquelch)
  99. {
  100. // Is this guy already squelched?
  101. if(GetPlayerBan(playerID))
  102. return;
  103. AddBannedPlayer(playerID);
  104. }
  105. else
  106. {
  107. BannedPlayer *pPlayer = InternalFindPlayerSquelch(playerID);
  108. if(pPlayer)
  109. {
  110. pPlayer->m_pPrev->m_pNext = pPlayer->m_pNext;
  111. pPlayer->m_pNext->m_pPrev = pPlayer->m_pPrev;
  112. delete pPlayer;
  113. }
  114. }
  115. }
  116. void CVoiceBanMgr::Clear()
  117. {
  118. // Tie off the hash table entries.
  119. for(int i=0; i < 256; i++)
  120. m_PlayerHash[i].m_pNext = m_PlayerHash[i].m_pPrev = &m_PlayerHash[i];
  121. // TODO memory leaks ???
  122. }
  123. CVoiceBanMgr::BannedPlayer* CVoiceBanMgr::InternalFindPlayerSquelch(char const playerID[SIGNED_GUID_LEN])
  124. {
  125. int index = HashPlayerID(playerID);
  126. BannedPlayer *pListHead = &m_PlayerHash[index];
  127. for(BannedPlayer *pCur=pListHead->m_pNext; pCur != pListHead; pCur=pCur->m_pNext)
  128. {
  129. if(memcmp(playerID, pCur->m_PlayerID, SIGNED_GUID_LEN) == 0)
  130. return pCur;
  131. }
  132. return NULL;
  133. }
  134. CVoiceBanMgr::BannedPlayer* CVoiceBanMgr::AddBannedPlayer(char const playerID[SIGNED_GUID_LEN])
  135. {
  136. BannedPlayer *pNew = new BannedPlayer;
  137. if(!pNew)
  138. return NULL;
  139. int index = HashPlayerID(playerID);
  140. memcpy(pNew->m_PlayerID, playerID, SIGNED_GUID_LEN);
  141. pNew->m_pNext = &m_PlayerHash[index];
  142. pNew->m_pPrev = m_PlayerHash[index].m_pPrev;
  143. pNew->m_pPrev->m_pNext = pNew->m_pNext->m_pPrev = pNew;
  144. return pNew;
  145. }