GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/charserverrecv.cpp Lines: 2 262 0.8 %
Date: 2021-03-17 Branches: 2 148 1.4 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
#include "net/eathena/charserverrecv.h"
25
26
#include "client.h"
27
#include "configuration.h"
28
#include "pincodemanager.h"
29
#include "settings.h"
30
31
#include "gui/windows/charcreatedialog.h"
32
#include "gui/windows/charselectdialog.h"
33
#include "gui/windows/okdialog.h"
34
35
#include "gui/widgets/createwidget.h"
36
37
#include "net/character.h"
38
#include "net/charserverhandler.h"
39
#include "net/playerhandler.h"
40
41
#include "net/ea/token.h"
42
43
#include "net/eathena/gamehandler.h"
44
#include "net/eathena/loginhandler.h"
45
#include "net/eathena/messageout.h"
46
#include "net/eathena/network.h"
47
#include "net/eathena/protocolout.h"
48
#include "net/eathena/sprite.h"
49
50
#include "resources/iteminfo.h"
51
52
#include "resources/db/itemdb.h"
53
54
#include "utils/dtor.h"
55
#include "utils/gettext.h"
56
57
#include "debug.h"
58
59
extern int packetVersion;
60
61
namespace EAthena
62
{
63
64
extern ServerInfo charServer;
65
extern ServerInfo mapServer;
66
67
namespace CharServerRecv
68
{
69
1
    std::string mNewName;
70
    BeingId mRenameId = BeingId_zero;
71
}  // namespace CharServerRecv
72
73
// callers must count each packet size by self
74
void CharServerRecv::readPlayerData(Net::MessageIn &msg,
75
                                    Net::Character *const character)
76
{
77
    if (character == nullptr)
78
        return;
79
80
    const Token &token =
81
        static_cast<LoginHandler*>(loginHandler)->getToken();
82
83
    LocalPlayer *const tempPlayer = new LocalPlayer(
84
        msg.readBeingId("player id"), BeingTypeId_zero);
85
    tempPlayer->setGender(token.sex);
86
87
    PlayerInfoBackend &data = character->data;
88
    if (packetVersion >= 20170830)
89
        data.mAttributes[Attributes::PLAYER_EXP] = msg.readInt64("exp");
90
    else
91
        data.mAttributes[Attributes::PLAYER_EXP] = msg.readInt32("exp");
92
    data.mAttributes[Attributes::MONEY] = msg.readInt32("money");
93
    if (packetVersion >= 20170830)
94
    {
95
        data.mAttributes[Attributes::PLAYER_JOB_EXP] =
96
            msg.readInt64("job exp");
97
    }
98
    else
99
    {
100
        data.mAttributes[Attributes::PLAYER_JOB_EXP] =
101
            msg.readInt32("job exp");
102
    }
103
    data.mAttributes[Attributes::PLAYER_JOB_LEVEL] =
104
        msg.readInt32("job level");
105
106
    msg.readInt16("shoes?");
107
    const int gloves = msg.readInt16("gloves");
108
    const int cape = msg.readInt16("cape");
109
    const int misc1 = msg.readInt16("misc1");
110
111
    msg.readInt32("option");
112
    tempPlayer->setKarma(msg.readInt32("karma"));
113
    tempPlayer->setManner(msg.readInt32("manner"));
114
    msg.readInt16("left points");
115
116
    if (packetVersion >= 20081217)
117
    {
118
        data.mAttributes[Attributes::PLAYER_HP] = msg.readInt32("hp");
119
        data.mAttributes[Attributes::PLAYER_MAX_HP] = msg.readInt32("max hp");
120
    }
121
    else
122
    {
123
        data.mAttributes[Attributes::PLAYER_HP] = msg.readInt16("hp");
124
        data.mAttributes[Attributes::PLAYER_MAX_HP] = msg.readInt16("max hp");
125
    }
126
    data.mAttributes[Attributes::PLAYER_MP] = msg.readInt16("mp/sp");
127
    data.mAttributes[Attributes::PLAYER_MAX_MP] = msg.readInt16("max mp/sp");
128
129
    msg.readInt16("speed");
130
    const uint16_t race = msg.readInt16("class");
131
//    tempPlayer->setSubtype(race, 0);
132
    const int hairStyle = msg.readInt16("hair style");
133
    if (packetVersion >= 20141022)
134
        msg.readInt16("body");
135
    const int option A_UNUSED = (msg.readInt16("weapon") | 1) ^ 1;
136
    const int weapon = 0;
137
138
    tempPlayer->setSpriteId(SPRITE_BODY,
139
        weapon);
140
    tempPlayer->setWeaponId(weapon);
141
142
    data.mAttributes[Attributes::PLAYER_BASE_LEVEL] = msg.readInt16("level");
143
144
    msg.readInt16("skill points");
145
    const int bottomClothes = msg.readInt16("head bottom");
146
    const int shield = msg.readInt16("shild");
147
    const int hat = msg.readInt16("head top");
148
    const int topClothes = msg.readInt16("head mid");
149
150
    const ItemColor color = fromInt(msg.readInt16("hair color"), ItemColor);
151
    tempPlayer->setHairColor(color);
152
    if (hairStyle == 0)
153
    {
154
        tempPlayer->unSetSprite(SPRITE_HAIR_COLOR);
155
    }
156
    else
157
    {
158
        tempPlayer->setSpriteColor(SPRITE_HAIR_COLOR,
159
            hairStyle * -1,
160
            ItemDB::get(-hairStyle).getDyeColorsString(
161
            color));
162
    }
163
164
    const uint16_t look = msg.readInt16("clothes color");
165
    tempPlayer->setSubtype(fromInt(race, BeingTypeId), look);
166
    tempPlayer->setName(msg.readString(24, "name"));
167
168
    character->dummy = tempPlayer;
169
170
    character->data.mStats[Attributes::PLAYER_STR].base = msg.readUInt8("str");
171
    character->data.mStats[Attributes::PLAYER_AGI].base = msg.readUInt8("agi");
172
    character->data.mStats[Attributes::PLAYER_VIT].base = msg.readUInt8("vit");
173
    character->data.mStats[Attributes::PLAYER_INT].base = msg.readUInt8("int");
174
    character->data.mStats[Attributes::PLAYER_DEX].base = msg.readUInt8("dex");
175
    character->data.mStats[Attributes::PLAYER_LUK].base = msg.readUInt8("luk");
176
177
    character->slot = msg.readInt16("character slot id");
178
    if (packetVersion >= 20061023)
179
        msg.readInt16("rename");
180
    if (packetVersion >= 20100803)
181
    {
182
        msg.readString(16, "map name");
183
        msg.readInt32("delete date");
184
    }
185
    int shoes = 0;
186
    if (packetVersion >= 20110111)
187
        shoes = msg.readInt32("robe");
188
    if (serverVersion == 0)
189
    {
190
        tempPlayer->setSpriteId(SPRITE_HAIR,
191
            shoes);
192
        tempPlayer->setSpriteId(SPRITE_SHOES,
193
            gloves);
194
        tempPlayer->setSpriteId(SPRITE_SHIELD,
195
            cape);
196
        tempPlayer->setSpriteId(SPRITE_HEAD_TOP,
197
            misc1);
198
        tempPlayer->setSpriteId(SPRITE_WEAPON,
199
            bottomClothes);
200
        tempPlayer->setSpriteId(SPRITE_FLOOR,
201
            shield);
202
        tempPlayer->setSpriteId(SPRITE_CLOTHES_COLOR,
203
            hat);
204
        tempPlayer->setSpriteId(SPRITE_HEAD_BOTTOM,
205
            topClothes);
206
//        tempPlayer->setSprite(SPRITE_HEAD_MID, misc2);
207
    }
208
    if (packetVersion >= 20110928)
209
        msg.readInt32("slot change");
210
    if (packetVersion >= 20111025)
211
        tempPlayer->setRename(msg.readInt32("rename (inverse)") != 0);
212
    uint8_t gender = 99U;
213
    if (packetVersion >= 20141016)
214
        gender = CAST_U8(msg.readUInt8("gender"));
215
    if (gender != 99)
216
        tempPlayer->setGender(Being::intToGender(gender));
217
}
218
219
void CharServerRecv::processCharLogin(Net::MessageIn &msg)
220
{
221
    msg.readInt16("packet len");
222
    int slots = 9;
223
    int offset = 0;
224
    if (packetVersion >= 20100413)
225
    {
226
        slots = msg.readInt8("MAX_CHARS");
227
        msg.readInt8("sd->char_slots");
228
        msg.readInt8("MAX_CHARS");
229
        offset = 3;
230
    }
231
    loginData.characterSlots = CAST_U16(slots);
232
233
    msg.skip(20, "unused 0");
234
235
    delete_all(Net::CharServerHandler::mCharacters);
236
    Net::CharServerHandler::mCharacters.clear();
237
238
    // Derive number of characters from message length
239
    const int count = (msg.getLength() - 24 - offset)
240
        / (106 + 4 + 2 + 16 + 4 + 4 + 4 + 4);
241
242
    for (int i = 0; i < count; ++i)
243
    {
244
        Net::Character *const character = new Net::Character;
245
        readPlayerData(msg, character);
246
        Net::CharServerHandler::mCharacters.push_back(character);
247
        if (character->dummy != nullptr)
248
        {
249
            logger->log("CharServer: Player: %s (%d)",
250
                character->dummy->getName().c_str(), character->slot);
251
        }
252
    }
253
254
    client->setState(State::CHAR_SELECT);
255
}
256
257
void CharServerRecv::processCharLogin2(Net::MessageIn &msg)
258
{
259
    // ignored
260
    msg.readInt16("len");
261
    msg.readUInt8("char slots");
262
    msg.readUInt8("left slots");
263
    msg.readUInt8("left slots");
264
    msg.readUInt8("char slots");
265
    msg.readUInt8("char slots");
266
    msg.skip(20, "unused");
267
}
268
269
void CharServerRecv::processCharMapInfo(Net::MessageIn &restrict msg)
270
{
271
    Network *const network = Network::mInstance;
272
    ServerInfo &server = mapServer;
273
    BLOCK_START("CharServerRecv::processCharMapInfo")
274
    PlayerInfo::setCharId(msg.readInt32("char id"));
275
    GameHandler::setMap(msg.readString(16, "map name"));
276
    if (config.getBoolValue("usePersistentIP") || settings.persistentIp)
277
    {
278
        msg.readInt32("map ip address");
279
        server.hostname = settings.serverName;
280
    }
281
    else
282
    {
283
        server.hostname = ipToString(msg.readInt32("map ip address"));
284
    }
285
    server.port = msg.readInt16("map ip port");
286
    if (msg.getVersion() >= 20170329)
287
    {
288
        for (int f = 0; f < 32; f ++)
289
            msg.readInt32("unused");
290
    }
291
292
    // Prevent the selected local player from being deleted
293
    localPlayer = Net::CharServerHandler::mSelectedCharacter->dummy;
294
    PlayerInfo::setBackend(Net::CharServerHandler::mSelectedCharacter->data);
295
    PlayerInfo::setStatBase(Attributes::PLAYER_WALK_SPEED,
296
        playerHandler->getDefaultWalkSpeed(),
297
        Notify_true);
298
299
    Net::CharServerHandler::mSelectedCharacter->dummy = nullptr;
300
301
    charServerHandler->clear();
302
    Net::CharServerHandler::updateCharSelectDialog();
303
304
    if (network != nullptr)
305
        network->disconnect();
306
    client->setState(State::CONNECT_GAME);
307
    BLOCK_END("CharServerRecv::processCharMapInfo")
308
}
309
310
void CharServerRecv::processChangeMapServer(Net::MessageIn &msg)
311
{
312
    Network *const network = Network::mInstance;
313
    ServerInfo &server = mapServer;
314
    BLOCK_START("CharServerRecv::processChangeMapServer")
315
    if (network == nullptr)
316
    {
317
        BLOCK_END("CharServerRecv::processChangeMapServer")
318
        return;
319
    }
320
    GameHandler::setMap(msg.readString(16, "map name"));
321
    const int x = msg.readInt16("x");
322
    const int y = msg.readInt16("y");
323
    if (config.getBoolValue("usePersistentIP") || settings.persistentIp)
324
    {
325
        msg.readInt32("host");
326
        server.hostname = settings.serverName;
327
    }
328
    else
329
    {
330
        server.hostname = ipToString(msg.readInt32("host"));
331
    }
332
    server.port = msg.readInt16("port");
333
    if (msg.getVersion() >= 20170315)
334
    {
335
        for (int f = 0; f < 32; f ++)
336
            msg.readInt32("unknown");
337
    }
338
339
    network->disconnect();
340
    client->setState(State::CHANGE_MAP);
341
    if (localPlayer != nullptr)
342
    {
343
        localPlayer->setTileCoords(x, y);
344
        localPlayer->setMap(nullptr);
345
    }
346
    BLOCK_END("CharServerRecv::processChangeMapServer")
347
}
348
349
void CharServerRecv::processPincodeStatus(Net::MessageIn &msg)
350
{
351
    pincodeManager.setSeed(msg.readUInt32("pincode seed"));
352
    pincodeManager.setAccountId(msg.readBeingId("account id"));
353
    pincodeManager.setPincodeLockFlag(false);
354
    if (pincodeManager.processPincodeStatus(CAST_U16(
355
        msg.readInt16("state"))) == false)
356
    {
357
        UNIMPLEMENTEDPACKET;
358
    }
359
}
360
361
void CharServerRecv::processPincodeStatus2(Net::MessageIn &msg)
362
{
363
    pincodeManager.setSeed(msg.readUInt32("pincode seed"));
364
    pincodeManager.setAccountId(msg.readBeingId("account id"));
365
    const uint16_t state = CAST_U16(msg.readInt16("state"));
366
    pincodeManager.setPincodeLockFlag(msg.readInt16("flag") == 0);
367
    if (pincodeManager.processPincodeStatus(state) == false)
368
    {
369
        UNIMPLEMENTEDPACKET;
370
    }
371
}
372
373
void CharServerRecv::processPincodeMakeStatus(Net::MessageIn &msg)
374
{
375
    // UNIMPLEMENTEDPACKET
376
    msg.readInt16("state");
377
    msg.readInt32("seed");
378
}
379
380
void CharServerRecv::processPincodeEditStatus(Net::MessageIn &msg)
381
{
382
    // UNIMPLEMENTEDPACKET
383
    msg.readInt16("state");
384
    msg.readInt32("seed");
385
}
386
387
void CharServerRecv::processCharCreate(Net::MessageIn &msg)
388
{
389
    BLOCK_START("CharServerRecv::processCharCreate")
390
    Net::Character *const character = new Net::Character;
391
    readPlayerData(msg, character);
392
    Net::CharServerHandler::mCharacters.push_back(character);
393
394
    Net::CharServerHandler::updateCharSelectDialog();
395
396
    // Close the character create dialog
397
    Net::CharServerHandler::mCharCreateDialog->scheduleDelete();
398
    Net::CharServerHandler::mCharCreateDialog = nullptr;
399
    BLOCK_END("CharServerRecv::processCharCreate")
400
}
401
402
void CharServerRecv::processCharCheckRename(Net::MessageIn &msg)
403
{
404
    if (msg.readInt16("flag") != 0)
405
    {
406
        createOutPacket(CMSG_CHAR_RENAME);
407
        outMsg.writeBeingId(mRenameId, "char id");
408
    }
409
    else
410
    {
411
        CREATEWIDGET(OkDialog,
412
            // TRANSLATORS: error header
413
            _("Error"),
414
            // TRANSLATORS: error message
415
            _("Character rename error."),
416
            // TRANSLATORS: ok dialog button
417
            _("Error"),
418
            DialogType::ERROR,
419
            Modal_true,
420
            ShowCenter_true,
421
            nullptr,
422
            260);
423
    }
424
}
425
426
void CharServerRecv::processCharRename(Net::MessageIn &msg)
427
{
428
    const int flag = msg.readInt16("flag");
429
    if (flag == 0)
430
    {
431
        Net::CharServerHandler::mCharSelectDialog->setName(
432
            mRenameId,
433
            mNewName);
434
        CREATEWIDGET(OkDialog,
435
            // TRANSLATORS: info header
436
            _("Info"),
437
            // TRANSLATORS: info message
438
            _("Character renamed."),
439
            // TRANSLATORS: ok dialog button
440
            _("OK"),
441
            DialogType::OK,
442
            Modal_true,
443
            ShowCenter_true,
444
            nullptr,
445
            260);
446
    }
447
    else
448
    {
449
        std::string message;
450
        switch (flag)
451
        {
452
            case 1:
453
                // TRANSLATORS: char rename error
454
                message = _("Rename not allowed.");
455
                break;
456
            case 2:
457
                // TRANSLATORS: char rename error
458
                message = _("New name is not set.");
459
                break;
460
            case 3:
461
            default:
462
                // TRANSLATORS: char rename error
463
                message = _("Character rename error.");
464
                break;
465
            case 4:
466
                // TRANSLATORS: char rename error
467
                message = _("Character not found.");
468
                break;
469
        }
470
        CREATEWIDGET(OkDialog,
471
            // TRANSLATORS: info message
472
            _("Info"),
473
            message,
474
            // TRANSLATORS: ok dialog button
475
            _("OK"),
476
            DialogType::OK,
477
            Modal_true,
478
            ShowCenter_true,
479
            nullptr,
480
            260);
481
    }
482
}
483
484
void CharServerRecv::processCharChangeSlot(Net::MessageIn &msg)
485
{
486
    UNIMPLEMENTEDPACKET;
487
    msg.readInt16("len");
488
    msg.readInt16("flag");  // 0 - ok, 1 - error
489
    msg.readInt16("unused");
490
}
491
492
void CharServerRecv::processCharDeleteFailed(Net::MessageIn &msg)
493
{
494
    BLOCK_START("CharServerRecv::processCharDeleteFailed")
495
    Net::CharServerHandler::unlockCharSelectDialog();
496
    msg.readUInt8("error");
497
    CREATEWIDGET(OkDialog,
498
        // TRANSLATORS: error header
499
        _("Error"),
500
        // TRANSLATORS: error message
501
        _("Failed to delete character."),
502
        // TRANSLATORS: ok dialog button
503
        _("OK"),
504
        DialogType::ERROR,
505
        Modal_true,
506
        ShowCenter_true,
507
        nullptr,
508
        260);
509
    BLOCK_END("CharServerRecv::processCharDeleteFailed")
510
}
511
512
void CharServerRecv::processCharCaptchaNotSupported(Net::MessageIn &msg)
513
{
514
    UNIMPLEMENTEDPACKET;
515
    msg.readInt16("5");
516
    msg.readUInt8("1");
517
}
518
519
void CharServerRecv::processCharDelete2Ack(Net::MessageIn &msg)
520
{
521
    UNIMPLEMENTEDPACKET;
522
    msg.readInt32("char id");
523
    msg.readInt32("result");
524
    // for packets before 20130000, this is raw time
525
    // in other case raw time - time(NULL)
526
    msg.readInt32("time");
527
}
528
529
void CharServerRecv::processCharDelete2AcceptActual(Net::MessageIn &msg)
530
{
531
    UNIMPLEMENTEDPACKET;
532
    msg.readInt32("char id");
533
    msg.readInt32("result");
534
}
535
536
void CharServerRecv::processCharDelete2CancelAck(Net::MessageIn &msg)
537
{
538
    UNIMPLEMENTEDPACKET;
539
    msg.readInt32("char id");
540
    msg.readInt32("result");
541
}
542
543
void CharServerRecv::processCharCharacters(Net::MessageIn &msg)
544
{
545
    msg.readInt16("packet len");
546
547
    delete_all(Net::CharServerHandler::mCharacters);
548
    Net::CharServerHandler::mCharacters.clear();
549
550
    // Derive number of characters from message length
551
    const int count = (msg.getLength() - 4)
552
        / (106 + 4 + 2 + 16 + 4 + 4 + 4 + 4);
553
554
    for (int i = 0; i < count; ++i)
555
    {
556
        Net::Character *const character = new Net::Character;
557
        readPlayerData(msg, character);
558
        Net::CharServerHandler::mCharacters.push_back(character);
559
        if (character->dummy != nullptr)
560
        {
561
            logger->log("CharServer: Player: %s (%d)",
562
                character->dummy->getName().c_str(), character->slot);
563
        }
564
    }
565
566
    client->setState(State::CHAR_SELECT);
567
}
568
569
void CharServerRecv::processCharBanCharList(Net::MessageIn &msg)
570
{
571
    UNIMPLEMENTEDPACKET;
572
    const int count = (msg.readInt16("len") - 4) / 24;
573
    for (int f = 0; f < count; f ++)
574
    {
575
        msg.readInt32("char id");
576
        msg.readString(20, "unbun time");
577
    }
578
}
579
580

3
}  // namespace EAthena