TrinityCore
Loading...
Searching...
No Matches
ChatCommandTags.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef TRINITY_CHATCOMMANDTAGS_H
19#define TRINITY_CHATCOMMANDTAGS_H
20
21#include "ChatCommandHelpers.h"
22#include "Hyperlinks.h"
23#include "ObjectGuid.h"
24#include "Optional.h"
25#include "Util.h"
26#include <boost/preprocessor/repetition/repeat.hpp>
27#include <boost/preprocessor/punctuation/comma_if.hpp>
28#include <fmt/ostream.h>
29#include <cmath>
30#include <cstring>
31#include <iostream>
32#include <string>
33#include <string_view>
34#include <tuple>
35#include <type_traits>
36#include <utility>
37#include <variant>
38
39class ChatHandler;
40class Player;
41class WorldSession;
42
44{
49
50 template <typename T>
51 struct tag_base<T, std::enable_if_t<std::is_base_of_v<ContainerTag, T>>>
52 {
53 using type = typename T::value_type;
54 };
55
56 template <size_t N>
57 inline constexpr char GetChar(char const (&s)[N], size_t i)
58 {
59 static_assert(N <= 25, "The EXACT_SEQUENCE macro can only be used with up to 25 character long literals. Specify them char-by-char (null terminated) as parameters to ExactSequence<> instead.");
60 return i >= N ? '\0' : s[i];
61 }
62
63#define CHATCOMMANDS_IMPL_SPLIT_LITERAL_EXTRACT_CHAR(z, i, strliteral) \
64 BOOST_PP_COMMA_IF(i) Trinity::Impl::ChatCommands::GetChar(strliteral, i)
65
66#define CHATCOMMANDS_IMPL_SPLIT_LITERAL_CONSTRAINED(maxlen, strliteral) \
67 BOOST_PP_REPEAT(maxlen, CHATCOMMANDS_IMPL_SPLIT_LITERAL_EXTRACT_CHAR, strliteral)
68
69 // this creates always 25 elements - "abc" -> 'a', 'b', 'c', '\0', '\0', ... up to 25
70#define CHATCOMMANDS_IMPL_SPLIT_LITERAL(strliteral) CHATCOMMANDS_IMPL_SPLIT_LITERAL_CONSTRAINED(25, strliteral)
71}
72
74{
75 /************************** CONTAINER TAGS **********************************************\
76 |* Simple holder classes to differentiate between extraction methods *|
77 |* Must inherit from Trinity::Impl::ChatCommands::ContainerTag *|
78 |* Must implement the following: *|
79 |* - TryConsume: ChatHandler const*, std::string_view -> ChatCommandResult *|
80 |* - on match, returns tail of the provided argument string (as std::string_view) *|
81 |* - on specific error, returns error message (as std::string&& or char const*) *|
82 |* - on generic error, returns std::nullopt (this will print command usage) *|
83 |* *|
84 |* - typedef value_type of type that is contained within the tag *|
85 |* - cast operator to value_type *|
86 |* *|
87 \****************************************************************************************/
88
89 template <char... chars>
91 {
92 using value_type = void;
93
94 ChatCommandResult TryConsume(ChatHandler const* handler, std::string_view args) const
95 {
96 if (args.empty())
97 return std::nullopt;
98 std::string_view start = args.substr(0, _string.length());
99 if (StringEqualI(start, _string))
100 {
101 auto [remainingToken, tail] = Trinity::Impl::ChatCommands::tokenize(args.substr(_string.length()));
102 if (remainingToken.empty()) // if this is not empty, then we did not consume the full token
103 return tail;
104 start = args.substr(0, _string.length() + remainingToken.length());
105 }
107 }
108
109 private:
110 static constexpr std::array<char, sizeof...(chars)> _storage = { chars... };
111 static_assert(!_storage.empty() && (_storage.back() == '\0'), "ExactSequence parameters must be null terminated! Use the EXACT_SEQUENCE macro to make this easier!");
112 static constexpr std::string_view _string = { _storage.data(), std::string_view::traits_type::length(_storage.data()) };
113 };
114
115#define EXACT_SEQUENCE(str) Trinity::ChatCommands::ExactSequence<CHATCOMMANDS_IMPL_SPLIT_LITERAL(str)>
116
118 {
119 using value_type = std::string_view;
120
121 using std::string_view::operator=;
122
123 ChatCommandResult TryConsume(ChatHandler const*,std::string_view args)
124 {
125 std::string_view::operator=(args);
126 return std::string_view();
127 }
128 };
129
131 {
132 using value_type = std::wstring;
133
134 using std::wstring::operator=;
135
136 ChatCommandResult TryConsume(ChatHandler const* handler, std::string_view args)
137 {
138 if (Utf8toWStr(args, *this))
139 return std::string_view();
140 else
142 }
143 };
144
146 {
147 using value_type = std::string;
148
149 TC_GAME_API ChatCommandResult TryConsume(ChatHandler const* handler, std::string_view args);
150 };
151
153 {
155
156 AccountIdentifier() : _id(), _name(), _session(nullptr) {}
158
159 operator uint32() const { return _id; }
160 operator std::string const& () const { return _name; }
161 operator std::string_view() const { return { _name }; }
162
163 uint32 GetID() const { return _id; }
164 std::string const& GetName() const { return _name; }
165 bool IsConnected() { return _session != nullptr; }
166 WorldSession* GetConnectedSession() { return _session; }
167
168 ChatCommandResult TryConsume(ChatHandler const* handler, std::string_view args);
169
170 static Optional<AccountIdentifier> FromTarget(ChatHandler* handler);
171
172 private:
174 std::string _name;
176 };
177
179 {
181
182 PlayerIdentifier() : _name(), _guid(), _player(nullptr) {}
183 PlayerIdentifier(Player& player);
184
185 operator ObjectGuid() const { return _guid; }
186 operator std::string const&() const { return _name; }
187 operator std::string_view() const { return _name; }
188
189 std::string const& GetName() const { return _name; }
190 ObjectGuid GetGUID() const { return _guid; }
191 bool IsConnected() const { return (_player != nullptr); }
192 Player* GetConnectedPlayer() const { return _player; }
193
194 ChatCommandResult TryConsume(ChatHandler const* handler, std::string_view args);
195
196 static Optional<PlayerIdentifier> FromTarget(ChatHandler* handler);
197 static Optional<PlayerIdentifier> FromSelf(ChatHandler* handler);
199 {
200 if (Optional<PlayerIdentifier> fromTarget = FromTarget(handler))
201 return fromTarget;
202 else
203 return FromSelf(handler);
204 }
205
206 private:
207 std::string _name;
210 };
211
212 template <typename linktag>
214 {
215 using value_type = typename linktag::value_type;
216 using storage_type = std::remove_cvref_t<value_type>;
217
218 operator value_type() const { return val; }
219 value_type operator*() const { return val; }
220 storage_type const* operator->() const { return &val; }
221
222 ChatCommandResult TryConsume(ChatHandler const* handler, std::string_view args)
223 {
225 // invalid hyperlinks cannot be consumed
226 if (!info)
227 return std::nullopt;
228
229 // check if we got the right tag
230 if (info.tag != linktag::tag())
231 return std::nullopt;
232
233 // store value
234 if (!linktag::StoreTo(val, info.data))
236
237 // finally, skip any potential delimiters
238 auto [token, next] = Trinity::Impl::ChatCommands::tokenize(info.tail);
239 if (token.empty()) /* empty token = first character is delimiter, skip past it */
240 return next;
241 else
242 return info.tail;
243 }
244
245 private:
247 };
248
249 // pull in link tags for user convenience
250 using namespace ::Trinity::Hyperlinks::LinkTags;
251}
252
253namespace Trinity::Impl
254{
255 template <typename T>
257 {
258 template <typename U>
259 T operator()(U const& v) const { return v; }
260 };
261}
262
263namespace Trinity::ChatCommands
264{
265 template <typename T1, typename... Ts>
266 struct Variant : public std::variant<T1, Ts...>
267 {
268 using base = std::variant<T1, Ts...>;
269
272
273 template <bool C = have_operators>
274 std::enable_if_t<C, first_type> operator*() const
275 {
277 }
278
279 template <bool C = have_operators>
280 operator std::enable_if_t<C, first_type>() const
281 {
282 return operator*();
283 }
284
285 template <bool C = have_operators>
286 std::enable_if_t<C, bool> operator!() const { return !**this; }
287
288 template <typename T>
289 Variant& operator=(T&& arg) { base::operator=(std::forward<T>(arg)); return *this; }
290
291 template <size_t index>
292 constexpr decltype(auto) get() { return std::get<index>(static_cast<base&>(*this)); }
293 template <size_t index>
294 constexpr decltype(auto) get() const { return std::get<index>(static_cast<base const&>(*this)); }
295 template <typename type>
296 constexpr decltype(auto) get() { return std::get<type>(static_cast<base&>(*this)); }
297 template <typename type>
298 constexpr decltype(auto) get() const { return std::get<type>(static_cast<base const&>(*this)); }
299
300 template <typename T>
301 constexpr decltype(auto) visit(T&& arg) { return std::visit(std::forward<T>(arg), static_cast<base&>(*this)); }
302 template <typename T>
303 constexpr decltype(auto) visit(T&& arg) const { return std::visit(std::forward<T>(arg), static_cast<base const&>(*this)); }
304
305 template <typename T>
306 constexpr bool holds_alternative() const { return std::holds_alternative<T>(static_cast<base const&>(*this)); }
307
308 template <bool C = have_operators>
309 friend std::enable_if_t<C, std::ostream&> operator<<(std::ostream& os, Trinity::ChatCommands::Variant<T1, Ts...> const& v)
310 {
311 return (os << *v);
312 }
313 };
314}
315
316template <typename T1, typename... Ts>
317struct fmt::formatter<Trinity::ChatCommands::Variant<T1, Ts...>> : ostream_formatter { };
318
319#endif
#define TC_GAME_API
Definition Define.h:114
#define STRING_VIEW_FMT_ARG(str)
Definition Define.h:126
uint32_t uint32
Definition Define.h:133
@ LANG_CMDPARSER_EXACT_SEQ_MISMATCH
Definition Language.h:1016
@ LANG_CMDPARSER_LINKDATA_INVALID
Definition Language.h:1005
@ LANG_CMDPARSER_INVALID_UTF8
Definition Language.h:1004
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition Optional.h:25
bool StringEqualI(std::string_view a, std::string_view b)
Definition Util.cpp:706
bool Utf8toWStr(char const *utf8str, size_t csize, wchar_t *wstr, size_t &wsize)
Definition Util.cpp:383
Player session in the World.
TokenizeResult tokenize(std::string_view args)
typename tag_base< T >::type tag_base_t
TC_GAME_API char const * GetTrinityString(ChatHandler const *handler, TrinityStrings which)
constexpr char GetChar(char const (&s)[N], size_t i)
TC_GAME_API std::string FormatTrinityString(std::string_view messageFormat, fmt::printf_args messageFormatArgs)
STL namespace.
static constexpr std::string_view _string
static constexpr std::array< char, sizeof...(chars)> _storage
ChatCommandResult TryConsume(ChatHandler const *handler, std::string_view args) const
std::string const & GetName() const
static Optional< PlayerIdentifier > FromTargetOrSelf(ChatHandler *handler)
TC_GAME_API ChatCommandResult TryConsume(ChatHandler const *handler, std::string_view args)
ChatCommandResult TryConsume(ChatHandler const *, std::string_view args)
constexpr bool holds_alternative() const
constexpr decltype(auto) visit(T &&arg)
Trinity::Impl::ChatCommands::tag_base_t< T1 > first_type
std::enable_if_t< C, first_type > operator*() const
constexpr decltype(auto) visit(T &&arg) const
constexpr decltype(auto) get()
std::enable_if_t< C, bool > operator!() const
std::variant< T1, Ts... > base
static constexpr bool have_operators
friend std::enable_if_t< C, std::ostream & > operator<<(std::ostream &os, Trinity::ChatCommands::Variant< T1, Ts... > const &v)
constexpr decltype(auto) get() const
ChatCommandResult TryConsume(ChatHandler const *handler, std::string_view args)
T operator()(U const &v) const