Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
commands.hpp
Go to the documentation of this file.
1
3#pragma once
5#include <aegis2d/core/export.hpp>
6
7#include <cstddef>
8#include <cstdint>
9#include <functional>
10#include <map>
11#include <optional>
12#include <span>
13#include <string>
14#include <string_view>
15#include <vector>
16
17namespace aegis2d::core {
18
20enum class ArgType : std::uint8_t {
22 Text,
24 Integer,
26 Number,
28 Boolean,
29};
30
32struct CommandArg {
34 std::string name{};
38 bool optional = false;
41 bool rest = false;
42};
43
45class AEGIS2D_CORE_EXPORT CommandCall {
46public:
50 CommandCall(std::span<const CommandArg> arguments, std::vector<std::optional<VarValue>> values);
51
56 [[nodiscard]] bool has(std::string_view argument) const;
57
63 [[nodiscard]] const std::string& text(std::string_view argument) const;
64
69 [[nodiscard]] std::int64_t integer(std::string_view argument) const;
70
75 [[nodiscard]] double number(std::string_view argument) const;
76
81 [[nodiscard]] bool boolean(std::string_view argument) const;
82
83private:
84 [[nodiscard]] const VarValue& value(std::string_view argument) const;
85 [[nodiscard]] std::size_t index(std::string_view argument) const;
86
87 std::span<const CommandArg> arguments_;
88 std::vector<std::optional<VarValue>> values_;
89};
90
94using CommandHandler = std::function<std::string(const CommandCall& call)>;
95
97enum class CommandStatus : std::uint8_t {
99 Done,
101 Empty,
103 Unknown,
107 Failed,
108};
109
118
140class AEGIS2D_CORE_EXPORT Commands {
141public:
144
153 void add(std::string_view name, std::vector<CommandArg> arguments, std::string_view description,
154 CommandHandler handler);
155
159 void add_variables(ConsoleVariables& variables) noexcept;
160
164 [[nodiscard]] bool contains(std::string_view name) const;
165
169 CommandResult run(std::string_view line);
170
175 [[nodiscard]] std::string usage(std::string_view name) const;
176
179 [[nodiscard]] std::vector<std::string> names() const;
180
181private:
182 struct Command {
183 std::vector<CommandArg> arguments;
184 std::string description;
185 CommandHandler handler;
186 };
187
188 [[nodiscard]] std::string help(std::span<const std::string> words) const;
189 CommandResult run_variable(std::string_view name, std::span<const std::string> words);
190
191 std::map<std::string, Command, std::less<>> commands_;
192 ConsoleVariables* variables_ = nullptr;
193};
194
195} // namespace aegis2d::core
The arguments of a command being run, read in their types.
Definition commands.hpp:45
bool has(std::string_view argument) const
Tells whether an argument was given.
bool boolean(std::string_view argument) const
A boolean argument.
CommandCall(std::span< const CommandArg > arguments, std::vector< std::optional< VarValue > > values)
Arguments as read.
const std::string & text(std::string_view argument) const
A text argument.
double number(std::string_view argument) const
A number argument; an integer argument reads as a number too.
std::int64_t integer(std::string_view argument) const
An integer argument.
The commands a console runs: each has a name, its arguments and a help.
Definition commands.hpp:140
void add_variables(ConsoleVariables &variables) noexcept
Makes the variables of a registry commands too.
std::string usage(std::string_view name) const
How to call a command.
CommandResult run(std::string_view line)
Runs a line.
bool contains(std::string_view name) const
Tells whether a line can start with a name.
std::vector< std::string > names() const
Names of the commands and the variables, in alphabetical order.
void add(std::string_view name, std::vector< CommandArg > arguments, std::string_view description, CommandHandler handler)
Adds a command.
Commands()
Creates the commands, with help only.
The settings a console reads and changes by name while the game runs.
Definition console_variables.hpp:80
Console variables: named, typed settings with a default, a description and flags.
Logging, time, main loop, events and configuration shared by client and server.
Definition clock.hpp:11
std::variant< bool, std::int64_t, double, std::string > VarValue
Value of a console variable: a boolean, an integer, a number or a text.
Definition console_variables.hpp:30
std::function< std::string(const CommandCall &call)> CommandHandler
What a command does with its arguments.
Definition commands.hpp:94
ArgType
Type of an argument of a command, read from its text as a console variable's.
Definition commands.hpp:20
@ Boolean
1, 0, true, false, on or off.
@ Text
A word, or words between double quotes.
@ Integer
A whole number.
CommandStatus
How running a line went.
Definition commands.hpp:97
@ WrongArguments
An argument is missing, too many are given, or one does not read in its type.
@ Unknown
No command and no variable has this name.
@ Empty
The line has no command (blank).
@ Failed
The command failed, or a variable refused its new value.
An argument a command takes: {.name = "count", .type = ArgType::Integer}.
Definition commands.hpp:32
ArgType type
Its type.
Definition commands.hpp:36
bool optional
May be left out; the optional arguments come after the others.
Definition commands.hpp:38
bool rest
Takes the rest of the words, joined by a space: only for the last argument, a text.
Definition commands.hpp:41
std::string name
Its name, as the help shows it and CommandCall reads it.
Definition commands.hpp:34
What running a line gave.
Definition commands.hpp:111
CommandStatus status
How it went.
Definition commands.hpp:113
std::string text
What the command showed, or why the line did not run (with the usage of the command for wrong argumen...
Definition commands.hpp:116