-
Notifications
You must be signed in to change notification settings - Fork 654
/
Copy pathbasic.h
173 lines (141 loc) · 4.57 KB
/
basic.h
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
/*
===========================================================================
Copyright (c) 2010-2015 Darkstar Dev Teams
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 3 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 _BASICPACKET_H
#define _BASICPACKET_H
#include "common/cbasetypes.h"
#include "common/socket.h"
#include "common/tracy.h"
#include <stdio.h>
#include <string.h>
// Max packet size
constexpr size_t PACKET_SIZE = 0x1FF;
enum ENTITYUPDATE
{
ENTITY_SPAWN,
ENTITY_SHOW,
ENTITY_UPDATE,
ENTITY_HIDE,
ENTITY_DESPAWN,
};
//
// Base class for all packets
//
// Contains a 0x1FF byte sized buffer
// Access the raw data with ref<T>(index)
//
class CBasicPacket
{
protected:
std::array<uint8, PACKET_SIZE> buffer_;
public:
CBasicPacket()
{
TracyZoneScoped;
std::fill(buffer_.data(), buffer_.data() + PACKET_SIZE, 0);
}
explicit CBasicPacket(const CBasicPacket& other)
{
TracyZoneScoped;
std::memcpy(buffer_.data(), other.buffer_.data(), PACKET_SIZE);
}
explicit CBasicPacket(const std::unique_ptr<CBasicPacket>& other)
{
TracyZoneScoped;
std::memcpy(buffer_.data(), other->buffer_.data(), PACKET_SIZE);
}
static auto createFromBuffer(const uint8* buffer) -> std::unique_ptr<CBasicPacket>
{
auto packet = std::make_unique<CBasicPacket>();
std::memcpy(packet->buffer_.data(), buffer, PACKET_SIZE);
return packet;
}
virtual ~CBasicPacket() = default;
// Copy and move operators
CBasicPacket& operator=(const CBasicPacket& other) = delete;
CBasicPacket& operator=(CBasicPacket&& other) noexcept = delete;
auto copy() -> std::unique_ptr<std::remove_pointer_t<decltype(this)>>
{
return std::make_unique<std::remove_pointer_t<decltype(this)>>(*this);
}
uint16 getType()
{
return ref<uint16>(0) & 0x1FF;
}
std::size_t getSize()
{
return std::min<std::size_t>(2U * (ref<uint8>(1) & ~1), PACKET_SIZE);
}
unsigned short getSequence()
{
return ref<uint16>(2);
}
// Set the first 9 bits to the ID. The highest bit overflows into the second byte.
void setType(unsigned int new_id)
{
ref<uint16>(0) &= ~0x1FF;
ref<uint16>(0) |= new_id & 0x1FF;
}
// The length "byte" is actually just the highest 7 bits.
// Need to preserve the lowest bit for the ID.
void setSize(std::size_t new_size)
{
ref<uint8>(1) &= 1;
ref<uint8>(1) |= ((new_size + 3) & ~3) / 2;
}
void setSequence(unsigned short new_sequence)
{
ref<uint16>(2) = new_sequence;
}
// Indexer for the buffer's data
template <typename T>
T& ref(std::size_t index)
{
return ::ref<T>(buffer_.data(), index);
}
// Reinterpret and use the underlying buffer as a different type
template <typename T>
auto as() -> std::remove_pointer_t<T>*
{
static_assert(std::is_standard_layout_v<T>, "Type must be standard layout (No virtual functions, inheritance, etc.)");
return reinterpret_cast<std::remove_pointer_t<T>*>(buffer_.data());
}
// Reinterpret the underlying buffer as a different type and apply a function to it
template <typename T>
void as(const auto& fn)
{
static_assert(std::is_standard_layout_v<T>, "Type must be standard layout (No virtual functions, inheritance, etc.)");
fn(reinterpret_cast<std::remove_pointer_t<T>*>(buffer_.data()));
}
operator uint8*()
{
return buffer_.data();
}
uint8* operator[](const std::size_t index)
{
return reinterpret_cast<uint8*>(buffer_.data()) + index;
}
// Used for setting "proper" packet sizes rounded to the nearest four away from zero
uint32 roundUpToNearestFour(uint32 input)
{
int remainder = input % 4;
if (remainder == 0)
{
return input;
}
return input + 4 - remainder;
}
};
#endif // _BASICPACKET_H