aboutsummaryrefslogtreecommitdiff
path: root/src/server/game/Chat/Hyperlinks.h
blob: 70508c2642d4bbc65433f3beb53d35ea0f9f8882 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * Copyright (C) 2008-2019 TrinityCore <https://www.trinitycore.org/>
 *
 * 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_HYPERLINKS_H
#define TRINITY_HYPERLINKS_H

#include "advstd.h"
#include "ObjectGuid.h"
#include <string>
#include <utility>

struct AchievementEntry;
struct GlyphPropertiesEntry;
struct GlyphSlotEntry;
struct ItemTemplate;
class SpellInfo;
class Quest;
struct TalentEntry;

namespace Trinity
{
namespace Hyperlinks
{

struct AchievementLinkData
{
    AchievementEntry const* Achievement;
    ObjectGuid::LowType CharacterId;
    bool IsFinished;
    uint16 Year;
    uint8 Month;
    uint8 Day;
    uint32 Criteria[4];
};

struct GlyphLinkData
{
    GlyphPropertiesEntry const* Glyph;
    GlyphSlotEntry const* Slot;
};

struct ItemLinkData
{
    ItemTemplate const* Item;
    uint32 EnchantId;
    uint32 GemEnchantId[3];
    int32 RandomPropertyId;
    int32 RandomPropertySeed;
    uint8 RenderLevel;
};

struct QuestLinkData
{
    ::Quest const* Quest;
    uint8 QuestLevel;
};

struct TalentLinkData
{
    TalentEntry const* Talent;
    uint8 Rank;
};

struct TradeskillLinkData
{
    SpellInfo const* Spell;
    uint16 CurValue;
    uint16 MaxValue;
    ObjectGuid Owner;
    std::string KnownRecipes;
};

namespace LinkTags {

    /************************** LINK TAGS ***************************************************\
    |* Link tags must abide by the following:                                               *|
    |* - MUST expose ::value_type typedef                                                   *|
    |*   - storage type is remove_cvref_t<value_type>                                       *|
    |* - MUST expose static ::tag method, void -> const char*                               *|
    |*   - this method SHOULD be constexpr                                                  *|
    |*   - returns identifier string for the link ("creature", "creature_entry", "item")    *|
    |* - MUST expose static ::StoreTo method, (storage&, char const*, size_t)               *|
    |*   - assign value_type& based on content of std::string(char const*, size_t)          *|
    |*   - return value indicates success/failure                                           *|
    |*   - for integral/string types this can be achieved by extending base_tag             *|
    \****************************************************************************************/
    struct base_tag
    {
        static bool StoreTo(std::string& val, char const* pos, size_t len)
        {
            val.assign(pos, len);
            return true;
        }

        template <typename T>
        static std::enable_if_t<advstd::is_integral_v<T> && advstd::is_unsigned_v<T>, bool> StoreTo(T& val, char const* pos, size_t len)
        {
            try { val = std::stoull(std::string(pos, len)); }
            catch (...) { return false; }
            return true;
        }

        template <typename T>
        static std::enable_if_t<advstd::is_integral_v<T> && advstd::is_signed_v<T>, bool> StoreTo(T& val, char const* pos, size_t len)
        {
            try { val = std::stoll(std::string(pos, len)); }
            catch (...) { return false; }
            return true;
        }

        static bool StoreTo(ObjectGuid& val, char const* pos, size_t len)
        {
            try { val.Set(std::stoul(std::string(pos, len), nullptr, 16)); }
            catch (...) { return false; }
            return true;
        }
    };

#define make_base_tag(ltag, type) struct ltag : public base_tag { using value_type = type; static constexpr char const* tag() { return #ltag; } }
    make_base_tag(area, uint32);
    make_base_tag(areatrigger, uint32);
    make_base_tag(creature, ObjectGuid::LowType);
    make_base_tag(creature_entry, uint32);
    make_base_tag(gameevent, uint32);
    make_base_tag(gameobject, ObjectGuid::LowType);
    make_base_tag(gameobject_entry, uint32);
    make_base_tag(itemset, uint32);
    make_base_tag(player, std::string const&);
    make_base_tag(skill, uint32);
    make_base_tag(taxinode, uint32);
    make_base_tag(tele, uint32);
    make_base_tag(title, uint32);
#undef make_base_tag

    struct TC_GAME_API achievement
    {
        using value_type = AchievementLinkData const&;
        static constexpr char const* tag() { return "achievement"; }
        static bool StoreTo(AchievementLinkData& val, char const* pos, size_t len);
    };

    struct TC_GAME_API enchant
    {
        using value_type = SpellInfo const*;
        static constexpr char const* tag() { return "enchant"; }
        static bool StoreTo(SpellInfo const*& val, char const* pos, size_t len);
    };

    struct TC_GAME_API glyph
    {
        using value_type = GlyphLinkData const&;
        static constexpr char const* tag() { return "glyph"; };
        static bool StoreTo(GlyphLinkData& val, char const* pos, size_t len);
    };

    struct TC_GAME_API item
    {
        using value_type = ItemLinkData const&;
        static constexpr char const* tag() { return "item"; }
        static bool StoreTo(ItemLinkData& val, char const* pos, size_t len);
    };

    struct TC_GAME_API quest
    {
        using value_type = QuestLinkData const&;
        static constexpr char const* tag() { return "quest"; }
        static bool StoreTo(QuestLinkData& val, char const* pos, size_t len);
    };

    struct TC_GAME_API spell
    {
        using value_type = SpellInfo const*;
        static constexpr char const* tag() { return "spell"; }
        static bool StoreTo(SpellInfo const*& val, char const* pos, size_t len);
    };

    struct TC_GAME_API talent
    {
        using value_type = TalentLinkData const&;
        static constexpr char const* tag() { return "talent"; }
        static bool StoreTo(TalentLinkData& val, char const* pos, size_t len);
    };

    struct TC_GAME_API trade
    {
        using value_type = TradeskillLinkData const&;
        static constexpr char const* tag() { return "trade"; }
        static bool StoreTo(TradeskillLinkData& val, char const* pos, size_t len);
    };
}

struct HyperlinkColor
{
    HyperlinkColor(uint32 c) : r(c >> 16), g(c >> 8), b(c), a(c >> 24) {}
    uint8 r, g, b, a;
    bool operator==(uint32 c) const
    {
        if ((c & 0xff) ^ b)
            return false;
        if (((c >>= 8) & 0xff) ^ g)
            return false;
        if (((c >>= 8) & 0xff) ^ r)
            return false;
        if ((c >>= 8) ^ a)
            return false;
        return true;
    }
};

struct HyperlinkInfo
{
    HyperlinkInfo(char const* n = nullptr, uint32 c = 0, char const* tS = nullptr, size_t tL = 0, char const* dS = nullptr, size_t dL = 0, char const* cS = nullptr, size_t cL = 0) :
        next(n), color(c), tag(tS, tL), data(dS, dL), text(cS, cL) {}

    explicit operator bool() { return next; }
    char const* const next;
    HyperlinkColor const color;
    std::pair<char const*, size_t> const tag;
    std::pair<char const*, size_t> const data;
    std::pair<char const*, size_t> const text;
};
HyperlinkInfo TC_GAME_API ParseHyperlink(char const* pos);
bool TC_GAME_API CheckAllLinks(std::string const&);

}
}

#endif