GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/charserverrecv.cpp Lines: 2 266 0.8 %
Date: 2018-05-19 03:07:18 Branches: 2 151 1.3 %

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

6
}  // namespace EAthena