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.

161 lines
6.0 KiB

  1. //====== Copyright � 1996-2005, 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. #include "ifilelist.h"
  12. class CBaseFileSystem;
  13. class CFileHandle;
  14. enum EFileFlags
  15. {
  16. k_eFileFlagsLoadedFromSteam = 0x0001,
  17. k_eFileFlagsHasCRC = 0x0002, // m_CRC represents the most recently-loaded version of the file. This might be
  18. // unset but m_CRC (and k_eFileFlagsGotCRCOnce) could be set, signifying that
  19. // the file has been opened since last time we calculated the CRC but we didn't
  20. // calculate a CRC for that file version.
  21. k_eFileFlagsForcedLoadFromSteam = 0x0004, // Set if k_eFileFlagsLoadedFromSteam is set AND we forced Steam to not check the disk for this file.
  22. k_eFileFlagsGotCRCOnce = 0x0008, // This is set if we EVER had k_eFileFlagsHasCRC set.. m_CRC will still be set in that case,
  23. // but it'll be the last CRC we calculated and not necessarily
  24. k_eFileFlagsFailedToLoadLastTime = 0x0010 // This is used if we had a record of the file and the game tried to open it, but
  25. // it couldn't be opened. This will happen if the file was loaded from disk, then
  26. // sv_pure is turned on and the file is forced to come from Steam, but there is no
  27. // Steam version. In that case, the game should be told to retry the file
  28. // next time sv_pure is changed because if sv_pure goes back to 0 it -would- load
  29. // the file legitimately.
  30. };
  31. class CPathIDFileList;
  32. class CFileInfo
  33. {
  34. public:
  35. CFileInfo( const char *pFilename );
  36. ~CFileInfo();
  37. const char* GetFilename();
  38. const char* GetPathIDString();
  39. public:
  40. //TODO: Optimize this. Probably should use a hierarchical structure with a list of files in each directory.
  41. char *m_pFilename;
  42. unsigned short m_Flags; // This is a combination of EFileFlags.
  43. CRC32_t m_CRC; // The CRC for this file.
  44. CPathIDFileList *m_pPathIDFileList;
  45. int m_iNeedsVerificationListIndex; // Index into m_NeedsVerificationList or -1 if not in the list.
  46. };
  47. // This tracks a list of files for the specified path ID.
  48. class CPathIDFileList
  49. {
  50. public:
  51. ~CPathIDFileList();
  52. CFileInfo* FindFileInfo( const char *pFilename );
  53. CFileInfo* AddFileInfo( const char *pFilename );
  54. public:
  55. CUtlSymbol m_PathID; // "" for a null path ID.
  56. CUtlLinkedList<CFileInfo*,int> m_Files;
  57. CUtlLinkedList<CFileInfo*,int> m_UnverifiedCRCFiles; // These are the files whose CRCs have not been verified yet.
  58. // These just point at entries in m_Files.
  59. };
  60. //-----------------------------------------------------------------------------
  61. // This tracks the files that have been opened by the filesystem.
  62. // It remembers if they were loaded from Steam or off-disk.
  63. // If the filesystem is tracking CRCs, then it will calculate a CRC
  64. // for each file that came off disk.
  65. //
  66. // TODO: This is similar to CBaseFileSystem::m_OpenedFiles - it could probably
  67. // manage both sets of files in the same list. Having 2 separate lists might
  68. // be confusing.
  69. //-----------------------------------------------------------------------------
  70. class CFileTracker
  71. {
  72. public:
  73. CFileTracker( CBaseFileSystem *pFileSystem );
  74. ~CFileTracker();
  75. // If this is true, then we'll calculate CRCs for each file that came off disk.
  76. void SetWantFileCRCs( bool bWantCRCs );
  77. // As files are opened, if it is calculating CRCs, it will add those files and their
  78. // CRCs to the "unverified CRC" list. The client can then ask the server to verify
  79. // those CRCs to make sure the client is "pure".
  80. void MarkAllCRCsUnverified();
  81. void MarkAllCRCsVerified( bool bLockMutex=true );
  82. // Cache a file's CRC. Loads the file and calculates the CRC if we don't have it yet.
  83. void CacheFileCRC( const char *pPathID, const char *pRelativeFilename );
  84. EFileCRCStatus CheckCachedFileCRC( const char *pPathID, const char *pRelativeFilename, CRC32_t *pCRC );
  85. // This is like CacheFileCRC, but it assumes that the same file would be found by pPathIDToCopyFrom, so it just
  86. // copies the CRC record from that path ID into the one in pPathID and avoids a redundant CRC calculation.
  87. void CacheFileCRC_Copy( const char *pPathID, const char *pRelativeFilename, const char *pPathIDToCopyFrom );
  88. // When we don't have a whitelist, it loads files without bothering to calculate their CRCs (we'd only
  89. // need them on a pure server), but when we get a whitelist, we'll want the CRCs, so it goes back, opens those
  90. // files, and calculates the CRCs for them.
  91. void CalculateMissingCRCs( IFileList *pForceMatchList );
  92. int GetUnverifiedCRCFiles( CUnverifiedCRCFile *pFiles, int nMaxFiles );
  93. // Note that we just opened this file and calculate a CRC for it.
  94. void NoteFileLoadedFromDisk( const char *pFilename, const char *pPathID, FileHandle_t fp );
  95. void NoteFileLoadedFromSteam( const char *pFilename, const char *pPathID, bool bForcedLoadFromSteam );
  96. void NoteFileFailedToLoad( const char *pFilename, const char *pPathID );
  97. // Get a file info from a specific path ID.
  98. CFileInfo* GetFileInfo( const char *pFilename, const char *pPathID );
  99. // Get all file infos with the specified filename (i.e. in all path IDs).
  100. int GetFileInfos( CFileInfo **ppFileInfos, int nMaxFileInfos, const char *pFilename );
  101. // Clear everything.
  102. void Clear();
  103. private:
  104. void CalculateMissingCRC( const char *pFilename, const char *pPathID );
  105. CPathIDFileList* GetPathIDFileList( const char *pPathID, bool bAutoAdd=true );
  106. CRC32_t CalculateCRCForFile( FileHandle_t fp );
  107. private:
  108. CUtlLinkedList<CFileInfo*> m_NeedsVerificationList; // The list of files that need CRCs verified.
  109. CUtlDict<CPathIDFileList*,int> m_PathIDs;
  110. CBaseFileSystem *m_pFileSystem;
  111. CThreadMutex m_Mutex; // Threads call into here, so we need to be safe.
  112. };
  113. inline const char* CFileInfo::GetFilename()
  114. {
  115. return m_pFilename;
  116. }
  117. inline const char* CFileInfo::GetPathIDString()
  118. {
  119. return m_pPathIDFileList->m_PathID.String();
  120. }
  121. #endif // FILETRACKER_H