Skip to content

Commit f67b887

Browse files
author
lhark
committed
Initial commit
0 parents  commit f67b887

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

Diff for: LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
© 2017 lhark <lhark at ntymail dot com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the “Software”), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9+
of the Software, and to permit persons to whom the Software is furnished to do
10+
so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

Diff for: Makefile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CC=x86_64-w64-mingw32-gcc
2+
3+
all: truckersmp-cli.exe
4+
5+
truckersmp-cli.exe: truckersmp-cli.c
6+
$(CC) $< -o $@
7+
8+
clean:
9+
rm -f truckersmp-cli.exe
10+
11+
.PHONY: clean

Diff for: truckersmp-cli.c

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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

Comments
 (0)