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.

313 lines
6.7 KiB

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