Botcraft 26.3
Loading...
Searching...
No Matches
LastSeenMessagesTracker.cpp
Go to the documentation of this file.
1#if PROTOCOL_VERSION > 759 /* > 1.19 */
2
4
5namespace Botcraft
6{
8 {
9#if PROTOCOL_VERSION > 760 /* > 1.19.2 */
10 tail = 0;
11 offset = 0;
12#endif
13 }
14
19
20#if PROTOCOL_VERSION < 761 /* < 1.19.3 */
21 const std::vector<unsigned char>& LastSeenMessagesTracker::GetLastSignature() const
22 {
23 return last_signature_sent;
24 }
25
26 void LastSeenMessagesTracker::SetLastSignature(const std::vector<unsigned char>& last_signature_sent_)
27 {
28 last_signature_sent = last_signature_sent_;
29 }
30
31 void LastSeenMessagesTracker::AddSeenMessage(const std::vector<unsigned char>& signature, const ProtocolCraft::UUID& sender)
32 {
34 entry.SetLastSignature(signature);
35 entry.SetProfileId(sender);
36
37 // Remove old messages from the same player
38 int index = 0;
39 while (index < last_seen.size())
40 {
41 if (last_seen[index].GetProfileId() == sender)
42 {
43 last_seen.erase(last_seen.begin() + index);
44 }
45 else
46 {
47 index += 1;
48 }
49 }
50
51 // Push new header in front
52 last_seen.push_front(entry);
53
54 // Limit the size to 5 messages
55 while (last_seen.size() > 5)
56 {
57 last_seen.pop_back();
58 }
59 }
60
62 {
63 ProtocolCraft::LastSeenMessagesUpdate last_seen_messages_update;
64 last_seen_messages_update.SetLastSeen({ last_seen.begin(), last_seen.begin() + std::min(static_cast<int>(last_seen.size()), 5) });
65 return last_seen_messages_update;
66 }
67#else
69 {
70 std::pair<std::vector<std::vector<unsigned char>>, ProtocolCraft::LastSeenMessagesUpdate> output;
71 output.second.SetOffset(GetAndResetOffset());
72
73 output.first.reserve(last_seen_signatures.size());
74 std::bitset<20> bitset;
75 for (size_t i = 0; i < last_seen_signatures.size(); ++i)
76 {
77 const size_t index = (tail + i) % last_seen_signatures.size();
78 if (last_seen_signatures[index].empty())
79 {
80 continue;
81 }
82 bitset.set(i, true);
83 output.first.push_back(last_seen_signatures[index]);
84 }
85 output.second.SetAcknowledged(bitset);
86
87#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
88 // Compute the checksum for last seen messages for 1.21.5+
89 int checksum = 1;
90
91 for (const auto& signature : output.first)
92 {
93 int signature_hashcode = 1;
94 for (const unsigned char c : signature)
95 {
96 signature_hashcode = 31 * signature_hashcode + static_cast<int>(c);
97 }
98 checksum = 31 * checksum + signature_hashcode;
99 }
100
101 const unsigned char byte_checksum = static_cast<unsigned char>(checksum);
102 output.second.SetChecksum(byte_checksum == 0 ? 1 : byte_checksum);
103#endif
104
105 return output;
106 }
107
108 void LastSeenMessagesTracker::AddSeenMessage(const std::vector<unsigned char>& signature)
109 {
110 last_seen_signatures[tail] = signature;
111 tail = (tail + 1) % last_seen_signatures.size();
112 offset += 1;
113 }
114
116 {
117 return offset;
118 }
119
121 {
122 const int current_offset = offset;
123 offset = 0;
124 return current_offset;
125 }
126#endif
127}
128#endif
std::array< std::vector< unsigned char >, 20 > last_seen_signatures
Circular buffer to store previously seen message signatures.
void AddSeenMessage(const std::vector< unsigned char > &signature)
std::pair< std::vector< std::vector< unsigned char > >, ProtocolCraft::LastSeenMessagesUpdate > GetLastSeenMessagesUpdate()
Get both a vector of previous messages signatures and the LastSeenMessagesUpdate object.
std::array< unsigned char, 16 > UUID