aboutsummaryrefslogtreecommitdiff
path: root/src/server/game/Chat/ChatCommands/ChatCommandHelpers.h
blob: 27bd1285fe9fc740f9d8e6bec005d17b356ed7d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef TRINITY_CHATCOMMANDHELPERS_H
#define TRINITY_CHATCOMMANDHELPERS_H

#include "Define.h"
#include "Language.h"
#include "Optional.h"
#include "StringFormat.h"
#include <fmt/printf.h>
#include <string>
#include <string_view>
#include <type_traits>
#include <variant>

class ChatHandler;

namespace Trinity::Impl::ChatCommands
{
    /***************** HELPERS *************************\
    |* These really aren't for outside use...          *|
    \***************************************************/

    static constexpr char COMMAND_DELIMITER = ' ';

    template <typename T, typename = void>
    struct tag_base
    {
        using type = T;
    };

    template <typename T>
    using tag_base_t = typename tag_base<T>::type;

    struct TokenizeResult {
        explicit operator bool() { return !token.empty(); }
        std::string_view token;
        std::string_view tail;
    };

    inline TokenizeResult tokenize(std::string_view args)
    {
        TokenizeResult result;
        if (size_t delimPos = args.find(COMMAND_DELIMITER); delimPos != std::string_view::npos)
        {
            result.token = args.substr(0, delimPos);
            if (size_t tailPos = args.find_first_not_of(COMMAND_DELIMITER, delimPos); tailPos != std::string_view::npos)
                result.tail = args.substr(tailPos);
        }
        else
            result.token = args;

        return result;
    }

    template <typename T, typename... Ts>
    struct are_all_assignable
    {
        static constexpr bool value = (std::is_assignable_v<T&, Ts> && ...);
    };

    template <typename... Ts>
    struct are_all_assignable<void, Ts...>
    {
        static constexpr bool value = false;
    };

    template <std::size_t index, typename T1, typename... Ts>
    struct get_nth : get_nth<index-1, Ts...> { };

    template <typename T1, typename... Ts>
    struct get_nth<0, T1, Ts...>
    {
        using type = T1;
    };

    template <std::size_t index, typename... Ts>
    using get_nth_t = typename get_nth<index, Ts...>::type;

    // this essentially models std::optional<std::string_view>, except it can also hold an error message
    // it has std::string_view's bool conversion and dereference operators
    //
    // monostate <-> unspecified error, typically end-of-string reached or parsing failed
    // std::string <-> specified error, typically character-not-found or invalid item link
    // std::string_view <-> success, string_view is remaining argument string
    struct ChatCommandResult
    {
        ChatCommandResult(std::nullopt_t) : _storage() {}
        ChatCommandResult(std::string const&) = delete;
        ChatCommandResult(std::string&& s) : _storage(std::in_place_type<std::string>, std::forward<std::string>(s)) {}
        ChatCommandResult(char const* c) : _storage(std::in_place_type<std::string>, c) {}
        ChatCommandResult(std::string_view s) : _storage(std::in_place_type<std::string_view>, s) {}

        ChatCommandResult(ChatCommandResult const&) = delete;
        ChatCommandResult(ChatCommandResult&&) = default;
        ChatCommandResult& operator=(ChatCommandResult const&) = delete;
        ChatCommandResult& operator=(ChatCommandResult&&) = default;

        std::string_view operator*() const { return std::get<std::string_view>(_storage); }
        bool IsSuccessful() const { return std::holds_alternative<std::string_view>(_storage); }
        explicit operator bool() const { return IsSuccessful(); }
        bool HasErrorMessage() const { return std::holds_alternative<std::string>(_storage); }
        std::string const& GetErrorMessage() const { return std::get<std::string>(_storage); }

        private:
            std::variant<std::monostate, std::string_view, std::string> _storage;
    };

    TC_GAME_API void SendErrorMessageToHandler(ChatHandler* handler, std::string_view str);
    TC_GAME_API char const* GetTrinityString(ChatHandler const* handler, TrinityStrings which);
    TC_GAME_API std::string FormatTrinityString(std::string_view messageFormat, fmt::printf_args messageFormatArgs);
    template <typename... Ts>
    std::string FormatTrinityString(ChatHandler const* handler, TrinityStrings which, Ts&&... args)
    {
        return FormatTrinityString(GetTrinityString(handler, which), fmt::make_printf_args(std::forward<Ts>(args)...));
    }
}

#endif