forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaunchdf.cpp
More file actions
334 lines (253 loc) · 7.69 KB
/
launchdf.cpp
File metadata and controls
334 lines (253 loc) · 7.69 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#ifdef WIN32
# include <process.h>
# include <windows.h>
# include <TlHelp32.h>
#else
# include <fcntl.h>
# include <unistd.h>
#endif
#include "steam_api.h"
#include <string>
#define xstr(s) str(s)
#define str(s) #s
#define DFHACK_STEAM_APPID 2346660
#define DF_STEAM_APPID 975370
#ifdef WIN32
static BOOL is_running_on_wine() {
typedef const char* (CDECL wine_get_version)(void);
static wine_get_version* pwine_get_version;
HMODULE hntdll = GetModuleHandle("ntdll.dll");
if(!hntdll)
return FALSE;
pwine_get_version = (wine_get_version*) GetProcAddress(hntdll, "wine_get_version");
return !!pwine_get_version;
}
static LPCWSTR launch_via_steam_posix() {
const char* argv[] = { "/bin/sh", "-c", "\"steam -applaunch " xstr(DF_STEAM_APPID) "\"", NULL };
// does not return on success
_execv(argv[0], argv);
return L"Could not launch Dwarf Fortress";
}
static LPCWSTR launch_via_steam() {
STARTUPINFOW si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
WCHAR steamPath[1024] = L"";
DWORD datasize = 1024;
LONG retCode = RegGetValueW(HKEY_CURRENT_USER, L"SOFTWARE\\Valve\\Steam",
L"SteamExe", RRF_RT_REG_SZ, NULL, &steamPath, &datasize);
if (retCode != ERROR_SUCCESS)
return L"Could not find Steam client executable";
WCHAR commandLine[1024] = L"steam.exe -applaunch " xstr(DF_STEAM_APPID);
BOOL res = CreateProcessW(steamPath, commandLine,
NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
if (res) {
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return NULL;
}
return L"Could not launch Dwarf Fortress";
}
#else
static const char * launch_via_steam() {
static const char * command = "steam -applaunch " xstr(DF_STEAM_APPID);
if (system(command) != 0)
return "failed to launch DF via steam";
return NULL;
}
#endif
#ifdef WIN32
static LPCWSTR launch_direct() {
STARTUPINFOW si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
BOOL res = CreateProcessW(L"Dwarf Fortress.exe",
NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
if (res) {
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return NULL;
}
return L"Could not launch via non-steam fallback method";
}
#else
static const char * launch_direct() {
static const char * command = "./dfhack";
if (system(command) != 0)
return "failed to launch DF via ./dfhack launcher";
return NULL;
}
#endif
#ifdef WIN32
bool wrap_launch(LPCWSTR (*launch_fn)()) {
LPCWSTR err = launch_fn();
if (err != NULL) {
MessageBoxW(NULL, err, NULL, 0);
return false;
}
return true;
}
#else
void notify(const char * msg) {
std::string command = "notify-send --app-name=DFHack \"";
command += msg;
command += "\"";
printf("%s\n", msg);
int result = system(command.c_str());
// if this fails; it's ok
(void)result;
}
bool wrap_launch(const char * (*launch_fn)()) {
const char * err = launch_fn();
if (err != NULL) {
notify(err);
return false;
}
return true;
}
#endif
#ifdef WIN32
DWORD findDwarfFortressProcess() {
PROCESSENTRY32W entry;
entry.dwSize = sizeof(PROCESSENTRY32W);
const auto snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (!Process32FirstW(snapshot, &entry))
{
CloseHandle(snapshot);
return -1;
}
do {
std::wstring executableName(entry.szExeFile);
if (executableName == L"Dwarf Fortress.exe")
{
CloseHandle(snapshot);
return entry.th32ProcessID;
}
} while (Process32NextW(snapshot, &entry));
CloseHandle(snapshot);
return -1;
}
bool waitForDF(bool nowait) {
DWORD df_pid = findDwarfFortressProcess();
if (df_pid == -1)
return false;
if (nowait)
return true;
HANDLE hDF = OpenProcess(PROCESS_QUERY_INFORMATION | SYNCHRONIZE, FALSE, df_pid);
// in the future open an IPC connection so that we can proxy SteamAPI calls for the DFSteam module
// this will eventually need to become a loop with a WaitForMultipleObjects call
WaitForSingleObject(hDF, INFINITE);
CloseHandle(hDF);
return true;
}
#else
static pid_t findDwarfFortressProcess() {
char buf[512];
static const char * command = "pidof -s dwarfort";
FILE *cmd_pipe = popen(command, "r");
if (!cmd_pipe)
return -1;
bool success = fgets(buf, 512, cmd_pipe) != NULL;
pclose(cmd_pipe);
if (!success)
return -1;
return strtoul(buf, NULL, 10);
}
bool waitForDF(bool nowait) {
pid_t df_pid = findDwarfFortressProcess();
if (df_pid <= 0)
return false;
if (nowait)
return true;
char cmd[64];
snprintf(cmd, 64, "tail --pid=%i -f /dev/null", df_pid);
return 0 == system(cmd);
}
#endif
#ifdef WIN32
int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nShowCmd) {
#else
int main(int argc, char* argv[]) {
#endif
// initialize steam context
if (SteamAPI_RestartAppIfNecessary(DFHACK_STEAM_APPID)) {
exit(0);
}
bool nowait = false;
#ifdef WIN32
std::wstring cmdline(lpCmdLine);
if (cmdline.find(L"--nowait") != std::wstring::npos)
nowait = true;
#else
for (int idx = 0; idx < argc; ++idx) {
if (strcmp(argv[idx], "--nowait") == 0)
nowait = true;
}
#endif
if (waitForDF(nowait))
exit(0);
if (!SteamAPI_Init()) {
// could not initialize steam context, attempt fallback launch
exit(wrap_launch(launch_direct) ? 0 : 1);
}
#ifdef WIN32
if (is_running_on_wine()) {
// attempt launch via steam client
LPCWSTR err = launch_via_steam_posix();
if (err != NULL)
// steam client launch failed, attempt fallback launch
err = launch_direct();
if (err != NULL)
{
MessageBoxW(NULL, err, NULL, 0);
exit(1);
}
exit(0);
}
#endif
// steam detected and not running in wine
if (!SteamApps()->BIsAppInstalled(DF_STEAM_APPID)) {
// Steam DF is not installed. Assume DF is installed in same directory as DFHack and do a fallback launch
exit(wrap_launch(launch_direct) ? 0 : 1);
}
// obtain DF app path
char buf[2048] = "";
int b1 = SteamApps()->GetAppInstallDir(DFHACK_STEAM_APPID, (char*)&buf, 2048);
std::string dfhack_install_folder = (b1 != -1) ? std::string(buf) : "";
int b2 = SteamApps()->GetAppInstallDir(DF_STEAM_APPID, (char*)&buf, 2048);
std::string df_install_folder = (b2 != -1) ? std::string(buf) : "";
if (df_install_folder != dfhack_install_folder) {
// DF and DFHack are not installed in the same library
#ifdef WIN32
MessageBoxW(NULL, L"DFHack and Dwarf Fortress must be installed in the same Steam library.\nAborting.", NULL, 0);
#else
notify("DFHack and Dwarf Fortress must be installed in the same Steam library.\nAborting.");
#endif
exit(1);
}
if (!wrap_launch(launch_via_steam))
exit(1);
int counter = 0;
while (!waitForDF(nowait)) {
if (counter++ > 60) {
#ifdef WIN32
MessageBoxW(NULL, L"Dwarf Fortress took too long to launch, aborting", NULL, 0);
#else
notify("Dwarf Fortress took too long to launch, aborting");
#endif
exit(1);
}
#ifdef WIN32
Sleep(1000);
#else
usleep(1000000);
#endif
}
exit(0);
}