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.

248 lines
7.3 KiB

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