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.

228 lines
9.2 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose: An application framework
  4. //
  5. // $Revision: $
  6. // $NoKeywords: $
  7. //===========================================================================//
  8. #ifndef APPFRAMEWORK_H
  9. #define APPFRAMEWORK_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "appframework/IAppSystemGroup.h"
  14. #include "ilaunchabledll.h"
  15. //-----------------------------------------------------------------------------
  16. // Gets the application instance..
  17. //-----------------------------------------------------------------------------
  18. void *GetAppInstance();
  19. //-----------------------------------------------------------------------------
  20. // Sets the application instance, should only be used if you're not calling AppMain.
  21. //-----------------------------------------------------------------------------
  22. void SetAppInstance( void* hInstance );
  23. //-----------------------------------------------------------------------------
  24. // Main entry point for the application
  25. //-----------------------------------------------------------------------------
  26. int AppMain( void* hInstance, void* hPrevInstance, const char* lpCmdLine, int nCmdShow, CAppSystemGroup *pAppSystemGroup );
  27. int AppMain( int argc, char **argv, CAppSystemGroup *pAppSystemGroup );
  28. //-----------------------------------------------------------------------------
  29. // Used to startup/shutdown the application
  30. //-----------------------------------------------------------------------------
  31. int AppStartup( void* hInstance, void* hPrevInstance, const char* lpCmdLine, int nCmdShow, CAppSystemGroup *pAppSystemGroup );
  32. int AppStartup( int argc, char **argv, CAppSystemGroup *pAppSystemGroup );
  33. void AppShutdown( CAppSystemGroup *pAppSystemGroup );
  34. //-----------------------------------------------------------------------------
  35. // Macros to create singleton application objects for windowed + console apps
  36. //-----------------------------------------------------------------------------
  37. // This assumes you've used one of the
  38. #define DEFINE_LAUNCHABLE_DLL_STEAM_APP() \
  39. class CAppLaunchableDLL : public ILaunchableDLL \
  40. { \
  41. public: \
  42. virtual int main( int argc, char **argv ) \
  43. { \
  44. return AppMain( argc, argv, &__s_SteamApplicationObject ); \
  45. } \
  46. }; \
  47. static CAppLaunchableDLL __g_AppLaunchableDLL; \
  48. EXPOSE_SINGLE_INTERFACE_GLOBALVAR( CAppLaunchableDLL, ILaunchableDLL, LAUNCHABLE_DLL_INTERFACE_VERSION, __g_AppLaunchableDLL );
  49. #if !defined( _X360 )
  50. #if defined( _OSX )
  51. #define DEFINE_WINDOWED_APPLICATION_OBJECT_GLOBALVAR( _globalVarName ) \
  52. int main( int argc, char **argv ) \
  53. { \
  54. extern int ValveCocoaMain( int argc, char **argv, CAppSystemGroup *pAppSystemGroup ); \
  55. return ValveCocoaMain( argc, argv, &_globalVarName ); \
  56. }
  57. #elif defined( PLATFORM_LINUX )
  58. #define DEFINE_WINDOWED_APPLICATION_OBJECT_GLOBALVAR( _globalVarName ) \
  59. int main( int argc, char **argv ) \
  60. { \
  61. extern int ValveLinuxWindowedMain( int argc, char **argv, CAppSystemGroup *pAppSystemGroup ); \
  62. return ValveLinuxWindowedMain( argc, argv, &_globalVarName ); \
  63. }
  64. #else
  65. #define DEFINE_WINDOWED_APPLICATION_OBJECT_GLOBALVAR( _globalVarName ) \
  66. int __stdcall WinMain( struct HINSTANCE__* hInstance, struct HINSTANCE__* hPrevInstance, NULLTERMINATED char *lpCmdLine, int nCmdShow ) \
  67. { \
  68. return AppMain( hInstance, hPrevInstance, lpCmdLine, nCmdShow, &_globalVarName ); \
  69. }
  70. #endif
  71. #else
  72. #define DEFINE_WINDOWED_APPLICATION_OBJECT_GLOBALVAR( _globalVarName ) \
  73. DLL_EXPORT int AppMain360( struct HINSTANCE__* hInstance, struct HINSTANCE__* hPrevInstance, NULLTERMINATED char *lpCmdLine, int nCmdShow ) \
  74. { \
  75. return AppMain( hInstance, hPrevInstance, lpCmdLine, nCmdShow, &_globalVarName ); \
  76. }
  77. #endif
  78. #if !defined( _X360 )
  79. #define DEFINE_CONSOLE_APPLICATION_OBJECT_GLOBALVAR( _globalVarName ) \
  80. int main( int argc, char **argv ) \
  81. { \
  82. return AppMain( argc, argv, &_globalVarName ); \
  83. }
  84. #else
  85. #define DEFINE_CONSOLE_APPLICATION_OBJECT_GLOBALVAR( _globalVarName ) \
  86. DLL_EXPORT int AppMain360( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) \
  87. { \
  88. return AppMain( hInstance, hPrevInstance, lpCmdLine, nCmdShow, &_globalVarName ); \
  89. }
  90. #endif
  91. #define DEFINE_BINLAUNCHABLE_APPLICATION_OBJECT_GLOBALVAR( _globalVarName ) \
  92. class CApplicationDLL : public ILaunchableDLL \
  93. { \
  94. public: \
  95. virtual int main( int argc, char **argv ) \
  96. { \
  97. return AppMain( argc, argv, &_globalVarName ); \
  98. } \
  99. }; \
  100. EXPOSE_SINGLE_INTERFACE( CApplicationDLL, ILaunchableDLL, LAUNCHABLE_DLL_INTERFACE_VERSION )
  101. #define DEFINE_WINDOWED_APPLICATION_OBJECT( _className ) \
  102. static _className __s_ApplicationObject; \
  103. DEFINE_WINDOWED_APPLICATION_OBJECT_GLOBALVAR( __s_ApplicationObject )
  104. #define DEFINE_CONSOLE_APPLICATION_OBJECT( _className ) \
  105. static _className __s_ApplicationObject; \
  106. DEFINE_CONSOLE_APPLICATION_OBJECT_GLOBALVAR( __s_ApplicationObject )
  107. #define DEFINE_BINLAUNCHABLE_APPLICATION_OBJECT( _className ) \
  108. static _className __s_ApplicationObject; \
  109. DEFINE_BINLAUNCHABLE_APPLICATION_OBJECT_GLOBALVAR( __s_ApplicationObject )
  110. //-----------------------------------------------------------------------------
  111. // This class is a helper class used for steam-based applications.
  112. // It loads up the file system in preparation for using it to load other
  113. // required modules from steam.
  114. //-----------------------------------------------------------------------------
  115. class CSteamApplication : public CAppSystemGroup
  116. {
  117. typedef CAppSystemGroup BaseClass;
  118. public:
  119. CSteamApplication( CSteamAppSystemGroup *pAppSystemGroup );
  120. // Implementation of IAppSystemGroup
  121. virtual bool Create( );
  122. virtual bool PreInit( );
  123. virtual int Main( );
  124. virtual void PostShutdown();
  125. virtual void Destroy();
  126. // Use this version in cases where you can't control the main loop and
  127. // expect to be ticked
  128. virtual int Startup();
  129. virtual void Shutdown();
  130. public:
  131. // Here's a hook to override the filesystem DLL that it tries to load.
  132. // By default, it uses FileSystem_GetFileSystemDLLName to figure this out.
  133. virtual bool GetFileSystemDLLName( char *pOut, int nMaxBytes, bool &bIsSteam );
  134. protected:
  135. IFileSystem *m_pFileSystem;
  136. CSteamAppSystemGroup *m_pChildAppSystemGroup;
  137. bool m_bSteam;
  138. };
  139. class CBinLaunchableSteamApp : public CSteamApplication
  140. {
  141. public:
  142. CBinLaunchableSteamApp( CSteamAppSystemGroup *pAppSystemGroup ) : CSteamApplication( pAppSystemGroup )
  143. {
  144. }
  145. virtual bool GetFileSystemDLLName( char *pOut, int nMaxChars, bool &bIsSteam )
  146. {
  147. // Our path should already include game\bin, so just use the filename directly
  148. // and don't try to figure out an absolute path to it as CSteamApplication does.
  149. V_strncpy( pOut, "filesystem_stdio", nMaxChars );
  150. bIsSteam = false;
  151. return true;
  152. }
  153. };
  154. //-----------------------------------------------------------------------------
  155. // Macros to help create singleton application objects for windowed + console steam apps
  156. //-----------------------------------------------------------------------------
  157. #define DEFINE_WINDOWED_STEAM_APPLICATION_OBJECT_GLOBALVAR( _className, _varName ) \
  158. static CSteamApplication __s_SteamApplicationObject( &_varName ); \
  159. DEFINE_WINDOWED_APPLICATION_OBJECT_GLOBALVAR( __s_SteamApplicationObject )
  160. #define DEFINE_WINDOWED_STEAM_APPLICATION_OBJECT( _className ) \
  161. static _className __s_ApplicationObject; \
  162. static CSteamApplication __s_SteamApplicationObject( &__s_ApplicationObject ); \
  163. DEFINE_WINDOWED_APPLICATION_OBJECT_GLOBALVAR( __s_SteamApplicationObject )
  164. #define DEFINE_CONSOLE_STEAM_APPLICATION_OBJECT_GLOBALVAR( _className, _varName ) \
  165. static CSteamApplication __s_SteamApplicationObject( &_varName ); \
  166. DEFINE_CONSOLE_APPLICATION_OBJECT_GLOBALVAR( __s_SteamApplicationObject )
  167. #define DEFINE_CONSOLE_STEAM_APPLICATION_OBJECT( _className ) \
  168. static _className __s_ApplicationObject; \
  169. static CSteamApplication __s_SteamApplicationObject( &__s_ApplicationObject ); \
  170. DEFINE_CONSOLE_APPLICATION_OBJECT_GLOBALVAR( __s_SteamApplicationObject )
  171. #define DEFINE_BINLAUNCHABLE_STEAM_APPLICATION_OBJECT_GLOBALVAR( _className, _varName ) \
  172. static CBinLaunchableSteamApp __s_SteamApplicationObject( &_varName ); \
  173. DEFINE_BINLAUNCHABLE_APPLICATION_OBJECT_GLOBALVAR( __s_SteamApplicationObject )
  174. #define DEFINE_BINLAUNCHABLE_STEAM_APPLICATION_OBJECT( _className ) \
  175. static _className __s_ApplicationObject; \
  176. static CBinLaunchableSteamApp __s_SteamApplicationObject( &__s_ApplicationObject ); \
  177. DEFINE_BINLAUNCHABLE_APPLICATION_OBJECT_GLOBALVAR( __s_SteamApplicationObject )
  178. // This defines your steam application object and ties it to your appsystemgroup.
  179. // This does NOT hookup its AppMain to get called. You'll have to call that from startup code
  180. // or use something like DEFINE_LAUNCHABLE_DLL_STEAM_APP() to call it.
  181. //
  182. // _steamApplicationClass derives from CSteamApplication.
  183. // _appClass derives from CAppSystemGroup (.. can derive from - or be - CTier2SteamApp for example).
  184. //
  185. #define DEFINE_CUSTOM_STEAM_APPLICATION_OBJECT( _steamApplicationClassName, _appClassName ) \
  186. static _appClassName __s_ApplicationObject; \
  187. static _steamApplicationClassName __s_SteamApplicationObject( &__s_ApplicationObject );
  188. #endif // APPFRAMEWORK_H