ManaPlus
charserverrecv.cpp
Go to the documentation of this file.
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 
25 
26 #include "client.h"
27 #include "configuration.h"
28 #include "pincodemanager.h"
29 #include "settings.h"
30 
33 #include "gui/windows/okdialog.h"
34 
36 
37 #include "net/character.h"
38 #include "net/charserverhandler.h"
39 #include "net/playerhandler.h"
40 
41 #include "net/ea/token.h"
42 
45 #include "net/eathena/messageout.h"
46 #include "net/eathena/network.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 
66 
67 namespace CharServerRecv
68 {
69  std::string mNewName;
71 } // namespace CharServerRecv
72 
73 // callers must count each packet size by self
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 
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  }
232 
233  msg.skip(20, "unused 0");
234 
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 
255 }
256 
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 
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
297  Notify_true);
298 
300 
303 
304  if (network != nullptr)
305  network->disconnect();
307  BLOCK_END("CharServerRecv::processCharMapInfo")
308 }
309 
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();
341  if (localPlayer != nullptr)
342  {
344  localPlayer->setMap(nullptr);
345  }
346  BLOCK_END("CharServerRecv::processChangeMapServer")
347 }
348 
350 {
351  pincodeManager.setSeed(msg.readUInt32("pincode seed"));
352  pincodeManager.setAccountId(msg.readBeingId("account id"));
355  msg.readInt16("state"))) == false)
356  {
358  }
359 }
360 
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  {
370  }
371 }
372 
374 {
375  // UNIMPLEMENTEDPACKET
376  msg.readInt16("state");
377  msg.readInt32("seed");
378 }
379 
381 {
382  // UNIMPLEMENTEDPACKET
383  msg.readInt16("state");
384  msg.readInt32("seed");
385 }
386 
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 
395 
396  // Close the character create dialog
399  BLOCK_END("CharServerRecv::processCharCreate")
400 }
401 
403 {
404  if (msg.readInt16("flag") != 0)
405  {
406  createOutPacket(CMSG_CHAR_RENAME);
407  outMsg.writeBeingId(mRenameId, "char id");
408  }
409  else
410  {
412  // TRANSLATORS: error header
413  _("Error"),
414  // TRANSLATORS: error message
415  _("Character rename error."),
416  // TRANSLATORS: ok dialog button
417  _("Error"),
419  Modal_true,
421  nullptr,
422  260);
423  }
424 }
425 
427 {
428  const int flag = msg.readInt16("flag");
429  if (flag == 0)
430  {
432  mRenameId,
433  mNewName);
435  // TRANSLATORS: info header
436  _("Info"),
437  // TRANSLATORS: info message
438  _("Character renamed."),
439  // TRANSLATORS: ok dialog button
440  _("OK"),
442  Modal_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  }
471  // TRANSLATORS: info message
472  _("Info"),
473  message,
474  // TRANSLATORS: ok dialog button
475  _("OK"),
477  Modal_true,
479  nullptr,
480  260);
481  }
482 }
483 
485 {
487  msg.readInt16("len");
488  msg.readInt16("flag"); // 0 - ok, 1 - error
489  msg.readInt16("unused");
490 }
491 
493 {
494  BLOCK_START("CharServerRecv::processCharDeleteFailed")
496  msg.readUInt8("error");
498  // TRANSLATORS: error header
499  _("Error"),
500  // TRANSLATORS: error message
501  _("Failed to delete character."),
502  // TRANSLATORS: ok dialog button
503  _("OK"),
505  Modal_true,
507  nullptr,
508  260);
509  BLOCK_END("CharServerRecv::processCharDeleteFailed")
510 }
511 
513 {
515  msg.readInt16("5");
516  msg.readUInt8("1");
517 }
518 
520 {
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 
530 {
532  msg.readInt32("char id");
533  msg.readInt32("result");
534 }
535 
537 {
539  msg.readInt32("char id");
540  msg.readInt32("result");
541 }
542 
544 {
545  msg.readInt16("packet len");
546 
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 
567 }
568 
570 {
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 } // namespace EAthena
int BeingId
Definition: beingid.h:30
const BeingId BeingId_zero
Definition: beingid.h:30
int BeingTypeId
Definition: beingtypeid.h:30
const BeingTypeId BeingTypeId_zero
Definition: beingtypeid.h:30
#define CAST_U16
Definition: cast.h:29
#define CAST_U8
Definition: cast.h:27
Net::CharServerHandler * charServerHandler
Definition: net.cpp:85
void setKarma(const int karma)
Definition: being.h:1031
void setWeaponId(const int id)
Definition: being.cpp:3135
static GenderT intToGender(const uint8_t sex) A_CONST
Definition: being.h:941
void setName(const std::string &name)
Definition: being.cpp:1136
void setSubtype(const BeingTypeId subtype, const uint16_t look)
Definition: being.cpp:371
void setHairColor(const unsigned int slot, const ItemColor color)
Definition: being.cpp:3389
const std::string & getName() const
Definition: being.h:232
void setSpriteId(const unsigned int slot, const int id)
Definition: being.cpp:2789
virtual void setGender(const GenderT gender)
Definition: being.cpp:3581
void unSetSprite(const unsigned int slot)
Definition: being.cpp:2861
void setManner(const int manner)
Definition: being.h:1037
void setSpriteColor(const unsigned int slot, const int id, const std::string &color)
Definition: being.cpp:2890
void setName(const BeingId id, const std::string &newName)
void setState(const StateT state)
Definition: client.h:66
bool getBoolValue(const std::string &key) const
static Network * mInstance
Definition: network.h:57
static void setMap(const std::string &map)
Definition: gamehandler.cpp:46
void disconnect()
Definition: network.cpp:139
void setTileCoords(const int x, const int y)
void setMap(Map *const map)
void setRename(const bool r)
Definition: localplayer.h:422
void log(const char *const log_text,...)
Definition: logger.cpp:269
uint16_t characterSlots
Definition: logindata.h:77
static void unlockCharSelectDialog()
static void updateCharSelectDialog()
virtual void clear() const =0
static Net::Character * mSelectedCharacter
static CharSelectDialog * mCharSelectDialog
static Net::Characters mCharacters
static CharCreateDialog * mCharCreateDialog
virtual int getDefaultWalkSpeed() const =0
void setAccountId(const BeingId id)
void setPincodeLockFlag(const bool flag)
bool processPincodeStatus(const uint16_t state)
void setSeed(const uint32_t seed)
std::string hostname
Definition: serverinfo.h:45
uint16_t port
Definition: serverinfo.h:58
bool persistentIp
Definition: settings.h:153
std::string serverName
Definition: settings.h:114
virtual void scheduleDelete()
Definition: window.cpp:831
Configuration config
#define CREATEWIDGET(type,...)
Definition: createwidget.h:29
void delete_all(Container &c)
Definition: dtor.h:56
Client * client
Definition: client.cpp:118
int serverVersion
Definition: client.cpp:124
int packetVersion
Definition: client.cpp:125
#define _(s)
Definition: gettext.h:35
#define fromInt(val, name)
Definition: intdefines.h:46
uint16_t ItemColor
Definition: itemcolor.h:30
#define restrict
Definition: localconsts.h:165
#define A_UNUSED
Definition: localconsts.h:160
LocalPlayer * localPlayer
Logger * logger
Definition: logger.cpp:89
#define UNIMPLEMENTEDPACKET
Definition: logger.h:56
Net::LoginHandler * loginHandler
Definition: net.cpp:90
LoginData loginData
Definition: client.cpp:185
uint32_t data
#define createOutPacket(name)
Definition: messageout.h:37
const bool Modal_true
Definition: modal.h:30
bool msg(InputEvent &event)
Definition: chat.cpp:39
bool hat(InputEvent &event)
Definition: chat.cpp:62
@ PLAYER_MAX_MP
Definition: attributes.h:35
@ PLAYER_BASE_LEVEL
Definition: attributes.h:31
@ PLAYER_JOB_EXP
Definition: attributes.h:66
@ PLAYER_JOB_LEVEL
Definition: attributes.h:41
@ PLAYER_WALK_SPEED
Definition: attributes.h:53
@ PLAYER_MAX_HP
Definition: attributes.h:33
void processCharDelete2CancelAck(Net::MessageIn &msg)
void processCharCreate(Net::MessageIn &msg)
void processCharDelete2AcceptActual(Net::MessageIn &msg)
void processCharCharacters(Net::MessageIn &msg)
void processCharRename(Net::MessageIn &msg)
void processPincodeMakeStatus(Net::MessageIn &msg)
void processCharLogin(Net::MessageIn &msg)
void processCharBanCharList(Net::MessageIn &msg)
void processCharDelete2Ack(Net::MessageIn &msg)
void processPincodeStatus(Net::MessageIn &msg)
void processCharDeleteFailed(Net::MessageIn &msg)
void processPincodeEditStatus(Net::MessageIn &msg)
void processCharLogin2(Net::MessageIn &msg)
void processPincodeStatus2(Net::MessageIn &msg)
void processCharMapInfo(Net::MessageIn &msg)
void processChangeMapServer(Net::MessageIn &msg)
void processCharCaptchaNotSupported(Net::MessageIn &msg)
void readPlayerData(Net::MessageIn &msg, Net::Character *const character)
void processCharChangeSlot(Net::MessageIn &msg)
void processCharCheckRename(Net::MessageIn &msg)
ServerInfo mapServer
ServerInfo charServer
const ItemInfo & get(const int id)
Definition: itemdb.cpp:792
void setBackend(const PlayerInfoBackend &backend)
Definition: playerinfo.cpp:376
void setStatBase(const AttributesT id, const int value, const Notify notify)
Definition: playerinfo.cpp:143
void setCharId(const int charId)
Definition: playerinfo.cpp:381
@ CHANGE_MAP
Definition: state.h:50
@ CHAR_SELECT
Definition: state.h:47
@ CONNECT_GAME
Definition: state.h:48
@ SPRITE_HAIR_COLOR
Definition: sprite.h:35
@ SPRITE_WEAPON
Definition: sprite.h:31
@ SPRITE_FLOOR
Definition: sprite.h:40
@ SPRITE_HEAD_TOP
Definition: sprite.h:33
@ SPRITE_SHOES
Definition: sprite.h:38
@ SPRITE_SHIELD
Definition: sprite.h:37
@ SPRITE_BODY
Definition: sprite.h:39
@ SPRITE_CLOTHES_COLOR
Definition: sprite.h:36
@ SPRITE_HEAD_BOTTOM
Definition: sprite.h:32
Net::PlayerHandler * playerHandler
Definition: net.cpp:96
const bool Notify_true
Definition: notify.h:30
#define BLOCK_END(name)
Definition: perfomance.h:80
#define BLOCK_START(name)
Definition: perfomance.h:79
PincodeManager pincodeManager
Settings settings
Definition: settings.cpp:32
const bool ShowCenter_true
Definition: showcenter.h:30
const char * ipToString(const uint32_t address)
Definition: stringutils.cpp:86
LocalPlayer * dummy
Definition: character.h:56
PlayerInfoBackend data
Definition: character.h:57
uint16_t slot
Definition: character.h:58
Definition: token.h:31
GenderT sex
Definition: token.h:44