Counter Strike : Global Offensive Source Code
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.

302 lines
10 KiB

  1. //======== (C) Copyright 1999, 2000 Valve, L.L.C. 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 "tier1/UtlStringMap.h"
  23. #include "iappsystem.h"
  24. //-----------------------------------------------------------------------------
  25. // forward declarations
  26. //-----------------------------------------------------------------------------
  27. class IAppSystem;
  28. class CSysModule;
  29. class IBaseInterface;
  30. class IFileSystem;
  31. //-----------------------------------------------------------------------------
  32. // Handle to a DLL
  33. //-----------------------------------------------------------------------------
  34. typedef int AppModule_t;
  35. enum
  36. {
  37. APP_MODULE_INVALID = (AppModule_t)~0
  38. };
  39. //-----------------------------------------------------------------------------
  40. // NOTE: The following methods must be implemented in your application
  41. // although they can be empty implementations if you like...
  42. //-----------------------------------------------------------------------------
  43. abstract_class IAppSystemGroup
  44. {
  45. public:
  46. // An installed application creation function, you should tell the group
  47. // the DLLs and the singleton interfaces you want to instantiate.
  48. // Return false if there's any problems and the app will abort
  49. virtual bool Create( ) = 0;
  50. // Allow the application to do some work after AppSystems are connected but
  51. // they are all Initialized.
  52. // Return false if there's any problems and the app will abort
  53. virtual bool PreInit() = 0;
  54. // Allow the application to do some work after AppSystems are initialized but
  55. // before main is run
  56. // Return false if there's any problems and the app will abort
  57. virtual bool PostInit() = 0;
  58. // Main loop implemented by the application
  59. virtual int Main( ) = 0;
  60. // Allow the application to do some work after all AppSystems are shut down
  61. virtual void PreShutdown() = 0;
  62. // Allow the application to do some work after all AppSystems are shut down
  63. virtual void PostShutdown() = 0;
  64. // Call an installed application destroy function, occurring after all modules
  65. // are unloaded
  66. virtual void Destroy() = 0;
  67. };
  68. //-----------------------------------------------------------------------------
  69. // This class represents a group of app systems that all have the same lifetime
  70. // that need to be connected/initialized, etc. in a well-defined order
  71. //-----------------------------------------------------------------------------
  72. class CAppSystemGroup : public IAppSystemGroup
  73. {
  74. public:
  75. // Used to determine where we exited out from the system
  76. enum AppSystemGroupStage_t
  77. {
  78. CREATION = 0,
  79. DEPENDENCIES,
  80. CONNECTION,
  81. PREINITIALIZATION,
  82. INITIALIZATION,
  83. POSTINITIALIZATION,
  84. RUNNING,
  85. PRESHUTDOWN,
  86. SHUTDOWN,
  87. POSTSHUTDOWN,
  88. DISCONNECTION,
  89. DESTRUCTION,
  90. APPSYSTEM_GROUP_STAGE_COUNT,
  91. NONE, // This means no error
  92. };
  93. public:
  94. // constructor
  95. CAppSystemGroup( CAppSystemGroup *pParentAppSystem = NULL );
  96. // Runs the app system group.
  97. // First, modules are loaded, next they are connected, followed by initialization
  98. // Then Main() is run
  99. // Then modules are shut down, disconnected, and unloaded
  100. int Run( );
  101. // Use this version in cases where you can't control the main loop and
  102. // expect to be ticked
  103. virtual int Startup();
  104. virtual void Shutdown();
  105. // Default implementations...
  106. virtual bool PostInit() { return true; }
  107. virtual void PreShutdown() { }
  108. // Returns the stage at which the app system group ran into an error
  109. AppSystemGroupStage_t GetCurrentStage() const;
  110. int ReloadModule( const char * pDLLName );
  111. protected:
  112. // These methods are meant to be called by derived classes of CAppSystemGroup
  113. // Methods to load + unload DLLs
  114. AppModule_t LoadModule( const char *pDLLName );
  115. AppModule_t LoadModule( CreateInterfaceFn factory );
  116. // Method to add various global singleton systems
  117. IAppSystem *AddSystem( AppModule_t module, const char *pInterfaceName );
  118. void AddSystem( IAppSystem *pAppSystem, const char *pInterfaceName );
  119. // Simpler method of doing the LoadModule/AddSystem thing.
  120. // Make sure the last AppSystemInfo has a NULL module name
  121. bool AddSystems( AppSystemInfo_t *pSystems );
  122. // Adds a factory to the system so other stuff can query it. Triggers a connect systems
  123. void AddNonAppSystemFactory( CreateInterfaceFn fn );
  124. // Removes a factory, triggers a disconnect call if it succeeds
  125. void RemoveNonAppSystemFactory( CreateInterfaceFn fn );
  126. // Causes the systems to reconnect to an interface
  127. void ReconnectSystems( const char *pInterfaceName );
  128. // Method to look up a particular named system...
  129. void *FindSystem( const char *pInterfaceName );
  130. // Creates the app window (windowed app only)
  131. virtual void *CreateAppWindow( void *hInstance, const char *pTitle, bool bWindowed, int w, int h, bool bResizing );
  132. void SetAppWindowTitle( void* hWnd, const char *pTitle );
  133. // Gets at a class factory for the topmost appsystem group in an appsystem stack
  134. static CreateInterfaceFn GetFactory();
  135. private:
  136. struct Module_t
  137. {
  138. CSysModule *m_pModule;
  139. CreateInterfaceFn m_Factory;
  140. char *m_pModuleName;
  141. };
  142. typedef CUtlStringMap< CUtlSymbolTable > LibraryDependencies_t;
  143. int OnStartup();
  144. void OnShutdown();
  145. void UnloadAllModules( );
  146. void RemoveAllSystems();
  147. // Method to load all dependent systems
  148. bool LoadDependentSystems();
  149. // Method to connect/disconnect all systems
  150. bool ConnectSystems( );
  151. void DisconnectSystems();
  152. // Method to initialize/shutdown all systems
  153. InitReturnVal_t InitSystems();
  154. void ShutdownSystems();
  155. // Gets at the parent appsystem group
  156. CAppSystemGroup *GetParent();
  157. // Loads a module the standard way
  158. virtual CSysModule *LoadModuleDLL( const char *pDLLName );
  159. void ComputeDependencies( LibraryDependencies_t &depend );
  160. void SortDependentLibraries( LibraryDependencies_t &depend );
  161. const char *FindSystemName( int nIndex );
  162. static bool SortLessFunc( const int &left, const int &right );
  163. void ReportStartupFailure( int nErrorStage, int nSysIndex );
  164. CUtlVector<Module_t> m_Modules;
  165. CUtlVector<IAppSystem*> m_Systems;
  166. CUtlVector<CreateInterfaceFn> m_NonAppSystemFactories;
  167. CUtlDict<int, unsigned short> m_SystemDict;
  168. CAppSystemGroup *m_pParentAppSystem;
  169. AppSystemGroupStage_t m_nCurrentStage;
  170. static LibraryDependencies_t *sm_pSortDependencies;
  171. friend void *AppSystemCreateInterfaceFn(const char *pName, int *pReturnCode);
  172. friend class CSteamAppSystemGroup;
  173. };
  174. //-----------------------------------------------------------------------------
  175. // This class represents a group of app systems that are loaded through steam
  176. //-----------------------------------------------------------------------------
  177. class CSteamAppSystemGroup : public CAppSystemGroup
  178. {
  179. public:
  180. CSteamAppSystemGroup( IFileSystem *pFileSystem = NULL, CAppSystemGroup *pParentAppSystem = NULL );
  181. // Used by CSteamApplication to set up necessary pointers if we can't do it in the constructor
  182. void Setup( IFileSystem *pFileSystem, CAppSystemGroup *pParentAppSystem );
  183. protected:
  184. // Sets up the search paths
  185. bool SetupSearchPaths( const char *pStartingDir, bool bOnlyUseStartingDir, bool bIsTool );
  186. // Returns the game info path. Only works if you've called SetupSearchPaths first
  187. const char *GetGameInfoPath() const;
  188. private:
  189. virtual CSysModule *LoadModuleDLL( const char *pDLLName );
  190. IFileSystem *m_pFileSystem;
  191. char m_pGameInfoPath[ MAX_PATH ];
  192. };
  193. //-----------------------------------------------------------------------------
  194. // Helper empty decorator implementation of an IAppSystemGroup
  195. //-----------------------------------------------------------------------------
  196. template< class CBaseClass >
  197. class CDefaultAppSystemGroup : public CBaseClass
  198. {
  199. public:
  200. virtual bool Create( ) { return true; }
  201. virtual bool PreInit() { return true; }
  202. virtual bool PostInit() { return true; }
  203. virtual void PreShutdown() { }
  204. virtual void PostShutdown() {}
  205. virtual void Destroy() {}
  206. };
  207. //-----------------------------------------------------------------------------
  208. // Special helper for game info directory suggestion
  209. //-----------------------------------------------------------------------------
  210. class CFSSteamSetupInfo; // Forward declaration
  211. //
  212. // SuggestGameInfoDirFn_t
  213. // Game info suggestion function.
  214. // Provided by the application to possibly detect the suggested game info
  215. // directory and initialize all the game-info-related systems appropriately.
  216. // Parameters:
  217. // pFsSteamSetupInfo steam file system setup information if available.
  218. // pchPathBuffer buffer to hold game info directory path on return.
  219. // nBufferLength length of the provided buffer to hold game info directory path.
  220. // pbBubbleDirectories should contain "true" on return to bubble the directories up searching for game info file.
  221. // Return values:
  222. // Returns "true" if the game info directory path suggestion is available and
  223. // was successfully copied into the provided buffer.
  224. // Returns "false" otherwise, interpreted that no suggestion will be used.
  225. //
  226. typedef bool ( * SuggestGameInfoDirFn_t ) ( CFSSteamSetupInfo const *pFsSteamSetupInfo, char *pchPathBuffer, int nBufferLength, bool *pbBubbleDirectories );
  227. //
  228. // SetSuggestGameInfoDirFn
  229. // Installs the supplied game info directory suggestion function.
  230. // Parameters:
  231. // pfnNewFn the new game info directory suggestion function.
  232. // Returns:
  233. // The previously installed suggestion function or NULL if none was installed before.
  234. // This function never fails.
  235. //
  236. SuggestGameInfoDirFn_t SetSuggestGameInfoDirFn( SuggestGameInfoDirFn_t pfnNewFn );
  237. #endif // APPSYSTEMGROUP_H