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.

151 lines
6.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #ifndef COMMON_H
  9. #define COMMON_H
  10. #pragma once
  11. #ifndef WORLDSIZE_H
  12. #include "worldsize.h"
  13. #endif
  14. #include "basetypes.h"
  15. #include "filesystem.h"
  16. #include "mathlib/vector.h" // @Note (toml 05-01-02): solely for definition of QAngle
  17. #include "qlimits.h"
  18. #define INCLUDED_STEAM2_USERID_STRUCTS
  19. #include "steamcommon.h"
  20. #include "steam/steamclientpublic.h"
  21. class Vector;
  22. struct cache_user_t;
  23. //============================================================================
  24. #define COM_COPY_CHUNK_SIZE 1024 // For copying operations
  25. #ifndef NULL
  26. #define NULL ((void *)0)
  27. #endif
  28. #include "tier1/strtools.h"
  29. //============================================================================
  30. char *COM_StringCopy(const char *text); // allocates memory and copys text
  31. void COM_StringFree(const char *text); // frees memory allocated by COM_StringCopy
  32. void COM_AddNoise( unsigned char *data, int length, int number ); // Changes n random bits in a data block
  33. //============================================================================
  34. extern void COM_WriteFile (const char *filename, void *data, int len);
  35. extern int COM_OpenFile( const char *filename, FileHandle_t* file );
  36. extern void COM_CloseFile( FileHandle_t hFile );
  37. extern void COM_CreatePath (const char *path);
  38. extern int COM_FileSize (const char *filename);
  39. extern int COM_ExpandFilename (char *filename, int maxlength);
  40. extern byte *COM_LoadFile (const char *path, int usehunk, int *pLength);
  41. extern bool COM_IsValidPath( const char *pszFilename );
  42. extern bool COM_IsValidLogFilename( const char *pszFilename );
  43. const char *COM_Parse (const char *data);
  44. const char *COM_ParseLine (const char *data);
  45. int COM_TokenWaiting( const char *buffer );
  46. extern bool com_ignorecolons;
  47. extern char com_token[1024];
  48. void COM_Init (void);
  49. void COM_Shutdown( void );
  50. bool COM_CheckGameDirectory( const char *gamedir );
  51. void COM_ParseDirectoryFromCmd( const char *pCmdName, char *pDirName, int maxlen, const char *pDefault );
  52. #define Bits2Bytes(b) ((b+7)>>3)
  53. // returns a temp buffer of at least 512 bytes
  54. char *tmpstr512();
  55. // does a varargs printf into a temp buffer.
  56. // Returns char* because of bad historical reasons.
  57. char *va(PRINTF_FORMAT_STRING const char *format, ...) FMTFUNCTION( 1, 2 );
  58. // prints a vector into a temp buffer.
  59. const char *vstr(Vector& v);
  60. //============================================================================
  61. extern char com_basedir[MAX_OSPATH];
  62. extern char com_gamedir[MAX_OSPATH];
  63. byte *COM_LoadStackFile (const char *path, void *buffer, int bufsize, int& filesize );
  64. void COM_LoadCacheFile (const char *path, cache_user_t *cu);
  65. byte* COM_LoadFile(const char *path, int usehunk, int *pLength);
  66. void COM_CopyFileChunk( FileHandle_t dst, FileHandle_t src, int nSize );
  67. bool COM_CopyFile( const char *pSourcePath, const char *pDestPath );
  68. void COM_SetupLogDir( const char *mapname );
  69. void COM_GetGameDir(char *szGameDir, int maxlen);
  70. int COM_CompareFileTime(const char *filename1, const char *filename2, int *iCompare);
  71. int COM_GetFileTime( const char *pFileName );
  72. const char *COM_ParseFile(const char *data, char *token, int maxtoken);
  73. extern char gszDisconnectReason[256];
  74. extern char gszExtendedDisconnectReason[256];
  75. extern bool gfExtendedError;
  76. extern uint8 g_eSteamLoginFailure;
  77. void COM_ExplainDisconnection( bool bPrint, PRINTF_FORMAT_STRING const char *fmt, ... );
  78. const char *COM_DXLevelToString( int dxlevel ); // convert DX level to string
  79. void COM_Log( const char *pszFile, PRINTF_FORMAT_STRING const char *fmt, ...) FMTFUNCTION( 2, 3 ); // Log a debug message to specified file ( if pszFile == NULL uses c:\\hllog.txt )
  80. void COM_LogString( char const *pchFile, char const *pchString );
  81. const char *COM_FormatSeconds( int seconds ); // returns seconds as hh:mm:ss string
  82. const char *COM_GetModDirectory(); // return the mod dir (rather than the complete -game param, which can be a path)
  83. void *COM_CompressBuffer_LZSS( const void *source, unsigned int sourceLen, unsigned int *compressedLen, unsigned int maxCompressedLen = 0 );
  84. bool COM_BufferToBufferCompress_LZSS( void *dest, unsigned int *destLen, const void *source, unsigned int sourceLen );
  85. unsigned int COM_GetIdealDestinationCompressionBufferSize_LZSS( unsigned int uncompressedSize );
  86. void *COM_CompressBuffer_Snappy( const void *source, unsigned int sourceLen, unsigned int *compressedLen, unsigned int maxCompressedLen = 0 );
  87. bool COM_BufferToBufferCompress_Snappy( void *dest, unsigned int *destLen, const void *source, unsigned int sourceLen );
  88. unsigned int COM_GetIdealDestinationCompressionBufferSize_Snappy( unsigned int uncompressedSize );
  89. /// Fetch ideal working buffer size. You should allocate the buffer you wish to compress into
  90. /// at least this big, in order to get the best performance when using COM_BufferToBufferCompress
  91. inline unsigned int COM_GetIdealDestinationCompressionBufferSize( unsigned int uncompressedSize )
  92. {
  93. return COM_GetIdealDestinationCompressionBufferSize_LZSS( uncompressedSize );
  94. }
  95. /// Compress the source data into a newly allocated buffer. Returns the buffer and its
  96. /// size. Note that the buffer may have been allocated to a larger size than necessary,
  97. /// and the compressed size may be larger than the size of the input!
  98. ///
  99. /// If maxCompressedLen is nonzero, then we will fail compression if the compressed data
  100. /// exceeds this size. Depending on the compressor used, we might be able to terminate
  101. /// early in this case
  102. inline void *COM_CompressBuffer( const void *source, unsigned int sourceLen, unsigned int *compressedLen, unsigned int maxCompressedLen = 0 )
  103. {
  104. return COM_CompressBuffer_LZSS( source, sourceLen, compressedLen, maxCompressedLen );
  105. }
  106. /// Compress data to the specified buffer. Returns false if compression fails or the data cannot fit into
  107. /// the specified buffer. If false is returned, the destination buffer and size field are not modified.
  108. /// (Note that this differs from previous behaviour.)
  109. inline bool COM_BufferToBufferCompress( void *dest, unsigned int *destLen, const void *source, unsigned int sourceLen )
  110. {
  111. return COM_BufferToBufferCompress_LZSS( dest, destLen, source, sourceLen );
  112. }
  113. /// Returns true if compression succeeded, false otherwise
  114. bool COM_BufferToBufferDecompress( void *dest, unsigned int *destLen, const void *source, unsigned int sourceLen );
  115. /// Fetch size of the decompressed data in a buffer that was created using COM_BufferToBufferCompress.
  116. /// Returns -1 if buffer does not appear to be compressed.
  117. int COM_GetUncompressedSize( const void *compressed, unsigned int compressedLen );
  118. #endif // COMMON_H