-
Notifications
You must be signed in to change notification settings - Fork 12
/
qtAuthClient.cpp
260 lines (226 loc) · 8.6 KB
/
qtAuthClient.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
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
#include "MoulKI.h"
#include "qtAuthClient.h"
qtAuthClient::qtAuthClient(MoulKI* ki) : QObject(ki),
pnAuthClient(ki->getResManager()), parent(ki), currentPlayerId(0) {
setKeys(ki->Keys.Auth.X, ki->Keys.Auth.N);
if(ki->Keys.Auth.G != 0)
setKeyG(ki->Keys.Auth.G);
setClientInfo(BUILD_NUMBER, 50, 1, s_moulUuid);
}
qtAuthClient::~qtAuthClient() {
}
void qtAuthClient::startLogin(QString user, QString pass) {
players.clear();
// apparently HSPlasma still doesn't lowercase the username
this->user = user.toLower().toUtf8().constData();
this->pass = pass.toUtf8().constData();
setStatus("Connecting...");
if(pnAuthClient::connect(parent->Host.toStdString().data()) != kNetSuccess) {
setStatus("Error Connecting To Server");
return;
}
sendClientRegisterRequest();
}
void qtAuthClient::setPlayer(uint32_t playerId) {
currentPlayerId = playerId;
sendAcctSetPlayerRequest(playerId);
}
void qtAuthClient::onClientRegisterReply(uint32_t serverChallenge) {
setStatus("Authenticating...");
sendAcctLoginRequest(serverChallenge, rand(), user, pass);
}
void qtAuthClient::onAcctPlayerInfo(uint32_t, uint32_t playerId, const ST::string& playerName, const ST::string& avatarModel, uint32_t) {
authPlayer player;
player.ID = playerId;
player.Name = playerName;
player.avatar = avatarModel;
players.append(player);
qWarning("Added player %s (%u)", playerName.c_str(), playerId);
}
void qtAuthClient::onAcctLoginReply(uint32_t, ENetError result,
const plUuid& acctUuid, uint32_t, uint32_t,
const uint32_t* encryptKey) {
if (result != kNetSuccess) {
setStatus(ST::format("Auth Failed ({})",
GetNetErrorString(result)));
return;
}
setStatus("Auth Successful");
this->acctUuid = acctUuid;
this->sendFileListRequest("SDL", "sdl");
emit gotEncKeys(encryptKey[0], encryptKey[1],
encryptKey[2], encryptKey[3]);
emit loginSuccessful();
}
void qtAuthClient::onAcctSetPlayerReply(uint32_t, ENetError) {
parent->vault.queueRoot(currentPlayerId);
sendVaultNodeFetch(currentPlayerId);
}
void qtAuthClient::onPublicAgeList(uint32_t, ENetError result, size_t count, const pnNetAgeInfo* ages) {
if(result != kNetSuccess) {
setStatus(ST::format("Get Public Ages Failed ({})",
GetNetErrorString(result)));
return;
}
setStatus(ST::format("Got {} Public Ages", count));
QList< QPair<QString, plUuid> > publicAges;
for(size_t i = 0; i < count; i++) {
publicAges.append(QPair<QString, plUuid>(ages[i].fAgeFilename.c_str(), ages[i].fAgeInstanceId));
}
emit gotPublicAges(publicAges);
}
void qtAuthClient::onFileListReply(uint32_t, ENetError,
size_t count, const pnAuthFileItem* files) {
pendingSdlFiles.clear();
pendingSdlFiles.reserve(count);
for (size_t i = 0; i < count; i++) {
pendingSdlFiles.append(files[i]);
}
currentPendingSdlFile = 0;
downloadNextSdlFile();
}
void qtAuthClient::onFileDownloadChunk(uint32_t transId, ENetError result,
uint32_t totalSize, uint32_t chunkOffset, size_t chunkSize,
const unsigned char* chunkData) {
if (result != kNetSuccess) {
setStatus(ST::format("File download failed ({})",
GetNetErrorString(result)));
return;
}
hsStream* S = sdlFiles[transId];
S->write(chunkSize, chunkData);
if (chunkOffset + chunkSize == totalSize) {
S->rewind();
qWarning("Successfully downloaded a %d byte file.", totalSize);
downloadNextSdlFile();
emit gotSDLFile(S);
}
}
void qtAuthClient::downloadNextSdlFile() {
if(currentPendingSdlFile == pendingSdlFiles.size()) {
qWarning("Done downloading SDL files.");
currentPendingSdlFile = 0;
pendingSdlFiles.clear();
}else{
qWarning("Downloading file %s (%d of %d, %d bytes)",
pendingSdlFiles[currentPendingSdlFile].fFilename.c_str(), currentPendingSdlFile+1,
pendingSdlFiles.size(), pendingSdlFiles[currentPendingSdlFile].fFileSize);
uint32_t fileTrans;
fileTrans = this->sendFileDownloadRequest(pendingSdlFiles[currentPendingSdlFile].fFilename);
sdlFiles.insert(fileTrans, new hsRAMStream(PlasmaVer::pvMoul));
currentPendingSdlFile++;
}
}
void qtAuthClient::onVaultNodeRefsFetched(uint32_t, ENetError, size_t count, const pnVaultNodeRef* refs) {
for(unsigned int i = 0; i < count; i++) {
setStatus(ST::format("Ref: {{{} -> {}} {}", refs[i].fParent, refs[i].fChild, refs[i].fOwner));
parent->vault.addRef(refs[i]);
if(!fetchQueue.contains(refs[i].fChild)) {
fetchQueue.append(refs[i].fChild);
sendVaultNodeFetch(refs[i].fChild);
}
if(!fetchQueue.contains(refs[i].fParent)) {
fetchQueue.append(refs[i].fParent);
sendVaultNodeFetch(refs[i].fParent);
}
}
}
void qtAuthClient::onVaultNodeFetched(uint32_t, ENetError, const pnVaultNode& node) {
setStatus(ST::format("Node: ({})", node.getNodeIdx()));
parent->vault.addNode(node);
}
void qtAuthClient::onVaultNodeChanged(uint32_t nodeId, const plUuid&) {
if(!fetchQueue.contains(nodeId))
fetchQueue.append(nodeId);
sendVaultNodeFetch(nodeId);
}
void qtAuthClient::onVaultNodeAdded(uint32_t parent, uint32_t child, uint32_t owner) {
pnVaultNodeRef ref;
ref.fParent = parent;
ref.fChild = child;
ref.fOwner = owner;
setStatus(ST::format("Ref: {{{} -> {}} {}", ref.fParent, ref.fChild, ref.fOwner));
this->parent->vault.addRef(ref);
if(!fetchQueue.contains(parent)) {
fetchQueue.append(parent);
sendVaultNodeFetch(parent);
sendVaultFetchNodeRefs(parent);
}
if(!fetchQueue.contains(child)) {
fetchQueue.append(child);
sendVaultNodeFetch(child);
sendVaultFetchNodeRefs(child);
}
}
void qtAuthClient::onVaultNodeRemoved(uint32_t parent, uint32_t child) {
setStatus(ST::format("UnRef: {{{} -> {}}", parent, child));
this->parent->vault.removeRef(parent, child);
}
void qtAuthClient::onVaultNodeCreated(uint32_t transId, ENetError result, uint32_t nodeId) {
if(result == kNetSuccess) {
foreach(queuedRef ref, refQueue) {
if(ref.fTransId == transId) {
sendVaultNodeAdd(ref.fParent, nodeId, 0);
refQueue.removeAll(ref);
break;
}
}
}else{
setStatus(ST::format("Create Node Failed ({})", GetNetErrorString(result)));
}
}
void qtAuthClient::queueRef(uint32_t transId, uint32_t parent) {
queuedRef ref;
ref.fTransId = transId;
ref.fParent = parent;
refQueue.append(ref);
}
bool qtAuthClient::queuedRef::operator ==(const qtAuthClient::queuedRef& ref) {
return fTransId == ref.fTransId;
}
void qtAuthClient::setStatus(ST::string msg) {
emit sigStatus(std::move(msg));
}
void qtAuthClient::onVaultNodeFindReply(uint32_t, ENetError result, size_t count, const uint32_t *nodes) {
if(result == kNetSuccess) {
setStatus(ST::format("Found {} Nodes", count));
// it's not safe to send an array allocated elsewhere via a queued signal
QList<uint32_t> nodeList;
for(uint32_t i = 0; i < count; i++) {
nodeList.append(nodes[i]);
}
emit foundNodes(nodeList);
}else{
setStatus(ST::format("Find Node Failure: ({})", GetNetErrorString(result)));
}
}
void qtAuthClient::onVaultAddNodeReply(uint32_t, ENetError result) {
if(result == kNetSuccess) {
setStatus("Add Node Successful");
}else{
setStatus(ST::format("Add Node Failure: ({})", GetNetErrorString(result)));
}
}
void qtAuthClient::onVaultSaveNodeReply(uint32_t transId, ENetError result) {
if(result == kNetSuccess) {
setStatus("Save Node Successful");
emit saveNodeSuccessful(transId);
}else{
setStatus(ST::format("Save Node Failure: ({})", GetNetErrorString(result)));
}
}
void qtAuthClient::onVaultRemoveNodeReply(uint32_t, ENetError result) {
if(result == kNetSuccess) {
setStatus("Remove Node Successful");
}else{
setStatus(ST::format("Remove Node Failure: ({})", GetNetErrorString(result)));
}
}
void qtAuthClient::onAgeReply(uint32_t, ENetError result, uint32_t mcpId, const plUuid &ageInstanceId, uint32_t ageVaultId, uint32_t gameServerAddress) {
if(result == kNetSuccess) {
setStatus("Age Request Successful");
emit gotAge(gameServerAddress, ageInstanceId, mcpId, ageVaultId);
}else{
setStatus(ST::format("Age Request Failed: ({})", GetNetErrorString(result)));
}
}