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.

259 lines
7.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef IGAMESYSTEM_H
  8. #define IGAMESYSTEM_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. //-----------------------------------------------------------------------------
  13. // Game systems are singleton objects in the client + server codebase responsible for
  14. // various tasks
  15. // The order in which the server systems appear in this list are the
  16. // order in which they are initialized and updated. They are shut down in
  17. // reverse order from which they are initialized.
  18. //-----------------------------------------------------------------------------
  19. // UNDONE: Do these need GameInit/GameShutdown as well?
  20. // UNDONE: Remove the Pre/Post entity semantics and rely on system ordering?
  21. // FIXME: Remove all ifdef CLIENT_DLL if we can...
  22. abstract_class IGameSystem
  23. {
  24. public:
  25. // GameSystems are expected to implement these methods.
  26. virtual char const *Name() = 0;
  27. // Init, shutdown
  28. // return true on success. false to abort DLL init!
  29. virtual bool Init() = 0;
  30. virtual void PostInit() = 0;
  31. virtual void Shutdown() = 0;
  32. // Level init, shutdown
  33. virtual void LevelInitPreEntity() = 0;
  34. // entities are created / spawned / precached here
  35. virtual void LevelInitPostEntity() = 0;
  36. virtual void LevelShutdownPreClearSteamAPIContext() {};
  37. virtual void LevelShutdownPreEntity() = 0;
  38. // Entities are deleted / released here...
  39. virtual void LevelShutdownPostEntity() = 0;
  40. // end of level shutdown
  41. // Called during game save
  42. virtual void OnSave() = 0;
  43. // Called during game restore, after the local player has connected and entities have been fully restored
  44. virtual void OnRestore() = 0;
  45. // Called every frame. It's safe to remove an igamesystem from within this callback.
  46. virtual void SafeRemoveIfDesired() = 0;
  47. virtual bool IsPerFrame() = 0;
  48. // destructor, cleans up automagically....
  49. virtual ~IGameSystem();
  50. // Client systems can use this to get at the map name
  51. static char const* MapName();
  52. // These methods are used to add and remove server systems from the
  53. // main server loop. The systems are invoked in the order in which
  54. // they are added.
  55. static void Add ( IGameSystem* pSys );
  56. static void Remove ( IGameSystem* pSys );
  57. static void RemoveAll ( );
  58. // These methods are used to initialize, shutdown, etc all systems
  59. static bool InitAllSystems();
  60. static void PostInitAllSystems();
  61. static void ShutdownAllSystems();
  62. static void LevelInitPreEntityAllSystems( char const* pMapName );
  63. static void LevelInitPostEntityAllSystems();
  64. static void LevelShutdownPreClearSteamAPIContextAllSystems(); // Called prior to steamgameserverapicontext->Clear()
  65. static void LevelShutdownPreEntityAllSystems();
  66. static void LevelShutdownPostEntityAllSystems();
  67. static void OnSaveAllSystems();
  68. static void OnRestoreAllSystems();
  69. static void SafeRemoveIfDesiredAllSystems();
  70. #ifdef CLIENT_DLL
  71. static void PreRenderAllSystems();
  72. static void UpdateAllSystems( float frametime );
  73. static void PostRenderAllSystems();
  74. #else
  75. static void FrameUpdatePreEntityThinkAllSystems();
  76. static void FrameUpdatePostEntityThinkAllSystems();
  77. static void PreClientUpdateAllSystems();
  78. // Accessors for the above function
  79. static CBasePlayer *RunCommandPlayer();
  80. static CUserCmd *RunCommandUserCmd();
  81. #endif
  82. };
  83. class IGameSystemPerFrame : public IGameSystem
  84. {
  85. public:
  86. // destructor, cleans up automagically....
  87. virtual ~IGameSystemPerFrame();
  88. #ifdef CLIENT_DLL
  89. // Called before rendering
  90. virtual void PreRender() = 0;
  91. // Gets called each frame
  92. virtual void Update( float frametime ) = 0;
  93. // Called after rendering
  94. virtual void PostRender() = 0;
  95. #else
  96. // Called each frame before entities think
  97. virtual void FrameUpdatePreEntityThink() = 0;
  98. // called after entities think
  99. virtual void FrameUpdatePostEntityThink() = 0;
  100. virtual void PreClientUpdate() = 0;
  101. #endif
  102. };
  103. // Quick and dirty server system for users who don't care about precise ordering
  104. // and usually only want to implement a few of the callbacks
  105. class CBaseGameSystem : public IGameSystem
  106. {
  107. public:
  108. virtual char const *Name() { return "unnamed"; }
  109. // Init, shutdown
  110. // return true on success. false to abort DLL init!
  111. virtual bool Init() { return true; }
  112. virtual void PostInit() {}
  113. virtual void Shutdown() {}
  114. // Level init, shutdown
  115. virtual void LevelInitPreEntity() {}
  116. virtual void LevelInitPostEntity() {}
  117. virtual void LevelShutdownPreClearSteamAPIContext() {}
  118. virtual void LevelShutdownPreEntity() {}
  119. virtual void LevelShutdownPostEntity() {}
  120. virtual void OnSave() {}
  121. virtual void OnRestore() {}
  122. virtual void SafeRemoveIfDesired() {}
  123. virtual bool IsPerFrame() { return false; }
  124. private:
  125. // Prevent anyone derived from CBaseGameSystem from implementing these, they need
  126. // to derive from CBaseGameSystemPerFrame below!!!
  127. #ifdef CLIENT_DLL
  128. // Called before rendering
  129. virtual void PreRender() {}
  130. // Gets called each frame
  131. virtual void Update( float frametime ) {}
  132. // Called after rendering
  133. virtual void PostRender() {}
  134. #else
  135. // Called each frame before entities think
  136. virtual void FrameUpdatePreEntityThink() {}
  137. // called after entities think
  138. virtual void FrameUpdatePostEntityThink() {}
  139. virtual void PreClientUpdate() {}
  140. #endif
  141. };
  142. // Quick and dirty server system for users who don't care about precise ordering
  143. // and usually only want to implement a few of the callbacks
  144. class CBaseGameSystemPerFrame : public IGameSystemPerFrame
  145. {
  146. public:
  147. virtual char const *Name() { return "unnamed"; }
  148. // Init, shutdown
  149. // return true on success. false to abort DLL init!
  150. virtual bool Init() { return true; }
  151. virtual void PostInit() {}
  152. virtual void Shutdown() {}
  153. // Level init, shutdown
  154. virtual void LevelInitPreEntity() {}
  155. virtual void LevelInitPostEntity() {}
  156. virtual void LevelShutdownPreClearSteamAPIContext() {}
  157. virtual void LevelShutdownPreEntity() {}
  158. virtual void LevelShutdownPostEntity() {}
  159. virtual void OnSave() {}
  160. virtual void OnRestore() {}
  161. virtual void SafeRemoveIfDesired() {}
  162. virtual bool IsPerFrame() { return true; }
  163. #ifdef CLIENT_DLL
  164. // Called before rendering
  165. virtual void PreRender () { }
  166. // Gets called each frame
  167. virtual void Update( float frametime ) { }
  168. // Called after rendering
  169. virtual void PostRender () { }
  170. #else
  171. // Called each frame before entities think
  172. virtual void FrameUpdatePreEntityThink() { }
  173. // called after entities think
  174. virtual void FrameUpdatePostEntityThink() { }
  175. virtual void PreClientUpdate() { }
  176. #endif
  177. };
  178. // Quick and dirty server system for users who don't care about precise ordering
  179. // and usually only want to implement a few of the callbacks
  180. class CAutoGameSystem : public CBaseGameSystem
  181. {
  182. public:
  183. CAutoGameSystem( char const *name = NULL ); // hooks in at startup, no need to explicitly add
  184. CAutoGameSystem *m_pNext;
  185. virtual char const *Name() { return m_pszName ? m_pszName : "unnamed"; }
  186. private:
  187. char const *m_pszName;
  188. };
  189. //-----------------------------------------------------------------------------
  190. // Purpose: This is a CAutoGameSystem which also cares about the "per frame" hooks
  191. //-----------------------------------------------------------------------------
  192. class CAutoGameSystemPerFrame : public CBaseGameSystemPerFrame
  193. {
  194. public:
  195. CAutoGameSystemPerFrame( char const *name = NULL );
  196. CAutoGameSystemPerFrame *m_pNext;
  197. virtual char const *Name() { return m_pszName ? m_pszName : "unnamed"; }
  198. private:
  199. char const *m_pszName;
  200. };
  201. //-----------------------------------------------------------------------------
  202. // Purpose: This interface is here to add more hooks than IGameSystemPerFrame exposes,
  203. // so we don't pollute it with hooks that only the tool cares about
  204. //-----------------------------------------------------------------------------
  205. class IToolFrameworkServer
  206. {
  207. public:
  208. virtual void PreSetupVisibility() = 0;
  209. };
  210. #endif // IGAMESYSTEM_H