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.

145 lines
3.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. // vrad_launcher.cpp : Defines the entry point for the console application.
  9. //
  10. #include "stdafx.h"
  11. #include <direct.h>
  12. #include "tier1/strtools.h"
  13. #include "tier0/icommandline.h"
  14. char* GetLastErrorString()
  15. {
  16. static char err[2048];
  17. LPVOID lpMsgBuf;
  18. FormatMessage(
  19. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  20. FORMAT_MESSAGE_FROM_SYSTEM |
  21. FORMAT_MESSAGE_IGNORE_INSERTS,
  22. NULL,
  23. GetLastError(),
  24. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  25. (LPTSTR) &lpMsgBuf,
  26. 0,
  27. NULL
  28. );
  29. strncpy( err, (char*)lpMsgBuf, sizeof( err ) );
  30. LocalFree( lpMsgBuf );
  31. err[ sizeof( err ) - 1 ] = 0;
  32. return err;
  33. }
  34. void MakeFullPath( const char *pIn, char *pOut, int outLen )
  35. {
  36. if ( pIn[0] == '/' || pIn[0] == '\\' || pIn[1] == ':' )
  37. {
  38. // It's already a full path.
  39. Q_strncpy( pOut, pIn, outLen );
  40. }
  41. else
  42. {
  43. _getcwd( pOut, outLen );
  44. Q_strncat( pOut, "\\", outLen, COPY_ALL_CHARACTERS );
  45. Q_strncat( pOut, pIn, outLen, COPY_ALL_CHARACTERS );
  46. }
  47. }
  48. int main(int argc, char* argv[])
  49. {
  50. char dllName[512];
  51. CommandLine()->CreateCmdLine( argc, argv );
  52. // check whether they used the -both switch. If this is specified, vrad will be run
  53. // twice, once with -hdr and once without
  54. int both_arg=0;
  55. for(int arg=1;arg<argc;arg++)
  56. if (Q_stricmp(argv[arg],"-both")==0)
  57. {
  58. both_arg=arg;
  59. }
  60. char fullPath[512], redirectFilename[512];
  61. MakeFullPath( argv[0], fullPath, sizeof( fullPath ) );
  62. Q_StripFilename( fullPath );
  63. Q_snprintf( redirectFilename, sizeof( redirectFilename ), "%s\\%s", fullPath, "vrad.redirect" );
  64. // First, look for vrad.redirect and load the dll specified in there if possible.
  65. CSysModule *pModule = NULL;
  66. FILE *fp = fopen( redirectFilename, "rt" );
  67. if ( fp )
  68. {
  69. if ( fgets( dllName, sizeof( dllName ), fp ) )
  70. {
  71. char *pEnd = strstr( dllName, "\n" );
  72. if ( pEnd )
  73. *pEnd = 0;
  74. pModule = Sys_LoadModule( dllName );
  75. if ( pModule )
  76. printf( "Loaded alternate VRAD DLL (%s) specified in vrad.redirect.\n", dllName );
  77. else
  78. printf( "Can't find '%s' specified in vrad.redirect.\n", dllName );
  79. }
  80. fclose( fp );
  81. }
  82. int returnValue = 0;
  83. for(int mode=0;mode<2;mode++)
  84. {
  85. if (mode && (! both_arg))
  86. continue;
  87. // If it didn't load the module above, then use the
  88. if ( !pModule )
  89. {
  90. strcpy( dllName, "vrad_dll.dll" );
  91. pModule = Sys_LoadModule( dllName );
  92. }
  93. if( !pModule )
  94. {
  95. printf( "vrad_launcher error: can't load %s\n%s", dllName, GetLastErrorString() );
  96. return 1;
  97. }
  98. CreateInterfaceFn fn = Sys_GetFactory( pModule );
  99. if( !fn )
  100. {
  101. printf( "vrad_launcher error: can't get factory from vrad_dll.dll\n" );
  102. Sys_UnloadModule( pModule );
  103. return 2;
  104. }
  105. int retCode = 0;
  106. IVRadDLL *pDLL = (IVRadDLL*)fn( VRAD_INTERFACE_VERSION, &retCode );
  107. if( !pDLL )
  108. {
  109. printf( "vrad_launcher error: can't get IVRadDLL interface from vrad_dll.dll\n" );
  110. Sys_UnloadModule( pModule );
  111. return 3;
  112. }
  113. if (both_arg)
  114. strcpy(argv[both_arg],(mode)?"-hdr":"-ldr");
  115. returnValue = pDLL->main( argc, argv );
  116. Sys_UnloadModule( pModule );
  117. pModule=0;
  118. }
  119. return returnValue;
  120. }