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.

156 lines
4.6 KiB

  1. //===== Copyright Valve Corporation, All rights reserved. ======//
  2. //
  3. //
  4. //
  5. //==============================================================//
  6. #ifndef UGC_REQUEST_MANAGER
  7. #define UGC_REQUEST_MANAGER
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "ugc_utils.h"
  12. #include "utlpriorityqueue.h"
  13. #include "utlmap.h"
  14. #if !defined( NO_STEAM ) && !defined ( _PS3 )
  15. class CUGCFileRequestManager;
  16. //
  17. //
  18. //
  19. enum UGCRequestType_t {
  20. UGC_REQUEST_INVALID = -1, // Uninitialized request!
  21. UGC_REQUEST_DOWNLOAD = 0,
  22. UGC_REQUEST_UPLOAD,
  23. };
  24. struct UGCFileRequest_t
  25. {
  26. public:
  27. uint32 GetPriority() const { return unPriority; }
  28. uint32 GetTimestamp() const { return unTimestamp; }
  29. float GetProgress() const { return fileRequest.GetDownloadProgress(); }
  30. UGCHandle_t GetFileHandle() const { return fileHandle; }
  31. PublishedFileId_t GetPublishedFileID() const { return publishedFileID; }
  32. private:
  33. UGCFileRequest_t() :
  34. fileHandle( k_UGCHandleInvalid ),
  35. publishedFileID( 0 ),
  36. unLastUpdateTime( 0 ),
  37. bForceUpdate( false ),
  38. unPriority( 0 ),
  39. unTimestamp( 0 ),
  40. nType( UGC_REQUEST_INVALID )
  41. {
  42. szTargetFilename[0] = '\0';
  43. szTargetDirectory[0] = '\0';
  44. szSourceFilename[0] = '\0';
  45. }
  46. // Core data
  47. UGCHandle_t fileHandle;
  48. PublishedFileId_t publishedFileID; // id for the whole content package this download is associated with
  49. char szSourceFilename[MAX_PATH]; // Only used for uploads
  50. char szTargetFilename[MAX_PATH];
  51. char szTargetDirectory[MAX_PATH];
  52. UGCRequestType_t nType; // Download or upload type
  53. uint32 unLastUpdateTime; // Set to 0 if only existence of file matters
  54. bool bForceUpdate; // If true, always download a new version
  55. // Priority data
  56. uint32 unPriority; // Higher is better
  57. uint32 unTimestamp; // When this request was initiated (for sorting)
  58. CUGCFileRequest fileRequest;
  59. friend class CUGCFileRequestManager;
  60. };
  61. //
  62. //
  63. //
  64. class CUGCFileRequestManager
  65. {
  66. public:
  67. CUGCFileRequestManager( void );
  68. // Move the processing of file requests forward (yielding where appropriate)
  69. void Update( void );
  70. // Create a file request
  71. bool CreateFileDownloadRequest( UGCHandle_t unFileHandle,
  72. PublishedFileId_t publishedFileID,
  73. const char *lpszTargetDirectory,
  74. const char *lpszTargetFilename,
  75. uint32 unPriority,
  76. uint32 unLastUpdateTime = 0, // Last time the file was updated (used for sync checks)
  77. bool bForceUpdate = false ); // Override sync checks and always grab the file
  78. bool CreateFileUploadRequest( const char *lpszSourceFilename,
  79. const char *lpszTargetDirectory,
  80. const char *lpszTargetFilename,
  81. uint32 unPriority );
  82. // Delete a file request from the system
  83. bool DeleteFileRequest( UGCHandle_t handle, bool bRemoveFromDisk = false );
  84. // Determine if there is a request for the handle provided
  85. bool FileRequestExists( UGCHandle_t handle ) const;
  86. // Get the full (local) path (including filename) for this file
  87. void GetFullPath( UGCHandle_t unFileHandle, char *pDest, size_t nSize ) const;
  88. // Get the (local path) directory this file resides in (can be "")
  89. const char *GetDirectory( UGCHandle_t unFileHandle ) const;
  90. // The filename, minus the directory
  91. const char *GetFilename( UGCHandle_t unFileHandle ) const;
  92. // Get the file handle for a request by its target filename
  93. UGCHandle_t GetFileRequestHandleByFilename( const char *lpszFilane ) const;
  94. // Get the status of the request. Note: Will return INVALID if there is no request by that handle found
  95. UGCFileRequestStatus_t GetStatus( UGCHandle_t unFileHandle ) const;
  96. UGCFileRequestStatus_t GetStatus( const char *lpszFilename ) const;
  97. // Returns the progress of a file being downloaded from the Steam cloud. Always 0 if nothing has begun, or 1 if past the point of downloading
  98. float GetDownloadProgress( UGCHandle_t handle ) const;
  99. // Promote the request to the top of the priority list (only works for pending requests)
  100. bool PromoteRequestToTop( UGCHandle_t handle );
  101. // Any pending operations
  102. bool HasPendingDownloads( void ) const;
  103. private:
  104. void Debug_LogPendingOperations( void );
  105. // Get the underlying data for the request, referenced by its file handle
  106. const UGCFileRequest_t *GetFileRequestByHandle( UGCHandle_t unFileHandle ) const;
  107. const UGCFileRequest_t *GetFileRequestByFilename( const char *lpszFilename ) const;
  108. // Map of all requested files by their Steam Cloud handles
  109. CUtlMap<UGCHandle_t, const UGCFileRequest_t *> m_FileRequests;
  110. // Prioritized queue of pending file operations, serviced by order of importance
  111. CUtlPriorityQueue<UGCFileRequest_t *> m_PendingFileOperations;
  112. };
  113. #endif // !NO_STEAM
  114. #endif // UGC_REQUEST_MANAGER