| 1748 | } |
| 1749 | |
| 1750 | void ScriptEngine::RunGameActionHooks(const GameActions::GameAction& action, GameActions::Result& result, bool isExecute) |
| 1751 | { |
| 1752 | auto hookType = isExecute ? HookType::actionExecute : HookType::actionQuery; |
| 1753 | if (_hookEngine.HasSubscriptions(hookType)) |
| 1754 | { |
| 1755 | JSContext* ctx = _replContext; |
| 1756 | JSValue obj = JS_NewObject(ctx); |
| 1757 | |
| 1758 | auto actionId = action.GetType(); |
| 1759 | if (action.GetType() == GameCommand::Custom) |
| 1760 | { |
| 1761 | auto customAction = static_cast<const GameActions::CustomAction&>(action); |
| 1762 | JS_SetPropertyStr(ctx, obj, "action", JSFromStdString(ctx, customAction.GetId())); |
| 1763 | |
| 1764 | JSValue jsArgs = JS_ParseJSON( |
| 1765 | ctx, customAction.GetJson().c_str(), customAction.GetJson().length(), customAction.GetName()); |
| 1766 | if (!JS_IsException(jsArgs)) |
| 1767 | { |
| 1768 | JS_SetPropertyStr(ctx, obj, "args", jsArgs); |
| 1769 | } |
| 1770 | else |
| 1771 | { |
| 1772 | JS_SetPropertyStr(ctx, obj, "args", JS_NewObject(ctx)); |
| 1773 | } |
| 1774 | } |
| 1775 | else |
| 1776 | { |
| 1777 | auto actionName = GetActionName(actionId); |
| 1778 | if (!actionName.empty()) |
| 1779 | { |
| 1780 | JS_SetPropertyStr(ctx, obj, "action", JSFromStdString(ctx, actionName)); |
| 1781 | } |
| 1782 | |
| 1783 | JSValue args = JS_NewObject(ctx); |
| 1784 | JSFromGameActionParameterVisitor visitor(ctx, args); |
| 1785 | const_cast<GameActions::GameAction&>(action).AcceptParameters(visitor); |
| 1786 | const_cast<GameActions::GameAction&>(action).AcceptFlags(visitor); |
| 1787 | JS_SetPropertyStr(ctx, obj, "args", args); |
| 1788 | } |
| 1789 | |
| 1790 | JS_SetPropertyStr(ctx, obj, "player", JS_NewInt32(ctx, action.GetPlayer())); |
| 1791 | JS_SetPropertyStr(ctx, obj, "type", JS_NewInt32(ctx, EnumValue(actionId))); |
| 1792 | |
| 1793 | auto flags = action.GetActionFlags(); |
| 1794 | JS_SetPropertyStr(ctx, obj, "isClientOnly", JS_NewBool(ctx, (flags & GameActions::Flags::ClientOnly) != 0)); |
| 1795 | |
| 1796 | JS_SetPropertyStr(ctx, obj, "result", GameActionResultToJS(ctx, action, result)); |
| 1797 | |
| 1798 | _hookEngine.Call(hookType, obj, false, true); |
| 1799 | |
| 1800 | if (!isExecute) |
| 1801 | { |
| 1802 | JSValue jsResult = JS_GetPropertyStr(ctx, obj, "result"); |
| 1803 | if (JS_IsObject(jsResult)) |
| 1804 | { |
| 1805 | int32_t error = AsOrDefault(ctx, jsResult, "error", int32_t()); |
| 1806 | if (error != 0) |
| 1807 | { |
no test coverage detected