-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcl_frontend.cpp
More file actions
358 lines (314 loc) · 7.98 KB
/
Copy pathcl_frontend.cpp
File metadata and controls
358 lines (314 loc) · 7.98 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include "cls_hook.h"
#include "hooks/cemu.h"
#include "hooks/citra.h"
#include "hooks/dolphin.h"
#include "hooks/infuse.h"
#include "hooks/kemulator.h"
#include "hooks/ryujinx.h"
#include "hooks/touchhle.h"
#include "hooks/vita3k.h"
#include "hooks/xemu.h"
#include "hooks/xenia.h"
#include "hooks/yuzu.h"
#include "cls_login_dialog.h"
#include "cls_main.h"
#include "cls_network_manager.h"
#include "cls_process_select.h"
#include "cls_thread.h"
extern "C"
{
#include <cl_abi.h>
#include <cl_common.h>
#include <cl_main.h>
#include <cl_memory.h>
}
#include <QApplication>
#include <QCloseEvent>
#include <QFileDialog>
#include <QMainWindow>
#include <QMessageBox>
#include <QTimer>
#include <fstream>
#include <regex>
#include <sstream>
#include <string>
static std::vector<std::unique_ptr<ClsHook>> hooks;
static ClsNetworkManager network_manager;
static cl_error cls_abi_display_message(unsigned level, const char *msg)
{
QMessageBox msg_box;
msg_box.setText(msg);
switch (level)
{
case CL_MSG_DEBUG:
case CL_MSG_INFO:
msg_box.setIcon(QMessageBox::Information);
break;
case CL_MSG_WARN:
msg_box.setIcon(QMessageBox::Warning);
break;
case CL_MSG_ERROR:
msg_box.setIcon(QMessageBox::Critical);
break;
default:
msg_box.setIcon(QMessageBox::Question);
}
msg_box.exec();
return CL_OK;
}
static cl_error cls_abi_install_memory_regions(cl_memory_region_t **regions,
unsigned *region_count)
{
if (hooks.size() != 1)
return CL_ERR_PARAMETER_INVALID;
else
{
*region_count = hooks[0]->regionCount();
*regions = reinterpret_cast<cl_memory_region_t*>(
malloc(sizeof(cl_memory_region_t) * *region_count));
memcpy(*regions, hooks[0]->regions(),
sizeof(cl_memory_region_t) * *region_count);
return CL_OK;
}
}
static cl_error cls_abi_library_name(const char **name)
{
if (hooks.size() != 1)
return CL_ERR_PARAMETER_INVALID;
else
{
*name = hooks[0]->getLibrary();
return *name ? CL_OK : CL_ERR_PARAMETER_NULL;
}
}
static cl_error cls_abi_read_buffer(void *dest, cl_addr_t address,
unsigned size, unsigned *read)
{
if (hooks.size() != 1)
return CL_ERR_PARAMETER_INVALID;
else
{
unsigned mread = hooks[0]->read(dest, address, size);
if (read)
*read = mread;
return CL_OK;
}
}
static cl_error cls_abi_read_value(void *dest, cl_addr_t address,
cl_value_type type)
{
if (hooks.size() != 1)
return CL_ERR_PARAMETER_INVALID;
else
{
hooks[0]->read(dest, address, cl_sizeof_memtype(type));
return cl_read_value(dest, dest, 0, type, hooks[0]->regions()[0].endianness);
}
}
static cl_error cls_abi_write_buffer(const void *src, cl_addr_t address,
unsigned size, unsigned *written)
{
if (hooks.size() != 1)
return CL_ERR_PARAMETER_INVALID;
else
{
unsigned mwritten = hooks[0]->write(src, address, size);
if (written)
*written = mwritten;
return CL_OK;
}
}
static cl_error cls_abi_write_value(const void *src, cl_addr_t address,
cl_value_type type)
{
if (hooks.size() != 1)
return CL_ERR_PARAMETER_INVALID;
else
{
cl_read_value(const_cast<void*>(src), src, 0, type, hooks[0]->regions()[0].endianness);
hooks[0]->write(src, address, cl_sizeof_memtype(type));
return CL_OK;
}
}
static cl_error cls_abi_network_post(const char *url, char *data,
cl_network_cb_t callback, void *userdata)
{
if (!url || !data)
return CL_ERR_PARAMETER_NULL;
else
{
cls_net_cb cb = { callback, userdata };
emit network_manager.request(url, data, cb);
}
return CL_OK;
}
static cl_error cls_abi_set_pause(unsigned mode)
{
if (hooks.size() != 1)
return CL_ERR_PARAMETER_INVALID;
else
{
if (mode)
return hooks[0]->pause() ? CL_OK : CL_ERR_PARAMETER_INVALID;
else
return hooks[0]->unpause() ? CL_OK : CL_ERR_PARAMETER_INVALID;
}
}
static cl_error cls_abi_thread(cl_task_t *task)
{
if (!task)
return CL_ERR_PARAMETER_NULL;
else
{
auto *thread = new ClsThread(task);
thread->start();
}
return CL_OK;
}
static cl_error cls_abi_user_data(cl_user_t *user, unsigned index)
{
if (!user)
return CL_ERR_PARAMETER_NULL;
else
{
ClsLoginDialog login(nullptr);
QString username, password;
CL_UNUSED(index);
memset(user, 0, sizeof(*user));
login.exec();
username = login.username();
password = login.password();
if (username.isEmpty() || password.isEmpty())
return CL_ERR_USER_CONFIG;
else
{
snprintf(user->username, sizeof(user->username), "%s",
username.toStdString().c_str());
snprintf(user->password, sizeof(user->password), "%s",
password.toStdString().c_str());
snprintf(user->language, sizeof(user->language), "%s", "en_US");
return CL_OK;
}
}
}
const cl_abi_t cls_abi
{
CL_ABI_VERSION,
{
{
cls_abi_display_message,
cls_abi_install_memory_regions,
cls_abi_library_name,
cls_abi_network_post,
cls_abi_set_pause,
cls_abi_thread,
cls_abi_user_data
},
{
cls_abi_read_buffer,
cls_abi_read_value,
cls_abi_write_buffer,
cls_abi_write_value
}
}
};
ClsMain::ClsMain(void)
{
m_Timer = new QTimer(this);
m_Timer->setInterval(16);
connect(m_Timer, SIGNAL(timeout()), this, SLOT(run()));
m_Timer->start();
m_ProcessSelect = new ClsProcessSelect();
connect(m_ProcessSelect, SIGNAL(selected(uint,void*)),
this, SLOT(selected(uint,void*)));
m_ProcessSelect->show();
}
void ClsMain::closeEvent(QCloseEvent *event)
{
cl_free();
qApp->quit();
event->accept();
}
void ClsMain::run(void)
{
if (hooks.size() == 1 && hooks[0]->run())
cl_run();
}
std::string getProcessTitle(uint32_t pid)
{
std::ostringstream path;
path << "/proc/" << pid << "/comm";
std::ifstream file(path.str());
if (!file.is_open())
{
return "";
}
std::string title;
std::getline(file, title);
return title;
}
static std::unique_ptr<ClsHook> createHook(uint pid, void *window)
{
cls_hook_type type = CLS_HOOK_GENERIC;
for (const auto& preset : cls_window_presets)
{
#if CL_HOST_PLATFORM == _CL_PLATFORM_WINDOWS
if (preset.window_class &&
std::regex_match(getWindowClassName(window), std::regex(preset.window_class)) &&
std::regex_match(getWindowTitle(window), std::regex(preset.window_title)))
#elif CL_HOST_PLATFORM == _CL_PLATFORM_LINUX
if (preset.process_title &&
std::regex_match(getProcessTitle(pid), std::regex(preset.process_title)))
#endif
{
type = preset.type;
break;
}
}
switch (type)
{
case CLS_HOOK_CEMU:
return std::make_unique<ClsHookCemu>(pid, nullptr, window);
case CLS_HOOK_CITRA:
return std::make_unique<ClsHookCitra>(pid, nullptr, window);
case CLS_HOOK_DOLPHIN:
return std::make_unique<ClsHookDolphin>(pid, nullptr, window);
case CLS_HOOK_INFUSE:
return std::make_unique<ClsHookInfuse>(pid, nullptr, window);
case CLS_HOOK_KEMULATOR:
return std::make_unique<ClsHookKemulator>(pid, nullptr, window);
case CLS_HOOK_RYUJINX:
return std::make_unique<ClsHookRyujinx>(pid, nullptr, window);
case CLS_HOOK_TOUCHHLE:
return std::make_unique<ClsHookTouchhle>(pid, nullptr, window);
case CLS_HOOK_VITA3K:
return std::make_unique<ClsHookVita3k>(pid, nullptr, window);
case CLS_HOOK_XEMU:
return std::make_unique<ClsHookXemu>(pid, nullptr, window);
case CLS_HOOK_XENIA:
return std::make_unique<ClsHookXenia>(pid, nullptr, window);
case CLS_HOOK_YUZU:
return std::make_unique<ClsHookYuzu>(pid, nullptr, window);
default:
return nullptr;
}
}
void ClsMain::selected(uint pid, void *window)
{
auto hook = createHook(pid, window);
cl_game_identifier_t identifier;
memset(&identifier, 0, sizeof(identifier));
if (hook && hook->init() && hook->getIdentification(&identifier))
{
hooks.push_back(std::move(hook));
cl_login_and_start(identifier);
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ClsMain clsmain;
clsmain.show();
cl_abi_register(&cls_abi);
return a.exec();
}