-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhldsv8_common.cpp
378 lines (321 loc) · 10.3 KB
/
hldsv8_common.cpp
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include <extdll.h>
#include <meta_api.h>
#include <cbase.h>
#include <player.h>
#include <usercmd.h>
#include <v8.h>
#include "common.h"
#include "hldsv8.h"
#include "hldsv8_shared.h"
using namespace v8;
/**
* V8 Common: Check whether args[id] is a valid client entity
*
* @params FunctionCallbackInfo args virtual arguments: id
*
* @return bool
*/
int v8c_CheckClient(const v8::FunctionCallbackInfo<Value> &args, const char* funcName)
{
if (args.Length() > 0) {
int id = args[0]->ToInt32()->Value();
if (id) {
if (entities[id]) {
return id;
} else {
ALERT(at_logged, "%s: invalid entity (disconnected?)\n", funcName);
}
}
} else {
ALERT(at_logged, "%s: missing arguments\n", funcName);
}
return 0;
}
/**
* V8 Common: Get player's name
*
* @params FunctionCallbackInfo args virtual arguments: id
*
* @return v8::String
*/
void v8c_GetPlayerName(const v8::FunctionCallbackInfo<Value> &args) {
Handle<Value> result = Null(isolate);
if (int id = v8c_CheckClient(args, __FUNCTION__)) {
result = V8STR(STRING(entities[id]->v.netname));
}
args.GetReturnValue().Set(result);
}
/**
* V8 Common: Get player's auth id (steam id)
*
* @params FunctionCallbackInfo args virtual arguments: id
*
* @return v8::String
*/
void v8c_GetPlayerAuthId(const v8::FunctionCallbackInfo<Value> &args) {
Handle<Value> result = Null(isolate);
if (int id = v8c_CheckClient(args, __FUNCTION__)) {
result = V8STR(g_engfuncs.pfnGetPlayerAuthId(entities[id]));
}
args.GetReturnValue().Set(result);
}
/**
* V8 Common: Get player's info key buffer (\key\value...)
*
* @params FunctionCallbackInfo args virtual arguments: id
*
* @return v8::String
*/
void v8c_GetPlayerInfoKeyBuffer(const v8::FunctionCallbackInfo<Value> &args) {
Handle<Value> result = Null(isolate);
if (int id = v8c_CheckClient(args, __FUNCTION__)) {
result = V8STR(g_engfuncs.pfnGetInfoKeyBuffer(entities[id]));
}
args.GetReturnValue().Set(result);
}
/**
* V8 Common: Get player's user id (#uid)
*
* @params FunctionCallbackInfo args virtual arguments: id
*
* @return v8::String
*/
void v8c_GetPlayerUserId(const v8::FunctionCallbackInfo<Value> &args) {
Handle<Value> result = Null(isolate);
if (int id = v8c_CheckClient(args, __FUNCTION__)) {
result = V8NUM(g_engfuncs.pfnGetPlayerUserId(entities[id]));
}
args.GetReturnValue().Set(result);
}
/**
* V8 Common: Get player's WON id
*
* @params FunctionCallbackInfo args virtual arguments: id
*
* @return v8::String
*/
void v8c_GetPlayerWONId(const v8::FunctionCallbackInfo<Value> &args) {
Handle<Value> result = Null(isolate);
if (int id = v8c_CheckClient(args, __FUNCTION__)) {
result = V8NUM(g_engfuncs.pfnGetPlayerWONId(entities[id]));
}
args.GetReturnValue().Set(result);
}
void v8c_RegUserMsg(const v8::FunctionCallbackInfo<Value> &args) {
String::Utf8Value name(args[0]->ToString());
int iSize = args[1]->ToInt32()->Value();
int msgid = g_engfuncs.pfnRegUserMsg(*name, iSize);
args.GetReturnValue().Set(V8NUM(msgid));
}
/**
* Convert a vec3_t array to an Object
*/
Handle<Object> v8c_Vec3tToObject(vec3_t vec)
{
Context::Scope context_scope(context);
Handle<Object> vecObj = V8OBJ();
vecObj->Set(V8STR("x"), V8NUM(vec[0]));
vecObj->Set(V8STR("y"), V8NUM(vec[1]));
vecObj->Set(V8STR("z"), V8NUM(vec[2]));
return vecObj;
}
/**
* Check whether a callback is defined in context
*
* @param char* name function name
*
* @return int CB_*
*/
int v8c_CheckCallbackIsDefined(const char *name)
{
int result = CB_NA;
Context::Scope context_scope(context);
Handle<String> myName = V8STR(name);
Handle<Function> fn = Handle<Function>::Cast(context->Global()->Get(myName));
if (fn->GetName()->ToString()->Equals(myName)) {
result = CB_DEFINED;
} else {
ALERT(at_logged, "[HLDSV8] No %s defined in script!\n", name);
result = CB_UNDEFINED;
}
return result;
}
/**
* Universal callback with params and return value
*
* @param char* name function name
* @param int isDefined is the callback defined in script
* @param Handle<Object> params js object with arguments
*
* @return Handle<Value>
*/
Handle<Value> v8c_UCallbackReturn(const char *name, Handle<Object> params)
{
Context::Scope context_scope(context);
Handle<Value> callbackReturn = Null(isolate);
Handle<String> myName = V8STR(name);
Handle<Function> fn = Handle<Function>::Cast(context->Global()->Get(myName));
const int fnArgc = 1;
Handle<Value> fnArgs[fnArgc] = { params };
Handle<Value> fnReturnValue = fn->Call(context->Global(), fnArgc, fnArgs);
int metaResult = 1;
if (fnReturnValue->IsInt32()) {
metaResult = fnReturnValue->ToInt32()->Value();
} else {
Handle<Object> fnReturnObject = Handle<Object>::Cast(fnReturnValue);
metaResult = fnReturnObject->Get(V8STR("vres"))->ToInt32()->Value();
callbackReturn = fnReturnObject->Get(V8STR("retval"));
}
SET_META_RESULT(META_RES(metaResult ? metaResult : 1));
return callbackReturn;
}
/**
* Universal callback with params
*
* @param char* name function name
* @param int isDefined is the callback defined in script
* @param Handle<Object> params js object with arguments
*
* @return Handle<Value>
*/
META_RES v8c_UCallback(const char *name, Handle<Object> params)
{
Context::Scope context_scope(context);
Handle<String> myName = V8STR(name);
Handle<Function> fn = Handle<Function>::Cast(context->Global()->Get(myName));
const int fnArgc = 1;
Handle<Value> fnArgs[fnArgc] = { params };
int res = fn->Call(context->Global(), fnArgc, fnArgs)->ToInt32()->Value();
META_RES result = META_RES(res ? res : 1);
return result;
}
META_RES v8c_UCallback(const char *name)
{
Context::Scope context_scope(context);
Handle<Object> params = V8OBJ();
return v8c_UCallback(name, params);
}
META_RES v8c_UCallback(const char *name, int id)
{
Context::Scope context_scope(context);
Handle<Object> params = V8OBJ();
params->Set(V8STR("id"), V8NUM(id));
return v8c_UCallback(name, params);
}
/**
* Global JS window object
*/
static void jsWindowObjectAccessor(Local<String> property, const PropertyCallbackInfo<Value>& info) {
info.GetReturnValue().Set(info.Holder());
}
/**
* window.console.log() implementation
*
* @params FunctionCallbackInfo args virtual arguments: str, ...
*
* @return v8::Null
*/
static void jsConsolePrint(const v8::FunctionCallbackInfo<Value> &args) {
for (int i=0; i < args.Length(); i++) {
String::Utf8Value str(args[i]->ToString());
ALERT(at_logged, "%s\n", *str);
}
args.GetReturnValue().Set(Null(isolate));
}
/**
* Report an exception from script compiling
*
* @param Isolate isolate
* @param TryCatch try_catch
*
* @return void
*/
void ReportException(Isolate* isolate, TryCatch* try_catch) {
HandleScope handle_scope(isolate);
String::Utf8Value exception(try_catch->Exception()->ToString());
Handle<Message> message = try_catch->Message();
if (message.IsEmpty()) {
// V8 didn't provide any extra information about this error; just
// print the exception.
ALERT(at_logged, "[HLDSV8] %s\n", *exception);
} else {
// Print (filename):(line number): (message).
String::Utf8Value filename(message->GetScriptResourceName());
int linenum = message->GetLineNumber();
ALERT(at_logged, "[HLDSV8] %s:%i: %s\n", *filename, linenum, *exception);
// Print line of source code.
String::Utf8Value sourceline(message->GetSourceLine());
ALERT(at_logged, "[HLDSV8] %s\n", *sourceline);
String::Utf8Value stack_trace(try_catch->StackTrace());
if (stack_trace.length() > 0) {
ALERT(at_logged, "[HLDSV8] %s\n", *stack_trace);
}
}
}
/**
* Execute JS snippet
*
* @param Isolate isolate
* @param Handle<String> source js code
* @param Handle<Value> name script name
* @param bool print_result print result?
* @param bool report_exceptions call exception reporting on compilation / runtime exceptions
*/
bool ExecuteString(Isolate* isolate, Handle<String> source, Handle<Value> name, bool print_result, bool report_exceptions) {
HandleScope handle_scope(isolate);
TryCatch try_catch;
Handle<Script> script = Script::Compile(source, name);
if (script.IsEmpty()) {
// Print errors that happened during compilation.
if (report_exceptions)
ReportException(isolate, &try_catch);
return false;
} else {
Handle<Value> result = script->Run();
if (result.IsEmpty()) {
// Print errors that happened during execution.
if (report_exceptions)
ReportException(isolate, &try_catch);
return false;
} else {
if (print_result && !result->IsUndefined()) {
// If all went well and the result wasn't undefined then print
// the returned value.
String::Utf8Value str(result);
ALERT(at_logged, "[HLDSV8] %s\n", *str);
}
return true;
}
}
}
/**
* Initialize v8 js engine
*
* @return void
*/
void jsInitialize()
{
Handle<ObjectTemplate> globalObject = ObjectTemplate::New();
Handle<ObjectTemplate> console = ObjectTemplate::New();
Handle<ObjectTemplate> v8Common = ObjectTemplate::New();
globalObject->SetAccessor(V8STR("window" ), jsWindowObjectAccessor);
globalObject->Set (V8STR("console"), console);
console ->Set (V8STR("log" ), FunctionTemplate::New(isolate, jsConsolePrint));
globalObject->Set (V8STR("v8"), v8Common);
v8Common ->Set (V8STR("getPlayerName" ), FunctionTemplate::New(isolate, v8c_GetPlayerName));
v8Common ->Set (V8STR("getPlayerUserId" ), FunctionTemplate::New(isolate, v8c_GetPlayerUserId));
v8Common ->Set (V8STR("getPlayerAuthId" ), FunctionTemplate::New(isolate, v8c_GetPlayerAuthId));
v8Common ->Set (V8STR("getPlayerInfoKeyBuffer"), FunctionTemplate::New(isolate, v8c_GetPlayerInfoKeyBuffer));
v8Common ->Set (V8STR("getPlayerWONId" ), FunctionTemplate::New(isolate, v8c_GetPlayerWONId));
v8Common ->Set (V8STR("regUserMsg" ), FunctionTemplate::New(isolate, v8c_RegUserMsg));
context = Context::New(isolate, NULL, globalObject);
Context::Scope context_scope(context);
char* scriptSource = fileReader("/home/cstrike/inst/debug/cstrike/plugins/hldsv8/scripts/test.js");
if (scriptSource != NULL) {
if (!ExecuteString(isolate, V8STR(scriptSource), V8STR("test.js"), true, true)) {
ALERT(at_logged, "[HLDSV8] Engine Started with errors!\n");
}
} else {
ALERT(at_logged, "[HLDSV8] Engine failed to start, no script\n");
}
}