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.

197 lines
5.9 KiB

  1. //===== Copyright Valve Corporation, All rights reserved. ======//
  2. //
  3. // Helper class to manager clients making requests for published file information and polling the results at a later time
  4. //
  5. //==============================================================//
  6. #ifndef UGC_FILE_INFO_MANAGER
  7. #define UGC_FILE_INFO_MANAGER
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "ugc_utils.h"
  12. #if !defined( NO_STEAM ) && !defined( _PS3 )
  13. class IWorkshopFileInfoManagerCallbackInterface
  14. {
  15. public:
  16. // File requests
  17. virtual void OnFileRequestFinished( UGCHandle_t hFileHandle ) = 0;
  18. virtual void OnFileRequestError( UGCHandle_t hFileHandle ) = 0;
  19. };
  20. //
  21. // Holds information about published files on the Workshop
  22. //
  23. class CWorkshopFileInfoManager;
  24. struct PublishedFileInfo_t
  25. {
  26. PublishedFileInfo_t( PublishedFileId_t nID = 0 ) :
  27. m_nPublishedFileId( nID ),
  28. m_hFile( k_UGCHandleInvalid ),
  29. m_hPreviewFile( k_UGCHandleInvalid ),
  30. m_ulSteamIDOwner( 0 ),
  31. m_rtimeCreated( 0 ),
  32. m_rtimeUpdated( 0 ),
  33. m_eVisibility( k_ERemoteStoragePublishedFileVisibilityPublic ),
  34. m_rtimeSubscribed( 0 ),
  35. m_rtimeLastPlayed( 0 ),
  36. m_rtimeCompleted( 0 ),
  37. m_unUpVotes( 0 ),
  38. m_unDownVotes( 0 ),
  39. m_flVotingScore( 0.0f ),
  40. m_bVotingDataValid( false ),
  41. m_unNumReports( 0 )
  42. {
  43. memset( m_rgchTitle, 0, ARRAYSIZE( m_rgchTitle ) );
  44. memset( m_rgchDescription, 0, ARRAYSIZE( m_rgchDescription ) );
  45. memset( m_pchFileName, 0, ARRAYSIZE( m_pchFileName ) );
  46. }
  47. friend class CWorkshopFileInfoManager;
  48. // Basic info
  49. PublishedFileId_t m_nPublishedFileId;
  50. char m_rgchTitle[k_cchPublishedDocumentTitleMax];
  51. UGCHandle_t m_hFile;
  52. UGCHandle_t m_hPreviewFile;
  53. uint64 m_ulSteamIDOwner;
  54. uint32 m_rtimeCreated;
  55. uint32 m_rtimeUpdated;
  56. ERemoteStoragePublishedFileVisibility m_eVisibility;
  57. uint32 m_rtimeSubscribed;
  58. uint32 m_rtimeLastPlayed;
  59. uint32 m_rtimeCompleted;
  60. char m_rgchDescription[k_cchPublishedDocumentDescriptionMax];
  61. char m_pchFileName[k_cchFilenameMax];
  62. char m_rgchTags[k_cchTagListMax];
  63. bool m_bTagsTruncated;
  64. CCopyableUtlVector<char *> m_vTags;
  65. // Whether or not the voting data has been properly collected for this item
  66. bool HasVoteData( void ) const { return m_bVotingDataValid; }
  67. // Get the vote information for this item
  68. // NOTE: Because this isn't native to the published file, it must be requested separately.
  69. // This function protects from the case where the data is not valid when requested (returns false)
  70. bool GetVoteData( float *pScore, uint32 *pUpVotes, uint32 *pDownVotes ) const
  71. {
  72. // We must have real data here to allow a query for it
  73. if ( HasVoteData() == false )
  74. return false;
  75. if ( pScore != NULL )
  76. {
  77. *pScore = m_flVotingScore;
  78. }
  79. if ( pUpVotes != NULL )
  80. {
  81. *pUpVotes = m_unUpVotes;
  82. }
  83. if ( pDownVotes != NULL )
  84. {
  85. *pDownVotes = m_unDownVotes;
  86. }
  87. return true;
  88. }
  89. bool HasTag( const char *pszTag ) const
  90. {
  91. for ( int i = 0; i < m_vTags.Count(); ++i )
  92. {
  93. if ( !V_stricmp( m_vTags[i], pszTag ) )
  94. {
  95. return true;
  96. }
  97. }
  98. return false;
  99. }
  100. private:
  101. // Voting info
  102. bool m_bVotingDataValid; // If this isn't true, then no request has been done to retrieve it
  103. uint32 m_unUpVotes;
  104. uint32 m_unDownVotes;
  105. float m_flVotingScore;
  106. uint32 m_unNumReports; // FIXME: This isn't exposed anywhere, currently
  107. };
  108. //
  109. // Class which packages up info requests and how to deal with errors and completion
  110. // FIXME: May be easier to switch some of these over to callback instead of this system
  111. //
  112. class CBasePublishedFileRequest
  113. {
  114. public:
  115. CBasePublishedFileRequest( PublishedFileId_t targetID ) :
  116. m_nTargetID( targetID )
  117. {}
  118. virtual void OnError( EResult nErrorCode ) { /* Do nothing */ }
  119. virtual void OnLoaded( PublishedFileInfo_t &info ) { /* Do nothing */ }
  120. PublishedFileId_t GetTargetID( void ) const { return m_nTargetID; }
  121. private:
  122. PublishedFileId_t m_nTargetID; // File ID to get information about
  123. };
  124. //
  125. //
  126. // Workshop file info manager
  127. //
  128. //
  129. class CWorkshopFileInfoManager
  130. {
  131. public:
  132. CWorkshopFileInfoManager( IWorkshopFileInfoManagerCallbackInterface *pCallbackInterface );
  133. ~CWorkshopFileInfoManager( void );
  134. // Add a query for a published file's information
  135. bool AddFileInfoQuery( CBasePublishedFileRequest *pRequest, bool bAllowUpdate = false );
  136. bool AddFileVoteInfoRequest( const PublishedFileInfo_t *pInfo, bool bForceUpdate = false );
  137. bool RemovePublishedFileInfo( PublishedFileId_t nID );
  138. void Update( void );
  139. const PublishedFileInfo_t *GetPublishedFileInfoByID( PublishedFileId_t nID ) const;
  140. // Check if an info request hasn't been sent off yet
  141. bool IsInfoRequestStillPending( PublishedFileId_t id ) const;
  142. private:
  143. void UpdatePublishedFileInfoQueries( void );
  144. void UpdatePublishedFileVotingInfoQueries( void );
  145. #if !defined( _GAMECONSOLE )
  146. CCallResult<CWorkshopFileInfoManager, RemoteStorageGetPublishedFileDetailsResult_t> m_callbackGetPublishedFileDetails;
  147. void Steam_OnGetPublishedFileDetails( RemoteStorageGetPublishedFileDetailsResult_t *pResult, bool bError );
  148. CCallResult<CWorkshopFileInfoManager, RemoteStorageGetPublishedItemVoteDetailsResult_t> m_callbackGetPublishedItemVoteDetails;
  149. void Steam_OnGetPublishedItemVoteDetails( RemoteStorageGetPublishedItemVoteDetailsResult_t *pResult, bool bError );
  150. #endif // !_GAMECONSOLE
  151. CUtlMap< PublishedFileId_t, PublishedFileInfo_t > m_mapPublishedFileInfoDepot; // Master list of all published file information we've queried for
  152. CUtlQueue< CBasePublishedFileRequest * > m_vecPublishedFileInfoQueryList; // List of file IDs that need to be queried for information
  153. CBasePublishedFileRequest *m_pActivePublishedFileRequest; // Currently active request query (used as a mutex)
  154. CUtlQueue< PublishedFileId_t > m_vecVotingInfoRequests; // List of published file IDs we'd like voting information on
  155. bool m_bActiveVotingRequest; // Whether or not there's a voting request already pending
  156. IWorkshopFileInfoManagerCallbackInterface *m_pCallbackInterface; // User-supplied callback interface
  157. };
  158. #endif // !NO_STEAM
  159. #endif // UGC_FILE_INFO_MANAGER