GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/charserverrecv.cpp Lines: 2 262 0.8 %
Date: 2018-09-20 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-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
1
    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
    pincodeManager.setPincodeLockFlag(false);
353
    if (pincodeManager.processPincodeStatus(CAST_U16(
354
        msg.readInt16("state"))) == false)
355
    {
356
        UNIMPLEMENTEDPACKET;
357
    }
358
}
359
360
void CharServerRecv::processPincodeStatus2(Net::MessageIn &msg)
361
{
362
    pincodeManager.setSeed(msg.readUInt32("pincode seed"));
363
    pincodeManager.setAccountId(msg.readBeingId("account id"));
364
    const uint16_t state = CAST_U16(msg.readInt16("state"));
365
    pincodeManager.setPincodeLockFlag(msg.readInt16("flag") == 0);
366
    if (pincodeManager.processPincodeStatus(state) == false)
367
    {
368
        UNIMPLEMENTEDPACKET;
369
    }
370
}
371
372
void CharServerRecv::processPincodeMakeStatus(Net::MessageIn &msg)
373
{
374
    // UNIMPLEMENTEDPACKET
375
    msg.readInt16("state");
376
    msg.readInt32("seed");
377
}
378
379
void CharServerRecv::processPincodeEditStatus(Net::MessageIn &msg)
380
{
381
    // UNIMPLEMENTEDPACKET
382
    msg.readInt16("state");
383
    msg.readInt32("seed");
384
}
385
386
void CharServerRecv::processCharCreate(Net::MessageIn &msg)
387
{
388
    BLOCK_START("CharServerRecv::processCharCreate")
389
    Net::Character *const character = new Net::Character;
390
    readPlayerData(msg, character);
391
    Net::CharServerHandler::mCharacters.push_back(character);
392
393
    Net::CharServerHandler::updateCharSelectDialog();
394
395
    // Close the character create dialog
396
    Net::CharServerHandler::mCharCreateDialog->scheduleDelete();
397
    Net::CharServerHandler::mCharCreateDialog = nullptr;
398
    BLOCK_END("CharServerRecv::processCharCreate")
399
}
400
401
void CharServerRecv::processCharCheckRename(Net::MessageIn &msg)
402
{
403
    if (msg.readInt16("flag") != 0)
404
    {
405
        createOutPacket(CMSG_CHAR_RENAME);
406
        outMsg.writeBeingId(mRenameId, "char id");
407
    }
408
    else
409
    {
410
        CREATEWIDGET(OkDialog,
411
            // TRANSLATORS: error header
412
            _("Error"),
413
            // TRANSLATORS: error message
414
            _("Character rename error."),
415
            // TRANSLATORS: ok dialog button
416
            _("Error"),
417
            DialogType::ERROR,
418
            Modal_true,
419
            ShowCenter_true,
420
            nullptr,
421
            260);
422
    }
423
}
424
425
void CharServerRecv::processCharRename(Net::MessageIn &msg)
426
{
427
    const int flag = msg.readInt16("flag");
428
    if (flag == 0)
429
    {
430
        Net::CharServerHandler::mCharSelectDialog->setName(
431
            mRenameId,
432
            mNewName);
433
        CREATEWIDGET(OkDialog,
434
            // TRANSLATORS: info header
435
            _("Info"),
436
            // TRANSLATORS: info message
437
            _("Character renamed."),
438
            // TRANSLATORS: ok dialog button
439
            _("OK"),
440
            DialogType::OK,
441
            Modal_true,
442
            ShowCenter_true,
443
            nullptr,
444
            260);
445
    }
446
    else
447
    {
448
        std::string message;
449
        switch (flag)
450
        {
451
            case 1:
452
                // TRANSLATORS: char rename error
453
                message = _("Rename not allowed.");
454
                break;
455
            case 2:
456
                // TRANSLATORS: char rename error
457
                message = _("New name is not set.");
458
                break;
459
            case 3:
460
            default:
461
                // TRANSLATORS: char rename error
462
                message = _("Character rename error.");
463
                break;
464
            case 4:
465
                // TRANSLATORS: char rename error
466
                message = _("Character not found.");
467
                break;
468
        }
469
        CREATEWIDGET(OkDialog,
470
            // TRANSLATORS: info message
471
            _("Info"),
472
            message,
473
            // TRANSLATORS: ok dialog button
474
            _("OK"),
475
            DialogType::OK,
476
            Modal_true,
477
            ShowCenter_true,
478
            nullptr,
479
            260);
480
    }
481
}
482
483
void CharServerRecv::processCharChangeSlot(Net::MessageIn &msg)
484
{
485
    UNIMPLEMENTEDPACKET;
486
    msg.readInt16("len");
487
    msg.readInt16("flag");  // 0 - ok, 1 - error
488
    msg.readInt16("unused");
489
}
490
491
void CharServerRecv::processCharDeleteFailed(Net::MessageIn &msg)
492
{
493
    BLOCK_START("CharServerRecv::processCharDeleteFailed")
494
    Net::CharServerHandler::unlockCharSelectDialog();
495
    msg.readUInt8("error");
496
    CREATEWIDGET(OkDialog,
497
        // TRANSLATORS: error header
498
        _("Error"),
499
        // TRANSLATORS: error message
500
        _("Failed to delete character."),
501
        // TRANSLATORS: ok dialog button
502
        _("OK"),
503
        DialogType::ERROR,
504
        Modal_true,
505
        ShowCenter_true,
506
        nullptr,
507
        260);
508
    BLOCK_END("CharServerRecv::processCharDeleteFailed")
509
}
510
511
void CharServerRecv::processCharCaptchaNotSupported(Net::MessageIn &msg)
512
{
513
    UNIMPLEMENTEDPACKET;
514
    msg.readInt16("5");
515
    msg.readUInt8("1");
516
}
517
518
void CharServerRecv::processCharDelete2Ack(Net::MessageIn &msg)
519
{
520
    UNIMPLEMENTEDPACKET;
521
    msg.readInt32("char id");
522
    msg.readInt32("result");
523
    // for packets before 20130000, this is raw time
524
    // in other case raw time - time(NULL)
525
    msg.readInt32("time");
526
}
527
528
void CharServerRecv::processCharDelete2AcceptActual(Net::MessageIn &msg)
529
{
530
    UNIMPLEMENTEDPACKET;
531
    msg.readInt32("char id");
532
    msg.readInt32("result");
533
}
534
535
void CharServerRecv::processCharDelete2CancelAck(Net::MessageIn &msg)
536
{
537
    UNIMPLEMENTEDPACKET;
538
    msg.readInt32("char id");
539
    msg.readInt32("result");
540
}
541
542
void CharServerRecv::processCharCharacters(Net::MessageIn &msg)
543
{
544
    msg.readInt16("packet len");
545
546
    delete_all(Net::CharServerHandler::mCharacters);
547
    Net::CharServerHandler::mCharacters.clear();
548
549
    // Derive number of characters from message length
550
    const int count = (msg.getLength() - 4)
551
        / (106 + 4 + 2 + 16 + 4 + 4 + 4 + 4);
552
553
    for (int i = 0; i < count; ++i)
554
    {
555
        Net::Character *const character = new Net::Character;
556
        readPlayerData(msg, character);
557
        Net::CharServerHandler::mCharacters.push_back(character);
558
        if (character->dummy != nullptr)
559
        {
560
            logger->log("CharServer: Player: %s (%d)",
561
                character->dummy->getName().c_str(), character->slot);
562
        }
563
    }
564
565
    client->setState(State::CHAR_SELECT);
566
}
567
568
void CharServerRecv::processCharBanCharList(Net::MessageIn &msg)
569
{
570
    UNIMPLEMENTEDPACKET;
571
    const int count = (msg.readInt16("len") - 4) / 24;
572
    for (int f = 0; f < count; f ++)
573
    {
574
        msg.readInt32("char id");
575
        msg.readString(20, "unbun time");
576
    }
577
}
578
579

3
}  // namespace EAthena