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
|
#pragma once
#include <array>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <string>
#include <unistd.h>
#include <vector>
#include "util/io.hpp"
#include "util/bitset.hpp"
namespace net {
namespace packet {
using packet_flag = struct { };
template <class T>
concept packet = requires(T x) { x.packet_id; };
template <class T>
concept compressed_packet = packet<T> && requires(T x) { x.compressed; };
template <class T>
concept netmodule = requires(T x) { x.module_id; };
struct nstring {
enum ns_type : uint8_t {
Literal,
Formattable,
LocalizationKey,
} type;
std::string text;
std::vector<std::string> substitutions;
template <class T>
ssize_t read(io::serialized_io<T>& io) {
uint8_t t;
auto offset = io.read(t);
type = (ns_type(t));
offset += io.read(text);
uint8_t subs_len;
offset += io.read(subs_len);
if (subs_len > 0) {
substitutions.resize(subs_len);
io.read(substitutions);
}
return offset;
}
template <class T>
ssize_t write(io::serialized_io<T>& io) {
auto offset = io.write(uint8_t(type));
offset += io.write(text);
offset += io.write(uint8_t(substitutions.size()));
offset += io.write(substitutions);
return offset;
}
};
struct rgb {
uint8_t r;
uint8_t g;
uint8_t b;
};
template<class T>
struct vec2 {
T x;
T y;
};
template<class T>
struct plist {
std::vector<T> v;
template <class K>
ssize_t read(io::serialized_io<K>& io) {
uint8_t len;
auto offset = io.read(len);
v.resize(len);
offset += io.read(v);
return offset;
}
template <class K>
ssize_t write(io::serialized_io<K>& io) {
auto offset = io.write(uint8_t(v.size()));
offset += io.write(v);
return offset;
}
};
// NetMessages
struct connect {
static constexpr uint8_t packet_id = 1;
std::string version;
};
struct disconnect {
static constexpr uint8_t packet_id = 2;
nstring reason;
};
struct accept {
static constexpr uint8_t packet_id = 3;
uint8_t slot;
uint8_t server_flags = 0;
};
struct player_info {
static constexpr uint8_t packet_id = 4;
uint8_t slot;
uint8_t skin_variant;
uint8_t voice_variant;
float voice_offset;
uint8_t hair;
std::string name;
uint8_t hair_dye;
bitset<16> hidden_accessories; // FIXME: this is rounded number of flags. Terraria may use less than 16
bitset<8> hide_misc; // same here
rgb hair_color;
rgb skin_color;
rgb eye_color;
rgb shirt_color;
rgb undershirt_color;
rgb pants_color;
rgb shoe_color;
bitset<4> player_difficulty;
bitset<5> permanent_buffs;
bitset<7> permanent_buffs_shimmer;
};
struct player_inventory_slot {
static constexpr uint8_t packet_id = 5;
enum class slot_flags : int {
Favourite = 0,
BlockTransfer = 1,
};
uint8_t slot;
uint16_t item_slot_id;
uint16_t stack;
uint8_t prefix;
uint16_t type;
bitset<2> flags;
};
struct request_world_info {
static constexpr uint8_t packet_id = 6;
};
struct world_info {
static constexpr uint8_t packet_id = 7;
int32_t time;
bitset<3> time_flags; // day? blood moon? eclipse?
uint8_t moon_phase;
vec2<uint16_t> tiles_limit;
vec2<uint16_t> spawn_tile;
uint16_t surface_level;
uint16_t rock_level;
int32_t worldid;
std::string worldname;
uint8_t gamemode;
std::array<uint8_t, 16> uuid;
uint64_t worldgen_version;
uint8_t moon_type;
std::array<uint8_t, 13> backgrounds;
std::array<uint8_t, 3> bg_styles;
float wind_speed_target;
uint8_t num_clouds;
std::array<int32_t, 3> trees;
std::array<uint8_t, 4> trees_styles;
std::array<int32_t, 3> cave_bg;
std::array<uint8_t, 4> cave_bg_styles;
std::array<uint8_t, 13> tree_tops;
float max_raining;
bitset<81> flags; // TODO: document them
uint8_t sundial_cooldown;
uint8_t moondial_cooldown;
std::array<uint16_t, 7> world_ores;
int8_t invasion_type;
uint64_t lobby_id = 0;
float sandstorm_severity;
plist<vec2<uint16_t>> extra_spawn_points;
};
struct request_spawn {
static constexpr uint8_t packet_id = 8;
vec2<int32_t> tile;
uint8_t team;
};
struct set_loading_message {
static constexpr uint8_t packet_id = 9;
int32_t sections;
nstring message;
uint8_t flags = 0;
};
inline nstring operator""_ns(const char* str, std::size_t)
{
return nstring{nstring::Literal, str};
}
}
}
using net::packet::operator""_ns;
|