2022-12-22 21:23:32 +00:00
|
|
|
#include "bool_command.hpp"
|
2023-07-26 22:22:40 +02:00
|
|
|
#include "fiber_pool.hpp"
|
2023-06-23 09:55:26 +02:00
|
|
|
#include "services/translation_service/translation_service.hpp"
|
2022-12-22 21:23:32 +00:00
|
|
|
|
|
|
|
namespace big
|
|
|
|
{
|
2023-03-11 00:06:09 +01:00
|
|
|
bool_command::bool_command(const std::string& name, const std::string& label, const std::string& description, bool& toggle, bool show_notify) :
|
2023-03-01 21:27:15 +00:00
|
|
|
command(name, label, description, std::nullopt),
|
2023-03-11 00:06:09 +01:00
|
|
|
m_toggle(toggle),
|
|
|
|
m_show_notify(show_notify)
|
2022-12-22 21:23:32 +00:00
|
|
|
{
|
2023-07-26 22:22:40 +02:00
|
|
|
g_bool_commands.push_back(this);
|
2022-12-22 21:23:32 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 22:22:40 +02:00
|
|
|
void bool_command::execute(const command_arguments& args, const std::shared_ptr<command_context> ctx)
|
2022-12-22 21:23:32 +00:00
|
|
|
{
|
|
|
|
if (args.size() == 0)
|
|
|
|
{
|
|
|
|
if (is_enabled())
|
|
|
|
{
|
|
|
|
m_toggle = false;
|
2023-03-11 00:06:09 +01:00
|
|
|
|
|
|
|
if (m_show_notify)
|
2023-06-23 09:55:26 +02:00
|
|
|
ctx->report_output(std::format("{} has been disabled", g_translation_service.get_translation(m_label)));
|
2022-12-22 21:23:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_toggle = true;
|
2023-03-11 00:06:09 +01:00
|
|
|
|
|
|
|
if (m_show_notify)
|
2023-06-23 09:55:26 +02:00
|
|
|
ctx->report_output(std::format("{} has been enabled", g_translation_service.get_translation(m_label)));
|
2022-12-22 21:23:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-07-26 22:22:40 +02:00
|
|
|
m_toggle = args.get<bool>(0);
|
2022-12-22 21:23:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this->refresh();
|
|
|
|
}
|
|
|
|
|
2023-07-26 22:22:40 +02:00
|
|
|
std::optional<command_arguments> bool_command::parse_args(const std::vector<std::string>& args, const std::shared_ptr<command_context> ctx)
|
2022-12-22 21:23:32 +00:00
|
|
|
{
|
2023-07-26 22:22:40 +02:00
|
|
|
command_arguments result(1);
|
2022-12-22 21:23:32 +00:00
|
|
|
|
|
|
|
if (args.size() == 0)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
if (args.size() > 1)
|
|
|
|
{
|
|
|
|
ctx->report_error(std::format("Too many arguments passed to command {}, Expected 1 or less, got {}", m_name, args.size()));
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args[0] == "yes" || args[0] == "on" || args[0] == "enable" || args[0] == "true")
|
|
|
|
{
|
2023-07-26 22:22:40 +02:00
|
|
|
result.push(true);
|
2022-12-22 21:23:32 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args[0] == "no" || args[0] == "off" || args[0] == "disable" || args[0] == "false")
|
|
|
|
{
|
2023-07-26 22:22:40 +02:00
|
|
|
result.push(false);
|
2022-12-22 21:23:32 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->report_error(std::format("Cannot convert\"{}\" into a boolean in command {}", args[0], m_name));
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2023-07-26 22:22:40 +02:00
|
|
|
|
|
|
|
void bool_command::enable()
|
|
|
|
{
|
|
|
|
if (!m_toggle)
|
|
|
|
{
|
|
|
|
m_toggle = true;
|
|
|
|
m_last_enabled = true;
|
2024-06-27 08:32:17 +00:00
|
|
|
g_fiber_pool->execute_on_game_thread([this] {
|
2023-07-26 22:22:40 +02:00
|
|
|
on_enable();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void bool_command::disable()
|
|
|
|
{
|
|
|
|
if (m_toggle)
|
|
|
|
{
|
|
|
|
m_toggle = false;
|
|
|
|
m_last_enabled = false;
|
2024-06-27 08:32:17 +00:00
|
|
|
g_fiber_pool->execute_on_game_thread([this] {
|
2023-07-26 22:22:40 +02:00
|
|
|
on_disable();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void bool_command::refresh()
|
|
|
|
{
|
|
|
|
if (m_toggle && !m_last_enabled)
|
|
|
|
{
|
|
|
|
m_last_enabled = true;
|
2024-06-27 08:32:17 +00:00
|
|
|
g_fiber_pool->execute_on_game_thread([this] {
|
2023-07-26 22:22:40 +02:00
|
|
|
on_enable();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if (!m_toggle && m_last_enabled)
|
|
|
|
{
|
|
|
|
m_last_enabled = false;
|
2024-06-27 08:32:17 +00:00
|
|
|
g_fiber_pool->execute_on_game_thread([this] {
|
2023-07-26 22:22:40 +02:00
|
|
|
on_disable();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-12-22 21:23:32 +00:00
|
|
|
}
|