-
-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathquery.hpp
More file actions
208 lines (175 loc) · 4.7 KB
/
query.hpp
File metadata and controls
208 lines (175 loc) · 4.7 KB
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
/*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/.
*
* The original code is copyright (c) 2022, open.mp team and contributors.
*/
#pragma once
#include "Server/Components/Console/console.hpp"
#include "sdk.hpp"
#include <map>
#include <sys/socket.h>
using namespace Impl;
constexpr size_t BASE_QUERY_SIZE = 11;
constexpr size_t QUERY_TYPE_INDEX = 10;
constexpr size_t QUERY_COPY_TO = 10;
constexpr size_t BASE_QUERY6_SIZE = 24;
constexpr size_t QUERY6_TYPE_INDEX = 23;
constexpr size_t QUERY6_COPY_TO = 23;
class Query : NoCopy
{
public:
void setCore(ICore* core)
{
this->core = core;
}
void setConsole(IConsoleComponent* console)
{
this->console = console;
}
IConsoleComponent* getConsole() const
{
return console;
}
Span<const char> handleQuery(Span<const char> buffer, uint32_t sock, const sockaddr_storage& client, int tolen);
void handleRCON(Span<const char> buffer, uint32_t sock, const sockaddr_storage& client, int tolen);
void buildRulesBuffer();
void buildPlayerDependentBuffers(IPlayer* except = nullptr)
{
buildPlayerInfoBuffer(except);
updateServerInfoBufferPlayerCount(except);
}
void buildConfigDependentBuffers()
{
buildServerInfoBuffer();
buildRulesBuffer();
buildExtraServerInfoBuffer();
}
void setMaxPlayers(uint16_t value)
{
maxPlayers = value;
}
void setPassworded(bool value)
{
passworded = value;
}
void setRconPassword(StringView value)
{
rconPassword = String(value);
}
void setRconEnabled(bool value)
{
rconEnabled = value;
}
void setLogQueries(bool value)
{
logQueries = value;
}
/// @param custom Whether the parameter is custom or set automatically by the server; if a user is setting the rule value it should be true
template <bool custom, typename... Args>
void setRuleValue(Args... args)
{
std::initializer_list<std::string> ruleData = { args... };
int ruleCount = ruleData.size();
if (ruleCount % 2)
{
ruleCount--;
}
for (auto it = ruleData.begin(); it != ruleData.end(); ++it)
{
const std::string& ruleName = *it;
++it;
const std::string& ruleValue = *it;
auto res = rules.emplace(ruleName, std::make_pair(ruleValue, custom));
if (!res.second)
{
auto& key = res.first->first;
auto& val = res.first->second;
// If we already have a custom rule set, skip setting the non-custom one (allows for overriding rules)
if (!custom && val.second)
{
continue;
}
rulesLength -= sizeof(uint8_t) + key.size();
rulesLength -= sizeof(uint8_t) + val.first.size();
val = std::make_pair(ruleValue, custom);
}
rulesLength += sizeof(uint8_t) + ruleName.size();
rulesLength += sizeof(uint8_t) + ruleValue.size();
}
}
void removeRule(StringView ruleName)
{
auto it = rules.find(String(ruleName));
if (it != rules.end())
{
rulesLength -= sizeof(uint8_t) + it->first.size();
rulesLength -= sizeof(uint8_t) + it->second.first.size();
rules.erase(String(ruleName));
}
}
bool isValidRule(StringView ruleName)
{
return rules.find(String(ruleName)) != rules.end();
}
void setServerName(StringView value)
{
serverName = String(value);
}
void setGameModeName(StringView value)
{
gameModeName = String(value);
}
void setLanguage(StringView value)
{
language = String(value);
}
void setDiscordLink(StringView value)
{
discordLink = String(value);
}
void setLightBannerUrl(StringView value)
{
lightBannerUrl = String(value);
}
void setDarkBannerUrl(StringView value)
{
darkBannerUrl = String(value);
}
void setLogoUrl(StringView value)
{
logoUrl = String(value);
}
private:
ICore* core = nullptr;
IConsoleComponent* console = nullptr;
uint16_t maxPlayers = 0;
String serverName = "open.mp server";
String gameModeName = "Unknown";
String language = "EN";
String rconPassword;
String discordLink = "";
String lightBannerUrl = "";
String darkBannerUrl = "";
String logoUrl = "";
bool passworded = false;
bool logQueries = false;
bool rconEnabled = false;
std::unique_ptr<char[]> playerListBuffer;
size_t playerListBufferLength = 0;
std::unique_ptr<char[]> serverInfoBuffer;
size_t serverInfoBufferLength = 0;
std::map<String, Pair<String, bool>> rules;
size_t rulesLength = 0;
std::unique_ptr<char[]> rulesBuffer;
size_t rulesBufferLength = 0;
std::unique_ptr<char[]> extraInfoBuffer;
size_t extraInfoBufferLength = 0;
std::unique_ptr<char[]> responseBuffer;
size_t responseBufferLength = 0;
void buildPlayerInfoBuffer(IPlayer* except = nullptr);
void updateServerInfoBufferPlayerCount(IPlayer* except = nullptr);
void buildServerInfoBuffer();
void buildExtraServerInfoBuffer();
};