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.

150 lines
5.6 KiB

  1. //========= Copyright � Valve Corporation, All rights reserved. ============//
  2. //
  3. // Utility helper functions for dealing with UGC files
  4. //
  5. //==========================================================================//
  6. #ifndef UGC_UTILS_H
  7. #define UGC_UTILS_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "steam/isteamremotestorage.h"
  12. // Version of V_ComposeFilename which can account for NULL path or filename
  13. void V_SafeComposeFilename( const char *pPathIn, const char *pFilenameIn, char *pDest, size_t nDestSize );
  14. // Unzip a file. If output dir is omitted, unzips to zip file's directory.
  15. // Use paths relative to GetCurrentDirectory, or absolute paths.
  16. bool UnzipFile( const char* szPathToZipFile, const char* szOutputDir = NULL );
  17. bool UGCUtil_IsOfficialMap( PublishedFileId_t id );
  18. #if !defined( NO_STEAM ) && !defined ( _PS3 )
  19. #include "steam/steam_api.h"
  20. #include "filesystem.h"
  21. DECLARE_LOGGING_CHANNEL( LOG_WORKSHOP );
  22. // This will log the UGC file requests as they're serviced
  23. #define LOG_FILEREQUEST_PROGRESS
  24. // Simulates stalling of the file IO for testing
  25. //#define FILEREQUEST_IO_STALL
  26. class CUGCFileRequestManager;
  27. extern ISteamRemoteStorage *GetISteamRemoteStorage();
  28. PublishedFileId_t GetMapIDFromMapPath( const char *pMapPath ); // get the map workshop ID by passing the full map path
  29. bool UGCUtil_TimestampFile( const char *pFileRelativePath, uint32 uTimestamp );
  30. void UGCUtil_Init();
  31. void UGCUtil_Shutdown();
  32. enum UGCFileRequestStatus_t
  33. {
  34. UGCFILEREQUEST_INVALID = -2, // This was an invalid request for some reason
  35. UGCFILEREQUEST_ERROR = -1, // An error occurred while processing the file operation
  36. UGCFILEREQUEST_READY, // File request is ready to do work
  37. UGCFILEREQUEST_DOWNLOADING, // Currently downloading a file
  38. UGCFILEREQUEST_UNZIPPING, // Threaded unzipping
  39. UGCFILEREQUEST_DOWNLOAD_WRITING, // Async write of the downloaded file to the disc
  40. UGCFILEREQUEST_UPLOADING, // Currently uploading a file
  41. UGCFILEREQUEST_FINISHED // Operation complete, no work waiting
  42. };
  43. #ifdef FILEREQUEST_IO_STALL
  44. enum
  45. {
  46. FILEREQUEST_STALL_NONE,
  47. FILEREQUEST_STALL_DOWNLOAD, // Download from UGC server
  48. FILEREQUEST_STALL_WRITE, // Write to disc
  49. };
  50. #endif // FILEREQUEST_IO_STALL
  51. class CUGCUnzipJob;
  52. class CUGCFileRequest
  53. {
  54. public:
  55. CUGCFileRequest( void );
  56. ~CUGCFileRequest( void );
  57. UGCFileRequestStatus_t StartDownload( UGCHandle_t hFileHandle, const char *lpszTargetDirectory = NULL, const char *lpszTargetFilename = NULL, uint32 timeUpdated = 0, bool bForceUpdate = false );
  58. UGCFileRequestStatus_t StartUpload( CUtlBuffer &buffer, const char *lpszFilename );
  59. UGCFileRequestStatus_t Update( void );
  60. UGCFileRequestStatus_t GetStatus( void ) const { return m_UGCStatus; }
  61. // Accessors
  62. const char *GetLastError( void ) const { return m_szErrorText; }
  63. UGCHandle_t GetCloudHandle( void ) const { return m_hCloudID; }
  64. void GetFullPath( char *pDest, size_t nSize ) const; // Filename on disk (including relative path)
  65. const char *GetFilename( void ) const; // Name on disk if not the same as in the cloud
  66. const char *GetDirectory( void ) const; // File directory on disk
  67. float GetDownloadProgress( void ) const { return m_flDownloadProgress; }
  68. void GetLocalFileName( char *pDest, size_t strSize );
  69. void GetLocalDirectory( char *pDest, size_t strSize );
  70. private:
  71. void MarkCompleteAndFree( bool bUpdated = true );
  72. #ifdef CLIENT_DLL
  73. void CreateSmallThumbNail( bool bForce );
  74. bool ResizeRGBAImage( const unsigned char *pData, unsigned int nDataSize, unsigned int nWidth, unsigned int nHeight,
  75. unsigned char **pDataOut, unsigned int nNewWidth, unsigned int nNewHeight );
  76. #endif
  77. bool FileInSync( const char *lpszTargetDirectory, const char *lpszTargetFilename, uint32 timeUpdated );
  78. void UpdateUnzip();
  79. UGCFileRequestStatus_t ThrowError( const char *lpszFormat, ... );
  80. void Steam_OnUGCDownload( RemoteStorageDownloadUGCResult_t *pResult, bool bError );
  81. CCallResult<CUGCFileRequest, RemoteStorageDownloadUGCResult_t> m_callbackUGCDownload;
  82. void Steam_OnFileShare( RemoteStorageFileShareResult_t *pResult, bool bError );
  83. CCallResult<CUGCFileRequest, RemoteStorageFileShareResult_t> m_callbackFileShare;
  84. char m_szTargetDirectory[MAX_PATH]; // If specified, the directory the file will be placed in
  85. char m_szTargetFilename[MAX_PATH]; // If specified, this name overrides the UGC filename
  86. char m_szFileName[MAX_PATH]; // Filename of in the cloud structure
  87. SteamAPICall_t m_hSteamAPICall; // Used to track Steam API calls which are non-blocking
  88. CUtlBuffer m_bufContents; // Contents of the file once read from the cloud
  89. UGCHandle_t m_hCloudID; // Cloud handle of this request
  90. FSAsyncControl_t m_AsyncControl; // Handle for the async requests this class can initiate
  91. char m_szErrorText[512]; // Holds information if an error occurred
  92. UGCFileRequestStatus_t m_UGCStatus; // The current status of this request
  93. uint32 m_tFileUpdateTime; // The update timestamp of the workshop file, 0 if not available
  94. // Timing information for logging purposes
  95. float m_flIOStartTime; // Time when the current request was initiated
  96. float m_flDownloadProgress; // Progress (only valid for downloads)
  97. friend class CUGCFileRequestManager;
  98. CUGCUnzipJob* m_pUnzipJob; // Pointer to threaded unzip job.
  99. #ifdef FILEREQUEST_IO_STALL
  100. // Debug data
  101. float m_flIOStallDuration; // Amount of time (in seconds) to stall all IO operations
  102. int m_nIOStallType; // Type of stall (0 - none, 1 - download, 2 - write )
  103. float m_flIOStallStart;
  104. #endif // FILEREQUEST_IO_STALL
  105. };
  106. #endif //!NO_STEAM
  107. #endif //UGC_UTILS_H