Advertisement
FriendlyFire

Modified Autoconnect.cpp

Sep 1st, 2016
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Autoconnect.cpp - Directly connect to a server upon starting the game.
  3.  
  4.   Jason Hood, 1 September, 2016.
  5.  
  6.   Usage:
  7.     Add "-I[server[:password]]" (internet) or "-L[server[:password]]" (LAN)
  8.     to automatically connect to that server.  Server may be either the name
  9.     or address; only a prefix is necessary (whatever matches first).  If
  10.     password is absent, it will be prompted as usual.
  11.  
  12.   Install:
  13.     Copy Autoconnect.dll to your EXE directory and add it to the
  14.     [Libraries] section of dacom.ini.
  15. */
  16.  
  17. #include "patch.h"
  18. #include <string>
  19. using std::wstring;
  20.  
  21. #define ADDR_MENU   0x575360
  22. #define ADDR_SERVER (0x56FF59+1)
  23. #define ADDR_PASSWORD   ((PDWORD)(0x5721A8+1))
  24. #define ADDR_INTERNET   ((bool*)0x615B10)
  25. #define ADDR_VERSION    ((PDWORD)0x67ECB8)
  26.  
  27. wstring server, password;
  28.  
  29. struct Connection   // partial
  30. {
  31.     UINT dunno1;
  32.     wchar_t* addr;
  33.     BYTE stuff[16 * 4];
  34.     wchar_t* name;
  35.     BYTE more[16 * 4];
  36.     DWORD version;
  37. };
  38.  
  39. DWORD Menu_Old;
  40.  
  41. NAKED
  42. void Menu_Hook( void )
  43. {
  44.     __asm {
  45.         mov eax, Menu_Old
  46.         mov ds:[ADDR_MENU], eax // restore the menu
  47.         jmp dword ptr ds:[ADDR_MENU+12] // Multiplayer state
  48.         align 16
  49.     }
  50. }
  51.  
  52. bool FASTCALL SvrTest( const Connection& srvr )
  53. {
  54.     return (srvr.version == *ADDR_VERSION &&
  55.             (_wcsnicmp( srvr.name, server.c_str(), server.length()) == 0 ||
  56.             _wcsnicmp( srvr.addr, server.c_str(), server.length()) == 0));
  57. }
  58.  
  59. DWORD Server_Old, Server_Org;
  60.  
  61. NAKED
  62. void Server_Hook( void )
  63. {
  64.     __asm {
  65.         cmp dword ptr [esi+0x114], 1
  66.         je done
  67.         push ecx
  68.         push eax
  69.         mov ecx, eax
  70.         call SvrTest
  71.         test al, al
  72.         pop eax
  73.         pop ecx
  74.         jz done
  75.         mov edx, Server_Org
  76.         mov dword ptr [esi+0x114], 1
  77.         mov ds:[ADDR_SERVER], edx
  78.         mov edx, 0x572100
  79.         push eax
  80.         call edx
  81.         mov edx, 0x571FC0
  82.         mov ecx, esi
  83.         call edx
  84.         ret 8
  85.     done:
  86.         jmp Server_Old
  87.         align 16
  88.     }
  89. }
  90.  
  91. DWORD Password_Old, Password_Org;
  92.  
  93. struct vc6wstring
  94. {
  95.     UINT allocator;
  96.     const wchar_t* ptr;
  97.     size_t len, res;
  98. };
  99.  
  100. bool Password_Hook( UINT, vc6wstring& pwd, UINT, UINT )
  101. {
  102.     *ADDR_PASSWORD = Password_Org;
  103.     wchar_t* sz = new wchar_t[password.capacity()];
  104.     memcpy_s(sz, password.capacity() * sizeof(wchar_t), password.c_str(), (password.size() + 1) * sizeof(wchar_t));
  105.     pwd.ptr = sz;
  106.     pwd.len = password.size();
  107.     pwd.res = password.capacity();
  108.     return true;
  109. }
  110.  
  111.  
  112. void Patch()
  113. {
  114.     ProtectX( (PVOID)ADDR_MENU,  4 );
  115.     ProtectX( (PVOID)ADDR_SERVER, 4 );
  116.  
  117.     Server_Org = *(PDWORD)ADDR_SERVER;
  118.  
  119.     NEWABS( (PDWORD)ADDR_MENU,   Menu );
  120.     NEWOFS( (PDWORD)ADDR_SERVER, Server );
  121.  
  122.     if (!password.empty())
  123.     {
  124.         ProtectX( ADDR_PASSWORD, 4 );
  125.         Password_Org = *ADDR_PASSWORD;
  126.         NEWOFS( ADDR_PASSWORD, Password );
  127.     }
  128. }
  129.  
  130.  
  131. BOOL WINAPI DllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved )
  132. {
  133.     if (fdwReason == DLL_PROCESS_ATTACH)
  134.     {
  135.         LPCWSTR cmdline = GetCommandLineW();
  136.         while (*cmdline != '\0')
  137.         {
  138.             if (*cmdline == ' ' && cmdline[1] == '-' &&
  139.                 (cmdline[2] == 'I' || cmdline[2] == 'L'))
  140.             {
  141.                 if (cmdline[2] == 'L')
  142.                     *ADDR_INTERNET = false;
  143.                 int len = 0;
  144.                 cmdline += 3;
  145.                 while (cmdline[len] > ' ' && cmdline[len] != ':')
  146.                     ++len;
  147.                 server.assign( cmdline, len );
  148.                 if (cmdline[len] == ':')
  149.                 {
  150.                     cmdline += len + 1;
  151.                     len = 0;
  152.                     while (cmdline[len] > ' ')
  153.                         ++len;
  154.                     password.assign( cmdline, len );
  155.                 }
  156.                 Patch();
  157.             }
  158.             ++cmdline;
  159.         }
  160.     }
  161.  
  162.     return TRUE;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement