1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#ifndef ENGINE_SERVER_H
4#define ENGINE_SERVER_H
5
6#include "kernel.h"
7#include "message.h"
8
9#include <base/dbg.h>
10#include <base/hash.h>
11#include <base/math.h>
12#include <base/mem.h>
13#include <base/str.h>
14
15#include <engine/shared/jsonwriter.h>
16#include <engine/shared/protocol.h>
17
18#include <generated/protocol.h>
19#include <generated/protocol7.h>
20#include <generated/protocolglue.h>
21
22#include <array>
23#include <optional>
24#include <type_traits>
25
26struct CAntibotRoundData;
27class IMap;
28
29// When recording a demo on the server, the ClientId -1 is used
30enum
31{
32 SERVER_DEMO_CLIENT = -1
33};
34
35class IServer : public IInterface
36{
37 MACRO_INTERFACE("server")
38protected:
39 int m_CurrentGameTick;
40
41public:
42 /*
43 Structure: CClientInfo
44 */
45 struct CClientInfo
46 {
47 const char *m_pName;
48 int m_Latency;
49 bool m_GotDDNetVersion;
50 int m_DDNetVersion;
51 const char *m_pDDNetVersionStr;
52 const CUuid *m_pConnectionId;
53 };
54
55 int Tick() const { return m_CurrentGameTick; }
56 int TickSpeed() const { return SERVER_TICK_SPEED; }
57
58 virtual int Port() const = 0;
59 virtual int MaxClients() const = 0;
60 virtual int ClientCount() const = 0;
61 virtual int DistinctClientCount() const = 0;
62 virtual const char *ClientName(int ClientId) const = 0;
63 virtual const char *ClientClan(int ClientId) const = 0;
64 virtual int ClientCountry(int ClientId) const = 0;
65 virtual bool ClientSlotEmpty(int ClientId) const = 0;
66 virtual bool ClientIngame(int ClientId) const = 0;
67 virtual bool GetClientInfo(int ClientId, CClientInfo *pInfo) const = 0;
68 virtual void SetClientDDNetVersion(int ClientId, int DDNetVersion) = 0;
69 virtual const NETADDR *ClientAddr(int ClientId) const = 0;
70 virtual const std::array<char, NETADDR_MAXSTRSIZE> &ClientAddrStringImpl(int ClientId, bool IncludePort) const = 0;
71 const char *ClientAddrString(int ClientId, bool IncludePort) const { return ClientAddrStringImpl(ClientId, IncludePort).data(); }
72
73 /**
74 * Returns the version of the client with the given client ID.
75 *
76 * @param ClientId the client Id, which must be between 0 and
77 * MAX_CLIENTS - 1, or equal to SERVER_DEMO_CLIENT for server demos.
78 *
79 * @return The version of the client with the given client ID.
80 * For server demos this is always the latest client version.
81 * On errors, VERSION_NONE is returned.
82 */
83 virtual int GetClientVersion(int ClientId) const = 0;
84 virtual int SendMsg(CMsgPacker *pMsg, int Flags, int ClientId) = 0;
85
86 template<class T>
87 requires(!protocol7::is_sixup<T>::value)
88 int SendPackMsg(const T *pMsg, int Flags, int ClientId)
89 {
90 int Result = 0;
91 if(ClientId == -1)
92 {
93 for(int i = 0; i < MaxClients(); i++)
94 if(ClientIngame(ClientId: i))
95 Result = SendPackMsgTranslate(pMsg, Flags, i);
96 }
97 else
98 {
99 Result = SendPackMsgTranslate(pMsg, Flags, ClientId);
100 }
101 return Result;
102 }
103
104 template<class T>
105 requires(protocol7::is_sixup<T>::value)
106 int SendPackMsg(const T *pMsg, int Flags, int ClientId)
107 {
108 int Result = 0;
109 if(ClientId == -1)
110 {
111 for(int i = 0; i < MaxClients(); i++)
112 if(ClientIngame(ClientId: i) && IsSixup(ClientId: i))
113 Result = SendPackMsgOne(pMsg, Flags, i);
114 }
115 else if(IsSixup(ClientId))
116 Result = SendPackMsgOne(pMsg, Flags, ClientId);
117
118 return Result;
119 }
120
121 template<class T>
122 int SendPackMsgTranslate(const T *pMsg, int Flags, int ClientId)
123 {
124 return SendPackMsgOne(pMsg, Flags, ClientId);
125 }
126
127 int SendPackMsgTranslate(const CNetMsg_Sv_Emoticon *pMsg, int Flags, int ClientId)
128 {
129 CNetMsg_Sv_Emoticon MsgCopy;
130 mem_copy(dest: &MsgCopy, source: pMsg, size: sizeof(MsgCopy));
131 return Translate(Target&: MsgCopy.m_ClientId, Client: ClientId) && SendPackMsgOne(pMsg: &MsgCopy, Flags, ClientId);
132 }
133
134 int SendPackMsgTranslate(const CNetMsg_Sv_Chat *pMsg, int Flags, int ClientId)
135 {
136 CNetMsg_Sv_Chat MsgCopy;
137 mem_copy(dest: &MsgCopy, source: pMsg, size: sizeof(MsgCopy));
138
139 char aBuf[1000];
140 if(MsgCopy.m_ClientId >= 0 && !Translate(Target&: MsgCopy.m_ClientId, Client: ClientId))
141 {
142 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s: %s", ClientName(ClientId: MsgCopy.m_ClientId), MsgCopy.m_pMessage);
143 MsgCopy.m_pMessage = aBuf;
144 MsgCopy.m_ClientId = VANILLA_MAX_CLIENTS - 1;
145 }
146
147 if(IsSixup(ClientId))
148 {
149 protocol7::CNetMsg_Sv_Chat Msg7;
150 Msg7.m_ClientId = MsgCopy.m_ClientId;
151 Msg7.m_pMessage = MsgCopy.m_pMessage;
152 Msg7.m_Mode = MsgCopy.m_Team > 0 ? protocol7::CHAT_TEAM : protocol7::CHAT_ALL;
153 Msg7.m_TargetId = -1;
154 return SendPackMsgOne(pMsg: &Msg7, Flags, ClientId);
155 }
156
157 return SendPackMsgOne(pMsg: &MsgCopy, Flags, ClientId);
158 }
159
160 int SendPackMsgTranslate(const CNetMsg_Sv_KillMsg *pMsg, int Flags, int ClientId)
161 {
162 CNetMsg_Sv_KillMsg MsgCopy;
163 mem_copy(dest: &MsgCopy, source: pMsg, size: sizeof(MsgCopy));
164 if(!Translate(Target&: MsgCopy.m_Victim, Client: ClientId))
165 return 0;
166 if(!Translate(Target&: MsgCopy.m_Killer, Client: ClientId))
167 MsgCopy.m_Killer = MsgCopy.m_Victim;
168 return SendPackMsgOne(pMsg: &MsgCopy, Flags, ClientId);
169 }
170
171 int SendPackMsgTranslate(const CNetMsg_Sv_RaceFinish *pMsg, int Flags, int ClientId)
172 {
173 if(IsSixup(ClientId))
174 {
175 protocol7::CNetMsg_Sv_RaceFinish Msg7;
176 Msg7.m_ClientId = pMsg->m_ClientId;
177 Msg7.m_Diff = pMsg->m_Diff;
178 Msg7.m_Time = pMsg->m_Time;
179 Msg7.m_RecordPersonal = pMsg->m_RecordPersonal;
180 Msg7.m_RecordServer = pMsg->m_RecordServer;
181 return SendPackMsgOne(pMsg: &Msg7, Flags, ClientId);
182 }
183 return SendPackMsgOne(pMsg, Flags, ClientId);
184 }
185
186 template<class T>
187 int SendPackMsgOne(const T *pMsg, int Flags, int ClientId)
188 {
189 dbg_assert(ClientId != -1, "SendPackMsgOne called with -1");
190 CMsgPacker Packer(T::ms_MsgId, false, protocol7::is_sixup<T>::value);
191
192 if(pMsg->Pack(&Packer))
193 return -1;
194 return SendMsg(pMsg: &Packer, Flags, ClientId);
195 }
196
197 bool Translate(int &Target, int Client)
198 {
199 if(IsSixup(ClientId: Client))
200 return true;
201 if(GetClientVersion(ClientId: Client) >= VERSION_DDNET_OLD)
202 return true;
203 int *pMap = GetIdMap(ClientId: Client);
204 bool Found = false;
205 for(int i = 0; i < VANILLA_MAX_CLIENTS; i++)
206 {
207 if(Target == pMap[i])
208 {
209 Target = i;
210 Found = true;
211 break;
212 }
213 }
214 return Found;
215 }
216
217 bool ReverseTranslate(int &Target, int Client)
218 {
219 if(IsSixup(ClientId: Client))
220 return true;
221 if(GetClientVersion(ClientId: Client) >= VERSION_DDNET_OLD)
222 return true;
223 Target = std::clamp(val: Target, lo: 0, hi: VANILLA_MAX_CLIENTS - 1);
224 int *pMap = GetIdMap(ClientId: Client);
225 if(pMap[Target] == -1)
226 return false;
227 Target = pMap[Target];
228 return true;
229 }
230
231 virtual bool WouldClientNameChange(int ClientId, const char *pNameRequest) = 0;
232 virtual bool WouldClientClanChange(int ClientId, const char *pClanRequest) = 0;
233 virtual void SetClientName(int ClientId, const char *pName) = 0;
234 virtual void SetClientClan(int ClientId, const char *pClan) = 0;
235 virtual void SetClientCountry(int ClientId, int Country) = 0;
236 virtual void SetClientScore(int ClientId, std::optional<int> Score) = 0;
237 virtual void SetClientFlags(int ClientId, int Flags) = 0;
238
239 virtual int SnapNewId() = 0;
240 virtual void SnapFreeId(int Id) = 0;
241 virtual bool SnapNewItem(int Type, int Id, const void *pData, int Size) = 0;
242
243 template<typename T>
244 bool SnapNewItem(int Id, const T &Data)
245 {
246 const int Type = protocol7::is_sixup<T>::value ? -T::ms_MsgId : T::ms_MsgId;
247 return SnapNewItem(Type, Id, &Data, sizeof(Data));
248 }
249
250 virtual void SnapSetStaticsize(int ItemType, int Size) = 0;
251 virtual void SnapSetStaticsize7(int ItemType, int Size) = 0;
252
253 enum
254 {
255 RCON_CID_SERV = -1,
256 RCON_CID_VOTE = -2,
257 };
258 virtual void SetRconCid(int ClientId) = 0;
259 virtual int GetAuthedState(int ClientId) const = 0;
260 virtual bool IsRconAuthed(int ClientId) const = 0;
261 virtual bool IsRconAuthedAdmin(int ClientId) const = 0;
262 virtual const char *GetAuthName(int ClientId) const = 0;
263 virtual bool HasAuthHidden(int ClientId) const = 0;
264 virtual void Kick(int ClientId, const char *pReason) = 0;
265 virtual void Ban(int ClientId, int Seconds, const char *pReason, bool VerbatimReason) = 0;
266 virtual void RedirectClient(int ClientId, int Port) = 0;
267 virtual void ChangeMap(const char *pMap) = 0;
268 virtual void ReloadMap() = 0;
269
270 virtual void DemoRecorder_HandleAutoStart() = 0;
271
272 // DDRace
273
274 virtual void SaveDemo(int ClientId, float Time) = 0;
275 virtual void StartRecord(int ClientId) = 0;
276 virtual void StopRecord(int ClientId) = 0;
277 virtual bool IsRecording(int ClientId) = 0;
278 virtual void StopDemos() = 0;
279
280 virtual int *GetIdMap(int ClientId) = 0;
281
282 virtual bool DnsblWhite(int ClientId) = 0;
283 virtual bool DnsblPending(int ClientId) = 0;
284 virtual bool DnsblBlack(int ClientId) = 0;
285 virtual const char *GetAnnouncementLine() = 0;
286 virtual bool ClientPrevIngame(int ClientId) = 0;
287 virtual const char *GetNetErrorString(int ClientId) = 0;
288 virtual void ResetNetErrorString(int ClientId) = 0;
289 virtual bool SetTimedOut(int ClientId, int OrigId) = 0;
290 virtual void SetTimeoutProtected(int ClientId) = 0;
291
292 virtual void SetErrorShutdown(const char *pReason) = 0;
293 virtual void ExpireServerInfo() = 0;
294
295 virtual void FillAntibot(CAntibotRoundData *pData) = 0;
296
297 virtual void SendMsgRaw(int ClientId, const void *pData, int Size, int Flags) = 0;
298
299 virtual bool IsSixup(int ClientId) const = 0;
300};
301
302class IGameServer : public IInterface
303{
304 MACRO_INTERFACE("gameserver")
305protected:
306public:
307 // `pPersistentData` may be null if this is the first time `IGameServer`
308 // is instantiated.
309 virtual void OnInit(const void *pPersistentData) = 0;
310 virtual void OnConsoleInit() = 0;
311 // Returns `true` if map change accepted.
312 [[nodiscard]] virtual bool OnMapChange(char *pNewMapName, int MapNameSize) = 0;
313 // `pPersistentData` may be null if this is the last time `IGameServer`
314 // is destroyed.
315 virtual void OnShutdown(void *pPersistentData) = 0;
316
317 virtual void OnTick() = 0;
318
319 // Snap for a specific client.
320 //
321 // GlobalSnap is true when sending snapshots to all clients,
322 // otherwise only forced high bandwidth clients would receive snap.
323 // RecordingDemo is true when this snapshot will be recorded to a demo.
324 virtual void OnSnap(int ClientId, bool GlobalSnap, bool RecordingDemo) = 0;
325
326 // Called after sending snapshots to all clients.
327 //
328 // Note if any client has force high bandwidth enabled,
329 // this will not be called when only sending snapshots to these clients.
330 virtual void OnPostGlobalSnap() = 0;
331
332 virtual void OnMessage(int MsgId, CUnpacker *pUnpacker, int ClientId) = 0;
333
334 // Called before map reload, for any data that the game wants to
335 // persist to the next map.
336 //
337 // Has the size of the return value of `PersistentClientDataSize()`.
338 //
339 // Returns whether the game should be supplied with the data when the
340 // client connects for the next map.
341 virtual bool OnClientDataPersist(int ClientId, void *pData) = 0;
342
343 // Called when a client connects.
344 //
345 // If it is reconnecting to the game after a map change, the
346 // `pPersistentData` point is nonnull and contains the data the game
347 // previously stored.
348 virtual void OnClientConnected(int ClientId, void *pPersistentData) = 0;
349
350 virtual void OnClientEnter(int ClientId) = 0;
351 virtual void OnClientDrop(int ClientId, const char *pReason) = 0;
352 virtual void OnClientPrepareInput(int ClientId, void *pInput) = 0;
353 virtual void OnClientDirectInput(int ClientId, const void *pInput) = 0;
354 virtual void OnClientPredictedInput(int ClientId, const void *pInput) = 0;
355 virtual void OnClientPredictedEarlyInput(int ClientId, const void *pInput) = 0;
356
357 virtual void PreInputClients(int ClientId, bool *pClients) = 0;
358
359 virtual bool IsClientReady(int ClientId) const = 0;
360 virtual bool IsClientPlayer(int ClientId) const = 0;
361 virtual bool IsClientHighBandwidth(int ClientId) const = 0;
362
363 virtual int PersistentDataSize() const = 0;
364 virtual int PersistentClientDataSize() const = 0;
365
366 virtual CUuid GameUuid() const = 0;
367 virtual const char *GameType() const = 0;
368 virtual const char *Version() const = 0;
369 virtual const char *NetVersion() const = 0;
370
371 virtual IMap *Map() = 0;
372 virtual const IMap *Map() const = 0;
373 virtual CNetObjHandler *GetNetObjHandler() = 0;
374 virtual protocol7::CNetObjHandler *GetNetObjHandler7() = 0;
375
376 // DDRace
377
378 virtual void OnPreTickTeehistorian() = 0;
379
380 virtual void OnSetAuthed(int ClientId, int Level) = 0;
381 virtual bool PlayerExists(int ClientId) const = 0;
382
383 virtual void TeehistorianRecordAntibot(const void *pData, int DataSize) = 0;
384 virtual void TeehistorianRecordPlayerJoin(int ClientId, bool Sixup) = 0;
385 virtual void TeehistorianRecordPlayerDrop(int ClientId, const char *pReason) = 0;
386 virtual void TeehistorianRecordPlayerRejoin(int ClientId) = 0;
387 virtual void TeehistorianRecordPlayerName(int ClientId, const char *pName) = 0;
388 virtual void TeehistorianRecordPlayerFinish(int ClientId, int TimeTicks) = 0;
389 virtual void TeehistorianRecordTeamFinish(int TeamId, int TimeTicks) = 0;
390 virtual void TeehistorianRecordAuthLogin(int ClientId, int Level, const char *pAuthName) = 0;
391
392 virtual void FillAntibot(CAntibotRoundData *pData) = 0;
393
394 /**
395 * Used to report custom player info to the master server.
396 *
397 * @param pJsonWriter A pointer to a @link CJsonWriter @endlink to which the custom data will written.
398 * @param ClientId The client ID.
399 */
400 virtual void OnUpdatePlayerServerInfo(CJsonWriter *pJsonWriter, int ClientId) = 0;
401};
402
403extern IGameServer *CreateGameServer();
404#endif
405