forked from otland/forgottenserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetworkmessage.cpp
138 lines (113 loc) · 3.2 KB
/
networkmessage.cpp
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
/**
* The Forgotten Server - a free and open-source MMORPG server emulator
* Copyright (C) 2016 Mark Samman <[email protected]>
*
* 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "otpch.h"
#include "networkmessage.h"
#include "container.h"
#include "creature.h"
std::string NetworkMessage::getString(uint16_t stringLen/* = 0*/)
{
if (stringLen == 0) {
stringLen = get<uint16_t>();
}
if (!canRead(stringLen)) {
return std::string();
}
char* v = reinterpret_cast<char*>(buffer) + position; //does not break strict aliasing
position += stringLen;
return std::string(v, stringLen);
}
Position NetworkMessage::getPosition()
{
Position pos;
pos.x = get<uint16_t>();
pos.y = get<uint16_t>();
pos.z = getByte();
return pos;
}
void NetworkMessage::addString(const std::string& value)
{
size_t stringLen = value.length();
if (!canAdd(stringLen + 2) || stringLen > 8192) {
return;
}
add<uint16_t>(stringLen);
memcpy(buffer + position, value.c_str(), stringLen);
position += stringLen;
length += stringLen;
}
void NetworkMessage::addDouble(double value, uint8_t precision/* = 2*/)
{
addByte(precision);
add<uint32_t>((value * std::pow(static_cast<float>(10), precision)) + std::numeric_limits<int32_t>::max());
}
void NetworkMessage::addBytes(const char* bytes, size_t size)
{
if (!canAdd(size) || size > 8192) {
return;
}
memcpy(buffer + position, bytes, size);
position += size;
length += size;
}
void NetworkMessage::addPaddingBytes(size_t n)
{
if (!canAdd(n)) {
return;
}
memset(buffer + position, 0x33, n);
length += n;
}
void NetworkMessage::addPosition(const Position& pos)
{
add<uint16_t>(pos.x);
add<uint16_t>(pos.y);
addByte(pos.z);
}
void NetworkMessage::addItem(uint16_t id, uint8_t count)
{
const ItemType& it = Item::items[id];
add<uint16_t>(it.clientId);
addByte(0xFF); // MARK_UNMARKED
if (it.stackable) {
addByte(count);
} else if (it.isSplash() || it.isFluidContainer()) {
addByte(fluidMap[count & 7]);
}
if (it.isAnimation) {
addByte(0xFE); // random phase (0xFF for async)
}
}
void NetworkMessage::addItem(const Item* item)
{
const ItemType& it = Item::items[item->getID()];
add<uint16_t>(it.clientId);
addByte(0xFF); // MARK_UNMARKED
if (it.stackable) {
addByte(std::min<uint16_t>(0xFF, item->getItemCount()));
} else if (it.isSplash() || it.isFluidContainer()) {
addByte(fluidMap[item->getFluidType() & 7]);
}
if (it.isAnimation) {
addByte(0xFE); // random phase (0xFF for async)
}
}
void NetworkMessage::addItemId(uint16_t itemId)
{
add<uint16_t>(Item::items[itemId].clientId);
}