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.

174 lines
4.4 KiB

  1. //========= Copyright 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. //-----------------------------------------------------------------------------
  9. // This is just a little redirection tool so I can get all the dlls in bin
  10. //-----------------------------------------------------------------------------
  11. #include <stdio.h>
  12. #ifdef _WIN32
  13. #include <windows.h>
  14. #include <assert.h>
  15. #include <direct.h>
  16. #elif POSIX
  17. #include <stdlib.h>
  18. #include <dlfcn.h>
  19. #include <string.h>
  20. #include <limits.h>
  21. #include <errno.h>
  22. #include <unistd.h>
  23. #define MAX_PATH PATH_MAX
  24. #endif
  25. #include "tier0/basetypes.h"
  26. #ifdef _WIN32
  27. typedef int (*DedicatedMain_t)( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28. LPSTR lpCmdLine, int nCmdShow );
  29. #elif POSIX
  30. typedef int (*DedicatedMain_t)( int argc, char *argv[] );
  31. #endif
  32. //-----------------------------------------------------------------------------
  33. // Purpose: Return the directory where this .exe is running from
  34. // Output : char
  35. //-----------------------------------------------------------------------------
  36. static char *GetBaseDir( const char *pszBuffer )
  37. {
  38. static char basedir[ MAX_PATH ];
  39. char szBuffer[ MAX_PATH ];
  40. size_t j;
  41. char *pBuffer = NULL;
  42. strcpy( szBuffer, pszBuffer );
  43. pBuffer = strrchr( szBuffer,'\\' );
  44. if ( pBuffer )
  45. {
  46. *(pBuffer+1) = '\0';
  47. }
  48. strcpy( basedir, szBuffer );
  49. j = strlen( basedir );
  50. if (j > 0)
  51. {
  52. if ( ( basedir[ j-1 ] == '\\' ) ||
  53. ( basedir[ j-1 ] == '/' ) )
  54. {
  55. basedir[ j-1 ] = 0;
  56. }
  57. }
  58. return basedir;
  59. }
  60. #ifdef _WIN32
  61. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
  62. {
  63. // Must add 'bin' to the path....
  64. char* pPath = getenv("PATH");
  65. // Use the .EXE name to determine the root directory
  66. char moduleName[ MAX_PATH ];
  67. char szBuffer[ 4096 ];
  68. if ( !GetModuleFileName( hInstance, moduleName, MAX_PATH ) )
  69. {
  70. MessageBox( 0, "Failed calling GetModuleFileName", "Launcher Error", MB_OK );
  71. return 0;
  72. }
  73. // Get the root directory the .exe is in
  74. char* pRootDir = GetBaseDir( moduleName );
  75. #ifdef _DEBUG
  76. int len =
  77. #endif
  78. _snprintf( szBuffer, sizeof( szBuffer ) - 1, "PATH=%s\\bin\\;%s", pRootDir, pPath );
  79. szBuffer[ sizeof(szBuffer) - 1 ] = 0;
  80. assert( len < 4096 );
  81. _putenv( szBuffer );
  82. HINSTANCE launcher = LoadLibrary( "bin\\dedicated" DLL_EXT_STRING ); // STEAM OK ... filesystem not mounted yet
  83. if (!launcher)
  84. {
  85. char *pszError;
  86. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&pszError, 0, NULL);
  87. char szBuf[1024];
  88. _snprintf(szBuf, sizeof( szBuf ) - 1, "Failed to load the launcher DLL:\n\n%s", pszError);
  89. szBuf[ sizeof(szBuf) - 1 ] = 0;
  90. MessageBox( 0, szBuf, "Launcher Error", MB_OK );
  91. LocalFree(pszError);
  92. return 0;
  93. }
  94. DedicatedMain_t main = (DedicatedMain_t)GetProcAddress( launcher, "DedicatedMain" );
  95. return main( hInstance, hPrevInstance, lpCmdLine, nCmdShow );
  96. }
  97. #elif defined(POSIX)
  98. #define stringize(a) #a
  99. #define dedicated_binary(a,b,c) a stringize(b) c
  100. int main( int argc, char *argv[] )
  101. {
  102. // Must add 'bin' to the path....
  103. char* pPath = getenv("LD_LIBRARY_PATH");
  104. char szBuffer[4096];
  105. char cwd[ MAX_PATH ];
  106. if ( !getcwd( cwd, sizeof(cwd)) )
  107. {
  108. printf( "getcwd failed (%s)", strerror(errno));
  109. }
  110. snprintf( szBuffer, sizeof( szBuffer ) - 1, "LD_LIBRARY_PATH=%s/bin:%s", cwd, pPath );
  111. printf( "%s\n", szBuffer );
  112. int ret = putenv( szBuffer );
  113. if ( ret )
  114. {
  115. printf( "%s\n", strerror(errno) );
  116. }
  117. void *tier0 = dlopen( "libtier0" DLL_EXT_STRING, RTLD_NOW );
  118. if ( !tier0 )
  119. {
  120. printf( "Failed to open %s (%s)\n", "libtier0" DLL_EXT_STRING, dlerror());
  121. return -1;
  122. }
  123. void *vstdlib = dlopen( "libvstdlib" DLL_EXT_STRING, RTLD_NOW );
  124. if ( !vstdlib )
  125. {
  126. printf( "Failed to open %s (%s)\n", "libvstdlib" DLL_EXT_STRING, dlerror());
  127. return -1;
  128. }
  129. const char *pBinaryName = "dedicated" DLL_EXT_STRING;
  130. void *dedicated = dlopen( pBinaryName, RTLD_NOW );
  131. if ( !dedicated )
  132. {
  133. printf( "Failed to open %s (%s)\n", pBinaryName, dlerror());
  134. return -1;
  135. }
  136. DedicatedMain_t main = (DedicatedMain_t)dlsym( dedicated, "DedicatedMain" );
  137. if ( !main )
  138. {
  139. printf( "Failed to find dedicated server entry point (%s)\n", dlerror() );
  140. return -1;
  141. }
  142. ret = main( argc,argv );
  143. dlclose( dedicated );
  144. dlclose( vstdlib );
  145. dlclose( tier0 );
  146. }
  147. #endif