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.

152 lines
3.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. //=======================================================================================//
  4. #include "shared_replaycontext.h"
  5. #include "replay/shared_defs.h"
  6. #include "replay/replayutils.h"
  7. #include "baserecordingsession.h"
  8. #include "baserecordingsessionblock.h"
  9. #include "baserecordingsessionmanager.h"
  10. #include "baserecordingsessionblockmanager.h"
  11. #include "thinkmanager.h"
  12. #include "filesystem.h"
  13. #include "errorsystem.h"
  14. #undef Yield
  15. #include "vstdlib/jobthread.h"
  16. // memdbgon must be the last include file in a .cpp file!!!
  17. #include "tier0/memdbgon.h"
  18. //----------------------------------------------------------------------------------------
  19. CSharedReplayContext::CSharedReplayContext( IReplayContext *pOwnerContext )
  20. : m_pOwnerContext( pOwnerContext ),
  21. m_pRecordingSessionManager( NULL ),
  22. m_pRecordingSessionBlockManager( NULL ),
  23. m_pErrorSystem( NULL ),
  24. m_pThreadPool( NULL ),
  25. m_bInit( false )
  26. {
  27. }
  28. CSharedReplayContext::~CSharedReplayContext()
  29. {
  30. delete m_pRecordingSessionManager;
  31. delete m_pRecordingSessionBlockManager;
  32. delete m_pErrorSystem;
  33. delete m_pThreadPool;
  34. }
  35. bool CSharedReplayContext::Init( CreateInterfaceFn fnFactory )
  36. {
  37. m_strRelativeBasePath.Format(
  38. "%s%c%s%c",
  39. SUBDIR_REPLAY,
  40. CORRECT_PATH_SEPARATOR,
  41. m_strSubDir.Get(),
  42. CORRECT_PATH_SEPARATOR
  43. );
  44. m_strBasePath.Format(
  45. "%s%c%s",
  46. g_pEngine->GetGameDir(),
  47. CORRECT_PATH_SEPARATOR,
  48. m_strRelativeBasePath.Get()
  49. );
  50. // Owning context should have initialized these by now
  51. // NOTE: Session manager init must come after block manager init since session manager
  52. // assumes all blocks have been loaded.
  53. //
  54. m_pRecordingSessionBlockManager->Init();
  55. m_pRecordingSessionManager->Init();
  56. if ( !InitThreadPool() )
  57. return false;
  58. m_bInit = true;
  59. return true;
  60. }
  61. bool CSharedReplayContext::InitThreadPool()
  62. {
  63. // Create thread pool
  64. Log( "Replay: Creating thread pool..." );
  65. IThreadPool *pThreadPool = CreateThreadPool();
  66. if ( !pThreadPool )
  67. {
  68. Log( "failed!\n" );
  69. return false;
  70. }
  71. Log( "succeeded.\n" );
  72. // Jon says: The client only really needs a single "ReplayContext" thread, so that the replay editor can write
  73. // data asynchronously. The game server does in fact require 4 threads, and can be configured to use more
  74. // via the replay_max_publish_threads convar.
  75. int nMaxThreads = 1;
  76. if ( g_pEngine->IsDedicated() )
  77. {
  78. // Use the convar for max threads on servers
  79. extern ConVar replay_max_publish_threads;
  80. nMaxThreads = replay_max_publish_threads.GetInt();
  81. }
  82. // Start thread pool
  83. Log( "Replay: Starting thread pool with %i threads...", nMaxThreads );
  84. if ( !pThreadPool->Start( ThreadPoolStartParams_t( true, nMaxThreads ), "ReplayContext" ) )
  85. {
  86. Log( "failed!\n" );
  87. return false;
  88. }
  89. Log( "succeeded.\n" );
  90. m_pThreadPool = pThreadPool;
  91. return true;
  92. }
  93. void CSharedReplayContext::Shutdown()
  94. {
  95. m_pRecordingSessionBlockManager->Shutdown();
  96. m_pRecordingSessionManager->Shutdown();
  97. m_pThreadPool->Stop();
  98. }
  99. void CSharedReplayContext::Think()
  100. {
  101. }
  102. const char *CSharedReplayContext::GetRelativeBaseDir() const
  103. {
  104. return m_strRelativeBasePath.Get();
  105. }
  106. const char *CSharedReplayContext::GetBaseDir() const
  107. {
  108. return m_strBasePath.Get();
  109. }
  110. const char *CSharedReplayContext::GetReplaySubDir() const
  111. {
  112. return m_strSubDir.Get();
  113. }
  114. void CSharedReplayContext::EnsureDirHierarchy()
  115. {
  116. g_pFullFileSystem->CreateDirHierarchy( m_strBasePath.Get() );
  117. }
  118. //----------------------------------------------------------------------------------------
  119. bool RunJobToCompletion( IThreadPool *pThreadPool, CJob *pJob )
  120. {
  121. pThreadPool->AddJob( pJob );
  122. pJob->WaitForFinish();
  123. return pJob->Executed();
  124. }
  125. //----------------------------------------------------------------------------------------