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.

231 lines
6.3 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef PURE_SERVER_H
  8. #define PURE_SERVER_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "ifilelist.h"
  13. #include "tier1/utldict.h"
  14. #include "tier1/utlbuffer.h"
  15. #include "filesystem.h"
  16. class KeyValues;
  17. class CPureServerWhitelist
  18. {
  19. public:
  20. friend class CAllowFromDiskList;
  21. friend class CForceMatchList;
  22. friend class CPureFileTracker;
  23. // Call this to free it.
  24. static CPureServerWhitelist* Create( IFileSystem *pFileSystem );
  25. void Release();
  26. // Get the file lists.
  27. IFileList* GetAllowFromDiskList(); // This is the list of files that can be loaded off disk. Make sure to Release() it when finished!
  28. IFileList* GetForceMatchList(); // This is the list of files that must match CRCs. Make sure to Release() it when finished!
  29. // Load up any entries in the KV file.
  30. bool LoadFromKeyValues( KeyValues *kv );
  31. // Encode for networking.
  32. void Encode( CUtlBuffer &buf );
  33. void Decode( CUtlBuffer &buf );
  34. // Calls IFileSystem::CacheFileCRCs for all files and directories marked check_crc.
  35. void CacheFileCRCs();
  36. // Instead of loading keyvalues, you can call this to make it force all files to come from Steam.
  37. // This is used with sv_pure 2.
  38. void EnableFullyPureMode();
  39. bool IsInFullyPureMode() const;
  40. // Used by sv_pure command when you're connected to a server.
  41. void PrintWhitelistContents();
  42. private:
  43. CPureServerWhitelist();
  44. ~CPureServerWhitelist();
  45. void Init( IFileSystem *pFileSystem );
  46. void Term();
  47. private:
  48. class CAllowFromDiskList : public IFileList
  49. {
  50. public:
  51. virtual bool IsFileInList( const char *pFilename );
  52. virtual void Release();
  53. CPureServerWhitelist *m_pWhitelist;
  54. };
  55. class CForceMatchList : public IFileList
  56. {
  57. public:
  58. virtual bool IsFileInList( const char *pFilename );
  59. virtual void Release();
  60. CPureServerWhitelist *m_pWhitelist;
  61. };
  62. class CCommand
  63. {
  64. public:
  65. CCommand();
  66. ~CCommand();
  67. public:
  68. bool m_bAllowFromDisk;
  69. bool m_bCheckCRC;
  70. unsigned short m_LoadOrder; // What order this thing was specified in the whitelist file.
  71. };
  72. private:
  73. void UpdateCommandStats( CUtlDict<CPureServerWhitelist::CCommand*,int> &commands, int *pHighest, int *pLongestPathName );
  74. void PrintCommand( const char *pFileSpec, const char *pExt, int maxPathnameLen, CPureServerWhitelist::CCommand *pCommand );
  75. int FindCommandByLoadOrder( CUtlDict<CPureServerWhitelist::CCommand*,int> &commands, int iLoadOrder );
  76. void InternalCacheFileCRCs( CUtlDict<CCommand*,int> &theList, ECacheCRCType eType );
  77. CCommand* GetBestEntry( const char *pFilename );
  78. void EncodeCommandList( CUtlDict<CPureServerWhitelist::CCommand*,int> &theList, CUtlBuffer &buf );
  79. void DecodeCommandList( CUtlDict<CPureServerWhitelist::CCommand*,int> &theList, CUtlBuffer &buf );
  80. CPureServerWhitelist::CCommand* CheckEntry(
  81. CUtlDict<CPureServerWhitelist::CCommand*,int> &dict,
  82. const char *pEntryName,
  83. CPureServerWhitelist::CCommand *pBestEntry );
  84. private:
  85. unsigned short m_LoadCounter; // Incremented as we load things so their m_LoadOrder increases.
  86. int m_RefCount;
  87. // Commands are applied to files in order.
  88. CUtlDict<CCommand*,int> m_FileCommands; // file commands
  89. CUtlDict<CCommand*,int> m_RecursiveDirCommands; // ... commands
  90. CUtlDict<CCommand*,int> m_NonRecursiveDirCommands; // *.* commands
  91. IFileSystem *m_pFileSystem;
  92. CAllowFromDiskList m_AllowFromDiskList;
  93. CForceMatchList m_ForceMatchList;
  94. // In this mode, all files must come from Steam.
  95. bool m_bFullyPureMode;
  96. };
  97. CPureServerWhitelist* CreatePureServerWhitelist( IFileSystem *pFileSystem );
  98. struct UserReportedFileHash_t
  99. {
  100. int m_idxFile;
  101. USERID_t m_userID;
  102. FileHash_t m_FileHash;
  103. static bool Less( const UserReportedFileHash_t& lhs, const UserReportedFileHash_t& rhs )
  104. {
  105. if ( lhs.m_idxFile < rhs.m_idxFile )
  106. return true;
  107. if ( lhs.m_idxFile > rhs.m_idxFile )
  108. return false;
  109. if ( lhs.m_userID.uid.steamid.m_SteamLocalUserID.As64bits < rhs.m_userID.uid.steamid.m_SteamLocalUserID.As64bits )
  110. return true;
  111. return false;
  112. }
  113. };
  114. struct UserReportedFile_t
  115. {
  116. CRC32_t m_crcIdentifier;
  117. CUtlString m_filename;
  118. CUtlString m_path;
  119. int m_nFileFraction;
  120. static bool Less( const UserReportedFile_t& lhs, const UserReportedFile_t& rhs )
  121. {
  122. if ( lhs.m_crcIdentifier < rhs.m_crcIdentifier )
  123. return true;
  124. if ( lhs.m_crcIdentifier > rhs.m_crcIdentifier )
  125. return false;
  126. if ( lhs.m_nFileFraction < rhs.m_nFileFraction )
  127. return true;
  128. if ( lhs.m_nFileFraction > rhs.m_nFileFraction )
  129. return false;
  130. int nCmp = Q_strcmp( lhs.m_filename.String(), rhs.m_filename.String() );
  131. if ( nCmp < 0 )
  132. return true;
  133. if ( nCmp > 0 )
  134. return false;
  135. nCmp = Q_strcmp( lhs.m_path.String(), rhs.m_path.String() );
  136. if ( nCmp < 0 )
  137. return true;
  138. return false;
  139. }
  140. };
  141. struct MasterFileHash_t
  142. {
  143. int m_idxFile;
  144. int m_cMatches;
  145. FileHash_t m_FileHash;
  146. static bool Less( const MasterFileHash_t& lhs, const MasterFileHash_t& rhs )
  147. {
  148. return lhs.m_idxFile < rhs.m_idxFile;
  149. }
  150. };
  151. class CPureFileTracker
  152. {
  153. public:
  154. CPureFileTracker():
  155. m_treeAllReportedFiles( UserReportedFile_t::Less ),
  156. m_treeMasterFileHashes( MasterFileHash_t::Less ),
  157. m_treeUserReportedFileHash( UserReportedFileHash_t::Less )
  158. {
  159. m_flLastFileReceivedTime = 0.f;
  160. m_cMatchedFile = 0;
  161. m_cMatchedMasterFile = 0;
  162. m_cMatchedMasterFileHash = 0;
  163. m_cMatchedFileFullHash = 0;
  164. }
  165. void AddUserReportedFileHash( int idxFile, FileHash_t *pFileHash, USERID_t userID, bool bAddMasterRecord );
  166. bool DoesFileMatch( const char *pPathID, const char *pRelativeFilename, int nFileFraction, FileHash_t *pFileHash, USERID_t userID );
  167. int ListUserFiles( bool bListAll, const char *pchFilenameFind );
  168. int ListAllTrackedFiles( bool bListAll, const char *pchFilenameFind, int nFileFractionMin, int nFileFractionMax );
  169. CUtlRBTree< UserReportedFile_t, int > m_treeAllReportedFiles;
  170. CUtlRBTree< MasterFileHash_t, int > m_treeMasterFileHashes;
  171. CUtlRBTree< UserReportedFileHash_t, int > m_treeUserReportedFileHash;
  172. float m_flLastFileReceivedTime;
  173. int m_cMatchedFile;
  174. int m_cMatchedMasterFile;
  175. int m_cMatchedMasterFileHash;
  176. int m_cMatchedFileFullHash;
  177. };
  178. extern CPureFileTracker g_PureFileTracker;
  179. #endif // PURE_SERVER_H