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.

235 lines
7.3 KiB

  1. //====== Copyright (c) 1996-2005, Valve Corporation, All rights reserved. =======//
  2. //
  3. // Purpose: An application framework
  4. //
  5. //=============================================================================//
  6. #include "appframework/AppFramework.h"
  7. #include "tier0/dbg.h"
  8. #include "tier0/icommandline.h"
  9. #include "interface.h"
  10. #include "filesystem.h"
  11. #include "appframework/IAppSystemGroup.h"
  12. #include "filesystem_init.h"
  13. #include "vstdlib/cvar.h"
  14. #include "tier2/tier2.h"
  15. #ifdef _X360
  16. #include "xbox/xbox_win32stubs.h"
  17. #include "xbox/xbox_console.h"
  18. #include "xbox/xbox_launch.h"
  19. #endif
  20. // NOTE: This has to be the last file included!
  21. #include "tier0/memdbgon.h"
  22. //-----------------------------------------------------------------------------
  23. // Globals...
  24. //-----------------------------------------------------------------------------
  25. void* s_HInstance;
  26. static CSimpleWindowsLoggingListener s_SimpleWindowsLoggingListener;
  27. static CSimpleLoggingListener s_SimpleLoggingListener;
  28. ILoggingListener *g_pDefaultLoggingListener = &s_SimpleLoggingListener;
  29. //-----------------------------------------------------------------------------
  30. // HACK: Since I don't want to refit vgui yet...
  31. //-----------------------------------------------------------------------------
  32. void *GetAppInstance()
  33. {
  34. return s_HInstance;
  35. }
  36. //-----------------------------------------------------------------------------
  37. // Sets the application instance, should only be used if you're not calling AppMain.
  38. //-----------------------------------------------------------------------------
  39. void SetAppInstance( void* hInstance )
  40. {
  41. s_HInstance = hInstance;
  42. }
  43. //-----------------------------------------------------------------------------
  44. // Version of AppMain used by windows applications
  45. //-----------------------------------------------------------------------------
  46. int AppMain( void* hInstance, void* hPrevInstance, const char* lpCmdLine, int nCmdShow, CAppSystemGroup *pAppSystemGroup )
  47. {
  48. Assert( pAppSystemGroup );
  49. g_pDefaultLoggingListener = &s_SimpleWindowsLoggingListener;
  50. s_HInstance = hInstance;
  51. #ifdef WIN32
  52. // Prepend the module filename since most apps expect arg 0 to be that.
  53. char szModuleFilename[MAX_PATH];
  54. Plat_GetModuleFilename( szModuleFilename, sizeof( szModuleFilename ) );
  55. int nAllocLen = strlen( lpCmdLine ) + strlen( szModuleFilename ) + 4;
  56. char *pNewCmdLine = new char[nAllocLen]; // 2 for quotes, 1 for a space, and 1 for a null-terminator.
  57. _snprintf( pNewCmdLine, nAllocLen, "\"%s\" %s", szModuleFilename, lpCmdLine );
  58. // Setup ICommandLine.
  59. CommandLine()->CreateCmdLine( pNewCmdLine );
  60. delete [] pNewCmdLine;
  61. #else
  62. CommandLine()->CreateCmdLine( lpCmdLine );
  63. #endif
  64. return pAppSystemGroup->Run();
  65. }
  66. //-----------------------------------------------------------------------------
  67. // Version of AppMain used by console applications
  68. //-----------------------------------------------------------------------------
  69. int AppMain( int argc, char **argv, CAppSystemGroup *pAppSystemGroup )
  70. {
  71. Assert( pAppSystemGroup );
  72. g_pDefaultLoggingListener = &s_SimpleLoggingListener;
  73. s_HInstance = NULL;
  74. CommandLine()->CreateCmdLine( argc, argv );
  75. return pAppSystemGroup->Run();
  76. }
  77. //-----------------------------------------------------------------------------
  78. // Used to startup/shutdown the application
  79. //-----------------------------------------------------------------------------
  80. int AppStartup( void* hInstance, void* hPrevInstance, const char* lpCmdLine, int nCmdShow, CAppSystemGroup *pAppSystemGroup )
  81. {
  82. Assert( pAppSystemGroup );
  83. g_pDefaultLoggingListener = &s_SimpleWindowsLoggingListener;
  84. s_HInstance = hInstance;
  85. CommandLine()->CreateCmdLine( lpCmdLine );
  86. return pAppSystemGroup->Startup();
  87. }
  88. int AppStartup( int argc, char **argv, CAppSystemGroup *pAppSystemGroup )
  89. {
  90. Assert( pAppSystemGroup );
  91. g_pDefaultLoggingListener = &s_SimpleLoggingListener;
  92. s_HInstance = NULL;
  93. CommandLine()->CreateCmdLine( argc, argv );
  94. return pAppSystemGroup->Startup();
  95. }
  96. void AppShutdown( CAppSystemGroup *pAppSystemGroup )
  97. {
  98. Assert( pAppSystemGroup );
  99. pAppSystemGroup->Shutdown();
  100. }
  101. //-----------------------------------------------------------------------------
  102. //
  103. // Default implementation of an application meant to be run using Steam
  104. //
  105. //-----------------------------------------------------------------------------
  106. //-----------------------------------------------------------------------------
  107. // Constructor
  108. //-----------------------------------------------------------------------------
  109. CSteamApplication::CSteamApplication( CSteamAppSystemGroup *pAppSystemGroup )
  110. {
  111. m_pChildAppSystemGroup = pAppSystemGroup;
  112. m_pFileSystem = NULL;
  113. m_bSteam = false;
  114. }
  115. //-----------------------------------------------------------------------------
  116. // Create necessary interfaces
  117. //-----------------------------------------------------------------------------
  118. bool CSteamApplication::Create()
  119. {
  120. FileSystem_SetErrorMode( FS_ERRORMODE_AUTO );
  121. char pFileSystemDLL[MAX_PATH];
  122. if ( !GetFileSystemDLLName( pFileSystemDLL, MAX_PATH, m_bSteam ) )
  123. return false;
  124. FileSystem_SetupSteamInstallPath();
  125. // Add in the cvar factory
  126. AppModule_t cvarModule = LoadModule( VStdLib_GetICVarFactory() );
  127. AddSystem( cvarModule, CVAR_INTERFACE_VERSION );
  128. AppModule_t fileSystemModule = LoadModule( pFileSystemDLL );
  129. m_pFileSystem = (IFileSystem*)AddSystem( fileSystemModule, FILESYSTEM_INTERFACE_VERSION );
  130. if ( !m_pFileSystem )
  131. {
  132. if( !IsPS3() )
  133. Error( "Unable to load %s", pFileSystemDLL );
  134. return false;
  135. }
  136. return true;
  137. }
  138. bool CSteamApplication::GetFileSystemDLLName( char *pOut, int nMaxBytes, bool &bIsSteam )
  139. {
  140. return FileSystem_GetFileSystemDLLName( pOut, nMaxBytes, bIsSteam ) == FS_OK;
  141. }
  142. //-----------------------------------------------------------------------------
  143. // The file system pointer is invalid at this point
  144. //-----------------------------------------------------------------------------
  145. void CSteamApplication::Destroy()
  146. {
  147. m_pFileSystem = NULL;
  148. }
  149. //-----------------------------------------------------------------------------
  150. // Pre-init, shutdown
  151. //-----------------------------------------------------------------------------
  152. bool CSteamApplication::PreInit()
  153. {
  154. return true;
  155. }
  156. void CSteamApplication::PostShutdown()
  157. {
  158. }
  159. //-----------------------------------------------------------------------------
  160. // Run steam main loop
  161. //-----------------------------------------------------------------------------
  162. int CSteamApplication::Main()
  163. {
  164. // Now that Steam is loaded, we can load up main libraries through steam
  165. if ( FileSystem_SetBasePaths( m_pFileSystem ) != FS_OK )
  166. return 0;
  167. m_pChildAppSystemGroup->Setup( m_pFileSystem, this );
  168. return m_pChildAppSystemGroup->Run();
  169. }
  170. //-----------------------------------------------------------------------------
  171. // Use this version in cases where you can't control the main loop and
  172. // expect to be ticked
  173. //-----------------------------------------------------------------------------
  174. int CSteamApplication::Startup()
  175. {
  176. int nRetVal = BaseClass::Startup();
  177. if ( GetCurrentStage() != RUNNING )
  178. return nRetVal;
  179. if ( FileSystem_SetBasePaths( m_pFileSystem ) != FS_OK )
  180. return 0;
  181. // Now that Steam is loaded, we can load up main libraries through steam
  182. m_pChildAppSystemGroup->Setup( m_pFileSystem, this );
  183. return m_pChildAppSystemGroup->Startup();
  184. }
  185. void CSteamApplication::Shutdown()
  186. {
  187. m_pChildAppSystemGroup->Shutdown();
  188. BaseClass::Shutdown();
  189. }