daily pastebin goal
64%
SHARE
TWEET

Untitled

Oskar1121 Sep 14th, 2018 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * The Forgotten Server - a free and open-source MMORPG server emulator
  3.  * Copyright (C) 2017  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.  
  27. std::string NetworkMessage::getString(uint16_t stringLen/* = 0*/)
  28. {
  29.     if (stringLen == 0) {
  30.         stringLen = get<uint16_t>();
  31.     }
  32.  
  33.     if (!canRead(stringLen)) {
  34.         return std::string();
  35.     }
  36.  
  37.     char* v = reinterpret_cast<char*>(buffer) + info.position; //does not break strict aliasing
  38.     info.position += stringLen;
  39.     return std::string(v, stringLen);
  40. }
  41.  
  42. Position NetworkMessage::getPosition()
  43. {
  44.     Position pos;
  45.     pos.x = get<uint16_t>();
  46.     pos.y = get<uint16_t>();
  47.     pos.z = getByte();
  48.     return pos;
  49. }
  50.  
  51. void NetworkMessage::addString(const std::string& value)
  52. {
  53.     size_t stringLen = value.length();
  54.     if (!canAdd(stringLen + 2) || stringLen > 8192) {
  55.         return;
  56.     }
  57.  
  58.     add<uint16_t>(stringLen);
  59.     memcpy(buffer + info.position, value.c_str(), stringLen);
  60.     info.position += stringLen;
  61.     info.length += stringLen;
  62. }
  63.  
  64. void NetworkMessage::addDouble(double value, uint8_t precision/* = 2*/)
  65. {
  66.     addByte(precision);
  67.     add<uint32_t>((value * std::pow(static_cast<float>(10), precision)) + std::numeric_limits<int32_t>::max());
  68. }
  69.  
  70. void NetworkMessage::addBytes(const char* bytes, size_t size)
  71. {
  72.     if (!canAdd(size) || size > 8192) {
  73.         return;
  74.     }
  75.  
  76.     memcpy(buffer + info.position, bytes, size);
  77.     info.position += size;
  78.     info.length += size;
  79. }
  80.  
  81. void NetworkMessage::addPaddingBytes(size_t n)
  82. {
  83.     if (!canAdd(n)) {
  84.         return;
  85.     }
  86.  
  87.     memset(buffer + info.position, 0x33, n);
  88.     info.length += n;
  89. }
  90.  
  91. void NetworkMessage::addPosition(const Position& pos)
  92. {
  93.     add<uint16_t>(pos.x);
  94.     add<uint16_t>(pos.y);
  95.     addByte(pos.z);
  96. }
  97.  
  98. void NetworkMessage::addItem(uint32_t id, uint8_t count)
  99. {
  100.     const ItemType& it = Item::items[id];
  101.  
  102.     add<uint32_t>(it.clientId);
  103.  
  104.     addByte(0xFF); // MARK_UNMARKED
  105.  
  106.     if (it.stackable) {
  107.         addByte(count);
  108.     } else if (it.isSplash() || it.isFluidContainer()) {
  109.         addByte(fluidMap[count & 7]);
  110.     }
  111.  
  112.     if (it.isAnimation) {
  113.         addByte(0xFE); // random phase (0xFF for async)
  114.     }
  115. }
  116.  
  117. void NetworkMessage::addItem(const Item* item)
  118. {
  119.     const ItemType& it = Item::items[item->getID()];
  120.  
  121.     add<uint32_t>(it.clientId);
  122.     addByte(0xFF); // MARK_UNMARKED
  123.  
  124.     if (it.stackable) {
  125.         addByte(std::min<uint16_t>(0xFF, item->getItemCount()));
  126.     } else if (it.isSplash() || it.isFluidContainer()) {
  127.         addByte(fluidMap[item->getFluidType() & 7]);
  128.     }
  129.  
  130.     if (it.isAnimation) {
  131.         addByte(0xFE); // random phase (0xFF for async)
  132.     }
  133. }
  134.  
  135. void NetworkMessage::addItemId(uint32_t itemId)
  136. {
  137.     add<uint32_t>(Item::items[itemId].clientId);
  138. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top