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.

413 lines
11 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ========//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include <windows.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <assert.h>
  12. #include <eh.h>
  13. #include "isys.h"
  14. #include "conproc.h"
  15. #include "dedicated.h"
  16. #include "engine_hlds_api.h"
  17. #include "checksum_md5.h"
  18. #include "tier0/dbg.h"
  19. #include "tier0/stacktools.h"
  20. #include "tier1/strtools.h"
  21. #include "tier0/icommandline.h"
  22. #include "inputsystem/iinputsystem.h"
  23. #include "SteamAppStartup.h"
  24. #include "console/textconsole.h"
  25. #include "vgui/vguihelpers.h"
  26. #include "appframework/appframework.h"
  27. #include "materialsystem/imaterialsystem.h"
  28. #include "istudiorender.h"
  29. #include "vgui/ivgui.h"
  30. #include "console/TextConsoleWin32.h"
  31. #include "icvar.h"
  32. #include "datacache/idatacache.h"
  33. #include "datacache/imdlcache.h"
  34. #include "vphysics_interface.h"
  35. #include "filesystem.h"
  36. #include "vscript/ivscript.h"
  37. #include "steam/steam_api.h"
  38. extern CTextConsoleWin32 console;
  39. extern bool g_bVGui;
  40. //-----------------------------------------------------------------------------
  41. // Purpose: Implements OS Specific layer ( loosely )
  42. //-----------------------------------------------------------------------------
  43. class CSys : public ISys
  44. {
  45. public:
  46. virtual ~CSys( void );
  47. virtual bool LoadModules( CDedicatedAppSystemGroup *pAppSystemGroup );
  48. void Sleep( int msec );
  49. bool GetExecutableName( char *out );
  50. void ErrorMessage( int level, const char *msg );
  51. void WriteStatusText( char *szText );
  52. void UpdateStatus( int force );
  53. long LoadLibrary( char *lib );
  54. void FreeLibrary( long library );
  55. bool CreateConsoleWindow( void );
  56. void DestroyConsoleWindow( void );
  57. void ConsoleOutput ( char *string );
  58. char *ConsoleInput (void);
  59. void Printf(const char *fmt, ...);
  60. };
  61. static CSys g_Sys;
  62. ISys *sys = &g_Sys;
  63. //-----------------------------------------------------------------------------
  64. // Purpose:
  65. //-----------------------------------------------------------------------------
  66. CSys::~CSys()
  67. {
  68. sys = NULL;
  69. }
  70. //-----------------------------------------------------------------------------
  71. // Purpose:
  72. // Input : msec -
  73. //-----------------------------------------------------------------------------
  74. void CSys::Sleep( int msec )
  75. {
  76. ::Sleep( msec );
  77. }
  78. //-----------------------------------------------------------------------------
  79. // Purpose:
  80. // Input : *lib -
  81. // Output : long
  82. //-----------------------------------------------------------------------------
  83. long CSys::LoadLibrary( char *lib )
  84. {
  85. void *hDll = ::LoadLibrary( lib );
  86. if ( hDll )
  87. StackToolsNotify_LoadedLibrary( lib );
  88. return (long)hDll;
  89. }
  90. //-----------------------------------------------------------------------------
  91. // Purpose:
  92. // Input : library -
  93. //-----------------------------------------------------------------------------
  94. void CSys::FreeLibrary( long library )
  95. {
  96. if ( !library )
  97. return;
  98. ::FreeLibrary( (HMODULE)library );
  99. }
  100. //-----------------------------------------------------------------------------
  101. // Purpose:
  102. // Input : *out -
  103. // Output : Returns true on success, false on failure.
  104. //-----------------------------------------------------------------------------
  105. bool CSys::GetExecutableName( char *out )
  106. {
  107. if ( !::GetModuleFileName( ( HINSTANCE )GetModuleHandle( NULL ), out, 256 ) )
  108. {
  109. return false;
  110. }
  111. return true;
  112. }
  113. //-----------------------------------------------------------------------------
  114. // Purpose:
  115. // Input : level -
  116. // *msg -
  117. //-----------------------------------------------------------------------------
  118. void CSys::ErrorMessage( int level, const char *msg )
  119. {
  120. MessageBox( NULL, msg, "Half-Life", MB_OK );
  121. PostQuitMessage(0);
  122. }
  123. //-----------------------------------------------------------------------------
  124. // Purpose:
  125. // Input : force -
  126. //-----------------------------------------------------------------------------
  127. void CSys::UpdateStatus( int force )
  128. {
  129. static double tLast = 0.0;
  130. double tCurrent;
  131. char szPrompt[256];
  132. int n, nMax;
  133. char szMap[64];
  134. char szHostname[128];
  135. float fps;
  136. if ( !engine )
  137. return;
  138. tCurrent = Sys_FloatTime();
  139. if ( !force )
  140. {
  141. if ( ( tCurrent - tLast ) < 0.5f )
  142. return;
  143. }
  144. tLast = tCurrent;
  145. engine->UpdateStatus( &fps, &n, &nMax, szMap, sizeof( szMap ) );
  146. engine->UpdateHostname( szHostname, sizeof( szHostname ) );
  147. console.SetTitle( szHostname );
  148. Q_snprintf( szPrompt, sizeof( szPrompt ), "%.1f fps %2i/%2i on map %16s", (float)fps, n, nMax, szMap);
  149. console.SetStatusLine(szPrompt);
  150. console.UpdateStatus();
  151. }
  152. //-----------------------------------------------------------------------------
  153. // Purpose:
  154. // Input : *string -
  155. // Output : void CSys::ConsoleOutput
  156. //-----------------------------------------------------------------------------
  157. void CSys::ConsoleOutput (char *string)
  158. {
  159. if ( g_bVGui )
  160. {
  161. VGUIPrintf( string );
  162. }
  163. else
  164. {
  165. console.Print(string);
  166. }
  167. }
  168. //-----------------------------------------------------------------------------
  169. // Purpose:
  170. // Input : *fmt -
  171. // ... -
  172. //-----------------------------------------------------------------------------
  173. void CSys::Printf(const char *fmt, ...)
  174. {
  175. // Dump text to debugging console.
  176. va_list argptr;
  177. char szText[1024];
  178. va_start (argptr, fmt);
  179. Q_vsnprintf (szText, sizeof( szText ), fmt, argptr);
  180. va_end (argptr);
  181. // Get Current text and append it.
  182. ConsoleOutput( szText );
  183. }
  184. //-----------------------------------------------------------------------------
  185. // Purpose:
  186. // Output : char *
  187. //-----------------------------------------------------------------------------
  188. char *CSys::ConsoleInput (void)
  189. {
  190. return console.GetLine();
  191. }
  192. //-----------------------------------------------------------------------------
  193. // Purpose:
  194. // Input : *szText -
  195. //-----------------------------------------------------------------------------
  196. void CSys::WriteStatusText( char *szText )
  197. {
  198. SetConsoleTitle( szText );
  199. }
  200. //-----------------------------------------------------------------------------
  201. // Purpose:
  202. // Output : Returns true on success, false on failure.
  203. //-----------------------------------------------------------------------------
  204. bool CSys::CreateConsoleWindow( void )
  205. {
  206. if ( !AllocConsole () )
  207. {
  208. return false;
  209. }
  210. InitConProc();
  211. return true;
  212. }
  213. //-----------------------------------------------------------------------------
  214. // Purpose:
  215. //-----------------------------------------------------------------------------
  216. void CSys::DestroyConsoleWindow( void )
  217. {
  218. FreeConsole ();
  219. // shut down QHOST hooks if necessary
  220. DeinitConProc ();
  221. }
  222. //-----------------------------------------------------------------------------
  223. // Loading modules used by the dedicated server.
  224. //-----------------------------------------------------------------------------
  225. bool CSys::LoadModules( CDedicatedAppSystemGroup *pAppSystemGroup )
  226. {
  227. AppSystemInfo_t appSystems[] =
  228. {
  229. { "engine.dll", CVAR_QUERY_INTERFACE_VERSION }, // NOTE: This one must be first!!
  230. { "soundemittersystem.dll", SOUNDEMITTERSYSTEM_INTERFACE_VERSION },
  231. { "inputsystem.dll", INPUTSYSTEM_INTERFACE_VERSION },
  232. { "inputsystem.dll", INPUTSTACKSYSTEM_INTERFACE_VERSION },
  233. { "materialsystem.dll", MATERIAL_SYSTEM_INTERFACE_VERSION },
  234. { "studiorender.dll", STUDIO_RENDER_INTERFACE_VERSION },
  235. { "vphysics.dll", VPHYSICS_INTERFACE_VERSION },
  236. { "datacache.dll", DATACACHE_INTERFACE_VERSION },
  237. { "datacache.dll", MDLCACHE_INTERFACE_VERSION },
  238. { "datacache.dll", STUDIO_DATA_CACHE_INTERFACE_VERSION },
  239. { "vgui2.dll", VGUI_IVGUI_INTERFACE_VERSION },
  240. #ifndef DOTA_DLL
  241. { "vscript.dll", VSCRIPT_INTERFACE_VERSION },
  242. #endif
  243. { "engine.dll", VENGINE_HLDS_API_VERSION },
  244. { "", "" } // Required to terminate the list
  245. };
  246. if ( !pAppSystemGroup->AddSystems( appSystems ) )
  247. return false;
  248. engine = (IDedicatedServerAPI *)pAppSystemGroup->FindSystem( VENGINE_HLDS_API_VERSION );
  249. IMaterialSystem* pMaterialSystem = (IMaterialSystem*)pAppSystemGroup->FindSystem( MATERIAL_SYSTEM_INTERFACE_VERSION );
  250. pMaterialSystem->SetShaderAPI( "shaderapiempty.dll" );
  251. return true;
  252. }
  253. //-----------------------------------------------------------------------------
  254. // Purpose:
  255. //-----------------------------------------------------------------------------
  256. bool NET_Init( void )
  257. {
  258. // Startup winock
  259. WORD version = MAKEWORD( 1, 1 );
  260. WSADATA wsaData;
  261. int err = WSAStartup( version, &wsaData );
  262. if ( err != 0 )
  263. {
  264. char msg[ 256 ];
  265. Q_snprintf( msg, sizeof( msg ), "Winsock 1.1 unavailable...\n" );
  266. sys->Printf( "%s", msg );
  267. Plat_DebugString( msg );
  268. return false;
  269. }
  270. return true;
  271. }
  272. //-----------------------------------------------------------------------------
  273. // Purpose:
  274. //-----------------------------------------------------------------------------
  275. void NET_Shutdown( void )
  276. {
  277. // Kill winsock
  278. WSACleanup();
  279. }
  280. //-----------------------------------------------------------------------------
  281. // Purpose:
  282. // Input : hInstance -
  283. // hPrevInstance -
  284. // lpszCmdLine -
  285. // nCmdShow -
  286. // Output : int PASCAL
  287. //-----------------------------------------------------------------------------
  288. int main(int argc, char **argv); // in sys_ded.cpp
  289. static char *GetBaseDir( const char *pszBuffer )
  290. {
  291. static char basedir[ MAX_PATH ];
  292. char szBuffer[ MAX_PATH ];
  293. int j;
  294. char *pBuffer = NULL;
  295. strcpy( szBuffer, pszBuffer );
  296. pBuffer = strrchr( szBuffer,'\\' );
  297. if ( pBuffer )
  298. {
  299. *(pBuffer+1) = '\0';
  300. }
  301. strcpy( basedir, szBuffer );
  302. j = strlen( basedir );
  303. if (j > 0)
  304. {
  305. if ( ( basedir[ j-1 ] == '\\' ) ||
  306. ( basedir[ j-1 ] == '/' ) )
  307. {
  308. basedir[ j-1 ] = 0;
  309. }
  310. }
  311. return basedir;
  312. }
  313. void MiniDumpFunction( unsigned int nExceptionCode, EXCEPTION_POINTERS *pException )
  314. {
  315. SteamAPI_WriteMiniDump( nExceptionCode, pException, 0 );
  316. }
  317. extern "C" __declspec(dllexport) int DedicatedMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
  318. {
  319. SetAppInstance( hInstance );
  320. // Check that we are running on Win32
  321. OSVERSIONINFO vinfo;
  322. vinfo.dwOSVersionInfoSize = sizeof(vinfo);
  323. if ( !GetVersionEx ( &vinfo ) )
  324. return -1;
  325. if ( vinfo.dwPlatformId == VER_PLATFORM_WIN32s )
  326. return -1;
  327. int argc, iret = -1;
  328. LPWSTR * argv= CommandLineToArgvW(GetCommandLineW(),&argc);
  329. CommandLine()->CreateCmdLine( GetCommandLine() );
  330. if ( !Plat_IsInDebugSession() && !CommandLine()->FindParm( "-nominidumps") )
  331. {
  332. // This warning is not actually true in this context.
  333. #pragma warning( suppress : 4535 ) // warning C4535: calling _set_se_translator() requires /EHa
  334. _set_se_translator( MiniDumpFunction );
  335. try // this try block allows the SE translator to work
  336. {
  337. iret = main(argc,(char **)argv);
  338. }
  339. catch( ... )
  340. {
  341. return -1;
  342. }
  343. }
  344. else
  345. {
  346. iret = main(argc,(char **)argv);
  347. }
  348. GlobalFree( argv );
  349. return iret;
  350. }