|
| 1 | +/* See LICENSE file for copyright and license details. |
| 2 | + * Inspired by mewrev "inject" tool |
| 3 | + * https://github.com/mewrev/inject |
| 4 | + * |
| 5 | + * This barebone truckersMP launcher only provides the most basic launching |
| 6 | + * capability: it perform the dll injection needed to start the mod and |
| 7 | + * nothing else. |
| 8 | + * |
| 9 | + * Author : lhark |
| 10 | + */ |
| 11 | + |
| 12 | +#include <stdio.h> |
| 13 | +#include <windows.h> |
| 14 | + |
| 15 | + |
| 16 | +#define BUF_SIZE 1024 |
| 17 | + |
| 18 | + |
| 19 | +static void inject(char *cmd, char *dll); |
| 20 | +static void die(const char *fmt, ...); |
| 21 | + |
| 22 | + |
| 23 | +int |
| 24 | +main(int argc, char **argv) |
| 25 | +{ |
| 26 | + int i, len; |
| 27 | + char *exepath = "\\bin\\win_x64\\eurotrucks2.exe"; |
| 28 | + char *dllpath = "\\core_ets2mp.dll"; |
| 29 | + const char opts[] = "-nointro -64bit"; |
| 30 | + char cmd[BUF_SIZE]; |
| 31 | + char dll[BUF_SIZE]; |
| 32 | + |
| 33 | + if (argc < 3) |
| 34 | + die("Usage: inject GAMEDIR MODDIR\n"); |
| 35 | + |
| 36 | + for (i = 1; i < 3; i++) { /* '\' and '/' can be mixed in windows type pathes */ |
| 37 | + len = strlen(argv[i]); |
| 38 | + if (argv[i][len - 1] == '\\' || argv[i][len - 1] == '/') |
| 39 | + argv[i][len - 1] = '\0'; |
| 40 | + } |
| 41 | + |
| 42 | + snprintf(cmd, sizeof(cmd), "%s%s %s", argv[1], exepath, opts); |
| 43 | + snprintf(dll, sizeof(dll), "%s%s", argv[2], dllpath); |
| 44 | + |
| 45 | + SetEnvironmentVariable("SteamGameId", "227300"); |
| 46 | + SetEnvironmentVariable("SteamAppID", "227300"); |
| 47 | + |
| 48 | + inject(cmd, dll); |
| 49 | + return 0; |
| 50 | +} |
| 51 | + |
| 52 | +static void |
| 53 | +inject(char *cmd, char *dll) |
| 54 | +{ |
| 55 | + int len; |
| 56 | + void *page; |
| 57 | + HANDLE hThread; |
| 58 | + STARTUPINFO si = {0}; |
| 59 | + PROCESS_INFORMATION pi = {0}; |
| 60 | + |
| 61 | + si.cb = sizeof(STARTUPINFO); |
| 62 | + if (!CreateProcess(NULL, cmd, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi)) |
| 63 | + die("CreateProcess(\"%s\") failed; error code = 0x%08X\n", cmd, GetLastError()); |
| 64 | + |
| 65 | + // Allocate a page in memory for the arguments of LoadLibrary. |
| 66 | + page = VirtualAllocEx(pi.hProcess, NULL, MAX_PATH, MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE); |
| 67 | + if (page == NULL) |
| 68 | + die("VirtualAllocEx failed; error code = 0x%08X\n", GetLastError()); |
| 69 | + |
| 70 | + /* Inject the core dll into the process address space */ |
| 71 | + len = strlen(dll) + 1; |
| 72 | + if (len > MAX_PATH) |
| 73 | + die("path length (%d) exceeds MAX_PATH (%d).\n", len, MAX_PATH); |
| 74 | + |
| 75 | + if (GetFileAttributes(dll) == INVALID_FILE_ATTRIBUTES) |
| 76 | + die("unable to locate library (%s).\n", dll); |
| 77 | + |
| 78 | + /* Write library path to the page used for LoadLibrary arguments. */ |
| 79 | + if (!WriteProcessMemory(pi.hProcess, page, dll, len, NULL)) |
| 80 | + die("WriteProcessMemory failed; error code = 0x%08X\n", GetLastError()); |
| 81 | + |
| 82 | + /* Inject the library */ |
| 83 | + hThread = CreateRemoteThread(pi.hProcess, NULL, 0, (LPTHREAD_START_ROUTINE) LoadLibraryA, page, 0, NULL); |
| 84 | + if (!hThread) |
| 85 | + die("CreateRemoteThread failed; error code = 0x%08X\n", GetLastError()); |
| 86 | + |
| 87 | + if (WaitForSingleObject(hThread, INFINITE) == WAIT_FAILED) |
| 88 | + die("WaitForSingleObject failed; error code = 0x%08X\n", GetLastError()); |
| 89 | + |
| 90 | + CloseHandle(hThread); |
| 91 | + |
| 92 | + if (ResumeThread(pi.hThread) == -1) |
| 93 | + die("ResumeThread failed; error code = 0x%08X\n", GetLastError()); |
| 94 | + |
| 95 | + CloseHandle(pi.hProcess); |
| 96 | + VirtualFreeEx(pi.hProcess, page, MAX_PATH, MEM_RELEASE); |
| 97 | +} |
| 98 | + |
| 99 | +static void |
| 100 | +die(const char *fmt, ...) |
| 101 | +{ |
| 102 | + va_list ap; |
| 103 | + |
| 104 | + va_start(ap, fmt); |
| 105 | + vfprintf(stderr, fmt, ap); |
| 106 | + va_end(ap); |
| 107 | + |
| 108 | + if (fmt[0] && fmt[strlen(fmt)-1] == ':') { |
| 109 | + fputc(' ', stderr); |
| 110 | + perror(NULL); |
| 111 | + } else { |
| 112 | + fputc('\n', stderr); |
| 113 | + } |
| 114 | + |
| 115 | + exit(1); |
| 116 | +} |
0 commit comments