-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmodels.h
More file actions
344 lines (311 loc) · 8.91 KB
/
models.h
File metadata and controls
344 lines (311 loc) · 8.91 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#pragma once
#include "common.h"
#include <dcserver/shared_this.hpp>
#include <string>
#include <memory>
#include <vector>
#include <cstring>
#include <array>
#include <sstream>
#include <cassert>
#include <algorithm>
enum SRVOpcode : uint16_t
{
S_PONG = 0x00,
S_TEAM_NAME_EXISTS = 0x03,
S_LOBBY_FULL = 0x05,
S_SEARCH_RESULT = 0x07,
S_MOTD = 0x0A,
S_LOGIN_OK = 0x11,
S_JOIN_LOBBY_ACK = 0x13,
S_DISCONNECTED = 0x16,
S_DO_DISCONNECT = 0x17,
S_LOBBY_LIST_ITEM = 0x18,
S_LOBBY_LIST_END = 0x19,
S_GAME_LIST_ITEM = 0x1B,
S_GAME_LIST_END = 0x1C,
S_GAME_SEL_ACK = 0x1D,
S_RECONNECT_ACK = 0x1F,
S_LICENSE = 0x22,
S_NEW_TEAM = 0x28,
S_TEAM_JOINED = 0x29,
S_LOBBY_CREATED = 0x2A,
S_LOBBY_LEFT = 0x2C,
S_LOBBY_CHAT = 0x2D,
S_LOBBY_DM = 0x2E,
S_PLAYER_LIST_ITEM = 0x30,
S_PLAYER_LIST_END = 0x31,
S_TEAM_LIST_ITEM = 0x32,
S_TEAM_LIST_END = 0x33,
S_TEAM_SHARED_MEM = 0x34,
S_TEAM_DELETED = 0x3A,
S_TEAM_LEFT = 0x3B,
S_GAME_SERVER = 0x3D,
S_LAUNCH_ACK = 0x3E,
S_LOBBY_SHARED_MEM = 0x41,
S_PLAYER_SHARED_MEM = 0x42,
S_TEAM_CHAT = 0x43,
S_CTCP_MSG = 0x44,
S_EXTUSER_MEM_ACK = 0x4F,
S_EXTUSER_MEM_START = 0x50,
S_EXTUSER_MEM_CHUNK = 0x51,
S_EXTUSER_MEM_END = 0x52,
S_LEAVE_LOBBY_ACK = 0xCB,
S_SENDLOG_ACK = 0xCF,
S_LOBBY_PLAYER_LIST_END = 0xD9,
S_LOBBY_PLAYER_LIST_ITEM = 0xDA,
S_EXT_MEM_READY = 0xE1,
S_MULTI_DATA_START = 0xE5,
S_MULTI_DATA_ITEM = 0xE6,
S_MULTI_DATA_END = 0xE7,
// E8, E9, EA multi data errors?
};
using sstream = std::stringstream;
class Player;
class Team;
class LobbyServer;
class LobbyConnection;
class Packet
{
public:
static std::vector<uint8_t> createSharedMemPacket(const std::vector<uint8_t>& sharedMemBytes, const std::string& stringData)
{
std::vector<uint8_t> data;
data.resize(1 + stringData.length() + sharedMemBytes.size());
data[0] = stringData.length();
memcpy(&data[1], &stringData[0], stringData.length());
memcpy(&data[1 + stringData.length()], &sharedMemBytes[0], sharedMemBytes.size());
return data;
}
};
class Lobby : public SharedThis<Lobby>
{
public:
void addPlayer(std::shared_ptr<Player> player);
void removePlayer(std::shared_ptr<Player> player);
void sendChat(const std::string& from, const std::string& message);
std::shared_ptr<Team> createTeam(std::shared_ptr<Player> creator, const std::string& name, unsigned capacity, const std::string& type);
void deleteTeam(std::shared_ptr<Team> team);
std::shared_ptr<Team> getTeam(const std::string& name);
void setSharedMem(const std::string& data);
std::string getSjisName() const;
void sendSharedMemPlayer(std::shared_ptr<Player> owner, const std::vector<uint8_t>& data);
std::string name;
unsigned flags = 0;
bool hasSharedMem = false;
std::string sharedMem;
const std::string gameName;
unsigned capacity;
std::vector<std::shared_ptr<Player>> members;
std::vector<std::shared_ptr<Team>> teams;
private:
Lobby(LobbyServer& parent, const std::string& gameName, const std::string& name, unsigned capacity, bool permanent)
: name(name), gameName(gameName), capacity(capacity), permanent(permanent), parent(parent) {}
bool permanent;
LobbyServer& parent;
friend super;
};
class Player : public SharedThis<Player>
{
public:
void login(const std::string& name);
std::string getIp();
std::array<uint8_t, 4> getIpBytes();
int getPort() const { return port; };
void disconnect(bool sendDCPacket = true);
void setSharedMem(const std::vector<uint8_t>& data);
std::vector<uint8_t> getSendDataPacket();
void joinLobby(Lobby::Ptr lobby)
{
if (lobby != nullptr) {
INFO_LOG(gameId, "%s joined lobby %s", name.c_str(), lobby->name.c_str());
this->lobby = lobby;
lobby->addPlayer(shared_from_this());
}
else {
// TODO Some Error
}
}
void leaveLobby()
{
if (lobby != nullptr)
{
INFO_LOG(gameId, "%s left lobby %s", name.c_str(), lobby->name.c_str());
lobby->removePlayer(shared_from_this());
lobby = nullptr;
}
else {
// TODO Some Error
}
}
void createTeam(const std::string& name, unsigned capacity, const std::string& type);
void joinTeam(const std::string& name, bool spectator);
void leaveTeam();
void getExtraMem(const std::string& playerName, int offset, int length);
void sendExtraMem();
void startExtraMem(int offset, int length);
void setExtraMem(int index, const uint8_t *data, int size);
void endExtraMem();
int send(uint16_t opcode, const std::string& payload = {}) {
//printf("send: %04x [%s]\n", opcode, payload.c_str());
return send(opcode, (const uint8_t *)&payload[0], payload.length());
}
int send(uint16_t opcode, const std::vector<uint8_t>& payload) {
return send(opcode, &payload[0], payload.size());
}
void receive(uint16_t opcode, const std::vector<uint8_t> payload);
std::string toUtf8(const std::string& str) const;
std::string fromUtf8(const std::string& str) const;
std::string name;
unsigned flags = 0;
std::vector<uint8_t> sharedMem;
Lobby::Ptr lobby;
std::shared_ptr<Team> team;
bool spectator = false;
GameId gameId;
LobbyServer& server;
private:
Player(std::shared_ptr<LobbyConnection> connection, LobbyServer& server);
int send(uint16_t opcode, const uint8_t *payload, unsigned length);
std::vector<uint8_t> makePacket(uint16_t opcode, const uint8_t *payload, unsigned length);
bool disconnected = false;
std::shared_ptr<LobbyConnection> connection;
std::vector<uint8_t> lastRecvPacket;
std::vector<uint8_t> extraUserMem;
int extraMemOffset = 0;
int extraMemEnd = 0;
int extraMemChunkNum = 0;
Player::Ptr extraMemPlayer;
std::string ipAddress;
std::array<uint8_t, 4> ipBytes;
int port = 0;
friend super;
};
class Team : public SharedThis<Team>
{
public:
void setSharedMem(std::string memAsStr)
{
sharedMem = memAsStr;
for (auto& player : members)
player->send(S_TEAM_SHARED_MEM, player->fromUtf8(name) + " " + sharedMem);
}
bool addPlayer(Player::Ptr player, bool spectator);
bool removePlayer(Player::Ptr player);
void sendChat(const std::string& from, const std::string& message)
{
INFO_LOG(host->gameId, "%s team chat: %s", from.c_str(), message.c_str());
for (auto& player : members)
player->send(S_TEAM_CHAT, player->fromUtf8(from + " " + message));
}
void sendGameServer(Player::Ptr p);
void launchGame(Player::Ptr p)
{
sstream ss;
ss << members.size();
for (auto& player : members)
ss << ' ' << (host == player ? "*" : "") << player->fromUtf8(player->name) << ' ' << player->getIp();
p->send(S_LAUNCH_ACK, ss.str());
}
std::string name;
unsigned capacity;
std::shared_ptr<Player> host;
std::string sharedMem;
std::vector<std::shared_ptr<Player>> members;
unsigned flags = 0;
private:
Team(Lobby::Ptr parent, std::string name, unsigned capacity, std::shared_ptr<Player> host)
: name(name), capacity(capacity), host(host), parent(parent) {
members.push_back(host);
}
std::shared_ptr<Lobby> parent;
friend super;
};
class LobbyServer
{
public:
LobbyServer(GameId gameId, const std::string& name = {});
Lobby::Ptr createLobby(const std::string& name, unsigned capacity, bool permanent = true)
{
if (capacity <= 1)
capacity = 100;
if (getLobby(name) == nullptr)
{
Lobby::Ptr lobby = Lobby::create(*this, getGameName(), name, capacity, permanent);
lobbies.push_back(lobby);
return lobby;
}
return nullptr;
}
void deleteLobby(std::string name)
{
auto it = std::find_if(lobbies.begin(), lobbies.end(), [&name](const Lobby::Ptr& lobby) {
return lobby->name == name;
});
if (it != lobbies.end())
lobbies.erase(it);
}
Lobby::Ptr getLobby(const std::string& name)
{
for (auto& lobby : lobbies)
if (lobby->name == name)
return lobby;
return nullptr;
}
const std::vector<Lobby::Ptr>& getLobbyList() {
return lobbies;
}
Player::Ptr getPlayer(const std::string& name, Player::Ptr except = {})
{
for (auto& player : players)
if (player != except && player->name == name)
return player;
return nullptr;
}
Player::Ptr IsIPUnique(Player::Ptr me)
{
std::string myIp = me->getIp();
for (auto& player : players)
if (player->getIp() == myIp && player != me)
return player;
return nullptr;
}
void addPlayer(Player::Ptr player) {
players.push_back(player);
}
void removePlayer(Player::Ptr player)
{
auto it = std::find(players.begin(), players.end(), player);
if (it != players.end())
players.erase(it);
}
const std::string& getName() const {
return name;
}
GameId getGameId() const {
return gameId;
}
uint16_t getIpPort() const;
std::string getGameName() const;
const std::string& getMotd() const {
return motd;
}
void setMotd(const std::string& motd) {
this->motd = motd;
}
static LobbyServer *getServer(GameId gameId)
{
for (LobbyServer *server : servers)
if (server->getGameId() == gameId
|| (server->getGameId() == GameId::Daytona && gameId == GameId::DaytonaJP))
return server;
return nullptr;
}
private:
GameId gameId;
std::string name = "IWANGO_Server_1";
std::string motd = "Welcome to IWANGO Emulator by Ioncannon";
std::vector<Player::Ptr> players;
std::vector<Lobby::Ptr> lobbies;
static std::vector<LobbyServer *> servers;
};