Team Fortress 2 Source Code as on 22/4/2020
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.

232 lines
7.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef FILETRACKER_H
  7. #define FILETRACKER_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. class CBaseFileSystem;
  12. class CPackedStoreFileHandle;
  13. #if !defined( DEDICATED )
  14. // Comments from Fletcher:
  15. //
  16. // TF isn’t sending any hashes to the server. (That’s probably what CSGO is doing,
  17. // but not TF.) Comparing hashes doesn’t work when there are optional updates. (We
  18. // release a new client without updating the server.) Also on TF, we don’t ship
  19. // any textures or audio to the dedicated server.
  20. //
  21. // On TF, the client just confirms that the files were loaded from a trusted source.
  22. //
  23. // When a client connects to a “pure” server, the client is supposed to limit
  24. // which modified files are allowed.)
  25. #include "ifilelist.h"
  26. #include "tier1/utldict.h"
  27. #include "tier0/tslist.h"
  28. #include "tier1/stringpool.h"
  29. struct TrackedFile_t
  30. {
  31. TrackedFile_t()
  32. {
  33. m_nFileFraction = 0;
  34. m_PackFileID = 0;
  35. m_nPackFileNumber = 0;
  36. m_bPackOrVPKFile = false;
  37. m_bFileInVPK = false;
  38. m_iLoadedSearchPathStoreId = 0;
  39. m_bIgnoredForPureServer = false;
  40. }
  41. FileHash_t m_filehashFinal;
  42. const char *m_filename;
  43. const char *m_path;
  44. int m_nFileFraction;
  45. int m_iLoadedSearchPathStoreId; // ID of the search path that we loaded from. Zero if we are not currently loaded.
  46. int m_PackFileID;
  47. int m_nPackFileNumber;
  48. bool m_bPackOrVPKFile;
  49. bool m_bFileInVPK;
  50. bool m_bIgnoredForPureServer; // Did we ignore a file by this name as a result of pure server rules, the last time it was opened?
  51. // The crcIdentifier is a CRC32 of the filename and path. It could be used for quick comparisons of
  52. // path+filename, but since we don't do that at the moment we don't need this.
  53. // CRC32_t m_crcIdentifier;
  54. void RebuildFileName( CStringPool &stringPool, const char *pFilename, const char *pPathID, int nFileFraction );
  55. static bool Less( const TrackedFile_t& lhs, const TrackedFile_t& rhs )
  56. {
  57. int nCmp = Q_strcmp( lhs.m_path, rhs.m_path );
  58. if ( nCmp < 0 )
  59. return true;
  60. else if ( nCmp > 0 )
  61. return false;
  62. nCmp = Q_strcmp( lhs.m_filename, rhs.m_filename );
  63. if ( nCmp < 0 )
  64. return true;
  65. return false;
  66. }
  67. };
  68. struct TrackedVPKFile_t
  69. {
  70. TrackedVPKFile_t()
  71. {
  72. m_PackFileID = 0;
  73. m_nPackFileNumber = 0;
  74. m_nFileFraction = 0;
  75. }
  76. int m_PackFileID;
  77. int m_nPackFileNumber;
  78. int m_nFileFraction;
  79. int m_idxAllOpenedFiles; // Index into m_treeAllOpenedFiles
  80. static bool Less( const TrackedVPKFile_t& lhs, const TrackedVPKFile_t& rhs )
  81. {
  82. if ( lhs.m_nPackFileNumber < rhs.m_nPackFileNumber )
  83. return true;
  84. else if ( lhs.m_nPackFileNumber > rhs.m_nPackFileNumber )
  85. return false;
  86. if ( lhs.m_nFileFraction < rhs.m_nFileFraction )
  87. return true;
  88. else if ( lhs.m_nFileFraction > rhs.m_nFileFraction )
  89. return false;
  90. if ( lhs.m_PackFileID < rhs.m_PackFileID )
  91. return true;
  92. return false;
  93. }
  94. };
  95. class StuffToMD5_t
  96. {
  97. public:
  98. uint8 *m_pubBuffer;
  99. int m_cubBuffer;
  100. MD5Value_t m_md5Value;
  101. int m_idxTrackedVPKFile;
  102. int m_idxListSubmittedJobs;
  103. };
  104. class SubmittedMd5Job_t
  105. {
  106. public:
  107. bool m_bFinished;
  108. MD5Value_t m_md5Value;
  109. };
  110. class CFileTracker2
  111. #ifdef SUPPORT_PACKED_STORE
  112. : IThreadedFileMD5Processor
  113. #endif
  114. {
  115. public:
  116. CFileTracker2( CBaseFileSystem *pFileSystem );
  117. ~CFileTracker2();
  118. void ShutdownAsync();
  119. void MarkAllCRCsUnverified();
  120. int GetUnverifiedFileHashes( CUnverifiedFileHash *pFiles, int nMaxFiles );
  121. EFileCRCStatus CheckCachedFileHash( const char *pPathID, const char *pRelativeFilename, int nFileFraction, FileHash_t *pFileHash );
  122. #ifdef SUPPORT_PACKED_STORE
  123. unsigned ThreadedProcessMD5Requests();
  124. virtual int SubmitThreadedMD5Request( uint8 *pubBuffer, int cubBuffer, int PackFileID, int nPackFileNumber, int nPackFileFraction );
  125. virtual bool BlockUntilMD5RequestComplete( int iRequest, MD5Value_t *pMd5ValueOut );
  126. virtual bool IsMD5RequestComplete( int iRequest, MD5Value_t *pMd5ValueOut );
  127. int NotePackFileOpened( const char *pVPKAbsPath, const char *pPathID, int64 nLength );
  128. void NotePackFileAccess( const char *pFilename, const char *pPathID, int iSearchPathStoreId, CPackedStoreFileHandle &VPKHandle );
  129. void AddFileHashForVPKFile( int nPackFileNumber, int nFileFraction, int cbFileLen, MD5Value_t &md5, CPackedStoreFileHandle &fhandle );
  130. #endif
  131. void NoteFileIgnoredForPureServer( const char *pFilename, const char *pPathID, int iSearchPathStoreId );
  132. void NoteFileLoadedFromDisk( const char *pFilename, const char *pPathID, int iSearchPathStoreId, FILE *fp, int64 nLength );
  133. void NoteFileUnloaded( const char *pFilename, const char *pPathID );
  134. int ListOpenedFiles( bool bAllOpened, const char *pchFilenameFind );
  135. IFileList *GetFilesToUnloadForWhitelistChange( IPureServerWhitelist *pNewWhiteList );
  136. private:
  137. int IdxFileFromName( const char *pFilename, const char *pPathID, int nFileFraction, bool bPackOrVPKFile );
  138. CStringPool m_stringPool;
  139. CUtlRBTree< TrackedFile_t, int > m_treeAllOpenedFiles;
  140. CUtlRBTree< TrackedVPKFile_t, int > m_treeTrackedVPKFiles;
  141. CBaseFileSystem *m_pFileSystem;
  142. CThreadMutex m_Mutex; // Threads call into here, so we need to be safe.
  143. #ifdef SUPPORT_PACKED_STORE
  144. CThreadEvent m_threadEventWorkToDo;
  145. CThreadEvent m_threadEventWorkCompleted;
  146. volatile bool m_bThreadShouldRun;
  147. ThreadHandle_t m_hWorkThread;
  148. CTSQueue< StuffToMD5_t > m_PendingJobs;
  149. CTSQueue< StuffToMD5_t > m_CompletedJobs;
  150. CUtlLinkedList< SubmittedMd5Job_t > m_SubmittedJobs;
  151. #endif // SUPPORT_PACKED_STORE
  152. // Stats
  153. int m_cThreadBlocks;
  154. int m_cDupMD5s;
  155. };
  156. #else
  157. //
  158. // Dedicated server NULL filetracker. Pretty much does nothing.
  159. //
  160. class CFileTracker2
  161. #ifdef SUPPORT_PACKED_STORE
  162. : IThreadedFileMD5Processor
  163. #endif
  164. {
  165. public:
  166. CFileTracker2( CBaseFileSystem *pFileSystem ) {}
  167. ~CFileTracker2() {}
  168. void ShutdownAsync() {}
  169. void MarkAllCRCsUnverified() {}
  170. int GetUnverifiedFileHashes( CUnverifiedFileHash *pFiles, int nMaxFiles ) { return 0; }
  171. EFileCRCStatus CheckCachedFileHash( const char *pPathID, const char *pRelativeFilename, int nFileFraction, FileHash_t *pFileHash )
  172. { return k_eFileCRCStatus_CantOpenFile; }
  173. #ifdef SUPPORT_PACKED_STORE
  174. virtual int SubmitThreadedMD5Request( uint8 *pubBuffer, int cubBuffer, int PackFileID, int nPackFileNumber, int nPackFileFraction )
  175. { return 0; }
  176. virtual bool BlockUntilMD5RequestComplete( int iRequest, MD5Value_t *pMd5ValueOut ) { Assert(0); return true; }
  177. virtual bool IsMD5RequestComplete( int iRequest, MD5Value_t *pMd5ValueOut ) { Assert(0); return true; }
  178. int NotePackFileOpened( const char *pVPKAbsPath, const char *pPathID, int64 nLength ) { return 0; }
  179. void NotePackFileAccess( const char *pFilename, const char *pPathID, int iSearchPathStoreId, CPackedStoreFileHandle &VPKHandle ) {}
  180. void AddFileHashForVPKFile( int nPackFileNumber, int nFileFraction, int cbFileLen, MD5Value_t &md5, CPackedStoreFileHandle &fhandle ) {}
  181. #endif
  182. void NoteFileIgnoredForPureServer( const char *pFilename, const char *pPathID, int iSearchPathStoreId ) {}
  183. void NoteFileLoadedFromDisk( const char *pFilename, const char *pPathID, int iSearchPathStoreId, FILE *fp, int64 nLength ) {}
  184. void NoteFileUnloaded( const char *pFilename, const char *pPathID ) {}
  185. IFileList *GetFilesToUnloadForWhitelistChange( IPureServerWhitelist *pNewWhiteList ) { return NULL; }
  186. };
  187. #endif // DEDICATED
  188. #endif // FILETRACKER_H