4
4
#include " ../extensions/ExtensionManager.h"
5
5
#include " ../server/commands/CommandsManager.h"
6
6
7
- LUALIB_API int luaopen_cmsgpack (lua_State * L);
7
+ LUALIB_API int luaopen_cmsgpack (lua_State* L);
8
8
extern " C"
9
9
{
10
10
LUALIB_API int luaopen_rapidjson (lua_State* L);
@@ -66,7 +66,7 @@ EventResult Plugin::TriggerEvent(std::string invokedBy, std::string eventName, s
66
66
PERF_RECORD (string_format (" event:%s:%s" , invokedBy.c_str (), eventName.c_str ()), this ->GetName ());
67
67
68
68
EValue payload (ctx, eventPayload);
69
- if (ctx->GetKind () == ContextKinds::JavaScript)
69
+ if (ctx->GetKind () == ContextKinds::JavaScript)
70
70
payload = EValue (ctx, JS_NewUint8ArrayCopy ((JSContext*)ctx->GetState (), (uint8_t *)(eventPayload.data ()), eventPayload.size ()));
71
71
72
72
int res = (int )EventResult::Continue;
@@ -86,7 +86,7 @@ EventResult Plugin::TriggerEvent(std::string invokedBy, std::string eventName, s
86
86
{
87
87
PRINTF (" An error has occured while executing event '%s':\n Error: %s\n " , eventName.c_str (), e.what ());
88
88
89
- if (eventName == " OnPluginStart" ) this ->SetLoadError (e.what ());
89
+ if (eventName == " OnPluginStart" ) this ->SetLoadError (e.what ());
90
90
}
91
91
92
92
return (EventResult)res;
@@ -159,8 +159,8 @@ void Plugin::ExecuteCommand(void* functionPtr, std::string name, int slot, std::
159
159
{
160
160
PERF_RECORD (string_format (" command:%s" , name.c_str ()), this ->GetName ())
161
161
162
- if (functionPtr == nullptr )
163
- return ;
162
+ if (functionPtr == nullptr )
163
+ return ;
164
164
165
165
EValue* commandRef = (EValue*)functionPtr;
166
166
@@ -183,17 +183,17 @@ void Plugin::ExecuteCommand(void* functionPtr, std::string name, int slot, std::
183
183
bool Plugin::LoadScriptingEnvironment ()
184
184
{
185
185
this ->SetLoadError (" " );
186
-
186
+
187
187
ctx = new EContext (GetKind () == PluginKind_t::Lua ? ContextKinds::Lua : ContextKinds::JavaScript);
188
188
189
- if (ctx->GetKind () == ContextKinds::Lua) {
189
+ if (ctx->GetKind () == ContextKinds::Lua) {
190
190
ctx->RegisterLuaLib (" msgpack" , luaopen_cmsgpack);
191
191
ctx->RegisterLuaLib (" json" , luaopen_rapidjson);
192
192
}
193
193
194
194
SetupScriptingEnvironment (this , ctx);
195
195
196
- if (GetKind () == PluginKind_t::JavaScript) {
196
+ if (GetKind () == PluginKind_t::JavaScript) {
197
197
for (Extension* ext : extManager->GetExtensionsList ())
198
198
if (ext->IsLoaded ()) {
199
199
std::string error = " " ;
@@ -239,7 +239,8 @@ bool Plugin::LoadScriptingEnvironment()
239
239
this ->SetLoadError (error);
240
240
return false ;
241
241
}
242
- } catch (EException& e) {
242
+ }
243
+ catch (EException& e) {
243
244
std::string error = e.what ();
244
245
PRINTF (" Failed to load plugin file '%s'.\n " , file.c_str ());
245
246
PRINTF (" Error: %s\n " , error.c_str ());
@@ -269,7 +270,8 @@ bool Plugin::LoadScriptingEnvironment()
269
270
this ->SetLoadError (error);
270
271
return false ;
271
272
}
272
- } catch (EException& e) {
273
+ }
274
+ catch (EException& e) {
273
275
std::string error = e.what ();
274
276
PRINTF (" Failed to load plugin file '%s'.\n " , file.c_str ());
275
277
PRINTF (" Error: %s\n " , error.c_str ());
@@ -338,7 +340,7 @@ bool Plugin::ExecuteStart()
338
340
TriggerEvent (" core" , " OnPluginStart" , encoders::msgpack::SerializeToString ({}), event);
339
341
delete event;
340
342
341
- if (GetLoadError () != " " ) return false ;
343
+ if (GetLoadError () != " " ) return false ;
342
344
343
345
return true ;
344
346
}
@@ -388,7 +390,7 @@ EValue SerializeData(std::any data, EContext* state)
388
390
if (starts_with (val, " JSON⇚" ) && ends_with (val, " ⇛" )) {
389
391
std::string json = explode (explode (val, " ⇚" )[1 ], " ⇛" )[0 ];
390
392
391
- if (state->GetKind () == ContextKinds::Lua) {
393
+ if (state->GetKind () == ContextKinds::Lua) {
392
394
EValue rapidJsonTable = EValue::getGlobal (state, " json" );
393
395
if (!rapidJsonTable[" decode" ].isFunction ())
394
396
return EValue (state, emptyTable);
@@ -408,9 +410,11 @@ EValue SerializeData(std::any data, EContext* state)
408
410
return EValue (state, emptyTable);
409
411
410
412
return decodedResult;
411
- } else if (state->GetKind () == ContextKinds::JavaScript) {
413
+ }
414
+ else if (state->GetKind () == ContextKinds::JavaScript) {
412
415
return EValue (state, JS_ParseJSON ((JSContext*)state->GetState (), json.c_str (), json.length (), " SerializeData" ));
413
- } else return EValue (state);
416
+ }
417
+ else return EValue (state, nullptr );
414
418
}
415
419
else return EValue (state, val);
416
420
}
@@ -454,29 +458,30 @@ EValue SerializeData(std::any data, EContext* state)
454
458
return EValue (state, nullptr );
455
459
else if (value.type () == typeid (std::vector<std::string>))
456
460
{
457
- if (state->GetKind () == ContextKinds::Lua) {
461
+ if (state->GetKind () == ContextKinds::Lua) {
458
462
std::vector<std::string> tmpval = std::any_cast<std::vector<std::string>>(value);
459
463
std::string tbl = tmpval[0 ];
460
-
464
+
461
465
EValue load = EValue::getGlobal (state, " load" );
462
466
try
463
467
{
464
468
EValue loadReturnValue = load (tbl);
465
469
if (!loadReturnValue.isFunction ())
466
470
return EValue (state, emptyTable);
467
-
471
+
468
472
EValue loadFuncRetVal = loadReturnValue ();
469
473
if (!loadFuncRetVal.isTable ())
470
474
return EValue (state, emptyTable);
471
-
475
+
472
476
return loadFuncRetVal;
473
477
}
474
478
catch (EException& e)
475
479
{
476
480
PRINTF (" Exception: %s\n " , e.what ());
477
481
return EValue (state, emptyTable);
478
482
}
479
- } else {
483
+ }
484
+ else {
480
485
PRINT (" Cannot convert Lua table to JS object.\n " );
481
486
return EValue (state);
482
487
}
@@ -512,18 +517,20 @@ std::any DeserializeData(EValue ref, EContext* state)
512
517
return ref.cast <std::string>();
513
518
else if (ref.isTable ())
514
519
{
515
- if (state->GetKind () == ContextKinds::Lua) {
520
+ if (state->GetKind () == ContextKinds::Lua) {
516
521
EValue serpentDump = EValue::getGlobal (state, " serpent" )[" dump" ];
517
522
EValue serpentDumpReturnValue = serpentDump (ref);
518
523
519
524
std::vector<std::string> tmptbl;
520
525
tmptbl.push_back (serpentDumpReturnValue.cast <std::string>());
521
526
522
527
return tmptbl;
523
- } else if (state->GetKind () == ContextKinds::JavaScript) {
528
+ }
529
+ else if (state->GetKind () == ContextKinds::JavaScript) {
524
530
std::vector<std::string> tmptbl;
525
531
return tmptbl;
526
- } else return nullptr ;
532
+ }
533
+ else return nullptr ;
527
534
}
528
535
else if (ref.isInstance <Color>())
529
536
return ref.cast <Color>();
0 commit comments