forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFSteam.cpp
More file actions
252 lines (203 loc) · 6.99 KB
/
DFSteam.cpp
File metadata and controls
252 lines (203 loc) · 6.99 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
#include "Internal.h"
#include "modules/DFSteam.h"
#include "Debug.h"
#include "PluginManager.h"
#include "df/gamest.h"
namespace DFHack
{
DBG_DECLARE(core, dfsteam, DebugCategory::LINFO);
}
using namespace DFHack;
static const int DFHACK_STEAM_APPID = 2346660;
static bool g_steam_initialized = false;
static DFLibrary* g_steam_handle = nullptr;
static const std::vector<std::string> STEAM_LIBS {
#ifdef WIN32
"steam_api64.dll"
#elif defined(_DARWIN)
"steam_api" // TODO: validate this on OSX
#else
"libsteam_api.so"
#endif
};
bool (*g_SteamAPI_Init)() = nullptr;
void (*g_SteamAPI_Shutdown)() = nullptr;
int (*g_SteamAPI_GetHSteamUser)() = nullptr;
bool (*g_SteamAPI_RestartAppIfNecessary)(uint32_t unOwnAppID) = nullptr;
void* (*g_SteamInternal_FindOrCreateUserInterface)(int, const char*) = nullptr;
bool (*g_SteamAPI_ISteamApps_BIsAppInstalled)(void *iSteamApps, uint32_t appID) = nullptr;
static void bind_all(color_ostream& out, DFLibrary* handle) {
#define bind(name) \
if (!handle) { \
g_##name = nullptr; \
} else { \
g_##name = (decltype(g_##name))LookupPlugin(handle, #name); \
if (!g_##name) { \
WARN(dfsteam, out).print("steam library function not found: " #name "\n"); \
} \
}
bind(SteamAPI_Init);
bind(SteamAPI_Shutdown);
bind(SteamAPI_GetHSteamUser);
bind(SteamInternal_FindOrCreateUserInterface);
bind(SteamAPI_RestartAppIfNecessary);
bind(SteamInternal_FindOrCreateUserInterface);
bind(SteamAPI_ISteamApps_BIsAppInstalled);
#undef bind
}
bool DFSteam::init(color_ostream& out) {
char *steam_client_launch = getenv("SteamClientLaunch");
if (!steam_client_launch || strncmp(steam_client_launch, "1", 2) != 0) {
DEBUG(dfsteam, out).print("not launched from Steam client; not initializing steam\n");
return false;
}
for (auto& lib_str : STEAM_LIBS) {
if ((g_steam_handle = OpenPlugin(lib_str.c_str())))
break;
}
if (!g_steam_handle) {
DEBUG(dfsteam, out).print("steam library not found; stubbing calls\n");
return false;
}
bind_all(out, g_steam_handle);
if (!g_SteamAPI_Init || !g_SteamAPI_Shutdown || !g_SteamAPI_Init()) {
DEBUG(dfsteam, out).print("steam detected but cannot be initialized\n");
return false;
}
DEBUG(dfsteam, out).print("steam library linked\n");
g_steam_initialized = true;
return true;
}
void DFSteam::cleanup(color_ostream& out) {
if (!g_steam_handle)
return;
if (g_SteamAPI_Shutdown)
g_SteamAPI_Shutdown();
ClosePlugin(g_steam_handle);
g_steam_handle = nullptr;
bind_all(out, nullptr);
g_steam_initialized = false;
}
#ifdef WIN32
#include <process.h>
#include <windows.h>
#include <TlHelp32.h>
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 DWORD findProcess(LPCWSTR name) {
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 == name) {
CloseHandle(snapshot);
return entry.th32ProcessID;
}
}
while (Process32NextW(snapshot, &entry));
CloseHandle(snapshot);
return -1;
}
static bool launchDFHack(color_ostream& out) {
if (is_running_on_wine()) {
DEBUG(dfsteam, out).print("not attempting to re-launch DFHack on wine\n");
return false;
}
if (findProcess(L"launchdf.exe") != -1) {
DEBUG(dfsteam, out).print("launchdf.exe already running\n");
return true;
}
STARTUPINFOW si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
static LPCWSTR procname = L"hack/launchdf.exe";
static const char * env = "\0";
// note that the environment must be explicitly zeroed out and not NULL,
// otherwise the launched process will inherit this process's environment,
// and the Steam API in the launchdf process will think it is in DF's context.
BOOL res = CreateProcessW(procname,
NULL, NULL, NULL, FALSE, 0, (LPVOID)env, NULL, &si, &pi);
return !!res;
}
#else
#include <unistd.h>
static bool findProcess(color_ostream& out, std::string name, pid_t &pid) {
char buf[512];
std::string command = "pidof -s ";
command += name;
FILE *cmd_pipe = popen(command.c_str(), "r");
if (!cmd_pipe) {
WARN(dfsteam, out).print("failed to exec '%s' (error: %d)\n",
command.c_str(), errno);
return false;
}
bool found = fgets(buf, 512, cmd_pipe) != NULL;
pclose(cmd_pipe);
pid = 0;
if (found)
pid = strtoul(buf, NULL, 10);
return true;
}
static bool launchDFHack(color_ostream& out) {
pid_t pid;
if (!findProcess(out, "launchdf", pid))
return false;
if (pid != 0) {
DEBUG(dfsteam, out).print("launchdf already running\n");
return true;
}
pid = fork();
if (pid == -1) {
WARN(dfsteam, out).print("failed to fork (error: %d)\n", errno);
return false;
} else if (pid == 0) {
// child process
static const char * command = "hack/launchdf";
unsetenv("SteamAppId");
execl(command, command, NULL);
_exit(EXIT_FAILURE);
}
// parent process
return true;
}
#endif
void DFSteam::launchSteamDFHackIfNecessary(color_ostream& out) {
if (!g_steam_initialized ||
!g_SteamAPI_GetHSteamUser ||
!g_SteamInternal_FindOrCreateUserInterface ||
!g_SteamAPI_ISteamApps_BIsAppInstalled) {
DEBUG(dfsteam, out).print("required Steam API calls are unavailable\n");
return;
}
const std::string & cmdline = df::global::game->command_line.original;
if (cmdline.find("--nosteam-dfhack") != std::string::npos) {
WARN(dfsteam, out).print("--nosteam-dfhack specified on commandline; not launching DFHack coprocess\n");
return;
}
void* iSteamApps = g_SteamInternal_FindOrCreateUserInterface(g_SteamAPI_GetHSteamUser(), "STEAMAPPS_INTERFACE_VERSION008");
if (!iSteamApps) {
DEBUG(dfsteam, out).print("cannot obtain iSteamApps interface\n");
return;
}
bool isDFHackInstalled = g_SteamAPI_ISteamApps_BIsAppInstalled(iSteamApps, DFHACK_STEAM_APPID);
if (!isDFHackInstalled) {
DEBUG(dfsteam, out).print("player has not installed DFHack through Steam\n");
return;
}
bool ret = launchDFHack(out);
DEBUG(dfsteam, out).print("launching DFHack via Steam: %s\n", ret ? "successful" : "unsuccessful");
}