-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCaptureFunctions.cc
More file actions
356 lines (294 loc) · 10.4 KB
/
CaptureFunctions.cc
File metadata and controls
356 lines (294 loc) · 10.4 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
#include "CaptureFunctions.h"
#include "WinAsyncWorker.h"
#include <string>
#include <cstdint>
#include <cinttypes>
#include <iostream>
using Nan::AsyncQueueWorker;
using Nan::AsyncWorker;
using Nan::Callback;
using Nan::Callback;
using v8::Function;
using v8::Local;
using v8::Number;
using v8::Object;
using v8::Value;
using v8::Array;
using Nan::AsyncQueueWorker;
using Nan::AsyncWorker;
using Nan::Callback;
using Nan::HandleScope;
using Nan::New;
using Nan::Set;
using Nan::Null;
using Nan::To;
#define EVENT_READ_REGISTRY "Global\\BEBO_CAPTURE_READ_REGISTRY"
void signalRegistryChangeEvent(bool &ok, std::string &message) {
HANDLE event;
event = OpenEvent(
EVENT_ALL_ACCESS, // request full access
FALSE, // handle not inheritable
TEXT(EVENT_READ_REGISTRY)); // object name
if (event == NULL) {
// cause we don't actually want to promise.reject here
// before web does set capture event before opening the bebo-game-capture camera
ok = true;
message = "signal event cannot be opened";
return;
}
ok = true;
SetEvent(event);
int sleep_count = 0;
while (true) {
DWORD result = WaitForSingleObject(event, 1);
if (sleep_count > 500) { // rougly 1s, 2 -- 1ms wait
ok = false;
message = "signalled but timeout for waiting for the respond.";
break;
}
if (result == WAIT_OBJECT_0) { // still processing;
Sleep(1);
sleep_count++;
continue;
}
break;
}
CloseHandle(event);
return;
}
class GetCaptureWorker: public WinAsyncWorker {
public:
GetCaptureWorker(Callback *callback)
: WinAsyncWorker(callback) {};
~GetCaptureWorker() {
if (capture) {
delete capture;
}
};
// Executed inside the worker-thread.
// It is not safe to access V8, or V8 data structures
// here, so everything we need for input and output
// should go on `this`.
void Execute() {
HKEY hkey = NULL;
if (!chk(RegOpen(KEY_READ, &hkey), "Can't open registry")) {
return;
}
this->readData(hkey);
chk(RegClose(hkey), "Can't close registry");
bool ok;
std::string message;
signalRegistryChangeEvent(ok, message);
if (!ok) {
SetErrorMessage(message.c_str());
}
}
// Executed when the async work is complete
// this function will be run inside the main event loop
// so it is safe to use V8 again
void HandleOKCallback() {
HandleScope scope;
Local<Object> obj = Nan::New<Object>();
Set(obj, New("id").ToLocalChecked(), New(capture->id).ToLocalChecked());
Set(obj, New("label").ToLocalChecked(), New(capture->label).ToLocalChecked());
Set(obj, New("type").ToLocalChecked(), New(capture->type).ToLocalChecked());
Set(obj, New("windowName").ToLocalChecked(), New(capture->windowName).ToLocalChecked());
Set(obj, New("windowClassName").ToLocalChecked(), New(capture->windowClassName).ToLocalChecked());
Set(obj, New("antiCheat").ToLocalChecked(), New(capture->antiCheat));
if (capture->type == "gdi") {
Set(obj, New("once").ToLocalChecked(), New(capture->once));
if (capture->hwnd != 0) {
char windowHandle[20] = {0};
std::sprintf(windowHandle, "0x%016" PRIx64, capture->hwnd);
Set(obj, New("windowHandle").ToLocalChecked(), New(windowHandle).ToLocalChecked());
}
Set(obj, New("exeFullName").ToLocalChecked(), New(capture->exeFullName).ToLocalChecked());
}
Local<Value> argv[] = {
Null()
, obj
};
callback->Call(2, argv);
}
protected:
CaptureEntity *capture;
bool chk(HRESULT hresult, std::string & msg) {
std::string mymsg(msg);
if (hresult != NOERROR) {
mymsg.append(errno_to_text(hresult));
SetErrorMessage(mymsg.c_str());
return false;
}
return true;
}
bool chk(HRESULT hresult, const char * msg) {
return chk(hresult, std::string(msg));
}
bool chkGetBool(HKEY hkey, char * key, bool *data) {
std::string msg("Can't read from registry (");
msg.append(key);
msg.append(") ");
return chk(getBool(hkey, key, data), msg);
}
bool chkGetQWord(HKEY hkey, char * key, uint64_t *data) {
std::string msg("Can't read from registry (");
msg.append(key);
msg.append(") ");
return chk(getQWord(hkey, key, data), msg);
}
bool chkPutQWord(HKEY hkey, char * key, uint64_t data) {
std::string msg("Can't write to registry (");
msg.append(key);
msg.append(") ");
return chk(putQWord(hkey, key, data), msg);
}
bool chkPutBool(HKEY hkey, char * key, bool data) {
std::string msg("Can't write to registry (");
msg.append(key);
msg.append(") ");
return chk(putBool(hkey, key, data), msg);
}
bool chkPutSZ(HKEY hkey, char * key, std::string & data) {
std::string msg("Can't write to registry (");
msg.append(key);
msg.append(") ");
return chk(putSZ(hkey, key, data), msg);
}
bool chkGetSZ(HKEY hkey, char * key, std::string & value) {
std::string msg("Can't read from registry (");
msg.append(key);
msg.append(") ");
return chk(getSZ(hkey, key, value), msg);
}
void readData(HKEY hkey) {
capture = new CaptureEntity();
if (!chkGetSZ(hkey, "CaptureId", capture->id)) return;
if (!chkGetSZ(hkey, "CaptureType", capture->type)) return;
if (capture->type.size() == 0) {
capture->type.assign("inject");
}
if (!chkGetSZ(hkey, "CaptureLabel", capture->label)) return;
if (!chkGetSZ(hkey, "CaptureWindowName", capture->windowName)) return;
if (!chkGetSZ(hkey, "CaptureWindowClassName", capture->windowClassName)) return;
if (!chkGetSZ(hkey, "CaptureExeFullName", capture->exeFullName)) return;
if (!chkGetQWord(hkey, "CaptureWindowHandle", &capture->hwnd)) return;
if (!chkGetBool(hkey, "CaptureAntiCheat", &capture->antiCheat)) return;
if (!chkGetBool(hkey, "CaptureOnce", &capture->once)) return;
}
};
class SetCaptureWorker: public GetCaptureWorker {
public:
SetCaptureWorker(CaptureEntity *capture, Callback *callback)
: GetCaptureWorker(callback) {
this->capture = capture;
};
~SetCaptureWorker() {
};
// Executed inside the worker-thread.
// It is not safe to access V8, or V8 data structures
// here, so everything we need for input and output
// should go on `this`.
void Execute() {
HKEY hkey = NULL;
if (!chk(RegOpen(KEY_ALL_ACCESS, &hkey), "Can't open registry for write")) {
return;
}
if (!chkPutSZ(hkey, "CaptureId", capture->id)) return;
if (!chkPutSZ(hkey, "CaptureType", capture->type)) return;
if (!chkPutSZ(hkey, "CaptureLabel", capture->label)) return;
if (!chkPutSZ(hkey, "CaptureWindowName", capture->windowName)) return;
if (!chkPutSZ(hkey, "CaptureWindowClassName", capture->windowClassName)) return;
if (!chkPutSZ(hkey, "CaptureExeFullName", capture->exeFullName)) return;
if (!chkPutQWord(hkey, "CaptureWindowHandle", capture->hwnd)) return;
if (!chkPutBool(hkey, "CaptureAntiCheat", capture->antiCheat)) return;
if (!chkPutBool(hkey, "CaptureOnce", capture->once)) return;
delete capture;
readData(hkey);
chk(RegClose(hkey), "Can't close registry");
bool ok;
std::string message;
signalRegistryChangeEvent(ok, message);
if (!ok) {
SetErrorMessage(message.c_str());
}
}
};
class SignalCaptureWorker: public WinAsyncWorker {
protected:
bool ok;
std::string message = "";
public:
SignalCaptureWorker(Callback *callback)
: WinAsyncWorker(callback){};
~SignalCaptureWorker() {
};
// Executed inside the worker-thread.
// It is not safe to access V8, or V8 data structures
// here, so everything we need for input and output
// should go on `this`.
void Execute() {
signalRegistryChangeEvent(ok, message);
if (!ok) {
SetErrorMessage(message.c_str());
}
}
void HandleOKCallback() {
HandleScope scope;
Local<Object> obj = Nan::New<Object>();
Set(obj, New("ok").ToLocalChecked(), New(ok));
Set(obj, New("message").ToLocalChecked(), New(message).ToLocalChecked());
Local<Value> argv[] = {
Null()
, obj
};
callback->Call(2, argv);
}
};
NAN_METHOD(getCapture) {
Callback *callback = new Callback(info[0].As<Function>());
AsyncQueueWorker(new GetCaptureWorker(callback));
}
char * cpStringArg(Nan::NAN_METHOD_ARGS_TYPE arg, int i) {
if (!arg[i].IsEmpty()) {
Nan::Utf8String nan_string(arg[i]);
std::string name(*nan_string);
return strdup(name.c_str());
}
return strdup("");
}
NAN_METHOD(setCapture) {
auto capture = new CaptureEntity();
capture->type = cpStringArg(info, 0);
capture->id = cpStringArg(info, 1);
capture->label = cpStringArg(info, 2);
capture->windowName = cpStringArg(info, 3);
capture->windowClassName = cpStringArg(info, 4);
if (!info[5].IsEmpty()) {
Nan::Utf8String nan_string(info[5]);
std::string hwnd(*nan_string);
if (hwnd.size() > 0) {
try {
capture->hwnd = stoull(hwnd, 0, 16);
} catch (std::invalid_argument) {
// TODO: throw back js exception...
std::cout << "Invalid number string" << hwnd << std::endl;
}
}
}
capture->exeFullName = cpStringArg(info, 6);
std::cout << "exeFullName" << capture->exeFullName << std::endl;
Nan::Maybe<bool> antiCheat = Nan::To<bool>(info[7]);
if (!info[7].IsEmpty()) {
capture->antiCheat = antiCheat.ToChecked();
}
Nan::Maybe<bool> once = Nan::To<bool>(info[8]);
if (!info[8].IsEmpty()) {
capture->once = once.ToChecked();
}
Callback *callback = new Callback(info[9].As<Function>());
AsyncQueueWorker(new SetCaptureWorker(capture, callback));
}
NAN_METHOD(signalCaptureReadRegistry) {
Callback *callback = new Callback(info[0].As<Function>());
AsyncQueueWorker(new SignalCaptureWorker(callback));
}