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.

187 lines
3.9 KiB

  1. //====== Copyright © 1996-2005, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "cbase.h"
  7. #include "GameStats.h"
  8. // NOTE: This has to be the last file included!
  9. #include "tier0/memdbgon.h"
  10. void BasicGameStatsRecord_t::Clear()
  11. {
  12. m_nCount = 0;
  13. m_nSeconds = 0;
  14. m_nCommentary = 0;
  15. m_nHDR = 0;
  16. m_nCaptions = 0;
  17. m_bSteam = true;
  18. m_bCyberCafe = false;
  19. Q_memset( m_nSkill, 0, sizeof( m_nSkill ) );
  20. m_nDeaths = 0;
  21. }
  22. void BasicGameStatsRecord_t::SaveToBuffer( CUtlBuffer &buf )
  23. {
  24. buf.PutInt( m_nCount );
  25. buf.PutInt( m_nSeconds );
  26. buf.PutInt( m_nCommentary );
  27. buf.PutInt( m_nHDR );
  28. buf.PutInt( m_nCaptions );
  29. for ( int i = 0; i < 3; ++i )
  30. {
  31. buf.PutInt( m_nSkill[ i ] );
  32. }
  33. buf.PutChar( m_bSteam ? 1 : 0 );
  34. buf.PutChar( m_bCyberCafe ? 1 : 0 );
  35. buf.PutInt( m_nDeaths );
  36. }
  37. bool BasicGameStatsRecord_t::ParseFromBuffer( CUtlBuffer &buf, int iBufferStatsVersion )
  38. {
  39. bool bret = true;
  40. m_nCount = buf.GetInt();
  41. if ( m_nCount > 100000 || m_nCount < 0 )
  42. {
  43. bret = false;
  44. }
  45. m_nSeconds = buf.GetInt();
  46. // Note, don't put the buf.GetInt() in the macro since it'll get evaluated twice!!!
  47. m_nSeconds = MAX( m_nSeconds, 0 );
  48. m_nCommentary = buf.GetInt();
  49. if ( m_nCommentary < 0 || m_nCommentary > 100000 )
  50. {
  51. bret = false;
  52. }
  53. m_nHDR = buf.GetInt();
  54. if ( m_nHDR < 0 || m_nHDR > 100000 )
  55. {
  56. bret = false;
  57. }
  58. m_nCaptions = buf.GetInt();
  59. if ( m_nCaptions < 0 || m_nCaptions > 100000 )
  60. {
  61. bret = false;
  62. }
  63. for ( int i = 0; i < 3; ++i )
  64. {
  65. m_nSkill[ i ] = buf.GetInt();
  66. if ( m_nSkill[ i ] < 0 || m_nSkill[ i ] > 100000 )
  67. {
  68. bret = false;
  69. }
  70. }
  71. if ( iBufferStatsVersion > GAMESTATS_FILE_VERSION_OLD )
  72. {
  73. m_bSteam = buf.GetChar() ? true : false;
  74. }
  75. if ( iBufferStatsVersion > GAMESTATS_FILE_VERSION_OLD2 )
  76. {
  77. m_bCyberCafe = buf.GetChar() ? true : false;
  78. }
  79. if ( iBufferStatsVersion > GAMESTATS_FILE_VERSION_OLD5 )
  80. {
  81. m_nDeaths = buf.GetInt();
  82. }
  83. return bret;
  84. }
  85. void BasicGameStats_t::Clear()
  86. {
  87. m_nSecondsToCompleteGame = 0;
  88. m_Summary.Clear();
  89. m_MapTotals.Purge();
  90. }
  91. void BasicGameStats_t::SaveToBuffer( CUtlBuffer& buf )
  92. {
  93. buf.PutInt( m_nSecondsToCompleteGame );
  94. m_Summary.SaveToBuffer( buf );
  95. int c = m_MapTotals.Count();
  96. buf.PutInt( c );
  97. for ( int i = m_MapTotals.First(); i != m_MapTotals.InvalidIndex(); i = m_MapTotals.Next( i ) )
  98. {
  99. char const *name = m_MapTotals.GetElementName( i );
  100. BasicGameStatsRecord_t &rec = m_MapTotals[ i ];
  101. buf.PutString( name );
  102. rec.SaveToBuffer( buf );
  103. }
  104. buf.PutChar( 0 );
  105. buf.PutChar( m_bSteam ? 1 : 0 );
  106. buf.PutChar( m_bCyberCafe ? 1 : 0 );
  107. buf.PutShort( (short)m_nDXLevel );
  108. }
  109. BasicGameStatsRecord_t *BasicGameStats_t::FindOrAddRecordForMap( char const *mapname )
  110. {
  111. int idx = m_MapTotals.Find( mapname );
  112. if ( idx == m_MapTotals.InvalidIndex() )
  113. {
  114. idx = m_MapTotals.Insert( mapname );
  115. }
  116. return &m_MapTotals[ idx ];
  117. }
  118. bool BasicGameStats_t::ParseFromBuffer( CUtlBuffer& buf, int iBufferStatsVersion )
  119. {
  120. bool bret = true;
  121. m_nSecondsToCompleteGame = buf.GetInt();
  122. if ( m_nSecondsToCompleteGame < 0 || m_nSecondsToCompleteGame > 10000000 )
  123. {
  124. bret = false;
  125. }
  126. m_Summary.ParseFromBuffer( buf, iBufferStatsVersion );
  127. int c = buf.GetInt();
  128. if ( c > 1024 || c < 0 )
  129. {
  130. bret = false;
  131. }
  132. for ( int i = 0; i < c; ++i )
  133. {
  134. char mapname[ 256 ];
  135. buf.GetString( mapname, sizeof( mapname ) );
  136. BasicGameStatsRecord_t *rec = FindOrAddRecordForMap( mapname );
  137. bool valid= rec->ParseFromBuffer( buf, iBufferStatsVersion );
  138. if ( !valid )
  139. {
  140. bret = false;
  141. }
  142. }
  143. if ( iBufferStatsVersion >= GAMESTATS_FILE_VERSION_OLD2 )
  144. {
  145. buf.GetChar(); // Gets the obsolete hl2 unlocked chapter field
  146. m_bSteam = buf.GetChar() ? true : false;
  147. }
  148. if ( iBufferStatsVersion > GAMESTATS_FILE_VERSION_OLD2 )
  149. {
  150. m_bCyberCafe = buf.GetChar() ? true : false;
  151. }
  152. if ( iBufferStatsVersion > GAMESTATS_FILE_VERSION_OLD3 )
  153. {
  154. m_nDXLevel = (int)buf.GetShort();
  155. }
  156. return bret;
  157. }