mirror of
https://github.com/mangosfour/server.git
synced 2025-12-21 10:37:06 +00:00
[7999] Added appendPackXYZ to ByteBuffer class.
This commit is contained in:
parent
d50916a6e0
commit
90d4d70f0c
2 changed files with 72 additions and 34 deletions
|
|
@ -34,11 +34,13 @@ class ByteBuffer
|
|||
{
|
||||
_storage.reserve(DEFAULT_SIZE);
|
||||
}
|
||||
|
||||
// constructor
|
||||
ByteBuffer(size_t res): _rpos(0), _wpos(0)
|
||||
{
|
||||
_storage.reserve(res);
|
||||
}
|
||||
|
||||
// copy constructor
|
||||
ByteBuffer(const ByteBuffer &buf): _rpos(buf._rpos), _wpos(buf._wpos), _storage(buf._storage) { }
|
||||
|
||||
|
|
@ -65,16 +67,19 @@ class ByteBuffer
|
|||
append<uint8>(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer &operator<<(uint16 value)
|
||||
{
|
||||
append<uint16>(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer &operator<<(uint32 value)
|
||||
{
|
||||
append<uint32>(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer &operator<<(uint64 value)
|
||||
{
|
||||
append<uint64>(value);
|
||||
|
|
@ -87,16 +92,19 @@ class ByteBuffer
|
|||
append<int8>(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer &operator<<(int16 value)
|
||||
{
|
||||
append<int16>(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer &operator<<(int32 value)
|
||||
{
|
||||
append<int32>(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer &operator<<(int64 value)
|
||||
{
|
||||
append<int64>(value);
|
||||
|
|
@ -109,17 +117,20 @@ class ByteBuffer
|
|||
append<float>(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer &operator<<(double value)
|
||||
{
|
||||
append<double>(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer &operator<<(const std::string &value)
|
||||
{
|
||||
append((uint8 const *)value.c_str(), value.length());
|
||||
append((uint8)0);
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer &operator<<(const char *str)
|
||||
{
|
||||
append((uint8 const *)str, str ? strlen(str) : 0);
|
||||
|
|
@ -138,16 +149,19 @@ class ByteBuffer
|
|||
value = read<uint8>();
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer &operator>>(uint16 &value)
|
||||
{
|
||||
value = read<uint16>();
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer &operator>>(uint32 &value)
|
||||
{
|
||||
value = read<uint32>();
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer &operator>>(uint64 &value)
|
||||
{
|
||||
value = read<uint64>();
|
||||
|
|
@ -160,16 +174,19 @@ class ByteBuffer
|
|||
value = read<int8>();
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer &operator>>(int16 &value)
|
||||
{
|
||||
value = read<int16>();
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer &operator>>(int32 &value)
|
||||
{
|
||||
value = read<int32>();
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer &operator>>(int64 &value)
|
||||
{
|
||||
value = read<int64>();
|
||||
|
|
@ -181,11 +198,13 @@ class ByteBuffer
|
|||
value = read<float>();
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer &operator>>(double &value)
|
||||
{
|
||||
value = read<double>();
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer &operator>>(std::string& value)
|
||||
{
|
||||
value.clear();
|
||||
|
|
@ -210,7 +229,7 @@ class ByteBuffer
|
|||
{
|
||||
_rpos = rpos_;
|
||||
return _rpos;
|
||||
};
|
||||
}
|
||||
|
||||
size_t wpos() const { return _wpos; }
|
||||
|
||||
|
|
@ -225,7 +244,8 @@ class ByteBuffer
|
|||
T r = read<T>(_rpos);
|
||||
_rpos += sizeof(T);
|
||||
return r;
|
||||
};
|
||||
}
|
||||
|
||||
template <typename T> T read(size_t pos) const
|
||||
{
|
||||
ASSERT(pos + sizeof(T) <= size() || PrintPosError(false, pos, sizeof(T)));
|
||||
|
|
@ -251,7 +271,7 @@ class ByteBuffer
|
|||
uint8 guidmark = 0;
|
||||
(*this) >> guidmark;
|
||||
|
||||
for(int i=0;i<8;i++)
|
||||
for(int i = 0; i < 8; ++i)
|
||||
{
|
||||
if(guidmark & (uint8(1) << i))
|
||||
{
|
||||
|
|
@ -277,28 +297,33 @@ class ByteBuffer
|
|||
_storage.resize(newsize);
|
||||
_rpos = 0;
|
||||
_wpos = size();
|
||||
};
|
||||
}
|
||||
|
||||
void reserve(size_t ressize)
|
||||
{
|
||||
if (ressize > size()) _storage.reserve(ressize);
|
||||
};
|
||||
if (ressize > size())
|
||||
_storage.reserve(ressize);
|
||||
}
|
||||
|
||||
void append(const std::string& str)
|
||||
{
|
||||
append((uint8 const*)str.c_str(), str.size() + 1);
|
||||
}
|
||||
|
||||
void append(const char *src, size_t cnt)
|
||||
{
|
||||
return append((const uint8 *)src, cnt);
|
||||
}
|
||||
template<class T>
|
||||
void append(const T *src, size_t cnt)
|
||||
|
||||
template<class T> void append(const T *src, size_t cnt)
|
||||
{
|
||||
return append((const uint8 *)src, cnt * sizeof(T));
|
||||
}
|
||||
|
||||
void append(const uint8 *src, size_t cnt)
|
||||
{
|
||||
if (!cnt) return;
|
||||
if (!cnt)
|
||||
return;
|
||||
|
||||
ASSERT(size() < 10000000);
|
||||
|
||||
|
|
@ -307,9 +332,21 @@ class ByteBuffer
|
|||
memcpy(&_storage[_wpos], src, cnt);
|
||||
_wpos += cnt;
|
||||
}
|
||||
|
||||
void append(const ByteBuffer& buffer)
|
||||
{
|
||||
if(buffer.wpos()) append(buffer.contents(),buffer.wpos());
|
||||
if(buffer.wpos())
|
||||
append(buffer.contents(), buffer.wpos());
|
||||
}
|
||||
|
||||
// can be used in SMSG_MONSTER_MOVE opcode
|
||||
void appendPackXYZ(float x, float y, float z)
|
||||
{
|
||||
uint32 packed = 0;
|
||||
packed |= ((int)(x / 0.25f) & 0x7FF);
|
||||
packed |= ((int)(y / 0.25f) & 0x7FF) << 11;
|
||||
packed |= ((int)(z / 0.25f) & 0x3FF) << 22;
|
||||
*this << packed;
|
||||
}
|
||||
|
||||
void appendPackGUID(uint64 guid)
|
||||
|
|
@ -319,7 +356,7 @@ class ByteBuffer
|
|||
|
||||
size_t mask_position = wpos();
|
||||
*this << uint8(0);
|
||||
for(uint8 i = 0; i < 8; i++)
|
||||
for(uint8 i = 0; i < 8; ++i)
|
||||
{
|
||||
if(guid & 0xFF)
|
||||
{
|
||||
|
|
@ -336,13 +373,14 @@ class ByteBuffer
|
|||
ASSERT(pos + cnt <= size() || PrintPosError(true, pos, cnt));
|
||||
memcpy(&_storage[pos], src, cnt);
|
||||
}
|
||||
|
||||
void print_storage() const
|
||||
{
|
||||
if(!sLog.IsOutDebug()) // optimize disabled debug output
|
||||
return;
|
||||
|
||||
sLog.outDebug("STORAGE_SIZE: %lu", (unsigned long)size() );
|
||||
for(uint32 i = 0; i < size(); i++)
|
||||
for(uint32 i = 0; i < size(); ++i)
|
||||
sLog.outDebugInLine("%u - ", read<uint8>(i) );
|
||||
sLog.outDebug(" ");
|
||||
}
|
||||
|
|
@ -353,7 +391,7 @@ class ByteBuffer
|
|||
return;
|
||||
|
||||
sLog.outDebug("STORAGE_SIZE: %lu", (unsigned long)size() );
|
||||
for(uint32 i = 0; i < size(); i++)
|
||||
for(uint32 i = 0; i < size(); ++i)
|
||||
sLog.outDebugInLine("%c", read<uint8>(i) );
|
||||
sLog.outDebug(" ");
|
||||
}
|
||||
|
|
@ -369,7 +407,7 @@ class ByteBuffer
|
|||
if(sLog.IsIncludeTime())
|
||||
sLog.outDebugInLine(" ");
|
||||
|
||||
for(uint32 i = 0; i < size(); i++)
|
||||
for(uint32 i = 0; i < size(); ++i)
|
||||
{
|
||||
if ((i == (j * 8)) && ((i != (k * 16))))
|
||||
{
|
||||
|
|
@ -436,7 +474,7 @@ class ByteBuffer
|
|||
template <typename T> ByteBuffer &operator<<(ByteBuffer &b, std::vector<T> v)
|
||||
{
|
||||
b << (uint32)v.size();
|
||||
for (typename std::vector<T>::iterator i = v.begin(); i != v.end(); i++)
|
||||
for (typename std::vector<T>::iterator i = v.begin(); i != v.end(); ++i)
|
||||
{
|
||||
b << *i;
|
||||
}
|
||||
|
|
@ -460,7 +498,7 @@ template <typename T> ByteBuffer &operator>>(ByteBuffer &b, std::vector<T> &v)
|
|||
template <typename T> ByteBuffer &operator<<(ByteBuffer &b, std::list<T> v)
|
||||
{
|
||||
b << (uint32)v.size();
|
||||
for (typename std::list<T>::iterator i = v.begin(); i != v.end(); i++)
|
||||
for (typename std::list<T>::iterator i = v.begin(); i != v.end(); ++i)
|
||||
{
|
||||
b << *i;
|
||||
}
|
||||
|
|
@ -484,7 +522,7 @@ template <typename T> ByteBuffer &operator>>(ByteBuffer &b, std::list<T> &v)
|
|||
template <typename K, typename V> ByteBuffer &operator<<(ByteBuffer &b, std::map<K, V> &m)
|
||||
{
|
||||
b << (uint32)m.size();
|
||||
for (typename std::map<K, V>::iterator i = m.begin(); i != m.end(); i++)
|
||||
for (typename std::map<K, V>::iterator i = m.begin(); i != m.end(); ++i)
|
||||
{
|
||||
b << i->first << i->second;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
#ifndef __REVISION_NR_H__
|
||||
#define __REVISION_NR_H__
|
||||
#define REVISION_NR "7998"
|
||||
#define REVISION_NR "7999"
|
||||
#endif // __REVISION_NR_H__
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue