This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlauncher.c
168 lines (135 loc) · 3.67 KB
/
launcher.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "launcher.h"
/* Make these sumbols as we need their sizes. */
static const char G4[] = LOTRBFME_G4;
//static_assert(sizeof(G4) == 36 + 1, "invalid G4 size");
static HANDLE setup_file_payload(void)
{
SECURITY_ATTRIBUTES attributes;
ZeroMemory(&attributes, sizeof(attributes));
attributes.nLength = 12;
attributes.lpSecurityDescriptor = 0;
attributes.bInheritHandle = 1;
HANDLE hFile = CreateFileMappingA(INVALID_HANDLE_VALUE, &attributes, PAGE_READWRITE, 0, sizeof(G4) - 1, NULL);
if(!hFile)
return NULL;
void *ptr = MapViewOfFileEx(hFile, 0xF001F, 0, 0, 0, 0);
if(!ptr)
{
CloseHandle(hFile);
return NULL;
}
memcpy(ptr, G4, sizeof(G4) - 1);
UnmapViewOfFile(ptr);
return hFile;
}
static void show_error_box(LPCWSTR msg)
{
MessageBoxW(NULL, msg, LOTRBFME_SKUNAME, MB_OK | MB_ICONERROR);
}
static LPWSTR lstrrchrW(LPCWSTR s, WCHAR c)
{
LPCWSTR p;
for(p = s + lstrlenW(s); p >= s && *p != c; p--)
;
return p >= s ? (LPWSTR)p : 0;
}
static LPWSTR get_executable_directory()
{
HMODULE hThis = GetModuleHandleW(NULL);
HANDLE hHeap = GetProcessHeap();
DWORD dwSize = MAX_PATH;
LPWSTR p = HeapAlloc(hHeap, 0, dwSize * sizeof(WCHAR));
if(!p)
return NULL;
while(GetModuleFileNameW(hThis, p, dwSize) == dwSize)
{
dwSize *= 2;
LPWSTR np = HeapReAlloc(hHeap, 0, p, dwSize * sizeof(WCHAR));
if(!np)
{
HeapFree(hHeap, 0, p);
return NULL;
}
p = np;
}
LPWSTR end = lstrrchrW(p, '\\');
if(end)
*end = L'\0';
return p;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
LPWSTR progDir = get_executable_directory();
if(!progDir)
{
show_error_box(L"Error getting executable directory.");
return 1;
}
if(!SetCurrentDirectoryW(progDir))
{
HeapFree(GetProcessHeap(), 0, progDir);
show_error_box(L"Error setting current directory.");
return 1;
}
HeapFree(GetProcessHeap(), 0, progDir);
HANDLE hFile = setup_file_payload();
if(!hFile)
{
show_error_box(L"Error mapping file.");
return -1;
}
BOOL result = FALSE;
DWORD exitCode = 0;
HANDLE hMutex = NULL;
HANDLE hEvent = NULL;
if(!(hMutex = CreateMutexA(NULL, FALSE, LOTRBFME_G1)))
{
DWORD dwErr = GetLastError();
if(dwErr == ERROR_ALREADY_EXISTS)
show_error_box(L"Game already running.");
else
show_error_box(L"Error creating mutex.");
goto create_mutex_fail;
}
/* Create the event to trigger game.dat */
if(!(hEvent = CreateEventA(0, 0, 0, LOTRBFME_G3)))
{
show_error_box(L"Error creating event.");
goto create_event_fail;
}
STARTUPINFOW si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
si.dwFlags |= STARTF_USESTDHANDLES;
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));
if(!CreateProcessW(L"game.dat", GetCommandLineW(), NULL, NULL, TRUE, 0 /*CREATE_SUSPENDED*/, NULL, NULL, &si, &pi))
{
show_error_box(L"Error launching game executable.");
goto create_process_fail;
}
HANDLE Handles[2] = {hEvent, pi.hProcess};
/* Wait for the event or process termination. */
DWORD dwResult = WaitForMultipleObjects(2, Handles, 0, INFINITE);
if(dwResult == WAIT_OBJECT_0)
{
/* We got the event, send the payload and wait for termination. */
PostThreadMessageA(pi.dwThreadId, 0xBEEF, 0, (LPARAM)hFile);
WaitForSingleObject(pi.hProcess, INFINITE);
}
result = GetExitCodeProcess(pi.hProcess, &exitCode);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
create_process_fail:
CloseHandle(hEvent);
create_event_fail:
CloseHandle(hMutex);
create_mutex_fail:
CloseHandle(hFile);
return result ? (int)exitCode : -1;
}