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.

1253 lines
38 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Engine system for loading and managing tools
  4. //
  5. //=============================================================================
  6. #include "quakedef.h"
  7. #include "tier0/icommandline.h"
  8. #include "toolframework/itooldictionary.h"
  9. #include "toolframework/itoolsystem.h"
  10. #include "toolframework/itoolframework.h"
  11. #include "toolframework/iclientenginetools.h"
  12. #include "toolframework/iserverenginetools.h"
  13. #include "tier1/KeyValues.h"
  14. #include "tier1/utlvector.h"
  15. #include "tier1/tier1.h"
  16. #include "filesystem_engine.h"
  17. class IToolSystem;
  18. extern CreateInterfaceFn g_AppSystemFactory;
  19. typedef bool (*FnQuitHandler)( void *pvUserData );
  20. void EngineTool_InstallQuitHandler( void *pvUserData, FnQuitHandler func );
  21. //-----------------------------------------------------------------------------
  22. // Purpose: -tools loads framework
  23. //-----------------------------------------------------------------------------
  24. class CToolFrameworkInternal : public IToolFrameworkInternal
  25. {
  26. public:
  27. // Here's where the app systems get to learn about each other
  28. virtual bool Connect( CreateInterfaceFn factory );
  29. virtual void Disconnect();
  30. // Here's where systems can access other interfaces implemented by this object
  31. // Returns NULL if it doesn't implement the requested interface
  32. virtual void *QueryInterface( const char *pInterfaceName );
  33. // Init, shutdown
  34. virtual InitReturnVal_t Init();
  35. virtual void Shutdown();
  36. virtual bool CanQuit();
  37. public:
  38. // Level init, shutdown
  39. virtual void ClientLevelInitPreEntityAllTools();
  40. // entities are created / spawned / precached here
  41. virtual void ClientLevelInitPostEntityAllTools();
  42. virtual void ClientLevelShutdownPreEntityAllTools();
  43. // Entities are deleted / released here...
  44. virtual void ClientLevelShutdownPostEntityAllTools();
  45. virtual void ClientPreRenderAllTools();
  46. virtual void ClientPostRenderAllTools();
  47. virtual bool IsThirdPersonCamera();
  48. virtual bool IsToolRecording();
  49. // Level init, shutdown
  50. virtual void ServerLevelInitPreEntityAllTools();
  51. // entities are created / spawned / precached here
  52. virtual void ServerLevelInitPostEntityAllTools();
  53. virtual void ServerLevelShutdownPreEntityAllTools();
  54. // Entities are deleted / released here...
  55. virtual void ServerLevelShutdownPostEntityAllTools();
  56. // end of level shutdown
  57. // Called each frame before entities think
  58. virtual void ServerFrameUpdatePreEntityThinkAllTools();
  59. // called after entities think
  60. virtual void ServerFrameUpdatePostEntityThinkAllTools();
  61. virtual void ServerPreClientUpdateAllTools();
  62. const char* GetEntityData( const char *pActualEntityData );
  63. virtual void ServerPreSetupVisibilityAllTools();
  64. virtual bool PostInit();
  65. virtual bool ServerInit( CreateInterfaceFn serverFactory );
  66. virtual bool ClientInit( CreateInterfaceFn clientFactory );
  67. virtual void ServerShutdown();
  68. virtual void ClientShutdown();
  69. virtual void Think( bool finalTick );
  70. virtual void PostToolMessage( HTOOLHANDLE hEntity, KeyValues *msg );
  71. virtual void AdjustEngineViewport( int& x, int& y, int& width, int& height );
  72. virtual bool SetupEngineView( Vector &origin, QAngle &angles, float &fov );
  73. virtual bool SetupAudioState( AudioState_t &audioState );
  74. virtual int GetToolCount();
  75. virtual const char* GetToolName( int index );
  76. virtual void SwitchToTool( int index );
  77. virtual IToolSystem* SwitchToTool( const char* pToolName );
  78. virtual bool IsTopmostTool( const IToolSystem *sys );
  79. virtual const IToolSystem *GetToolSystem( int index ) const;
  80. virtual IToolSystem *GetTopmostTool();
  81. virtual void PostMessage( KeyValues *msg );
  82. virtual bool GetSoundSpatialization( int iUserData, int guid, SpatializationInfo_t& info );
  83. virtual void HostRunFrameBegin();
  84. virtual void HostRunFrameEnd();
  85. virtual void RenderFrameBegin();
  86. virtual void RenderFrameEnd();
  87. virtual void VGui_PreRenderAllTools( int paintMode );
  88. virtual void VGui_PostRenderAllTools( int paintMode );
  89. virtual void VGui_PreSimulateAllTools();
  90. virtual void VGui_PostSimulateAllTools();
  91. // Are we using tools?
  92. virtual bool InToolMode();
  93. // Should the game be allowed to render the world?
  94. virtual bool ShouldGameRenderView();
  95. virtual IMaterialProxy *LookupProxy( const char *proxyName );
  96. private:
  97. void LoadTools();
  98. void LoadToolsFromLibrary( const char *dllname );
  99. void InvokeMethod( ToolSystemFunc_t f );
  100. void InvokeMethodInt( ToolSystemFunc_Int_t f, int arg );
  101. void ShutdownTools();
  102. // Purpose: Shuts down all modules
  103. void ShutdownModules();
  104. // Purpose: Shuts down all tool dictionaries
  105. void ShutdownToolDictionaries();
  106. CUtlVector< IToolSystem * > m_ToolSystems;
  107. CUtlVector< IToolDictionary * > m_Dictionaries;
  108. CUtlVector< CSysModule * > m_Modules;
  109. int m_nActiveToolIndex;
  110. bool m_bInToolMode;
  111. };
  112. static CToolFrameworkInternal g_ToolFrameworkInternal;
  113. IToolFrameworkInternal *toolframework = &g_ToolFrameworkInternal;
  114. //-----------------------------------------------------------------------------
  115. // Purpose: Used to invoke a method of all added Game systems in order
  116. // Input : f - function to execute
  117. //-----------------------------------------------------------------------------
  118. void CToolFrameworkInternal::InvokeMethod( ToolSystemFunc_t f )
  119. {
  120. int toolCount = m_ToolSystems.Count();
  121. for ( int i = 0; i < toolCount; ++i )
  122. {
  123. IToolSystem *sys = m_ToolSystems[i];
  124. (sys->*f)();
  125. }
  126. }
  127. //-----------------------------------------------------------------------------
  128. // Purpose: Used to invoke a method of all added Game systems in order
  129. // Input : f - function to execute
  130. //-----------------------------------------------------------------------------
  131. void CToolFrameworkInternal::InvokeMethodInt( ToolSystemFunc_Int_t f, int arg )
  132. {
  133. int toolCount = m_ToolSystems.Count();
  134. for ( int i = 0; i < toolCount; ++i )
  135. {
  136. IToolSystem *sys = m_ToolSystems[i];
  137. (sys->*f)( arg );
  138. }
  139. }
  140. //-----------------------------------------------------------------------------
  141. // Purpose: Here's where the app systems get to learn about each other
  142. // Input : factory -
  143. // Output : Returns true on success, false on failure.
  144. //-----------------------------------------------------------------------------
  145. bool CToolFrameworkInternal::Connect( CreateInterfaceFn factory )
  146. {
  147. return true;
  148. }
  149. //-----------------------------------------------------------------------------
  150. // Purpose:
  151. // Input : -
  152. //-----------------------------------------------------------------------------
  153. void CToolFrameworkInternal::Disconnect()
  154. {
  155. }
  156. //-----------------------------------------------------------------------------
  157. // Purpose: Here's where systems can access other interfaces implemented by this object
  158. // Returns NULL if it doesn't implement the requested interface
  159. // Input : *pInterfaceName -
  160. //-----------------------------------------------------------------------------
  161. void *CToolFrameworkInternal::QueryInterface( const char *pInterfaceName )
  162. {
  163. return NULL;
  164. }
  165. //-----------------------------------------------------------------------------
  166. // Purpose:
  167. // Input : *pvUserData -
  168. // Output : static bool
  169. //-----------------------------------------------------------------------------
  170. static bool CToolFrameworkInternal_QuitHandler( void *pvUserData )
  171. {
  172. CToolFrameworkInternal *tfm = reinterpret_cast< CToolFrameworkInternal * >( pvUserData );
  173. if ( tfm )
  174. {
  175. return tfm->CanQuit();
  176. }
  177. return true;
  178. }
  179. //-----------------------------------------------------------------------------
  180. // Purpose: Init, shutdown
  181. // Input : -
  182. // Output : InitReturnVal_t
  183. //-----------------------------------------------------------------------------
  184. InitReturnVal_t CToolFrameworkInternal::Init()
  185. {
  186. m_bInToolMode = false;
  187. m_nActiveToolIndex = -1;
  188. // Disabled in REL for now
  189. #if 1
  190. #ifndef SWDS
  191. EngineTool_InstallQuitHandler( this, CToolFrameworkInternal_QuitHandler );
  192. // FIXME: Eventually this should be -edit
  193. if ( CommandLine()->FindParm( "-tools" ) )
  194. {
  195. LoadTools();
  196. }
  197. #endif
  198. #endif
  199. return INIT_OK;
  200. }
  201. //-----------------------------------------------------------------------------
  202. // Purpose: Called at end of Host_Init
  203. //-----------------------------------------------------------------------------
  204. bool CToolFrameworkInternal::PostInit()
  205. {
  206. bool bRetVal = true;
  207. int toolCount = m_ToolSystems.Count();
  208. for ( int i = 0; i < toolCount; ++i )
  209. {
  210. IToolSystem *system = m_ToolSystems[ i ];
  211. // FIXME: Should this really get access to a list if factories
  212. bool success = system->Init( );
  213. if ( !success )
  214. {
  215. bRetVal = false;
  216. }
  217. }
  218. // Activate first tool if we didn't encounter an error
  219. if ( bRetVal )
  220. {
  221. SwitchToTool( 0 );
  222. }
  223. return bRetVal;
  224. }
  225. //-----------------------------------------------------------------------------
  226. // Purpose:
  227. // Input : -
  228. //-----------------------------------------------------------------------------
  229. void CToolFrameworkInternal::Shutdown()
  230. {
  231. // Shut down all tools
  232. ShutdownTools();
  233. }
  234. //-----------------------------------------------------------------------------
  235. // Purpose:
  236. // Input : finalTick -
  237. //-----------------------------------------------------------------------------
  238. void CToolFrameworkInternal::Think( bool finalTick )
  239. {
  240. int toolCount = m_ToolSystems.Count();
  241. for ( int i = 0; i < toolCount; ++i )
  242. {
  243. IToolSystem *system = m_ToolSystems[ i ];
  244. system->Think( finalTick );
  245. }
  246. }
  247. //-----------------------------------------------------------------------------
  248. // Purpose:
  249. // Input : serverFactory -
  250. // Output : Returns true on success, false on failure.
  251. //-----------------------------------------------------------------------------
  252. bool CToolFrameworkInternal::ServerInit( CreateInterfaceFn serverFactory )
  253. {
  254. bool retval = true;
  255. int toolCount = m_ToolSystems.Count();
  256. for ( int i = 0; i < toolCount; ++i )
  257. {
  258. IToolSystem *system = m_ToolSystems[ i ];
  259. // FIXME: Should this really get access to a list if factories
  260. bool success = system->ServerInit( serverFactory );
  261. if ( !success )
  262. {
  263. retval = false;
  264. }
  265. }
  266. return retval;
  267. }
  268. //-----------------------------------------------------------------------------
  269. // Purpose:
  270. // Input : clientFactory -
  271. // Output : Returns true on success, false on failure.
  272. //-----------------------------------------------------------------------------
  273. bool CToolFrameworkInternal::ClientInit( CreateInterfaceFn clientFactory )
  274. {
  275. bool retval = true;
  276. int toolCount = m_ToolSystems.Count();
  277. for ( int i = 0; i < toolCount; ++i )
  278. {
  279. IToolSystem *system = m_ToolSystems[ i ];
  280. // FIXME: Should this really get access to a list if factories
  281. bool success = system->ClientInit( clientFactory );
  282. if ( !success )
  283. {
  284. retval = false;
  285. }
  286. }
  287. return retval;
  288. }
  289. //-----------------------------------------------------------------------------
  290. // Purpose:
  291. // Input : -
  292. //-----------------------------------------------------------------------------
  293. void CToolFrameworkInternal::ServerShutdown()
  294. {
  295. // Reverse order
  296. int toolCount = m_ToolSystems.Count();
  297. for ( int i = toolCount - 1; i >= 0; --i )
  298. {
  299. IToolSystem *system = m_ToolSystems[ i ];
  300. system->ServerShutdown();
  301. }
  302. }
  303. //-----------------------------------------------------------------------------
  304. // Purpose:
  305. // Input : -
  306. //-----------------------------------------------------------------------------
  307. void CToolFrameworkInternal::ClientShutdown()
  308. {
  309. // Reverse order
  310. int toolCount = m_ToolSystems.Count();
  311. for ( int i = toolCount - 1; i >= 0; --i )
  312. {
  313. IToolSystem *system = m_ToolSystems[ i ];
  314. system->ClientShutdown();
  315. }
  316. }
  317. //-----------------------------------------------------------------------------
  318. // Purpose:
  319. // Input : -
  320. // Output : Returns true on success, false on failure.
  321. //-----------------------------------------------------------------------------
  322. bool CToolFrameworkInternal::CanQuit()
  323. {
  324. int toolCount = m_ToolSystems.Count();
  325. for ( int i = 0; i < toolCount; ++i )
  326. {
  327. IToolSystem *system = m_ToolSystems[ i ];
  328. bool canquit = system->CanQuit();
  329. if ( !canquit )
  330. {
  331. return false;
  332. }
  333. }
  334. return true;
  335. }
  336. //-----------------------------------------------------------------------------
  337. // Purpose: Shuts down all modules
  338. //-----------------------------------------------------------------------------
  339. void CToolFrameworkInternal::ShutdownModules()
  340. {
  341. // Shutdown dictionaries
  342. int i;
  343. for ( i = m_Modules.Count(); --i >= 0; )
  344. {
  345. Sys_UnloadModule( m_Modules[i] );
  346. }
  347. m_Modules.RemoveAll();
  348. }
  349. //-----------------------------------------------------------------------------
  350. // Purpose: Shuts down all tool dictionaries
  351. //-----------------------------------------------------------------------------
  352. void CToolFrameworkInternal::ShutdownToolDictionaries()
  353. {
  354. // Shutdown dictionaries
  355. int i;
  356. for ( i = m_Dictionaries.Count(); --i >= 0; )
  357. {
  358. m_Dictionaries[i]->Shutdown();
  359. }
  360. for ( i = m_Dictionaries.Count(); --i >= 0; )
  361. {
  362. m_Dictionaries[i]->Disconnect();
  363. }
  364. m_Dictionaries.RemoveAll();
  365. }
  366. //-----------------------------------------------------------------------------
  367. // Purpose: Shuts down all tools
  368. // Input : -
  369. //-----------------------------------------------------------------------------
  370. void CToolFrameworkInternal::ShutdownTools()
  371. {
  372. // Deactivate tool
  373. SwitchToTool( -1 );
  374. // Reverse order
  375. int i;
  376. int toolCount = m_ToolSystems.Count();
  377. for ( i = toolCount - 1; i >= 0; --i )
  378. {
  379. IToolSystem *system = m_ToolSystems[ i ];
  380. system->Shutdown();
  381. }
  382. m_ToolSystems.RemoveAll();
  383. ShutdownToolDictionaries();
  384. ShutdownModules();
  385. }
  386. //-----------------------------------------------------------------------------
  387. // Purpose: Adds tool from specified library
  388. // Input : *dllname -
  389. //-----------------------------------------------------------------------------
  390. void CToolFrameworkInternal::LoadToolsFromLibrary( const char *dllname )
  391. {
  392. CSysModule *module = Sys_LoadModule( dllname );
  393. if ( !module )
  394. {
  395. Warning( "CToolFrameworkInternal::LoadToolsFromLibrary: Unable to load '%s'\n", dllname );
  396. return;
  397. }
  398. CreateInterfaceFn factory = Sys_GetFactory( module );
  399. if ( !factory )
  400. {
  401. Sys_UnloadModule( module );
  402. Warning( "CToolFrameworkInternal::LoadToolsFromLibrary: Dll '%s' has no factory\n", dllname );
  403. return;
  404. }
  405. IToolDictionary *dictionary = ( IToolDictionary * )factory( VTOOLDICTIONARY_INTERFACE_VERSION, NULL );
  406. if ( !dictionary )
  407. {
  408. Sys_UnloadModule( module );
  409. Warning( "CToolFrameworkInternal::LoadToolsFromLibrary: Dll '%s' doesn't support '%s'\n", dllname, VTOOLDICTIONARY_INTERFACE_VERSION );
  410. return;
  411. }
  412. if ( !dictionary->Connect( g_AppSystemFactory ) )
  413. {
  414. Sys_UnloadModule( module );
  415. Warning( "CToolFrameworkInternal::LoadToolsFromLibrary: Dll '%s' connection phase failed.\n", dllname );
  416. return;
  417. }
  418. if ( dictionary->Init( ) != INIT_OK )
  419. {
  420. Sys_UnloadModule( module );
  421. Warning( "CToolFrameworkInternal::LoadToolsFromLibrary: Dll '%s' initialization phase failed.\n", dllname );
  422. return;
  423. }
  424. dictionary->CreateTools();
  425. int toolCount = dictionary->GetToolCount();
  426. for ( int i = 0; i < toolCount; ++i )
  427. {
  428. IToolSystem *tool = dictionary->GetTool( i );
  429. if ( tool )
  430. {
  431. Msg( "Loaded tool '%s'\n", tool->GetToolName() );
  432. m_ToolSystems.AddToTail( tool );
  433. }
  434. }
  435. m_Dictionaries.AddToTail( dictionary );
  436. m_Modules.AddToTail( module );
  437. }
  438. //-----------------------------------------------------------------------------
  439. // Are we using tools?
  440. //-----------------------------------------------------------------------------
  441. bool CToolFrameworkInternal::InToolMode()
  442. {
  443. return m_bInToolMode;
  444. }
  445. //-----------------------------------------------------------------------------
  446. // Should the game be allowed to render the world?
  447. //-----------------------------------------------------------------------------
  448. bool CToolFrameworkInternal::ShouldGameRenderView()
  449. {
  450. if ( m_nActiveToolIndex >= 0 )
  451. {
  452. IToolSystem *tool = m_ToolSystems[ m_nActiveToolIndex ];
  453. Assert( tool );
  454. return tool->ShouldGameRenderView( );
  455. }
  456. return true;
  457. }
  458. IMaterialProxy *CToolFrameworkInternal::LookupProxy( const char *proxyName )
  459. {
  460. int toolCount = GetToolCount();
  461. for ( int i = 0; i < toolCount; ++i )
  462. {
  463. IToolSystem *tool = m_ToolSystems[ i ];
  464. Assert( tool );
  465. IMaterialProxy *matProxy = tool->LookupProxy( proxyName );
  466. if ( matProxy )
  467. {
  468. return matProxy;
  469. }
  470. }
  471. return NULL;
  472. }
  473. //-----------------------------------------------------------------------------
  474. // Purpose: FIXME: Should scan a KeyValues file
  475. // Input : -
  476. //-----------------------------------------------------------------------------
  477. void CToolFrameworkInternal::LoadTools()
  478. {
  479. m_bInToolMode = true;
  480. // Load rootdir/bin/enginetools.txt
  481. KeyValues *kv = new KeyValues( "enginetools" );
  482. Assert( kv );
  483. // We don't ship enginetools.txt to Steam public, so we'll need to load sdkenginetools.txt if enginetools.txt isn't present
  484. bool bLoadSDKFile = !g_pFileSystem->FileExists( "enginetools.txt", "EXECUTABLE_PATH" );
  485. if ( kv && kv->LoadFromFile( g_pFileSystem, bLoadSDKFile ? "sdkenginetools.txt" : "enginetools.txt", "EXECUTABLE_PATH" ) )
  486. {
  487. for ( KeyValues *tool = kv->GetFirstSubKey();
  488. tool != NULL;
  489. tool = tool->GetNextKey() )
  490. {
  491. if ( !Q_stricmp( tool->GetName(), "library" ) )
  492. {
  493. // CHECK both bin/tools and gamedir/bin/tools
  494. LoadToolsFromLibrary( tool->GetString() );
  495. }
  496. }
  497. kv->deleteThis();
  498. }
  499. }
  500. //-----------------------------------------------------------------------------
  501. // Purpose: Level init, shutdown
  502. // Input : -
  503. //-----------------------------------------------------------------------------
  504. void CToolFrameworkInternal::ClientLevelInitPreEntityAllTools()
  505. {
  506. InvokeMethod( &IToolSystem::ClientLevelInitPreEntity );
  507. }
  508. //-----------------------------------------------------------------------------
  509. // Purpose:
  510. // Input : -
  511. //-----------------------------------------------------------------------------
  512. void CToolFrameworkInternal::ClientLevelInitPostEntityAllTools()
  513. {
  514. InvokeMethod( &IToolSystem::ClientLevelInitPostEntity );
  515. }
  516. //-----------------------------------------------------------------------------
  517. // Purpose:
  518. // Input : -
  519. //-----------------------------------------------------------------------------
  520. void CToolFrameworkInternal::ClientLevelShutdownPreEntityAllTools()
  521. {
  522. InvokeMethod( &IToolSystem::ClientLevelShutdownPreEntity );
  523. }
  524. //-----------------------------------------------------------------------------
  525. // Purpose: Entities are deleted / released here...
  526. //-----------------------------------------------------------------------------
  527. void CToolFrameworkInternal::ClientLevelShutdownPostEntityAllTools()
  528. {
  529. InvokeMethod( &IToolSystem::ClientLevelShutdownPostEntity );
  530. }
  531. //-----------------------------------------------------------------------------
  532. // Purpose:
  533. //-----------------------------------------------------------------------------
  534. void CToolFrameworkInternal::ClientPreRenderAllTools()
  535. {
  536. InvokeMethod( &IToolSystem::ClientPreRender );
  537. }
  538. //-----------------------------------------------------------------------------
  539. // Purpose:
  540. //-----------------------------------------------------------------------------
  541. bool CToolFrameworkInternal::IsThirdPersonCamera()
  542. {
  543. if ( m_nActiveToolIndex >= 0 )
  544. {
  545. IToolSystem *tool = m_ToolSystems[ m_nActiveToolIndex ];
  546. Assert( tool );
  547. return tool->IsThirdPersonCamera( );
  548. }
  549. return false;
  550. }
  551. // is the current tool recording?
  552. bool CToolFrameworkInternal::IsToolRecording()
  553. {
  554. if ( m_nActiveToolIndex >= 0 )
  555. {
  556. IToolSystem *tool = m_ToolSystems[ m_nActiveToolIndex ];
  557. Assert( tool );
  558. return tool->IsToolRecording( );
  559. }
  560. return false;
  561. }
  562. //-----------------------------------------------------------------------------
  563. // Purpose:
  564. //-----------------------------------------------------------------------------
  565. void CToolFrameworkInternal::ClientPostRenderAllTools()
  566. {
  567. InvokeMethod( &IToolSystem::ClientPostRender );
  568. }
  569. //-----------------------------------------------------------------------------
  570. // Purpose: Level init, shutdown
  571. //-----------------------------------------------------------------------------
  572. void CToolFrameworkInternal::ServerLevelInitPreEntityAllTools()
  573. {
  574. InvokeMethod( &IToolSystem::ServerLevelInitPreEntity );
  575. }
  576. //-----------------------------------------------------------------------------
  577. // Purpose: entities are created / spawned / precached here
  578. // Input : -
  579. //-----------------------------------------------------------------------------
  580. void CToolFrameworkInternal::ServerLevelInitPostEntityAllTools()
  581. {
  582. InvokeMethod( &IToolSystem::ServerLevelInitPostEntity );
  583. }
  584. //-----------------------------------------------------------------------------
  585. // Purpose:
  586. // Input : -
  587. //-----------------------------------------------------------------------------
  588. void CToolFrameworkInternal::ServerLevelShutdownPreEntityAllTools()
  589. {
  590. InvokeMethod( &IToolSystem::ServerLevelShutdownPreEntity );
  591. }
  592. //-----------------------------------------------------------------------------
  593. // Purpose: Entities are deleted / released here...
  594. // Input : -
  595. //-----------------------------------------------------------------------------
  596. void CToolFrameworkInternal::ServerLevelShutdownPostEntityAllTools()
  597. {
  598. InvokeMethod( &IToolSystem::ServerLevelShutdownPostEntity );
  599. }
  600. //-----------------------------------------------------------------------------
  601. // Purpose: Called each frame before entities think
  602. // Input : -
  603. //-----------------------------------------------------------------------------
  604. void CToolFrameworkInternal::ServerFrameUpdatePreEntityThinkAllTools()
  605. {
  606. InvokeMethod( &IToolSystem::ServerFrameUpdatePreEntityThink );
  607. }
  608. //-----------------------------------------------------------------------------
  609. // Purpose: Called after entities think
  610. // Input : -
  611. //-----------------------------------------------------------------------------
  612. void CToolFrameworkInternal::ServerFrameUpdatePostEntityThinkAllTools()
  613. {
  614. InvokeMethod( &IToolSystem::ServerFrameUpdatePostEntityThink );
  615. }
  616. //-----------------------------------------------------------------------------
  617. // Purpose: Called before client networking occurs on the server
  618. // Input : -
  619. //-----------------------------------------------------------------------------
  620. void CToolFrameworkInternal::ServerPreClientUpdateAllTools()
  621. {
  622. InvokeMethod( &IToolSystem::ServerPreClientUpdate );
  623. }
  624. //-----------------------------------------------------------------------------
  625. // The server uses this to call into the tools to get the actual
  626. // entities to spawn on startup
  627. //-----------------------------------------------------------------------------
  628. const char* CToolFrameworkInternal::GetEntityData( const char *pActualEntityData )
  629. {
  630. if ( m_nActiveToolIndex >= 0 )
  631. {
  632. IToolSystem *tool = m_ToolSystems[ m_nActiveToolIndex ];
  633. Assert( tool );
  634. return tool->GetEntityData( pActualEntityData );
  635. }
  636. return pActualEntityData;
  637. }
  638. void CToolFrameworkInternal::ServerPreSetupVisibilityAllTools()
  639. {
  640. InvokeMethod( &IToolSystem::ServerPreSetupVisibility );
  641. }
  642. //-----------------------------------------------------------------------------
  643. // Purpose: Post a message to tools
  644. // Input : hEntity -
  645. // *msg -
  646. //-----------------------------------------------------------------------------
  647. void CToolFrameworkInternal::PostToolMessage( HTOOLHANDLE hEntity, KeyValues *msg )
  648. {
  649. // FIXME: Only message topmost tool?
  650. int toolCount = m_ToolSystems.Count();
  651. for ( int i = 0; i < toolCount; ++i )
  652. {
  653. IToolSystem *tool = m_ToolSystems[ i ];
  654. Assert( tool );
  655. tool->PostMessage( hEntity, msg );
  656. }
  657. }
  658. //-----------------------------------------------------------------------------
  659. // Purpose: Only active tool gets to adjust viewport
  660. // Input : x -
  661. // y -
  662. // width -
  663. // height -
  664. //-----------------------------------------------------------------------------
  665. void CToolFrameworkInternal::AdjustEngineViewport( int& x, int& y, int& width, int& height )
  666. {
  667. if ( m_nActiveToolIndex >= 0 )
  668. {
  669. IToolSystem *tool = m_ToolSystems[ m_nActiveToolIndex ];
  670. Assert( tool );
  671. tool->AdjustEngineViewport( x, y, width, height );
  672. }
  673. }
  674. //-----------------------------------------------------------------------------
  675. // Purpose: Only active tool gets to set the camera/view
  676. //-----------------------------------------------------------------------------
  677. bool CToolFrameworkInternal::SetupEngineView( Vector &origin, QAngle &angles, float &fov )
  678. {
  679. if ( m_nActiveToolIndex < 0 )
  680. return false;
  681. IToolSystem *tool = m_ToolSystems[ m_nActiveToolIndex ];
  682. Assert( tool );
  683. return tool->SetupEngineView( origin, angles, fov );
  684. }
  685. //-----------------------------------------------------------------------------
  686. // Purpose: Only active tool gets to set the microphone
  687. //-----------------------------------------------------------------------------
  688. bool CToolFrameworkInternal::SetupAudioState( AudioState_t &audioState )
  689. {
  690. if ( m_nActiveToolIndex < 0 )
  691. return false;
  692. IToolSystem *tool = m_ToolSystems[ m_nActiveToolIndex ];
  693. Assert( tool );
  694. return tool->SetupAudioState( audioState );
  695. }
  696. //-----------------------------------------------------------------------------
  697. // Purpose:
  698. // Input : -
  699. //-----------------------------------------------------------------------------
  700. void CToolFrameworkInternal::VGui_PreRenderAllTools( int paintMode )
  701. {
  702. InvokeMethodInt( &IToolSystem::VGui_PreRender, paintMode );
  703. }
  704. //-----------------------------------------------------------------------------
  705. // Purpose:
  706. // Input : -
  707. //-----------------------------------------------------------------------------
  708. void CToolFrameworkInternal::VGui_PostRenderAllTools( int paintMode )
  709. {
  710. InvokeMethodInt( &IToolSystem::VGui_PostRender, paintMode );
  711. }
  712. void CToolFrameworkInternal::VGui_PreSimulateAllTools()
  713. {
  714. InvokeMethod( &IToolSystem::VGui_PreSimulate );
  715. }
  716. void CToolFrameworkInternal::VGui_PostSimulateAllTools()
  717. {
  718. InvokeMethod( &IToolSystem::VGui_PostSimulate );
  719. }
  720. //-----------------------------------------------------------------------------
  721. // Purpose:
  722. // Input : -
  723. // Output : int
  724. //-----------------------------------------------------------------------------
  725. int CToolFrameworkInternal::GetToolCount()
  726. {
  727. return m_ToolSystems.Count();
  728. }
  729. //-----------------------------------------------------------------------------
  730. // Purpose:
  731. // Input : index -
  732. // Output : const char
  733. //-----------------------------------------------------------------------------
  734. const char *CToolFrameworkInternal::GetToolName( int index )
  735. {
  736. if ( index < 0 || index >= m_ToolSystems.Count() )
  737. {
  738. return "";
  739. }
  740. IToolSystem *sys = m_ToolSystems[ index ];
  741. if ( sys )
  742. {
  743. return sys->GetToolName();
  744. }
  745. return "";
  746. }
  747. //-----------------------------------------------------------------------------
  748. // Purpose:
  749. // Input : index -
  750. //-----------------------------------------------------------------------------
  751. void CToolFrameworkInternal::SwitchToTool( int index )
  752. {
  753. if ( ( m_ToolSystems.Count() < 1 ) || ( index >= m_ToolSystems.Count() ) )
  754. return;
  755. if ( index != m_nActiveToolIndex )
  756. {
  757. if ( m_nActiveToolIndex >= 0 )
  758. {
  759. IToolSystem *pOldTool = m_ToolSystems[ m_nActiveToolIndex ];
  760. pOldTool->OnToolDeactivate();
  761. }
  762. m_nActiveToolIndex = index;
  763. if ( m_nActiveToolIndex >= 0 )
  764. {
  765. IToolSystem *pNewTool = m_ToolSystems[ m_nActiveToolIndex ];
  766. pNewTool->OnToolActivate();
  767. }
  768. }
  769. }
  770. //-----------------------------------------------------------------------------
  771. // Switches to a named tool
  772. //-----------------------------------------------------------------------------
  773. IToolSystem *CToolFrameworkInternal::SwitchToTool( const char* pToolName )
  774. {
  775. int nCount = GetToolCount();
  776. for ( int i = 0; i < nCount; ++i )
  777. {
  778. if ( !Q_stricmp( pToolName, GetToolName(i) ) )
  779. {
  780. SwitchToTool( i );
  781. return m_ToolSystems[i];
  782. }
  783. }
  784. return NULL;
  785. }
  786. //-----------------------------------------------------------------------------
  787. // Purpose:
  788. // Input : *sys -
  789. // Output : Returns true on success, false on failure.
  790. //-----------------------------------------------------------------------------
  791. bool CToolFrameworkInternal::IsTopmostTool( const IToolSystem *sys )
  792. {
  793. if ( m_ToolSystems.Count() <= 0 || ( m_nActiveToolIndex < 0 ) )
  794. return false;
  795. return ( m_ToolSystems[ m_nActiveToolIndex ] == sys );
  796. }
  797. IToolSystem *CToolFrameworkInternal::GetTopmostTool()
  798. {
  799. return m_nActiveToolIndex >= 0 ? m_ToolSystems[ m_nActiveToolIndex ] : NULL;
  800. }
  801. //-----------------------------------------------------------------------------
  802. // returns a tool system by index
  803. //-----------------------------------------------------------------------------
  804. const IToolSystem *CToolFrameworkInternal::GetToolSystem( int index ) const
  805. {
  806. if ( ( index < 0 ) || ( index >= m_ToolSystems.Count() ) )
  807. return NULL;
  808. return m_ToolSystems[index];
  809. }
  810. void CToolFrameworkInternal::PostMessage( KeyValues *msg )
  811. {
  812. if ( m_nActiveToolIndex >= 0 )
  813. {
  814. IToolSystem *tool = m_ToolSystems[ m_nActiveToolIndex ];
  815. Assert( tool );
  816. tool->PostMessage( 0, msg );
  817. }
  818. }
  819. bool CToolFrameworkInternal::GetSoundSpatialization( int iUserData, int guid, SpatializationInfo_t& info )
  820. {
  821. if ( m_nActiveToolIndex >= 0 )
  822. {
  823. IToolSystem *tool = m_ToolSystems[ m_nActiveToolIndex ];
  824. Assert( tool );
  825. return tool->GetSoundSpatialization( iUserData, guid, info );
  826. }
  827. return true;
  828. }
  829. void CToolFrameworkInternal::HostRunFrameBegin()
  830. {
  831. InvokeMethod( &IToolSystem::HostRunFrameBegin );
  832. }
  833. void CToolFrameworkInternal::HostRunFrameEnd()
  834. {
  835. InvokeMethod( &IToolSystem::HostRunFrameEnd );
  836. }
  837. //-----------------------------------------------------------------------------
  838. // Purpose:
  839. // Input : -
  840. //-----------------------------------------------------------------------------
  841. void CToolFrameworkInternal::RenderFrameBegin()
  842. {
  843. InvokeMethod( &IToolSystem::RenderFrameBegin );
  844. }
  845. //-----------------------------------------------------------------------------
  846. // Purpose:
  847. // Input : -
  848. //-----------------------------------------------------------------------------
  849. void CToolFrameworkInternal::RenderFrameEnd()
  850. {
  851. InvokeMethod( &IToolSystem::RenderFrameEnd );
  852. }
  853. // Exposed because it's an IAppSystem
  854. EXPOSE_SINGLE_INTERFACE_GLOBALVAR( CToolFrameworkInternal, IToolFrameworkInternal, VTOOLFRAMEWORK_INTERFACE_VERSION, g_ToolFrameworkInternal );
  855. //-----------------------------------------------------------------------------
  856. // Purpose: exposed from engine to client .dll
  857. //-----------------------------------------------------------------------------
  858. class CClientEngineTools : public IClientEngineTools
  859. {
  860. public:
  861. virtual void LevelInitPreEntityAllTools();
  862. virtual void LevelInitPostEntityAllTools();
  863. virtual void LevelShutdownPreEntityAllTools();
  864. virtual void LevelShutdownPostEntityAllTools();
  865. virtual void PreRenderAllTools();
  866. virtual void PostRenderAllTools();
  867. virtual void PostToolMessage( HTOOLHANDLE hEntity, KeyValues *msg );
  868. virtual void AdjustEngineViewport( int& x, int& y, int& width, int& height );
  869. virtual bool SetupEngineView( Vector &origin, QAngle &angles, float &fov );
  870. virtual bool SetupAudioState( AudioState_t &audioState );
  871. virtual void VGui_PreRenderAllTools( int paintMode );
  872. virtual void VGui_PostRenderAllTools( int paintMode );
  873. virtual bool IsThirdPersonCamera( );
  874. virtual bool InToolMode();
  875. };
  876. EXPOSE_SINGLE_INTERFACE( CClientEngineTools, IClientEngineTools, VCLIENTENGINETOOLS_INTERFACE_VERSION );
  877. //-----------------------------------------------------------------------------
  878. // Purpose:
  879. // Input : -
  880. //-----------------------------------------------------------------------------
  881. void CClientEngineTools::LevelInitPreEntityAllTools()
  882. {
  883. g_ToolFrameworkInternal.ClientLevelInitPreEntityAllTools();
  884. }
  885. //-----------------------------------------------------------------------------
  886. // Purpose:
  887. // Input : -
  888. //-----------------------------------------------------------------------------
  889. void CClientEngineTools::LevelInitPostEntityAllTools()
  890. {
  891. g_ToolFrameworkInternal.ClientLevelInitPostEntityAllTools();
  892. }
  893. //-----------------------------------------------------------------------------
  894. // Purpose:
  895. // Input : -
  896. //-----------------------------------------------------------------------------
  897. void CClientEngineTools::LevelShutdownPreEntityAllTools()
  898. {
  899. g_ToolFrameworkInternal.ClientLevelShutdownPreEntityAllTools();
  900. }
  901. //-----------------------------------------------------------------------------
  902. // Purpose:
  903. // Input : -
  904. //-----------------------------------------------------------------------------
  905. void CClientEngineTools::LevelShutdownPostEntityAllTools()
  906. {
  907. g_ToolFrameworkInternal.ClientLevelShutdownPostEntityAllTools();
  908. }
  909. //-----------------------------------------------------------------------------
  910. // Purpose:
  911. // Input : -
  912. //-----------------------------------------------------------------------------
  913. void CClientEngineTools::PreRenderAllTools()
  914. {
  915. g_ToolFrameworkInternal.ClientPreRenderAllTools();
  916. }
  917. //-----------------------------------------------------------------------------
  918. // Purpose:
  919. // Input : -
  920. //-----------------------------------------------------------------------------
  921. void CClientEngineTools::PostRenderAllTools()
  922. {
  923. g_ToolFrameworkInternal.ClientPostRenderAllTools();
  924. }
  925. //-----------------------------------------------------------------------------
  926. // Purpose:
  927. // Input : hEntity -
  928. // *msg -
  929. //-----------------------------------------------------------------------------
  930. void CClientEngineTools::PostToolMessage( HTOOLHANDLE hEntity, KeyValues *msg )
  931. {
  932. g_ToolFrameworkInternal.PostToolMessage( hEntity, msg );
  933. }
  934. //-----------------------------------------------------------------------------
  935. // Purpose:
  936. // Input : x -
  937. // y -
  938. // width -
  939. // height -
  940. //-----------------------------------------------------------------------------
  941. void CClientEngineTools::AdjustEngineViewport( int& x, int& y, int& width, int& height )
  942. {
  943. g_ToolFrameworkInternal.AdjustEngineViewport( x, y, width, height );
  944. }
  945. bool CClientEngineTools::SetupEngineView( Vector &origin, QAngle &angles, float &fov )
  946. {
  947. return g_ToolFrameworkInternal.SetupEngineView( origin, angles, fov );
  948. }
  949. bool CClientEngineTools::SetupAudioState( AudioState_t &audioState )
  950. {
  951. return g_ToolFrameworkInternal.SetupAudioState( audioState );
  952. }
  953. //-----------------------------------------------------------------------------
  954. // Purpose:
  955. //-----------------------------------------------------------------------------
  956. void CClientEngineTools::VGui_PreRenderAllTools( int paintMode )
  957. {
  958. g_ToolFrameworkInternal.VGui_PreRenderAllTools( paintMode );
  959. }
  960. //-----------------------------------------------------------------------------
  961. // Purpose:
  962. //-----------------------------------------------------------------------------
  963. void CClientEngineTools::VGui_PostRenderAllTools( int paintMode )
  964. {
  965. g_ToolFrameworkInternal.VGui_PostRenderAllTools( paintMode );
  966. }
  967. //-----------------------------------------------------------------------------
  968. // Purpose:
  969. //-----------------------------------------------------------------------------
  970. bool CClientEngineTools::IsThirdPersonCamera( )
  971. {
  972. return g_ToolFrameworkInternal.IsThirdPersonCamera( );
  973. }
  974. bool CClientEngineTools::InToolMode()
  975. {
  976. return g_ToolFrameworkInternal.InToolMode();
  977. }
  978. //-----------------------------------------------------------------------------
  979. // Purpose: Exposed to server.dll
  980. //-----------------------------------------------------------------------------
  981. class CServerEngineTools : public IServerEngineTools
  982. {
  983. public:
  984. // Inherited from IServerEngineTools
  985. virtual void LevelInitPreEntityAllTools();
  986. virtual void LevelInitPostEntityAllTools();
  987. virtual void LevelShutdownPreEntityAllTools();
  988. virtual void LevelShutdownPostEntityAllTools();
  989. virtual void FrameUpdatePreEntityThinkAllTools();
  990. virtual void FrameUpdatePostEntityThinkAllTools();
  991. virtual void PreClientUpdateAllTools();
  992. virtual void PreSetupVisibilityAllTools();
  993. virtual const char* GetEntityData( const char *pActualEntityData );
  994. virtual bool InToolMode();
  995. };
  996. EXPOSE_SINGLE_INTERFACE( CServerEngineTools, IServerEngineTools, VSERVERENGINETOOLS_INTERFACE_VERSION );
  997. //-----------------------------------------------------------------------------
  998. // Purpose:
  999. // Input : -
  1000. //-----------------------------------------------------------------------------
  1001. void CServerEngineTools::LevelInitPreEntityAllTools()
  1002. {
  1003. g_ToolFrameworkInternal.ServerLevelInitPreEntityAllTools();
  1004. }
  1005. //-----------------------------------------------------------------------------
  1006. // Purpose:
  1007. // Input : -
  1008. //-----------------------------------------------------------------------------
  1009. void CServerEngineTools::LevelInitPostEntityAllTools()
  1010. {
  1011. g_ToolFrameworkInternal.ServerLevelInitPostEntityAllTools();
  1012. }
  1013. //-----------------------------------------------------------------------------
  1014. // Purpose:
  1015. // Input : -
  1016. //-----------------------------------------------------------------------------
  1017. void CServerEngineTools::LevelShutdownPreEntityAllTools()
  1018. {
  1019. g_ToolFrameworkInternal.ServerLevelShutdownPreEntityAllTools();
  1020. }
  1021. //-----------------------------------------------------------------------------
  1022. // Purpose:
  1023. // Input : -
  1024. //-----------------------------------------------------------------------------
  1025. void CServerEngineTools::LevelShutdownPostEntityAllTools()
  1026. {
  1027. g_ToolFrameworkInternal.ServerLevelShutdownPostEntityAllTools();
  1028. }
  1029. //-----------------------------------------------------------------------------
  1030. // Purpose:
  1031. // Input : -
  1032. //-----------------------------------------------------------------------------
  1033. void CServerEngineTools::FrameUpdatePreEntityThinkAllTools()
  1034. {
  1035. g_ToolFrameworkInternal.ServerFrameUpdatePreEntityThinkAllTools();
  1036. }
  1037. //-----------------------------------------------------------------------------
  1038. // Purpose:
  1039. // Input : -
  1040. //-----------------------------------------------------------------------------
  1041. void CServerEngineTools::FrameUpdatePostEntityThinkAllTools()
  1042. {
  1043. g_ToolFrameworkInternal.ServerFrameUpdatePostEntityThinkAllTools();
  1044. }
  1045. //-----------------------------------------------------------------------------
  1046. // Purpose:
  1047. // Input : -
  1048. //-----------------------------------------------------------------------------
  1049. void CServerEngineTools::PreClientUpdateAllTools()
  1050. {
  1051. g_ToolFrameworkInternal.ServerPreClientUpdateAllTools();
  1052. }
  1053. //-----------------------------------------------------------------------------
  1054. // The server uses this to call into the tools to get the actual
  1055. // entities to spawn on startup
  1056. //-----------------------------------------------------------------------------
  1057. const char* CServerEngineTools::GetEntityData( const char *pActualEntityData )
  1058. {
  1059. return g_ToolFrameworkInternal.GetEntityData( pActualEntityData );
  1060. }
  1061. void CServerEngineTools::PreSetupVisibilityAllTools()
  1062. {
  1063. return g_ToolFrameworkInternal.ServerPreSetupVisibilityAllTools();
  1064. }
  1065. bool CServerEngineTools::InToolMode()
  1066. {
  1067. return g_ToolFrameworkInternal.InToolMode();
  1068. }