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,20 +198,22 @@ class ByteBuffer
|
|||
value = read<float>();
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer &operator>>(double &value)
|
||||
{
|
||||
value = read<double>();
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer &operator>>(std::string& value)
|
||||
{
|
||||
value.clear();
|
||||
while (rpos() < size()) // prevent crash at wrong string format in packet
|
||||
{
|
||||
char c=read<char>();
|
||||
if (c==0)
|
||||
char c = read<char>();
|
||||
if (c == 0)
|
||||
break;
|
||||
value+=c;
|
||||
value += c;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
|
@ -210,7 +229,7 @@ class ByteBuffer
|
|||
{
|
||||
_rpos = rpos_;
|
||||
return _rpos;
|
||||
};
|
||||
}
|
||||
|
||||
size_t wpos() const { return _wpos; }
|
||||
|
||||
|
|
@ -222,13 +241,14 @@ class ByteBuffer
|
|||
|
||||
template <typename T> T read()
|
||||
{
|
||||
T r=read<T>(_rpos);
|
||||
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)));
|
||||
ASSERT(pos + sizeof(T) <= size() || PrintPosError(false, pos, sizeof(T)));
|
||||
T val = *((T const*)&_storage[pos]);
|
||||
EndianConvert(val);
|
||||
return val;
|
||||
|
|
@ -236,31 +256,31 @@ class ByteBuffer
|
|||
|
||||
void read(uint8 *dest, size_t len)
|
||||
{
|
||||
ASSERT(_rpos + len <= size() || PrintPosError(false,_rpos,len));
|
||||
ASSERT(_rpos + len <= size() || PrintPosError(false, _rpos, len));
|
||||
memcpy(dest, &_storage[_rpos], len);
|
||||
_rpos += len;
|
||||
}
|
||||
|
||||
bool readPackGUID(uint64& guid)
|
||||
{
|
||||
if(rpos()+1 > size())
|
||||
if(rpos() + 1 > size())
|
||||
return false;
|
||||
|
||||
guid = 0;
|
||||
|
||||
uint8 guidmark=0;
|
||||
uint8 guidmark = 0;
|
||||
(*this) >> guidmark;
|
||||
|
||||
for(int i=0;i<8;i++)
|
||||
for(int i = 0; i < 8; ++i)
|
||||
{
|
||||
if(guidmark & (uint8(1) << i))
|
||||
{
|
||||
if(rpos()+1 > size())
|
||||
if(rpos() + 1 > size())
|
||||
return false;
|
||||
|
||||
uint8 bit;
|
||||
(*this) >> bit;
|
||||
guid |= (uint64(bit) << (i*8));
|
||||
guid |= (uint64(bit) << (i * 8));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -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);
|
||||
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));
|
||||
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,11 +356,11 @@ 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)
|
||||
{
|
||||
_storage[mask_position] |= uint8(1<<i);
|
||||
_storage[mask_position] |= uint8(1 << i);
|
||||
*this << uint8(guid & 0xFF);
|
||||
}
|
||||
|
||||
|
|
@ -333,16 +370,17 @@ class ByteBuffer
|
|||
|
||||
void put(size_t pos, const uint8 *src, size_t cnt)
|
||||
{
|
||||
ASSERT(pos + cnt <= size() || PrintPosError(true,pos,cnt));
|
||||
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,9 +407,9 @@ 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))))
|
||||
if ((i == (j * 8)) && ((i != (k * 16))))
|
||||
{
|
||||
if (read<uint8>(i) < 0x10)
|
||||
{
|
||||
|
|
@ -383,7 +421,7 @@ class ByteBuffer
|
|||
}
|
||||
++j;
|
||||
}
|
||||
else if (i == (k*16))
|
||||
else if (i == (k * 16))
|
||||
{
|
||||
if (read<uint8>(i) < 0x10)
|
||||
{
|
||||
|
|
@ -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