| 1333 | } |
| 1334 | |
| 1335 | GameActions::Result ScriptEngine::QueryOrExecuteCustomGameAction(const GameActions::CustomAction& customAction, bool isExecute) |
| 1336 | { |
| 1337 | JSContext* ctx = _replContext; |
| 1338 | std::string actionz = customAction.GetId(); |
| 1339 | auto kvp = _customActions.find(actionz); |
| 1340 | if (kvp != _customActions.end()) |
| 1341 | { |
| 1342 | const auto& customActionInfo = kvp->second; |
| 1343 | |
| 1344 | // Deserialise the JSON args |
| 1345 | const std::string& argsz = customAction.GetJson(); |
| 1346 | |
| 1347 | auto jsArgs = JS_ParseJSON(ctx, argsz.c_str(), argsz.size(), customActionInfo.Name.c_str()); |
| 1348 | if (JS_IsException(jsArgs)) |
| 1349 | { |
| 1350 | auto res = GameActions::Result(); |
| 1351 | res.error = GameActions::Status::invalidParameters; |
| 1352 | res.errorTitle = "Invalid JSON"; |
| 1353 | return res; |
| 1354 | } |
| 1355 | |
| 1356 | std::vector<JSValue> pluginCallArgs; |
| 1357 | if (customActionInfo.Owner->GetTargetAPIVersion() <= kApiVersionCustomActionArgs) |
| 1358 | { |
| 1359 | pluginCallArgs = { jsArgs }; |
| 1360 | } |
| 1361 | else |
| 1362 | { |
| 1363 | JSValue obj = JS_NewObject(ctx); |
| 1364 | JS_SetPropertyStr(ctx, obj, "action", JSFromStdString(ctx, actionz)); |
| 1365 | JS_SetPropertyStr(ctx, obj, "args", jsArgs); |
| 1366 | JS_SetPropertyStr(ctx, obj, "player", JS_NewInt32(ctx, customAction.GetPlayer())); |
| 1367 | JS_SetPropertyStr(ctx, obj, "type", JS_NewInt32(ctx, EnumValue(customAction.GetType()))); |
| 1368 | |
| 1369 | auto flags = customAction.GetActionFlags(); |
| 1370 | JS_SetPropertyStr(ctx, obj, "isClientOnly", JS_NewBool(ctx, (flags & GameActions::Flags::ClientOnly) != 0)); |
| 1371 | pluginCallArgs = { obj }; |
| 1372 | } |
| 1373 | |
| 1374 | // Ready to call plugin handler |
| 1375 | JSValue jsResult; |
| 1376 | if (!isExecute) |
| 1377 | { |
| 1378 | jsResult = ExecutePluginCall( |
| 1379 | customActionInfo.Owner, customActionInfo.Query.callback, JS_UNDEFINED, pluginCallArgs, false, false, true); |
| 1380 | } |
| 1381 | else |
| 1382 | { |
| 1383 | jsResult = ExecutePluginCall( |
| 1384 | customActionInfo.Owner, customActionInfo.Execute.callback, JS_UNDEFINED, pluginCallArgs, true, false, true); |
| 1385 | } |
| 1386 | |
| 1387 | GameActions::Result res = JSToGameActionResult(ctx, jsResult); |
| 1388 | JS_FreeValue(ctx, jsResult); |
| 1389 | return res; |
| 1390 | } |
| 1391 | |
| 1392 | auto res = GameActions::Result(); |
no test coverage detected