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.

265 lines
8.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // The copyright to the contents herein is the property of Valve, L.L.C.
  4. // The contents may be used and/or copied only with the written permission of
  5. // Valve, L.L.C., or in accordance with the terms and conditions stipulated in
  6. // the agreement/contract under which the contents have been supplied.
  7. //
  8. // Purpose: Defines a group of app systems that all have the same lifetime
  9. // that need to be connected/initialized, etc. in a well-defined order
  10. //
  11. // $Revision: $
  12. // $NoKeywords: $
  13. //=============================================================================
  14. #ifndef IAPPSYSTEMGROUP_H
  15. #define IAPPSYSTEMGROUP_H
  16. #ifdef _WIN32
  17. #pragma once
  18. #endif
  19. #include "tier1/interface.h"
  20. #include "tier1/utlvector.h"
  21. #include "tier1/utldict.h"
  22. #include "IAppSystem.h"
  23. //-----------------------------------------------------------------------------
  24. // forward declarations
  25. //-----------------------------------------------------------------------------
  26. class IAppSystem;
  27. class CSysModule;
  28. class IBaseInterface;
  29. class IFileSystem;
  30. //-----------------------------------------------------------------------------
  31. // Handle to a DLL
  32. //-----------------------------------------------------------------------------
  33. typedef int AppModule_t;
  34. enum
  35. {
  36. APP_MODULE_INVALID = (AppModule_t)~0
  37. };
  38. //-----------------------------------------------------------------------------
  39. // NOTE: The following methods must be implemented in your application
  40. // although they can be empty implementations if you like...
  41. //-----------------------------------------------------------------------------
  42. abstract_class IAppSystemGroup
  43. {
  44. public:
  45. // An installed application creation function, you should tell the group
  46. // the DLLs and the singleton interfaces you want to instantiate.
  47. // Return false if there's any problems and the app will abort
  48. virtual bool Create( ) = 0;
  49. // Allow the application to do some work after AppSystems are connected but
  50. // they are all Initialized.
  51. // Return false if there's any problems and the app will abort
  52. virtual bool PreInit() = 0;
  53. // Main loop implemented by the application
  54. virtual int Main( ) = 0;
  55. // Allow the application to do some work after all AppSystems are shut down
  56. virtual void PostShutdown() = 0;
  57. // Call an installed application destroy function, occurring after all modules
  58. // are unloaded
  59. virtual void Destroy() = 0;
  60. };
  61. //-----------------------------------------------------------------------------
  62. // Specifies a module + interface name for initialization
  63. //-----------------------------------------------------------------------------
  64. struct AppSystemInfo_t
  65. {
  66. const char *m_pModuleName;
  67. const char *m_pInterfaceName;
  68. };
  69. //-----------------------------------------------------------------------------
  70. // This class represents a group of app systems that all have the same lifetime
  71. // that need to be connected/initialized, etc. in a well-defined order
  72. //-----------------------------------------------------------------------------
  73. class CAppSystemGroup : public IAppSystemGroup
  74. {
  75. public:
  76. // Used to determine where we exited out from the system
  77. enum AppSystemGroupStage_t
  78. {
  79. CREATION = 0,
  80. CONNECTION,
  81. PREINITIALIZATION,
  82. INITIALIZATION,
  83. SHUTDOWN,
  84. POSTSHUTDOWN,
  85. DISCONNECTION,
  86. DESTRUCTION,
  87. NONE, // This means no error
  88. };
  89. public:
  90. // constructor
  91. CAppSystemGroup( CAppSystemGroup *pParentAppSystem = NULL );
  92. // Runs the app system group.
  93. // First, modules are loaded, next they are connected, followed by initialization
  94. // Then Main() is run
  95. // Then modules are shut down, disconnected, and unloaded
  96. int Run( );
  97. // Use this version in cases where you can't control the main loop and
  98. // expect to be ticked
  99. virtual int Startup();
  100. virtual void Shutdown();
  101. // Returns the stage at which the app system group ran into an error
  102. AppSystemGroupStage_t GetErrorStage() const;
  103. protected:
  104. // These methods are meant to be called by derived classes of CAppSystemGroup
  105. // Methods to load + unload DLLs
  106. AppModule_t LoadModule( const char *pDLLName );
  107. AppModule_t LoadModule( CreateInterfaceFn factory );
  108. // Method to add various global singleton systems
  109. IAppSystem *AddSystem( AppModule_t module, const char *pInterfaceName );
  110. void AddSystem( IAppSystem *pAppSystem, const char *pInterfaceName );
  111. // Simpler method of doing the LoadModule/AddSystem thing.
  112. // Make sure the last AppSystemInfo has a NULL module name
  113. bool AddSystems( AppSystemInfo_t *pSystems );
  114. // Method to look up a particular named system...
  115. void *FindSystem( const char *pInterfaceName );
  116. // Gets at a class factory for the topmost appsystem group in an appsystem stack
  117. static CreateInterfaceFn GetFactory();
  118. private:
  119. int OnStartup();
  120. void OnShutdown();
  121. void UnloadAllModules( );
  122. void RemoveAllSystems();
  123. // Method to connect/disconnect all systems
  124. bool ConnectSystems( );
  125. void DisconnectSystems();
  126. // Method to initialize/shutdown all systems
  127. InitReturnVal_t InitSystems();
  128. void ShutdownSystems();
  129. // Gets at the parent appsystem group
  130. CAppSystemGroup *GetParent();
  131. // Loads a module the standard way
  132. virtual CSysModule *LoadModuleDLL( const char *pDLLName );
  133. void ReportStartupFailure( int nErrorStage, int nSysIndex );
  134. struct Module_t
  135. {
  136. CSysModule *m_pModule;
  137. CreateInterfaceFn m_Factory;
  138. char *m_pModuleName;
  139. };
  140. CUtlVector<Module_t> m_Modules;
  141. CUtlVector<IAppSystem*> m_Systems;
  142. CUtlDict<int, unsigned short> m_SystemDict;
  143. CAppSystemGroup *m_pParentAppSystem;
  144. AppSystemGroupStage_t m_nErrorStage;
  145. friend void *AppSystemCreateInterfaceFn(const char *pName, int *pReturnCode);
  146. friend class CSteamAppSystemGroup;
  147. };
  148. //-----------------------------------------------------------------------------
  149. // This class represents a group of app systems that are loaded through steam
  150. //-----------------------------------------------------------------------------
  151. class CSteamAppSystemGroup : public CAppSystemGroup
  152. {
  153. public:
  154. CSteamAppSystemGroup( IFileSystem *pFileSystem = NULL, CAppSystemGroup *pParentAppSystem = NULL );
  155. // Used by CSteamApplication to set up necessary pointers if we can't do it in the constructor
  156. void Setup( IFileSystem *pFileSystem, CAppSystemGroup *pParentAppSystem );
  157. protected:
  158. // Sets up the search paths
  159. bool SetupSearchPaths( const char *pStartingDir, bool bOnlyUseStartingDir, bool bIsTool );
  160. // Returns the game info path. Only works if you've called SetupSearchPaths first
  161. const char *GetGameInfoPath() const;
  162. private:
  163. virtual CSysModule *LoadModuleDLL( const char *pDLLName );
  164. IFileSystem *m_pFileSystem;
  165. char m_pGameInfoPath[ MAX_PATH ];
  166. };
  167. //-----------------------------------------------------------------------------
  168. // Helper empty decorator implementation of an IAppSystemGroup
  169. //-----------------------------------------------------------------------------
  170. template< class CBaseClass >
  171. class CDefaultAppSystemGroup : public CBaseClass
  172. {
  173. public:
  174. virtual bool Create( ) { return true; }
  175. virtual bool PreInit() { return true; }
  176. virtual void PostShutdown() {}
  177. virtual void Destroy() {}
  178. };
  179. //-----------------------------------------------------------------------------
  180. // Special helper for game info directory suggestion
  181. //-----------------------------------------------------------------------------
  182. class CFSSteamSetupInfo; // Forward declaration
  183. //
  184. // SuggestGameInfoDirFn_t
  185. // Game info suggestion function.
  186. // Provided by the application to possibly detect the suggested game info
  187. // directory and initialize all the game-info-related systems appropriately.
  188. // Parameters:
  189. // pFsSteamSetupInfo steam file system setup information if available.
  190. // pchPathBuffer buffer to hold game info directory path on return.
  191. // nBufferLength length of the provided buffer to hold game info directory path.
  192. // pbBubbleDirectories should contain "true" on return to bubble the directories up searching for game info file.
  193. // Return values:
  194. // Returns "true" if the game info directory path suggestion is available and
  195. // was successfully copied into the provided buffer.
  196. // Returns "false" otherwise, interpreted that no suggestion will be used.
  197. //
  198. typedef bool ( * SuggestGameInfoDirFn_t ) ( CFSSteamSetupInfo const *pFsSteamSetupInfo, char *pchPathBuffer, int nBufferLength, bool *pbBubbleDirectories );
  199. //
  200. // SetSuggestGameInfoDirFn
  201. // Installs the supplied game info directory suggestion function.
  202. // Parameters:
  203. // pfnNewFn the new game info directory suggestion function.
  204. // Returns:
  205. // The previously installed suggestion function or NULL if none was installed before.
  206. // This function never fails.
  207. //
  208. SuggestGameInfoDirFn_t SetSuggestGameInfoDirFn( SuggestGameInfoDirFn_t pfnNewFn );
  209. #endif // APPSYSTEMGROUP_H