aboutsummaryrefslogtreecommitdiff
path: root/src/server/bnetserver/Packets/PacketsCommon.h
blob: 2705a369ad589b115beefa67343eaa71ebaba7b0 (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
/*
 * Copyright (C) 2008-2015 TrinityCore <http://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 PacketsCommon_h__
#define PacketsCommon_h__

#include "Common.h"
#include "Realm/Realm.h"
#include <typeinfo>

namespace Battlenet
{
    struct PrintableComponent
    {
        virtual ~PrintableComponent() { }

        virtual std::string ToString() const = 0;
    };

    namespace Format
    {
        template<typename T>
        struct is_printable : std::is_base_of<PrintableComponent, typename std::remove_pointer<T>::type>::type
        {
        };

        template<typename T>
        typename std::enable_if<!std::is_pointer<T>::value, T>::type const& Dereference(T const& t) { return t; };

        template<typename T>
        typename std::enable_if<std::is_pointer<T>::value, typename std::remove_pointer<T>::type>::type const& Dereference(T const& t) { return *t; };

        template<typename T>
        std::ostream& FieldToString(std::ostream& stream, char const* fieldName, T const& u, std::false_type)
        {
            if (fieldName[0])
                stream << fieldName << ": ";
            return stream << u << std::endl;
        }

        std::ostream& FieldToString(std::ostream& stream, char const* fieldName, PrintableComponent const& u, std::true_type);
        std::ostream& FieldToString(std::ostream& stream, char const* fieldName, uint8 const& u, std::false_type);

        template<typename T>
        std::ostream& FieldToString(std::ostream& stream, char const* fieldName, std::vector<T> const& u, std::false_type)
        {
            stream << fieldName << ":" << std::endl;
            for (T const& t : u)
                FieldToString(stream, "", Dereference(t), is_printable<T>());
            return stream;
        }

        template<typename T>
        std::ostream& FieldToString(std::ostream& stream, char const* fieldName, Optional<T> const& u)
        {
            if (u)
                FieldToString(stream, fieldName, Dereference(*u), is_printable<T>());
            return stream;
        }

        template<typename T>
        std::ostream& FieldToString(std::ostream& stream, char const* fieldName, T const& u)
        {
            return FieldToString(stream, fieldName, Dereference(u), is_printable<T>());
        }
    }

#define APPEND_FIELD(stream, field) Format::FieldToString(stream, #field, field)

    namespace Version
    {
        struct Record : public PrintableComponent
        {
            std::string ProgramId;
            std::string Component;
            uint32 Version;

            std::string ToString() const override;
        };
    }

    namespace Cache
    {
        struct Handle : public PrintableComponent
        {
            std::string Type;
            std::string Region;
            uint8 ModuleId[32] = { };

            std::string ToString() const override;
        };
    }

    namespace Account
    {
        struct FullName : public PrintableComponent
        {
            std::string GivenName;
            std::string Surname;

            std::string ToString() const override;
        };
    }

    namespace GameAccount
    {
        struct Handle : public PrintableComponent
        {
            uint8 Region = 0;
            std::string ProgramId;
            uint32 Id = 0;

            std::string ToString() const override;
        };
    }

    // For use in packets
    struct PrintableRealmHandle : public RealmHandle, public PrintableComponent
    {
        PrintableRealmHandle() : RealmHandle() { }
        PrintableRealmHandle(uint8 region, uint8 battlegroup, uint32 index)
            : RealmHandle(region, battlegroup, index) { }

        PrintableRealmHandle& operator=(RealmHandle const& r)
        {
            RealmHandle::operator=(r);
            return *this;
        }

        std::string ToString() const override;
    };

    namespace Toon
    {
        struct FullName : public PrintableComponent
        {
            uint8 Region = 0;
            std::string ProgramId;
            uint32 Realm = 0;
            std::string Name;

            std::string ToString() const override;
        };

        struct Handle : public PrintableComponent
        {
            uint8 Region = 0;
            std::string ProgramId;
            uint32 Realm = 0;
            uint64 Id = 0;

            std::string ToString() const override;
        };
    }

    namespace Profile
    {
        struct RecordAddress : public PrintableComponent
        {
            uint32 Label = 0;
            uint64 Id = 0;

            std::string ToString() const override;
        };
    }
}

#endif // PacketsCommon_h__