Advertisement
Guest User

Untitled

a guest
Dec 19th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. /**
  2. * The Forgotten Server - a free and open-source MMORPG server emulator
  3. * Copyright (C) 2016 Mark Samman <mark.samman@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19.  
  20. #include "otpch.h"
  21.  
  22. #include "networkmessage.h"
  23.  
  24. #include "container.h"
  25. #include "creature.h"
  26. #include "player.h"
  27.  
  28. #include "position.h"
  29. #include "rsa.h"
  30.  
  31. std::string NetworkMessage::getString(uint16_t stringLen/* = 0*/)
  32. {
  33. if (stringLen == 0) {
  34. stringLen = get<uint16_t>();
  35. }
  36.  
  37. if (!canRead(stringLen)) {
  38. return std::string();
  39. }
  40.  
  41. char* v = reinterpret_cast<char*>(buffer) + position; //does not break strict aliasing
  42. position += stringLen;
  43. return std::string(v, stringLen);
  44. }
  45.  
  46. Position NetworkMessage::getPosition()
  47. {
  48. Position pos;
  49. pos.x = get<uint16_t>();
  50. pos.y = get<uint16_t>();
  51. pos.z = getByte();
  52. return pos;
  53. }
  54.  
  55. void NetworkMessage::addString(const std::string& value)
  56. {
  57. size_t stringLen = value.length();
  58. if (!canAdd(stringLen + 2) || stringLen > 8192) {
  59. return;
  60. }
  61.  
  62. add<uint16_t>(stringLen);
  63. memcpy(buffer + position, value.c_str(), stringLen);
  64. position += stringLen;
  65. length += stringLen;
  66. }
  67.  
  68. void NetworkMessage::addDouble(double value, uint8_t precision/* = 2*/)
  69. {
  70. addByte(precision);
  71. add<uint32_t>((value * std::pow(static_cast<float>(10), precision)) + std::numeric_limits<int32_t>::max());
  72. }
  73.  
  74. void NetworkMessage::addBytes(const char* bytes, size_t size)
  75. {
  76. if (!canAdd(size) || size > 8192) {
  77. return;
  78. }
  79.  
  80. memcpy(buffer + position, bytes, size);
  81. position += size;
  82. length += size;
  83. }
  84.  
  85. void NetworkMessage::addPaddingBytes(size_t n)
  86. {
  87. if (!canAdd(n)) {
  88. return;
  89. }
  90.  
  91. memset(buffer + position, 0x33, n);
  92. length += n;
  93. }
  94.  
  95. void NetworkMessage::addPosition(const Position& pos)
  96. {
  97. add<uint16_t>(pos.x);
  98. add<uint16_t>(pos.y);
  99. addByte(pos.z);
  100. }
  101.  
  102. void NetworkMessage::addItem(uint16_t id, uint8_t count)
  103. {
  104. const ItemType& it = Item::items[id];
  105.  
  106. add<uint16_t>(it.clientId);
  107.  
  108. addByte(0xFF); // MARK_UNMARKED
  109.  
  110. if (it.stackable) {
  111. addByte(count);
  112. } else if (it.isSplash() || it.isFluidContainer()) {
  113. addByte(fluidMap[count & 7]);
  114. }
  115.  
  116. if (it.isAnimation) {
  117. addByte(0xFE); // random phase (0xFF for async)
  118. }
  119. }
  120.  
  121. void NetworkMessage::addItem(const Item* item)
  122. {
  123. const ItemType& it = Item::items[item->getID()];
  124.  
  125. add<uint16_t>(it.clientId);
  126. addByte(0xFF); // MARK_UNMARKED
  127.  
  128. if (it.stackable) {
  129. addByte(std::min<uint16_t>(0xFF, item->getItemCount()));
  130. } else if (it.isSplash() || it.isFluidContainer()) {
  131. addByte(fluidMap[item->getFluidType() & 7]);
  132. }
  133.  
  134. if (it.isAnimation) {
  135. addByte(0xFE); // random phase (0xFF for async)
  136. }
  137. }
  138.  
  139. void NetworkMessage::addItemId(uint16_t itemId)
  140. {
  141. add<uint16_t>(Item::items[itemId].clientId);
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement