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.

306 lines
12 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: VCR mode records a client's game and allows you to
  4. // play it back and reproduce it exactly. When playing it back, nothing
  5. // is simulated on the server, but all server packets are recorded.
  6. //
  7. // Most of the VCR mode functionality is accomplished through hooks
  8. // called at various points in the engine.
  9. //
  10. // $NoKeywords: $
  11. //===========================================================================//
  12. #ifndef VCRMODE_H
  13. #define VCRMODE_H
  14. #ifdef _WIN32
  15. #include <process.h>
  16. #endif
  17. #ifdef _WIN32
  18. #pragma once
  19. #endif
  20. #include "tier0/platform.h"
  21. #include "tier0/vcr_shared.h"
  22. #include "tier0/dbg.h"
  23. #ifdef POSIX
  24. DBG_INTERFACE const char *BuildCmdLine( int argc, char **argv, bool fAddSteam = true );
  25. tchar *GetCommandLine();
  26. #endif
  27. #ifdef _X360
  28. #define NO_VCR 1
  29. #endif
  30. // Enclose lines of code in this if you don't want anything in them written to or read from the VCR file.
  31. #ifndef NO_VCR
  32. #define NOVCR(x) \
  33. {\
  34. VCRSetEnabled(0);\
  35. x;\
  36. VCRSetEnabled(1);\
  37. }
  38. #else
  39. #define NOVCR(x) \
  40. {\
  41. x;\
  42. }
  43. #endif
  44. //-----------------------------------------------------------------------------
  45. // Forward declarations
  46. //-----------------------------------------------------------------------------
  47. struct InputEvent_t;
  48. //-----------------------------------------------------------------------------
  49. // Definitions.
  50. //-----------------------------------------------------------------------------
  51. enum VCRMode_t
  52. {
  53. VCR_Invalid=-1,
  54. VCR_Disabled=0,
  55. VCR_Record,
  56. VCR_Playback
  57. };
  58. //-----------------------------------------------------------------------------
  59. // Functions.
  60. //-----------------------------------------------------------------------------
  61. abstract_class IVCRHelpers
  62. {
  63. public:
  64. virtual void ErrorMessage( const tchar *pMsg ) = 0;
  65. virtual void* GetMainWindow() = 0;
  66. };
  67. // Used by the vcrtrace program.
  68. abstract_class IVCRTrace
  69. {
  70. public:
  71. virtual VCREvent ReadEvent() = 0;
  72. virtual void Read( void *pDest, int size ) = 0;
  73. };
  74. typedef struct VCR_s
  75. {
  76. // Start VCR record or play.
  77. int (*Start)( tchar const *pFilename, bool bRecord, IVCRHelpers *pHelpers );
  78. void (*End)();
  79. // Used by the VCR trace app.
  80. IVCRTrace* (*GetVCRTraceInterface)();
  81. // Get the current mode the VCR is in.
  82. VCRMode_t (*GetMode)();
  83. // This can be used to block out areas of code that are unpredictable (like things triggered by WM_TIMER messages).
  84. // Note: this enables/disables VCR mode usage on a PER-THREAD basis. The assumption is that you're marking out
  85. // specific sections of code that you don't want to use VCR mode inside of, but you're not intending to
  86. // stop all the other threads from using VCR mode.
  87. void (*SetEnabled)(int bEnabled);
  88. // This can be called any time to put in a debug check to make sure things are synchronized.
  89. void (*SyncToken)(tchar const *pToken);
  90. // Hook for Sys_FloatTime().
  91. double (*Hook_Sys_FloatTime)(double time);
  92. // Note: this makes no guarantees about msg.hwnd being the same on playback. If it needs to be, then we need to add
  93. // an ID system for Windows and store the ID like in Goldsrc.
  94. int (*Hook_PeekMessage)(
  95. struct tagMSG *msg,
  96. void *hWnd,
  97. unsigned int wMsgFilterMin,
  98. unsigned int wMsgFilterMax,
  99. unsigned int wRemoveMsg
  100. );
  101. // Call this to record game messages.
  102. void (*Hook_RecordGameMsg)( const InputEvent_t &event );
  103. void (*Hook_RecordEndGameMsg)();
  104. // Call this to playback game messages until it returns false.
  105. bool (*Hook_PlaybackGameMsg)( InputEvent_t *pEvent );
  106. // Hook for recvfrom() calls. This replaces the recvfrom() call.
  107. int (*Hook_recvfrom)(int s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen);
  108. void (*Hook_GetCursorPos)(struct tagPOINT *pt);
  109. void (*Hook_ScreenToClient)(void *hWnd, struct tagPOINT *pt);
  110. void (*Hook_Cmd_Exec)(tchar **f);
  111. tchar* (*Hook_GetCommandLine)();
  112. // Registry hooks.
  113. long (*Hook_RegOpenKeyEx)( void *hKey, const tchar *lpSubKey, unsigned long ulOptions, unsigned long samDesired, void *pHKey );
  114. long (*Hook_RegSetValueEx)(void *hKey, tchar const *lpValueName, unsigned long Reserved, unsigned long dwType, uint8 const *lpData, unsigned long cbData);
  115. long (*Hook_RegQueryValueEx)(void *hKey, tchar const *lpValueName, unsigned long *lpReserved, unsigned long *lpType, uint8 *lpData, unsigned long *lpcbData);
  116. long (*Hook_RegCreateKeyEx)(void *hKey, tchar const *lpSubKey, unsigned long Reserved, tchar *lpClass, unsigned long dwOptions, unsigned long samDesired, void *lpSecurityAttributes, void *phkResult, unsigned long *lpdwDisposition);
  117. void (*Hook_RegCloseKey)(void *hKey);
  118. // hInput is a HANDLE.
  119. int (*Hook_GetNumberOfConsoleInputEvents)( void *hInput, unsigned long *pNumEvents );
  120. // hInput is a HANDLE.
  121. // pRecs is an INPUT_RECORD pointer.
  122. int (*Hook_ReadConsoleInput)( void *hInput, void *pRecs, int nMaxRecs, unsigned long *pNumRead );
  123. // This calls time() then gives you localtime()'s result.
  124. void (*Hook_LocalTime)( struct tm *today );
  125. short (*Hook_GetKeyState)( int nVirtKey );
  126. // TCP calls.
  127. int (*Hook_recv)( int s, char *buf, int len, int flags );
  128. int (*Hook_send)( int s, const char *buf, int len, int flags );
  129. // These can be used to add events without having to modify VCR mode.
  130. // pEventName is used for verification to make sure it's playing back correctly.
  131. // If pEventName is null, then verification is not performed.
  132. void (*GenericRecord)( const tchar *pEventName, const void *pData, int len );
  133. // Returns the number of bytes written in the generic event.
  134. // If bForceLenSame is true, then it will error out unless the value in the VCR file is the same as maxLen.
  135. int (*GenericPlayback)( const tchar *pEventName, void *pOutData, int maxLen, bool bForceLenSame );
  136. // If you just want to record and playback a value and not worry about whether or not you're
  137. // recording or playing back, use this. It also will do nothing if you're not recording or playing back.
  138. //
  139. // NOTE: also see GenericValueVerify, which allows you to have it VERIFY that pData's contents are the same upon playback
  140. // (rather than just copying whatever is in the VCR file into pData).
  141. void (*GenericValue)( const tchar *pEventName, void *pData, int maxLen );
  142. // Get the current percent (0.0 - 1.0) that it's played back through the file (only valid in playback).
  143. double (*GetPercentCompleted)();
  144. // If you use this, then any VCR stuff the thread does will work with VCR mode.
  145. // This mirrors the Windows API CreateThread function and returns a HANDLE the same way.
  146. void* (*Hook_CreateThread)(
  147. void *lpThreadAttributes,
  148. unsigned long dwStackSize,
  149. void *lpStartAddress,
  150. void *lpParameter,
  151. unsigned long dwCreationFlags,
  152. unsigned long *lpThreadID );
  153. unsigned long (*Hook_WaitForSingleObject)(
  154. void *handle,
  155. unsigned long dwMilliseconds );
  156. void (*Hook_EnterCriticalSection)( void *pCS );
  157. void (*Hook_Time)( long *pTime );
  158. // String value. Playback just verifies that the incoming string is the same as it was when recording.
  159. void (*GenericString)( const char *pEventName, const char *pString );
  160. // Works like GenericValue, except upon playback it will verify that pData's contents are the same as it was during recording.
  161. void (*GenericValueVerify)( const tchar *pEventName, const void *pData, int maxLen );
  162. unsigned long (*Hook_WaitForMultipleObjects)( uint32 nHandles, const void **pHandles, int bWaitAll, uint32 timeout );
  163. } VCR_t;
  164. #ifndef NO_VCR
  165. // In the launcher, this is created by vcrmode.c.
  166. // In the engine, this is set when the launcher initializes its DLL.
  167. PLATFORM_INTERFACE VCR_t *g_pVCR;
  168. #endif
  169. #ifndef NO_VCR
  170. #define VCRStart g_pVCR->Start
  171. #define VCREnd g_pVCR->End
  172. #define VCRGetVCRTraceInterface g_pVCR->GetVCRTraceInterface
  173. #define VCRGetMode g_pVCR->GetMode
  174. #define VCRSetEnabled g_pVCR->SetEnabled
  175. #define VCRSyncToken g_pVCR->SyncToken
  176. #define VCRGenericString g_pVCR->GenericString
  177. #define VCRGenericValueVerify g_pVCR->GenericValueVerify
  178. #define VCRHook_Sys_FloatTime g_pVCR->Hook_Sys_FloatTime
  179. #define VCRHook_PeekMessage g_pVCR->Hook_PeekMessage
  180. #define VCRHook_RecordGameMsg g_pVCR->Hook_RecordGameMsg
  181. #define VCRHook_RecordEndGameMsg g_pVCR->Hook_RecordEndGameMsg
  182. #define VCRHook_PlaybackGameMsg g_pVCR->Hook_PlaybackGameMsg
  183. #define VCRHook_recvfrom g_pVCR->Hook_recvfrom
  184. #define VCRHook_GetCursorPos g_pVCR->Hook_GetCursorPos
  185. #define VCRHook_ScreenToClient g_pVCR->Hook_ScreenToClient
  186. #define VCRHook_Cmd_Exec g_pVCR->Hook_Cmd_Exec
  187. #define VCRHook_GetCommandLine g_pVCR->Hook_GetCommandLine
  188. #define VCRHook_RegOpenKeyEx g_pVCR->Hook_RegOpenKeyEx
  189. #define VCRHook_RegSetValueEx g_pVCR->Hook_RegSetValueEx
  190. #define VCRHook_RegQueryValueEx g_pVCR->Hook_RegQueryValueEx
  191. #define VCRHook_RegCreateKeyEx g_pVCR->Hook_RegCreateKeyEx
  192. #define VCRHook_RegCloseKey g_pVCR->Hook_RegCloseKey
  193. #define VCRHook_GetNumberOfConsoleInputEvents g_pVCR->Hook_GetNumberOfConsoleInputEvents
  194. #define VCRHook_ReadConsoleInput g_pVCR->Hook_ReadConsoleInput
  195. #define VCRHook_LocalTime g_pVCR->Hook_LocalTime
  196. #define VCRHook_GetKeyState g_pVCR->Hook_GetKeyState
  197. #define VCRHook_recv g_pVCR->Hook_recv
  198. #define VCRHook_send g_pVCR->Hook_send
  199. #define VCRGenericRecord g_pVCR->GenericRecord
  200. #define VCRGenericPlayback g_pVCR->GenericPlayback
  201. #define VCRGenericValue g_pVCR->GenericValue
  202. #define VCRGetPercentCompleted g_pVCR->GetPercentCompleted
  203. #define VCRHook_CreateThread g_pVCR->Hook_CreateThread
  204. #define VCRHook_WaitForSingleObject g_pVCR->Hook_WaitForSingleObject
  205. #define VCRHook_EnterCriticalSection g_pVCR->Hook_EnterCriticalSection
  206. #define VCRHook_Time g_pVCR->Hook_Time
  207. #define VCRHook_WaitForMultipleObjects( a, b, c, d) g_pVCR->Hook_WaitForMultipleObjects( a, (const void **)b, c, d)
  208. #else
  209. #define VCRStart( a, b, c ) (1)
  210. #define VCREnd ((void)(0))
  211. #define VCRGetVCRTraceInterface (NULL)
  212. #define VCRGetMode() (VCR_Disabled)
  213. #define VCRSetEnabled( a ) ((void)(0))
  214. #define VCRSyncToken( a ) ((void)(0))
  215. #define VCRGenericRecord MUST_IFDEF_OUT_GenericRecord
  216. #define VCRGenericPlayback MUST_IFDEF_OUT_GenericPlayback
  217. #define VCRGenericValue MUST_IFDEF_OUT_GenericValue
  218. #define VCRGenericString MUST_IFDEF_OUT_GenericString
  219. #define VCRGenericValueVerify MUST_IFDEF_OUT_GenericValueVerify
  220. #define VCRGetPercentCompleted() (0.0f)
  221. #define VCRHook_Sys_FloatTime Sys_FloatTime
  222. #define VCRHook_PeekMessage PeekMessage
  223. #define VCRHook_RecordGameMsg RecordGameMsg
  224. #define VCRHook_RecordEndGameMsg RecordEndGameMsg
  225. #define VCRHook_PlaybackGameMsg PlaybackGameMsg
  226. #define VCRHook_recvfrom recvfrom
  227. #define VCRHook_GetCursorPos GetCursorPos
  228. #define VCRHook_ScreenToClient ScreenToClient
  229. #define VCRHook_Cmd_Exec( a ) ((void)(0))
  230. #define VCRHook_GetCommandLine GetCommandLine
  231. #define VCRHook_RegOpenKeyEx RegOpenKeyEx
  232. #define VCRHook_RegSetValueEx RegSetValueEx
  233. #define VCRHook_RegQueryValueEx RegQueryValueEx
  234. #define VCRHook_RegCreateKeyEx RegCreateKeyEx
  235. #define VCRHook_RegCloseKey RegCloseKey
  236. #define VCRHook_GetNumberOfConsoleInputEvents GetNumberOfConsoleInputEvents
  237. #define VCRHook_ReadConsoleInput ReadConsoleInput
  238. #define VCRHook_LocalTime( a ) memset(a, 0, sizeof(*a));
  239. #define VCRHook_GetKeyState GetKeyState
  240. #define VCRHook_recv recv
  241. #define VCRHook_send send
  242. #if defined( _X360 )
  243. #define VCRHook_CreateThread CreateThread
  244. #else
  245. #define VCRHook_CreateThread (void*)_beginthreadex
  246. #endif
  247. #define VCRHook_WaitForSingleObject WaitForSingleObject
  248. #define VCRHook_EnterCriticalSection EnterCriticalSection
  249. #define VCRHook_WaitForMultipleObjects( a, b, c, d) WaitForMultipleObjects( a, (const HANDLE *)b, c, d)
  250. #define VCRHook_Time Time
  251. #endif
  252. #endif // VCRMODE_H