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-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 
24 
25 #include "client.h"
26 #include "configuration.h"
27 #include "pincodemanager.h"
28 #include "settings.h"
29 
32 #include "gui/windows/okdialog.h"
33 
35 
36 #include "net/character.h"
37 #include "net/charserverhandler.h"
38 #include "net/playerhandler.h"
39 
40 #include "net/ea/token.h"
41 
44 #include "net/eathena/messageout.h"
45 #include "net/eathena/network.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 
65 
66 namespace CharServerRecv
67 {
68  std::string mNewName;
70 } // namespace CharServerRecv
71 
72 // callers must count each packet size by self
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  {
95  msg.readInt64("job exp");
96  }
97  else
98  {
100  msg.readInt32("job exp");
101  }
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 
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 
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  }
231 
232  msg.skip(20, "unused 0");
233 
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 
254 }
255 
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 
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
296  Notify_true);
297 
299 
302 
303  if (network != nullptr)
304  network->disconnect();
306  BLOCK_END("CharServerRecv::processCharMapInfo")
307 }
308 
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();
340  if (localPlayer != nullptr)
341  {
342  localPlayer->setTileCoords(x, y);
343  localPlayer->setMap(nullptr);
344  }
345  BLOCK_END("CharServerRecv::processChangeMapServer")
346 }
347 
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
357  break;
358  case 1: // ask for pin
360  break;
361  case 2: // create new pin
362  case 4: // create new pin?
363  {
365  break;
366  }
367  case 3: // pin must be changed
369  break;
370  case 8: // pincode was incorrect
371  case 5: // client show error?
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:
380  break;
381  }
382  if (client)
384 }
385 
387 {
388  // UNIMPLEMENTEDPACKET
389  msg.readInt16("state");
390  msg.readInt32("seed");
391 }
392 
394 {
395  // UNIMPLEMENTEDPACKET
396  msg.readInt16("state");
397  msg.readInt32("seed");
398 }
399 
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 
408 
409  // Close the character create dialog
412  BLOCK_END("CharServerRecv::processCharCreate")
413 }
414 
416 {
417  if (msg.readInt16("flag") != 0)
418  {
420  outMsg.writeBeingId(mRenameId, "char id");
421  }
422  else
423  {
425  // TRANSLATORS: error header
426  _("Error"),
427  // TRANSLATORS: error message
428  _("Character rename error."),
429  // TRANSLATORS: ok dialog button
430  _("Error"),
432  Modal_true,
434  nullptr,
435  260);
436  }
437 }
438 
440 {
441  const int flag = msg.readInt16("flag");
442  if (flag == 0)
443  {
445  mRenameId,
446  mNewName);
448  // TRANSLATORS: info header
449  _("Info"),
450  // TRANSLATORS: info message
451  _("Character renamed."),
452  // TRANSLATORS: ok dialog button
453  _("OK"),
455  Modal_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  }
484  // TRANSLATORS: info message
485  _("Info"),
486  message,
487  // TRANSLATORS: ok dialog button
488  _("OK"),
490  Modal_true,
492  nullptr,
493  260);
494  }
495 }
496 
498 {
500  msg.readInt16("len");
501  msg.readInt16("flag"); // 0 - ok, 1 - error
502  msg.readInt16("unused");
503 }
504 
506 {
507  BLOCK_START("CharServerRecv::processCharDeleteFailed")
509  msg.readUInt8("error");
511  // TRANSLATORS: error header
512  _("Error"),
513  // TRANSLATORS: error message
514  _("Failed to delete character."),
515  // TRANSLATORS: ok dialog button
516  _("OK"),
518  Modal_true,
520  nullptr,
521  260);
522  BLOCK_END("CharServerRecv::processCharDeleteFailed")
523 }
524 
526 {
528  msg.readInt16("5");
529  msg.readUInt8("1");
530 }
531 
533 {
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 
543 {
545  msg.readInt32("char id");
546  msg.readInt32("result");
547 }
548 
550 {
552  msg.readInt32("char id");
553  msg.readInt32("result");
554 }
555 
557 {
558  msg.readInt16("packet len");
559 
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 
580 }
581 
583 {
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 } // namespace EAthena
int packetVersion
Definition: client.cpp:124
Net::PlayerHandler * playerHandler
Definition: net.cpp:92
uint16_t characterSlots
Definition: logindata.h:76
void processPincodeStatus(Net::MessageIn &msg)
unsigned char readUInt8(const char *const str)
Definition: messagein.cpp:98
#define _(s)
Definition: gettext.h:34
const ItemInfo & get(const int id)
Definition: itemdb.cpp:792
#define CAST_U8
Definition: cast.h:26
std::string serverName
Definition: settings.h:112
BeingId readBeingId(const char *const str)
Definition: messagein.cpp:211
void processChangeMapServer(Net::MessageIn &msg)
void setState(const StateT state)
Definition: client.h:65
void processCharRename(Net::MessageIn &msg)
LoginData loginData
Definition: client.cpp:183
std::string hostname
Definition: serverinfo.h:44
void setStatBase(const AttributesT id, const int value, const Notify notify)
Definition: playerinfo.cpp:141
void processCharCharacters(Net::MessageIn &msg)
void setTileCoords(const int x, const int y)
Definition: being.cpp:5010
#define fromInt(val, name)
Definition: intdefines.h:45
void setMap(Map *const map)
bool persistentIp
Definition: settings.h:150
static void setMap(const std::string &map)
Definition: gamehandler.cpp:45
int serverVersion
Definition: client.cpp:123
unsigned int getLength() const
Definition: messagein.h:58
#define BLOCK_START(name)
Definition: perfomance.h:78
Configuration config
virtual void setGender(const GenderT gender)
Definition: being.cpp:3581
const BeingTypeId BeingTypeId_zero
Definition: beingtypeid.h:29
int BeingId
Definition: beingid.h:29
#define BLOCK_END(name)
Definition: perfomance.h:79
uint16_t ItemColor
Definition: itemcolor.h:29
bool msg(InputEvent &event)
Definition: chat.cpp:38
void setRename(const bool r)
Definition: localplayer.h:418
Client * client
Definition: client.cpp:117
void processCharCheckRename(Net::MessageIn &msg)
uint32_t readUInt32(const char *const str)
Definition: messagein.cpp:190
static CharCreateDialog * mCharCreateDialog
void setCharId(const int charId)
Definition: playerinfo.cpp:378
signed char readInt8(const char *const str)
Definition: messagein.cpp:111
static GenderT intToGender(const uint8_t sex) A_CONST
Definition: being.h:942
Definition: token.h:29
virtual void clear() const =0
void setSpriteColor(const unsigned int slot, const int id, const std::string &color)
Definition: being.cpp:2890
void processCharDeleteFailed(Net::MessageIn &msg)
static CharSelectDialog * mCharSelectDialog
int64_t readInt64(const char *const str)
Definition: messagein.cpp:216
Logger * logger
Definition: logger.cpp:88
GenderT sex
Definition: token.h:43
Settings settings
Definition: settings.cpp:31
int16_t readInt16(const char *const str)
Definition: messagein.cpp:125
#define CAST_U16
Definition: cast.h:28
bool getBoolValue(const std::string &key) const
void delete_all(Container &c)
Definition: dtor.h:55
uint16_t slot
Definition: character.h:57
void processCharLogin2(Net::MessageIn &msg)
Net::LoginHandler * loginHandler
Definition: net.cpp:86
void setAccountId(const BeingId id)
void processCharChangeSlot(Net::MessageIn &msg)
void processCharCaptchaNotSupported(Net::MessageIn &msg)
void processCharDelete2AcceptActual(Net::MessageIn &msg)
uint32_t data
void processPincodeEditStatus(Net::MessageIn &msg)
LocalPlayer * localPlayer
void skip(const unsigned int length, const char *const str)
Definition: messagein.cpp:337
uint16_t port
Definition: serverinfo.h:57
void disconnect()
Definition: network.cpp:138
AtrIntMap mAttributes
Definition: playerinfo.h:72
const bool ShowCenter_true
Definition: showcenter.h:29
void processPincodeMakeStatus(Net::MessageIn &msg)
bool hat(InputEvent &event)
Definition: chat.cpp:61
const char * ipToString(const uint32_t address)
Definition: stringutils.cpp:85
void setSpriteId(const unsigned int slot, const int id)
Definition: being.cpp:2789
unsigned int getVersion() const
Definition: messagein.h:131
static Network * mInstance
Definition: network.h:56
void setManner(const int manner)
Definition: being.h:1040
const std::string & getName() const
Definition: being.h:231
#define createOutPacket(name)
Definition: messageout.h:36
ServerInfo mapServer
void processCharMapInfo(Net::MessageIn &msg)
virtual void scheduleDelete()
Definition: window.cpp:826
void setName(const BeingId id, const std::string &newName)
#define A_UNUSED
Definition: localconsts.h:151
PincodeManager pincodeManager
void setSubtype(const BeingTypeId subtype, const uint16_t look)
Definition: being.cpp:370
void setKarma(const int karma)
Definition: being.h:1034
#define CREATEWIDGET(type,...)
Definition: createwidget.h:28
void readPlayerData(Net::MessageIn &msg, Net::Character *const character)
static void updateCharSelectDialog()
int CMSG_CHAR_RENAME
Definition: protocolout.cpp:55
void processCharBanCharList(Net::MessageIn &msg)
void setName(const std::string &name)
Definition: being.cpp:1135
void processCharDelete2Ack(Net::MessageIn &msg)
static Net::Characters mCharacters
static Net::Character * mSelectedCharacter
void processCharCreate(Net::MessageIn &msg)
void setState(const PincodeStateT state)
#define restrict
Definition: localconsts.h:156
std::string readString(int length, const char *const dstr)
Definition: messagein.cpp:355
#define UNIMPLEMENTEDPACKET
Definition: logger.h:55
const BeingId BeingId_zero
Definition: beingid.h:29
void setBackend(const PlayerInfoBackend &backend)
Definition: playerinfo.cpp:373
PlayerInfoBackend data
Definition: character.h:56
void setHairColor(const unsigned int slot, const ItemColor color)
Definition: being.cpp:3389
void log(const char *const log_text,...)
Definition: logger.cpp:264
int32_t readInt32(const char *const str)
Definition: messagein.cpp:169
void processCharDelete2CancelAck(Net::MessageIn &msg)
int BeingTypeId
Definition: beingtypeid.h:29
Net::CharServerHandler * charServerHandler
Definition: net.cpp:81
void setWeaponId(const int id)
Definition: being.cpp:3135
void processCharLogin(Net::MessageIn &msg)
void setSeed(const uint32_t seed)
ServerInfo charServer
LocalPlayer * dummy
Definition: character.h:55
void unSetSprite(const unsigned int slot)
Definition: being.cpp:2861
void updatePinState()
Definition: client.cpp:815
static void unlockCharSelectDialog()
const bool Notify_true
Definition: notify.h:29
virtual int getDefaultWalkSpeed() const =0