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
6.1 KiB

  1. //===== Copyright 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #ifdef _WIN32
  8. #include <windows.h>
  9. #elif POSIX
  10. #include <unistd.h>
  11. #else
  12. #error
  13. #endif
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include "isys.h"
  17. #include "dedicated.h"
  18. #include "engine_hlds_api.h"
  19. #include "filesystem.h"
  20. #include "tier0/dbg.h"
  21. #include "tier1/strtools.h"
  22. #include "tier0/icommandline.h"
  23. #include "idedicatedexports.h"
  24. #include "mathlib/expressioncalculator.h"
  25. #include "vgui/vguihelpers.h"
  26. static long hDLLThirdParty = 0L;
  27. //-----------------------------------------------------------------------------
  28. // Modules...
  29. //-----------------------------------------------------------------------------
  30. CSysModule *s_hMatSystemModule = NULL;
  31. CSysModule *s_hEngineModule = NULL;
  32. CSysModule *s_hSoundEmitterModule = NULL;
  33. CreateInterfaceFn s_MaterialSystemFactory;
  34. CreateInterfaceFn s_EngineFactory;
  35. CreateInterfaceFn s_SoundEmitterFactory;
  36. #ifdef _WIN32
  37. extern bool g_bVGui;
  38. #endif
  39. /*
  40. ==============
  41. Load3rdParty
  42. Load support for third party .dlls ( gamehost )
  43. ==============
  44. */
  45. void Load3rdParty( void )
  46. {
  47. // Only do this if the server operator wants the support.
  48. // ( In case of malicious code, too )
  49. if ( CommandLine()->CheckParm( "-usegh" ) )
  50. {
  51. hDLLThirdParty = sys->LoadLibrary( "ghostinj.dll" );
  52. }
  53. }
  54. /*
  55. ==============
  56. EF_VID_ForceUnlockedAndReturnState
  57. Dummy funcion called by engine
  58. ==============
  59. */
  60. int EF_VID_ForceUnlockedAndReturnState(void)
  61. {
  62. return 0;
  63. }
  64. /*
  65. ==============
  66. EF_VID_ForceLockState
  67. Dummy funcion called by engine
  68. ==============
  69. */
  70. void EF_VID_ForceLockState(int)
  71. {
  72. }
  73. /*
  74. ==============
  75. InitInstance
  76. ==============
  77. */
  78. bool InitInstance( )
  79. {
  80. Load3rdParty();
  81. return true;
  82. }
  83. /*
  84. ==============
  85. ProcessConsoleInput
  86. ==============
  87. */
  88. void ProcessConsoleInput( void )
  89. {
  90. char *s;
  91. if ( !engine )
  92. return;
  93. do
  94. {
  95. s = sys->ConsoleInput();
  96. if (s)
  97. {
  98. char szBuf[ 256 ];
  99. Q_snprintf( szBuf, sizeof( szBuf ), "%s\n", s );
  100. engine->AddConsoleText ( szBuf );
  101. }
  102. } while (s);
  103. }
  104. #ifdef _WIN32
  105. extern bool g_bVGui;
  106. #endif
  107. class CDedicatedExports : public CBaseAppSystem<IDedicatedExports>
  108. {
  109. public:
  110. virtual void Sys_Printf( char *text )
  111. {
  112. if ( sys )
  113. {
  114. sys->Printf( "%s", text );
  115. }
  116. }
  117. virtual void RunServer( void );
  118. virtual bool IsGuiDedicatedServer();
  119. };
  120. void PerformCommandLineSubstitutions( int nIndex )
  121. {
  122. // modify the command line, replacing all occurrences of ## with nIndex
  123. for( int i = 0; i < CommandLine()->ParmCount(); i++ )
  124. {
  125. char newBuf[2048];
  126. char const *ppParm = CommandLine()->GetParm( i );
  127. V_strncpy( newBuf, ppParm, sizeof( newBuf ) );
  128. bool bDidReplace = false;
  129. bool bWasExpression = false;
  130. for(;;)
  131. {
  132. char *pReplace = V_strstr( newBuf, "##" );
  133. if (! pReplace )
  134. break;
  135. pReplace[0] = '0' + ( nIndex / 10 );
  136. pReplace[1] = '0' + ( nIndex % 10 );
  137. bDidReplace = true;
  138. if ( ( pReplace != newBuf ) &&
  139. ( strchr( "+-/*", pReplace[-1] ) ) ) // is this an expression involving "##"?
  140. {
  141. bWasExpression = true;
  142. }
  143. }
  144. if ( bDidReplace )
  145. {
  146. if ( bWasExpression )
  147. {
  148. sprintf( newBuf, "%d", ( int ) ( EvaluateExpression( newBuf, -1 ) ) );
  149. }
  150. printf("setparm %d %s\n", i, newBuf );
  151. CommandLine()->SetParm( i, newBuf );
  152. }
  153. }
  154. }
  155. #ifdef _LINUX // linux uses the implementation in sys_subproc
  156. void CDedicatedExports::RunServer( void )
  157. {
  158. // check for forking
  159. char const *pForkParam = CommandLine()->ParmValue( "-fork" );
  160. if ( pForkParam )
  161. {
  162. int nNumChildInstances = atoi( pForkParam );
  163. if ( nNumChildInstances >= 1 )
  164. {
  165. RunServerSubProcesses( nNumChildInstances );
  166. }
  167. }
  168. else
  169. {
  170. ::RunServer( false );
  171. }
  172. }
  173. #else
  174. void CDedicatedExports::RunServer( void )
  175. {
  176. PerformCommandLineSubstitutions( 0 );
  177. ::RunServer( false );
  178. }
  179. #endif
  180. bool CDedicatedExports::IsGuiDedicatedServer()
  181. {
  182. #ifndef _WIN32
  183. return false;
  184. #else
  185. return g_bVGui;
  186. #endif
  187. }
  188. EXPOSE_SINGLE_INTERFACE( CDedicatedExports, IDedicatedExports, VENGINE_DEDICATEDEXPORTS_API_VERSION );
  189. int Sys_GetExecutableName( char *out )
  190. {
  191. #ifdef _WIN32
  192. if ( !::GetModuleFileName( ( HINSTANCE )GetModuleHandle( NULL ), out, 256 ) )
  193. {
  194. return 0;
  195. }
  196. #else
  197. extern char g_szEXEName[ 256 ];
  198. strcpy( out, g_szEXEName );
  199. #endif
  200. return 1;
  201. }
  202. //-----------------------------------------------------------------------------
  203. // Purpose: Return the directory where this .exe is running from
  204. // Output : char
  205. //-----------------------------------------------------------------------------
  206. const char *UTIL_GetExecutableDir( )
  207. {
  208. static char exedir[ MAX_PATH ];
  209. exedir[ 0 ] = 0;
  210. if ( !Sys_GetExecutableName(exedir) )
  211. return NULL;
  212. char *pSlash;
  213. char *pSlash2;
  214. pSlash = strrchr( exedir,'\\' );
  215. pSlash2 = strrchr( exedir,'/' );
  216. if ( pSlash2 > pSlash )
  217. {
  218. pSlash = pSlash2;
  219. }
  220. if (pSlash)
  221. {
  222. *pSlash = 0;
  223. }
  224. // Return the bin directory as the executable dir if it's not in there
  225. // because that's really where we're running from...
  226. int exeLen = strlen(exedir);
  227. if ( exedir[exeLen-4] != CORRECT_PATH_SEPARATOR ||
  228. exedir[exeLen-3] != 'b' ||
  229. exedir[exeLen-2] != 'i' ||
  230. exedir[exeLen-1] != 'n' )
  231. {
  232. Q_strncat( exedir, "\\bin", sizeof( exedir ), COPY_ALL_CHARACTERS );
  233. Q_FixSlashes( exedir );
  234. }
  235. return exedir;
  236. }
  237. //-----------------------------------------------------------------------------
  238. // Purpose: Return the directory where this .exe is running from
  239. // Output : char
  240. //-----------------------------------------------------------------------------
  241. const char *UTIL_GetBaseDir( void )
  242. {
  243. static char basedir[ MAX_PATH ];
  244. char const *pOverrideDir = CommandLine()->CheckParm( "-basedir" );
  245. if ( pOverrideDir )
  246. return pOverrideDir;
  247. basedir[ 0 ] = 0;
  248. const char *pExeDir = UTIL_GetExecutableDir( );
  249. if ( pExeDir )
  250. {
  251. strcpy( basedir, pExeDir );
  252. int dirlen = strlen( basedir );
  253. if ( basedir[ dirlen - 3 ] == 'b' &&
  254. basedir[ dirlen - 2 ] == 'i' &&
  255. basedir[ dirlen - 1 ] == 'n' )
  256. {
  257. basedir[ dirlen - 4 ] = 0;
  258. }
  259. }
  260. return basedir;
  261. }