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.

300 lines
6.2 KiB

  1. //========= Copyright 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/vcrmode.h"
  21. #include "tier0/dbg.h"
  22. #include "tier1/strtools.h"
  23. #include "tier0/icommandline.h"
  24. #include "idedicatedexports.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. /*
  37. ==============
  38. Load3rdParty
  39. Load support for third party .dlls ( gamehost )
  40. ==============
  41. */
  42. void Load3rdParty( void )
  43. {
  44. // Only do this if the server operator wants the support.
  45. // ( In case of malicious code, too )
  46. if ( CommandLine()->CheckParm( "-usegh" ) )
  47. {
  48. hDLLThirdParty = sys->LoadLibrary( "ghostinj.dll" );
  49. }
  50. }
  51. /*
  52. ==============
  53. EF_VID_ForceUnlockedAndReturnState
  54. Dummy funcion called by engine
  55. ==============
  56. */
  57. int EF_VID_ForceUnlockedAndReturnState(void)
  58. {
  59. return 0;
  60. }
  61. /*
  62. ==============
  63. EF_VID_ForceLockState
  64. Dummy funcion called by engine
  65. ==============
  66. */
  67. void EF_VID_ForceLockState(int)
  68. {
  69. }
  70. /*
  71. ==============
  72. InitInstance
  73. ==============
  74. */
  75. bool InitInstance( )
  76. {
  77. Load3rdParty();
  78. return true;
  79. }
  80. /*
  81. ==============
  82. ProcessConsoleInput
  83. ==============
  84. */
  85. int ProcessConsoleInput(void)
  86. {
  87. char *s;
  88. int count = 0;
  89. if ( engine )
  90. {
  91. do
  92. {
  93. char szBuf[ 256 ];
  94. s = sys->ConsoleInput( count++, szBuf, sizeof( szBuf ) );
  95. if (s && s[0] )
  96. {
  97. V_strcat_safe( szBuf, "\n" );
  98. engine->AddConsoleText ( szBuf );
  99. }
  100. } while (s);
  101. }
  102. return count;
  103. }
  104. void RunServer( void );
  105. class CDedicatedExports : public CBaseAppSystem<IDedicatedExports>
  106. {
  107. public:
  108. virtual void Sys_Printf( char *text )
  109. {
  110. if ( sys )
  111. {
  112. sys->Printf( "%s", text );
  113. }
  114. }
  115. virtual void RunServer()
  116. {
  117. void RunServer( void );
  118. ::RunServer();
  119. }
  120. };
  121. EXPOSE_SINGLE_INTERFACE( CDedicatedExports, IDedicatedExports, VENGINE_DEDICATEDEXPORTS_API_VERSION );
  122. static const char *get_consolelog_filename()
  123. {
  124. static bool s_bInited = false;
  125. static char s_consolelog[ MAX_PATH ];
  126. if ( !s_bInited )
  127. {
  128. s_bInited = true;
  129. // Don't do the -consolelog thing if -consoledebug is present.
  130. // CTextConsoleUnix::Print() looks for -consoledebug.
  131. const char *filename = NULL;
  132. if ( !CommandLine()->FindParm( "-consoledebug" ) &&
  133. CommandLine()->CheckParm( "-consolelog", &filename ) &&
  134. filename )
  135. {
  136. V_strcpy_safe( s_consolelog, filename );
  137. }
  138. }
  139. return s_consolelog;
  140. }
  141. SpewRetval_t DedicatedSpewOutputFunc( SpewType_t spewType, char const *pMsg )
  142. {
  143. if ( sys )
  144. {
  145. sys->Printf( "%s", pMsg );
  146. // If they have specified -consolelog, log this message there. Otherwise these
  147. // wind up being lost because Sys_InitGame hasn't been called yet, and
  148. // Sys_SpewFunc is the thing that logs stuff to -consolelog, etc.
  149. const char *filename = get_consolelog_filename();
  150. if ( filename[ 0 ] && pMsg[ 0 ] )
  151. {
  152. FileHandle_t fh = g_pFullFileSystem->Open( filename, "a" );
  153. if ( fh != FILESYSTEM_INVALID_HANDLE )
  154. {
  155. g_pFullFileSystem->Write( pMsg, V_strlen( pMsg ), fh );
  156. g_pFullFileSystem->Close( fh );
  157. }
  158. }
  159. }
  160. #ifdef _WIN32
  161. Plat_DebugString( pMsg );
  162. #endif
  163. if (spewType == SPEW_ERROR)
  164. {
  165. // In Windows vgui mode, make a message box or they won't ever see the error.
  166. #ifdef _WIN32
  167. extern bool g_bVGui;
  168. if ( g_bVGui )
  169. {
  170. MessageBox( NULL, pMsg, "Error", MB_OK | MB_TASKMODAL );
  171. }
  172. TerminateProcess( GetCurrentProcess(), 1 );
  173. #elif POSIX
  174. fflush(stdout);
  175. _exit(1);
  176. #else
  177. #error "Implement me"
  178. #endif
  179. return SPEW_ABORT;
  180. }
  181. if (spewType == SPEW_ASSERT)
  182. {
  183. if ( CommandLine()->FindParm( "-noassert" ) == 0 )
  184. return SPEW_DEBUGGER;
  185. else
  186. return SPEW_CONTINUE;
  187. }
  188. return SPEW_CONTINUE;
  189. }
  190. int Sys_GetExecutableName( char *out )
  191. {
  192. #ifdef _WIN32
  193. if ( !::GetModuleFileName( ( HINSTANCE )GetModuleHandle( NULL ), out, 256 ) )
  194. {
  195. return 0;
  196. }
  197. #else
  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. }