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.

298 lines
6.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include <unistd.h>
  8. #include <string.h>
  9. #include <dlfcn.h>
  10. #include <stdarg.h>
  11. #include <sys/types.h>
  12. #include <sys/time.h>
  13. #include <malloc.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <dlfcn.h>
  17. #include "isys.h"
  18. #include "console/conproc.h"
  19. #include "dedicated.h"
  20. #include "engine_hlds_api.h"
  21. #include "checksum_md5.h"
  22. #include "idedicatedexports.h"
  23. #include "tier0/vcrmode.h"
  24. #include "tier0/dbg.h"
  25. #include "mathlib/mathlib.h"
  26. #include "interface.h"
  27. #include "tier1/strtools.h"
  28. #include "tier0/icommandline.h"
  29. #include "materialsystem/imaterialsystem.h"
  30. #include "istudiorender.h"
  31. #include "SoundEmitterSystem/isoundemittersystembase.h"
  32. #include "datacache/idatacache.h"
  33. #include "datacache/imdlcache.h"
  34. #include "vphysics_interface.h"
  35. #include "icvar.h"
  36. #include "filesystem/IQueuedLoader.h"
  37. #include "console/TextConsoleUnix.h"
  38. bool InitInstance( );
  39. char g_szEXEName[ MAX_PATH ];
  40. extern CTextConsoleUnix console;
  41. //-----------------------------------------------------------------------------
  42. // Purpose: Implements OS Specific layer ( loosely )
  43. //-----------------------------------------------------------------------------
  44. class CSys : public ISys
  45. {
  46. public:
  47. virtual ~CSys();
  48. virtual bool LoadModules( CDedicatedAppSystemGroup *pAppSystemGroup );
  49. void Sleep( int msec );
  50. bool GetExecutableName( char *out );
  51. void ErrorMessage( int level, const char *msg );
  52. void WriteStatusText( char *szText );
  53. void UpdateStatus( int force );
  54. long LoadLibrary( char *lib );
  55. void FreeLibrary( long library );
  56. void *GetProcAddress( long library, const char *name );
  57. bool CreateConsoleWindow( void );
  58. void DestroyConsoleWindow( void );
  59. void ConsoleOutput ( char *string );
  60. char *ConsoleInput ( int index, char *buf, int buflen );
  61. void Printf( const char *fmt, ...);
  62. };
  63. static CSys g_Sys;
  64. ISys *sys = &g_Sys;
  65. //-----------------------------------------------------------------------------
  66. // Purpose:
  67. //-----------------------------------------------------------------------------
  68. CSys::~CSys()
  69. {
  70. sys = NULL;
  71. }
  72. //-----------------------------------------------------------------------------
  73. // Purpose:
  74. // Input : msec
  75. // Output :
  76. //-----------------------------------------------------------------------------
  77. void CSys::Sleep( int msec )
  78. {
  79. usleep(msec * 1000);
  80. }
  81. //-----------------------------------------------------------------------------
  82. // Purpose:
  83. // Input : handle, function name-
  84. // Output : void *
  85. //-----------------------------------------------------------------------------
  86. void *CSys::GetProcAddress( long library, const char *name )
  87. {
  88. return dlsym( library, name );
  89. }
  90. //-----------------------------------------------------------------------------
  91. // Purpose:
  92. // Input : *lib -
  93. // Output : long
  94. //-----------------------------------------------------------------------------
  95. long CSys::LoadLibrary( char *lib )
  96. {
  97. void *hDll = NULL;
  98. char cwd[1024];
  99. char absolute_lib[1024];
  100. if (!getcwd(cwd, sizeof(cwd)))
  101. ErrorMessage(1, "Sys_LoadLibrary: Couldn't determine current directory.");
  102. if (cwd[strlen(cwd)-1] == '/')
  103. cwd[strlen(cwd)-1] = 0;
  104. Q_snprintf(absolute_lib, sizeof( absolute_lib ), "%s/%s", cwd, lib);
  105. hDll = dlopen( absolute_lib, RTLD_NOW );
  106. if ( !hDll )
  107. {
  108. ErrorMessage( 1, dlerror() );
  109. }
  110. return (long)hDll;
  111. }
  112. void CSys::FreeLibrary( long library )
  113. {
  114. if ( !library )
  115. return;
  116. dlclose( (void *)library );
  117. }
  118. bool CSys::GetExecutableName( char *out )
  119. {
  120. char *name = strrchr(g_szEXEName, '/' );
  121. if ( name )
  122. {
  123. strcpy( out, name + 1);
  124. return true;
  125. }
  126. else
  127. {
  128. return false;
  129. }
  130. }
  131. /*
  132. ==============
  133. ErrorMessage
  134. Engine is erroring out, display error in message box
  135. ==============
  136. */
  137. void CSys::ErrorMessage( int level, const char *msg )
  138. {
  139. Error( "%s\n", msg );
  140. exit( -1 );
  141. }
  142. void CSys::UpdateStatus( int force )
  143. {
  144. }
  145. /*
  146. ================
  147. ConsoleOutput
  148. Print text to the dedicated console
  149. ================
  150. */
  151. void CSys::ConsoleOutput (char *string)
  152. {
  153. console.Print(string);
  154. }
  155. /*
  156. ==============
  157. Printf
  158. Engine is printing to console
  159. ==============
  160. */
  161. void CSys::Printf( const char *fmt, ...)
  162. {
  163. // Dump text to debugging console.
  164. va_list argptr;
  165. char szText[1024];
  166. va_start (argptr, fmt);
  167. Q_vsnprintf (szText, sizeof( szText ), fmt, argptr);
  168. va_end (argptr);
  169. // Get Current text and append it.
  170. ConsoleOutput( szText );
  171. }
  172. /*
  173. ================
  174. ConsoleInput
  175. ================
  176. */
  177. char *CSys::ConsoleInput( int index, char *buf, int buflen )
  178. {
  179. return console.GetLine( index, buf, buflen );
  180. }
  181. /*
  182. ==============
  183. WriteStatusText
  184. ==============
  185. */
  186. void CSys::WriteStatusText( char *szText )
  187. {
  188. }
  189. /*
  190. ==============
  191. CreateConsoleWindow
  192. Create console window ( overridable? )
  193. ==============
  194. */
  195. bool CSys::CreateConsoleWindow( void )
  196. {
  197. return true;
  198. }
  199. /*
  200. ==============
  201. DestroyConsoleWindow
  202. ==============
  203. */
  204. void CSys::DestroyConsoleWindow( void )
  205. {
  206. }
  207. /*
  208. ================
  209. GameInit
  210. ================
  211. */
  212. bool CSys::LoadModules( CDedicatedAppSystemGroup *pAppSystemGroup )
  213. {
  214. AppSystemInfo_t appSystems[] =
  215. {
  216. { "engine" DLL_EXT_STRING, CVAR_QUERY_INTERFACE_VERSION },
  217. { "soundemittersystem" DLL_EXT_STRING, SOUNDEMITTERSYSTEM_INTERFACE_VERSION }, // loaded for backwards compatability, prevents crash on exit for old game dlls
  218. { "materialsystem" DLL_EXT_STRING, MATERIAL_SYSTEM_INTERFACE_VERSION },
  219. { "studiorender" DLL_EXT_STRING, STUDIO_RENDER_INTERFACE_VERSION },
  220. { "vphysics" DLL_EXT_STRING, VPHYSICS_INTERFACE_VERSION },
  221. { "datacache" DLL_EXT_STRING, DATACACHE_INTERFACE_VERSION },
  222. { "datacache" DLL_EXT_STRING, MDLCACHE_INTERFACE_VERSION },
  223. { "datacache" DLL_EXT_STRING, STUDIO_DATA_CACHE_INTERFACE_VERSION },
  224. { "dedicated" DLL_EXT_STRING, QUEUEDLOADER_INTERFACE_VERSION },
  225. { "engine" DLL_EXT_STRING, VENGINE_HLDS_API_VERSION },
  226. { "", "" } // Required to terminate the list
  227. };
  228. if ( !pAppSystemGroup->AddSystems( appSystems ) )
  229. return false;
  230. engine = (IDedicatedServerAPI *)pAppSystemGroup->FindSystem( VENGINE_HLDS_API_VERSION );
  231. // obsolete i think SetCVarIF( (ICvar*)pAppSystemGroup->FindSystem( VENGINE_CVAR_INTERFACE_VERSION ) );
  232. IMaterialSystem* pMaterialSystem = (IMaterialSystem*)pAppSystemGroup->FindSystem( MATERIAL_SYSTEM_INTERFACE_VERSION );
  233. pMaterialSystem->SetShaderAPI( "shaderapiempty" DLL_EXT_STRING );
  234. return true;
  235. }
  236. bool NET_Init()
  237. {
  238. return true;
  239. }
  240. void NET_Shutdown()
  241. {
  242. }
  243. extern int main(int argc, char *argv[]);
  244. DLL_EXPORT int DedicatedMain( int argc, char *argv[] );
  245. int DedicatedMain( int argc, char *argv[] )
  246. {
  247. return main(argc,argv);
  248. }