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

6
}  // namespace EAthena