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"));
354  msg.readInt16("state"))) == false)
355  {
357  }
358 }
359 
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  {
369  }
370 }
371 
373 {
374  // UNIMPLEMENTEDPACKET
375  msg.readInt16("state");
376  msg.readInt32("seed");
377 }
378 
380 {
381  // UNIMPLEMENTEDPACKET
382  msg.readInt16("state");
383  msg.readInt32("seed");
384 }
385 
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 
394 
395  // Close the character create dialog
398  BLOCK_END("CharServerRecv::processCharCreate")
399 }
400 
402 {
403  if (msg.readInt16("flag") != 0)
404  {
406  outMsg.writeBeingId(mRenameId, "char id");
407  }
408  else
409  {
411  // TRANSLATORS: error header
412  _("Error"),
413  // TRANSLATORS: error message
414  _("Character rename error."),
415  // TRANSLATORS: ok dialog button
416  _("Error"),
418  Modal_true,
420  nullptr,
421  260);
422  }
423 }
424 
426 {
427  const int flag = msg.readInt16("flag");
428  if (flag == 0)
429  {
431  mRenameId,
432  mNewName);
434  // TRANSLATORS: info header
435  _("Info"),
436  // TRANSLATORS: info message
437  _("Character renamed."),
438  // TRANSLATORS: ok dialog button
439  _("OK"),
441  Modal_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  }
470  // TRANSLATORS: info message
471  _("Info"),
472  message,
473  // TRANSLATORS: ok dialog button
474  _("OK"),
476  Modal_true,
478  nullptr,
479  260);
480  }
481 }
482 
484 {
486  msg.readInt16("len");
487  msg.readInt16("flag"); // 0 - ok, 1 - error
488  msg.readInt16("unused");
489 }
490 
492 {
493  BLOCK_START("CharServerRecv::processCharDeleteFailed")
495  msg.readUInt8("error");
497  // TRANSLATORS: error header
498  _("Error"),
499  // TRANSLATORS: error message
500  _("Failed to delete character."),
501  // TRANSLATORS: ok dialog button
502  _("OK"),
504  Modal_true,
506  nullptr,
507  260);
508  BLOCK_END("CharServerRecv::processCharDeleteFailed")
509 }
510 
512 {
514  msg.readInt16("5");
515  msg.readUInt8("1");
516 }
517 
519 {
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 
529 {
531  msg.readInt32("char id");
532  msg.readInt32("result");
533 }
534 
536 {
538  msg.readInt32("char id");
539  msg.readInt32("result");
540 }
541 
543 {
544  msg.readInt16("packet len");
545 
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 
566 }
567 
569 {
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 } // namespace EAthena
int packetVersion
Definition: client.cpp:124
Net::PlayerHandler * playerHandler
Definition: net.cpp:93
uint16_t characterSlots
Definition: logindata.h:76
void processPincodeStatus(Net::MessageIn &msg)
unsigned char readUInt8(const char *const str)
Definition: messagein.cpp:103
#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:223
void processChangeMapServer(Net::MessageIn &msg)
void setState(const StateT state)
Definition: client.h:65
void processCharRename(Net::MessageIn &msg)
LoginData loginData
Definition: client.cpp:184
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:195
static CharCreateDialog * mCharCreateDialog
void setCharId(const int charId)
Definition: playerinfo.cpp:379
void setPincodeLockFlag(const bool flag)
signed char readInt8(const char *const str)
Definition: messagein.cpp:116
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:228
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:130
#define CAST_U16
Definition: cast.h:28
bool getBoolValue(const std::string &key) const
bool processPincodeStatus(const uint16_t state)
void processPincodeStatus2(Net::MessageIn &msg)
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:87
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)
int getVersion() const
Definition: messagein.h:133
LocalPlayer * localPlayer
void skip(const unsigned int length, const char *const str)
Definition: messagein.cpp:349
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
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:830
void setName(const BeingId id, const std::string &newName)
#define A_UNUSED
Definition: localconsts.h:159
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)
#define restrict
Definition: localconsts.h:164
std::string readString(int length, const char *const dstr)
Definition: messagein.cpp:367
#define UNIMPLEMENTEDPACKET
Definition: logger.h:55
const BeingId BeingId_zero
Definition: beingid.h:29
void setBackend(const PlayerInfoBackend &backend)
Definition: playerinfo.cpp:374
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:174
void processCharDelete2CancelAck(Net::MessageIn &msg)
int BeingTypeId
Definition: beingtypeid.h:29
Net::CharServerHandler * charServerHandler
Definition: net.cpp:82
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
static void unlockCharSelectDialog()
const bool Notify_true
Definition: notify.h:29
virtual int getDefaultWalkSpeed() const =0