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.

354 lines
7.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. //----------------------------------------------------------------------------------------
  4. #if defined( REPLAY_ENABLED )
  5. #include "replay/ienginereplay.h"
  6. #include "replay/replayutils.h"
  7. #include "client.h"
  8. #include "server.h"
  9. #include "cl_demo.h"
  10. #include "ivideomode.h"
  11. #include "replayserver.h"
  12. #include "cl_steamauth.h"
  13. #include "host_state.h"
  14. #include "globalvars_base.h"
  15. #include "vgui_baseui_interface.h"
  16. #include "replay_internal.h"
  17. #include "sv_steamauth.h"
  18. #include "lzss.h"
  19. #include "checksum_engine.h"
  20. #if !defined( DEDICATED )
  21. #include "con_nprint.h"
  22. #include "net_chan.h"
  23. #include "download.h"
  24. #include "audio/public/snd_device.h"
  25. #include "audio/private/snd_wave_temp.h"
  26. #endif
  27. // memdbgon must be the last include file in a .cpp file!!!
  28. #include "tier0/memdbgon.h"
  29. //-----------------------------------------------------------------------------
  30. class CEngineReplay : public IEngineReplay
  31. {
  32. public:
  33. virtual bool IsSupportedModAndPlatform()
  34. {
  35. return Replay_IsSupportedModAndPlatform();
  36. }
  37. virtual const char *GetGameDir()
  38. {
  39. return com_gamedir;
  40. }
  41. virtual float GetHostTime()
  42. {
  43. return host_time;
  44. }
  45. virtual int GetHostTickCount()
  46. {
  47. return host_tickcount;
  48. }
  49. virtual int TimeToTicks( float flTime )
  50. {
  51. return TIME_TO_TICKS( flTime );
  52. }
  53. virtual float TicksToTime( int nTick )
  54. {
  55. return TICKS_TO_TIME( nTick );
  56. }
  57. virtual void Cbuf_AddText( const char *pCmd )
  58. {
  59. ::Cbuf_AddText( pCmd );
  60. }
  61. virtual void Cbuf_Execute()
  62. {
  63. ::Cbuf_Execute();
  64. }
  65. virtual void Host_Disconnect( bool bShowMainMenu )
  66. {
  67. ::Host_Disconnect( bShowMainMenu );
  68. }
  69. virtual void HostState_Shutdown()
  70. {
  71. ::HostState_Shutdown();
  72. }
  73. virtual const char *GetModDir()
  74. {
  75. return COM_GetModDirectory();
  76. }
  77. virtual bool CopyFile( const char *pSource, const char *pDest )
  78. {
  79. return COM_CopyFile( pSource, pDest );
  80. }
  81. virtual bool LZSS_Compress( char *pDest, unsigned int *pDestLen, const char *pSource, unsigned int nSourceLen )
  82. {
  83. return COM_BufferToBufferCompress( pDest, pDestLen, pSource, nSourceLen );
  84. }
  85. virtual bool LZSS_Decompress( char *pDest, unsigned int *pDestLen, const char *pSource, unsigned int nSourceLen )
  86. {
  87. return COM_BufferToBufferDecompress( pDest, pDestLen, pSource, nSourceLen );
  88. }
  89. virtual bool MD5_HashBuffer( unsigned char pDigest[16], const unsigned char *pBuffer, int nSize, unsigned int pSeed[4] )
  90. {
  91. return ::MD5_Hash_Buffer( pDigest, pBuffer, nSize, pSeed != NULL, pSeed );
  92. }
  93. virtual bool ReadDemoHeader( const char *pFilename, demoheader_t &header )
  94. {
  95. V_memset( &header, 0, sizeof( header ) );
  96. CDemoFile demofile;
  97. if ( !demofile.Open( pFilename, true ) )
  98. return false;
  99. demofile.ReadDemoHeader();
  100. V_memcpy( &header, &demofile.m_DemoHeader, sizeof( header ) );
  101. return true;
  102. }
  103. virtual IReplayServer *GetReplayServer()
  104. {
  105. return replay;
  106. }
  107. virtual IServer *GetReplayServerAsIServer()
  108. {
  109. return replay;
  110. }
  111. virtual IServer *GetGameServer()
  112. {
  113. if ( sv.IsDedicated() )
  114. {
  115. return &sv;
  116. }
  117. return NULL;
  118. }
  119. virtual bool GetSessionRecordBuffer( uint8 **ppSessionBuffer, int *pSize )
  120. {
  121. if ( !replay )
  122. {
  123. AssertMsg( 0, "Why is this being called when replay is inactive?" );
  124. *ppSessionBuffer = NULL;
  125. *pSize = 0;
  126. return false;
  127. }
  128. *ppSessionBuffer = (uint8 *)replay->m_DemoRecorder.m_DemoFile.m_pBuffer->Base();
  129. *pSize = replay->m_DemoRecorder.m_DemoFile.m_pBuffer->TellPut();
  130. return true;
  131. }
  132. virtual void ResetReplayRecordBuffer()
  133. {
  134. replay->m_DemoRecorder.m_DemoFile.m_pBuffer->SeekPut( CUtlBuffer::SEEK_HEAD, 0 );
  135. }
  136. virtual bool IsDedicated()
  137. {
  138. return sv.IsDedicated();
  139. }
  140. virtual demoheader_t *GetReplayDemoHeader()
  141. {
  142. return &replay->m_DemoRecorder.m_DemoFile.m_DemoHeader;
  143. }
  144. virtual void RecalculateTags()
  145. {
  146. sv.RecalculateTags();
  147. }
  148. virtual bool NET_GetHostnameAsIP( const char *pHostname, char *pOut, int nOutSize )
  149. {
  150. netadr_t adr;
  151. if ( !NET_StringToAdr( pHostname, &adr ) )
  152. return false;
  153. V_strncpy( pOut, adr.ToString( true ), nOutSize );
  154. return true;
  155. }
  156. };
  157. //-----------------------------------------------------------------------------
  158. #if !defined( DEDICATED )
  159. class CEngineClientReplay : public IEngineClientReplay
  160. {
  161. public:
  162. virtual float GetLastServerTickTime()
  163. {
  164. return TIME_TO_TICKS( cl.m_flLastServerTickTime );
  165. }
  166. virtual const char *GetLevelName()
  167. {
  168. return cl.m_szLevelFileName;
  169. }
  170. virtual const char *GetLevelNameShort()
  171. {
  172. return cl.m_szLevelBaseName;
  173. }
  174. virtual int GetPlayerSlot()
  175. {
  176. return cl.m_nPlayerSlot;
  177. }
  178. virtual bool IsPlayingReplayDemo()
  179. {
  180. extern IDemoPlayer *g_pReplayDemoPlayer;
  181. return demoplayer == g_pReplayDemoPlayer &&
  182. demoplayer->IsPlayingBack();
  183. }
  184. virtual INetChannel *GetNetChannel()
  185. {
  186. return cl.m_NetChannel;
  187. }
  188. virtual bool IsConnected()
  189. {
  190. return cl.IsConnected();
  191. }
  192. virtual bool IsListenServer()
  193. {
  194. return sv.IsActive();
  195. }
  196. virtual IClientEntityList *GetClientEntityList()
  197. {
  198. extern IClientEntityList *entitylist;
  199. return entitylist;
  200. }
  201. virtual IClientReplay *GetClientReplayInt()
  202. {
  203. extern IClientReplay *g_pClientReplay;
  204. return g_pClientReplay;
  205. }
  206. virtual uint32 GetClientSteamID()
  207. {
  208. CSteamID steamID = Steam3Client().SteamUser()->GetSteamID();
  209. return steamID.GetAccountID();
  210. }
  211. void Con_NPrintf( int nPos, const char *pFormat, ... )
  212. {
  213. va_list argptr;
  214. char szText[4096];
  215. va_start ( argptr, pFormat );
  216. Q_vsnprintf( szText, sizeof( szText ), pFormat, argptr );
  217. va_end ( argptr );
  218. ::Con_NPrintf( nPos, "%s", szText );
  219. }
  220. virtual CGlobalVarsBase *GetClientGlobalVars()
  221. {
  222. return &g_ClientGlobalVariables;
  223. }
  224. virtual void VGui_PlaySound( const char *pSound )
  225. {
  226. ::VGui_PlaySound( pSound );
  227. }
  228. virtual void EngineVGui_ConfirmQuit()
  229. {
  230. EngineVGui()->ConfirmQuit();
  231. }
  232. virtual bool IsDemoPlayingBack()
  233. {
  234. return demoplayer->IsPlayingBack();
  235. }
  236. virtual int GetScreenWidth()
  237. {
  238. return videomode->GetModeStereoWidth();
  239. }
  240. virtual int GetScreenHeight()
  241. {
  242. return videomode->GetModeStereoHeight();
  243. }
  244. virtual bool IsGamePathValidAndSafeForDownload( const char *pGamePath )
  245. {
  246. return CL_IsGamePathValidAndSafeForDownload( pGamePath );
  247. }
  248. virtual bool IsInGame()
  249. {
  250. return cl.IsActive();
  251. }
  252. virtual void InitSoundRecord()
  253. {
  254. extern void SND_RecordInit();
  255. SND_RecordInit();
  256. }
  257. virtual void Wave_CreateTmpFile( const char *pFilename )
  258. {
  259. ::WaveCreateTmpFile( pFilename, SOUND_DMA_SPEED, 16, 2 );
  260. }
  261. virtual void Wave_AppendTmpFile( const char *pFilename, void *pBuffer, int nNumSamples )
  262. {
  263. ::WaveAppendTmpFile( pFilename, pBuffer, 16, nNumSamples );
  264. }
  265. virtual void Wave_FixupTmpFile( const char *pFilename )
  266. {
  267. ::WaveFixupTmpFile( pFilename );
  268. }
  269. };
  270. #endif // !defined( DEDICATED )
  271. //-----------------------------------------------------------------------------
  272. static CEngineReplay s_EngineReplay;
  273. IEngineReplay *g_pEngineReplay = &s_EngineReplay;
  274. EXPOSE_SINGLE_INTERFACE_GLOBALVAR( CEngineReplay, IEngineReplay, ENGINE_REPLAY_INTERFACE_VERSION, s_EngineReplay );
  275. #if !defined( DEDICATED )
  276. static CEngineClientReplay s_EngineClientReplay;
  277. IEngineClientReplay *g_pEngineClientReplay = &s_EngineClientReplay;
  278. EXPOSE_SINGLE_INTERFACE_GLOBALVAR( CEngineClientReplay, IEngineClientReplay, ENGINE_REPLAY_CLIENT_INTERFACE_VERSION, s_EngineClientReplay );
  279. #endif
  280. //-----------------------------------------------------------------------------
  281. #endif