fix(lua): better error reporting when calling player commands (#2932)

This commit is contained in:
Quentin 2024-04-10 11:43:55 +02:00 committed by GitHub
parent 58e1ccf36c
commit 0dc5ad6137
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 62 additions and 8 deletions

View File

@ -2,7 +2,7 @@
Table for calling menu commands.
## Functions (2)
## Functions (3)
### `call(command_name, _args)`
@ -31,4 +31,14 @@ Call a menu command on a given player.
command.call_player(player_idx, command_name, _args)
```
### `get_all_player_command_names()`
- **Returns:**
- `table<integer, string>`: Table that contains the names of all the player commands.
**Example Usage:**
```lua
table<integer, string> = command.get_all_player_command_names()
```

View File

@ -2,7 +2,7 @@
Table containing helper functions for network related features.
## Functions (12)
## Functions (13)
### `trigger_script_event(bitset, _args)`
@ -157,4 +157,17 @@ Sends a message to the in game chat.
network.send_chat_message(msg, team_only)
```
### `send_chat_message_to_player(player_idx, msg)`
Sends a chat message to the specified player. Other players would not be able to see the message
- **Parameters:**
- `player_idx` (integer): Index of the player.
- `msg` (string): Message to be sent.
**Example Usage:**
```lua
network.send_chat_message_to_player(player_idx, msg)
```

View File

@ -29,6 +29,8 @@ namespace big
{
if (make_all_version)
m_all_component = std::make_unique<player_all_component>(this, name, label, description, num_args);
g_player_commands[rage::joaat(name)] = this;
}
void player_command::execute(const command_arguments& args, const std::shared_ptr<command_context> ctx)
@ -111,12 +113,11 @@ namespace big
void player_command::call(player_ptr player, const command_arguments& args, const std::shared_ptr<command_context> ctx)
{
// TODO: Code duplication
if (m_num_args.has_value() && args.size() != (m_num_args.value() - 1))
{
ctx->report_error(std::format("Command {} called with the wrong number of arguments. Expected {}, got {}",
m_name,
m_num_args.value(),
m_num_args.value() - 1,
args.size()));
return;
}

View File

@ -18,6 +18,8 @@ namespace big
player_all_component(player_command* parent, const std::string& name, const std::string& label, const std::string& description, std::optional<uint8_t> num_args);
};
inline std::unordered_map<rage::joaat_t, player_command*> g_player_commands;
class player_command : public command
{
friend player_all_component;
@ -33,7 +35,13 @@ namespace big
};
public:
static player_command* get(rage::joaat_t command)
{
return g_player_commands[command];
}
void call(player_ptr player, const command_arguments& args, const std::shared_ptr<command_context> ctx = std::make_shared<default_command_context>());
player_command(const std::string& name, const std::string& label, const std::string& description, std::optional<uint8_t> num_args, bool make_all_version = true);
};
}

View File

@ -1,5 +1,6 @@
#pragma once
#include "command.hpp"
#include "backend/command.hpp"
#include "backend/player_command.hpp"
#include "network.hpp" // for convert_sequence
@ -37,7 +38,7 @@ namespace lua::command
{
const auto args = convert_sequence<uint64_t>(_args.value_or(sol::table()));
const auto command = (big::player_command*)big::command::get(rage::joaat(command_name));
const auto command = big::player_command::get(rage::joaat(command_name));
if (command)
{
@ -48,6 +49,26 @@ namespace lua::command
command->call(player, args);
}
}
else
{
LOG(FATAL) << "No player command called " << command_name;
}
}
// Lua API: Function
// Table: command
// Name: get_all_player_command_names
// Returns: table<integer, string>: Table that contains the names of all the player commands.
static std::vector<std::string> get_all_player_command_names()
{
std::vector<std::string> res;
for (const auto& cmd : big::g_player_commands | std::ranges::views::values)
{
res.push_back(cmd->get_name());
}
return res;
}
void bind(sol::state& state)
@ -55,5 +76,6 @@ namespace lua::command
auto ns = state["command"].get_or_create<sol::table>();
ns["call"] = call;
ns["call_player"] = call_player;
ns["get_all_player_command_names"] = get_all_player_command_names;
}
}