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.

199 lines
5.5 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "quakedef.h"
  7. #include "logofile_shared.h"
  8. #include "filesystem_engine.h"
  9. #include "vtf/vtf.h"
  10. // NOTE: This has to be the last file included!
  11. #include "tier0/memdbgon.h"
  12. // ------------------------------------------------------------------------------------ //
  13. // CLogoFilename implementation.
  14. // ------------------------------------------------------------------------------------ //
  15. CLogoFilename::CLogoFilename( CRC32_t value, bool bInDownloadsDirectory )
  16. {
  17. char hex[16];
  18. Q_binarytohex( (byte *)&value, sizeof( value ), hex, sizeof( hex ) );
  19. if ( bInDownloadsDirectory )
  20. {
  21. Q_snprintf( m_Filename, sizeof( m_Filename ), "materials/decals/downloads/%s.vtf", hex );
  22. }
  23. else
  24. {
  25. Q_strncpy( m_Filename, hex, sizeof( m_Filename ) );
  26. }
  27. }
  28. // ------------------------------------------------------------------------------------ //
  29. // SVC_LogoFileData.
  30. // ------------------------------------------------------------------------------------ //
  31. bool SVC_LogoFileData::ReadFromBuffer( bf_read &buffer )
  32. {
  33. m_Data.SetSize( buffer.ReadShort() );
  34. buffer.ReadBytes( m_Data.Base(), m_Data.Count() );
  35. return !buffer.IsOverflowed();
  36. }
  37. bool SVC_LogoFileData::WriteToBuffer( bf_write &buffer ) const
  38. {
  39. buffer.WriteUBitLong( GetType(), NETMSG_TYPE_BITS );
  40. buffer.WriteShort( m_Data.Count() );
  41. buffer.WriteBytes( m_Data.Base(), m_Data.Count() );
  42. return !buffer.IsOverflowed();
  43. }
  44. const char* SVC_LogoFileData::ToString() const
  45. {
  46. return "SVC_LogoFileData";
  47. }
  48. // ------------------------------------------------------------------------------------ //
  49. // CLC_LogoFileData.
  50. // ------------------------------------------------------------------------------------ //
  51. bool CLC_LogoFileData::ReadFromBuffer( bf_read &buffer )
  52. {
  53. m_Data.SetSize( buffer.ReadShort() );
  54. buffer.ReadBytes( m_Data.Base(), m_Data.Count() );
  55. return !buffer.IsOverflowed();
  56. }
  57. bool CLC_LogoFileData::WriteToBuffer( bf_write &buffer ) const
  58. {
  59. buffer.WriteUBitLong( GetType(), NETMSG_TYPE_BITS );
  60. buffer.WriteShort( m_Data.Count() );
  61. buffer.WriteBytes( m_Data.Base(), m_Data.Count() );
  62. return !buffer.IsOverflowed();
  63. }
  64. const char* CLC_LogoFileData::ToString() const
  65. {
  66. return "CLC_LogoFileData";
  67. }
  68. // ------------------------------------------------------------------------------------ //
  69. // SVC_LogoFileCRC.
  70. // ------------------------------------------------------------------------------------ //
  71. bool SVC_LogoFileCRC::ReadFromBuffer( bf_read &buffer )
  72. {
  73. m_nLogoFileCRC = buffer.ReadLong();
  74. return !buffer.IsOverflowed();
  75. }
  76. bool SVC_LogoFileCRC::WriteToBuffer( bf_write &buffer ) const
  77. {
  78. buffer.WriteUBitLong( GetType(), NETMSG_TYPE_BITS );
  79. buffer.WriteLong( m_nLogoFileCRC );
  80. return !buffer.IsOverflowed();
  81. }
  82. const char* SVC_LogoFileCRC::ToString() const
  83. {
  84. return "LogoFileCRC";
  85. }
  86. // ------------------------------------------------------------------------------------ //
  87. // CLC_LogoFileRequest.
  88. // ------------------------------------------------------------------------------------ //
  89. bool CLC_LogoFileRequest::ReadFromBuffer( bf_read &buffer )
  90. {
  91. m_nLogoFileCRC = buffer.ReadLong();
  92. return !buffer.IsOverflowed();
  93. }
  94. bool CLC_LogoFileRequest::WriteToBuffer( bf_write &buffer ) const
  95. {
  96. buffer.WriteUBitLong( GetType(), NETMSG_TYPE_BITS );
  97. buffer.WriteLong( m_nLogoFileCRC );
  98. return !buffer.IsOverflowed();
  99. }
  100. const char* CLC_LogoFileRequest::ToString() const
  101. {
  102. return "LogoFileRequest";
  103. }
  104. // ------------------------------------------------------------------------------------ //
  105. // CLC_LogoFileRequest.
  106. // ------------------------------------------------------------------------------------ //
  107. bool SVC_LogoFileRequest::ReadFromBuffer( bf_read &buffer )
  108. {
  109. m_nLogoFileCRC = buffer.ReadLong();
  110. return !buffer.IsOverflowed();
  111. }
  112. bool SVC_LogoFileRequest::WriteToBuffer( bf_write &buffer ) const
  113. {
  114. buffer.WriteUBitLong( GetType(), NETMSG_TYPE_BITS );
  115. buffer.WriteLong( m_nLogoFileCRC );
  116. return !buffer.IsOverflowed();
  117. }
  118. const char* SVC_LogoFileRequest::ToString() const
  119. {
  120. return "LogoFileRequest";
  121. }
  122. // ------------------------------------------------------------------------------------ //
  123. // Other functions.
  124. // ------------------------------------------------------------------------------------ //
  125. bool LogoFile_IsValidVTFFile( const void *pData, int len )
  126. {
  127. CUtlBuffer buf( pData, len, CUtlBuffer::READ_ONLY );
  128. IVTFTexture *pTex = CreateVTFTexture();
  129. bool bUnserialized = pTex->Unserialize( buf );
  130. DestroyVTFTexture( pTex );
  131. return bUnserialized;
  132. }
  133. bool LogoFile_ReadFile( CRC32_t crcValue, CUtlVector<char> &fileData )
  134. {
  135. CLogoFilename filename( crcValue, true );
  136. FileHandle_t hFile = g_pFileSystem->Open( filename.m_Filename, "rb" );
  137. if ( hFile == FILESYSTEM_INVALID_HANDLE )
  138. {
  139. Warning( "LogoFile_ReadFile: can't open file '%s'.\n", filename.m_Filename );
  140. return false;
  141. }
  142. fileData.SetSize( g_pFileSystem->Size( hFile ) );
  143. int amtRead = g_pFileSystem->Read( fileData.Base(), fileData.Count(), hFile );
  144. g_pFileSystem->Close( hFile );
  145. if ( fileData.Count() == 0 || amtRead != fileData.Count() )
  146. {
  147. Warning( "LogoFile_ReadFile: error reading file '%s'.\n", filename.m_Filename );
  148. return false;
  149. }
  150. // Validate it one last time.
  151. if ( !LogoFile_IsValidVTFFile( fileData.Base(), fileData.Count() ) )
  152. {
  153. g_pFileSystem->RemoveFile( filename.m_Filename );
  154. Warning( "LogoFile_ReadFile: !IsValidVTFFile for '%s'.\n", filename.m_Filename );
  155. return false;
  156. }
  157. return true;
  158. }