[7999] Added appendPackXYZ to ByteBuffer class.

This commit is contained in:
tomrus88 2009-06-12 16:11:16 +04:00
parent d50916a6e0
commit 90d4d70f0c
2 changed files with 72 additions and 34 deletions

View file

@ -34,11 +34,13 @@ class ByteBuffer
{ {
_storage.reserve(DEFAULT_SIZE); _storage.reserve(DEFAULT_SIZE);
} }
// constructor // constructor
ByteBuffer(size_t res): _rpos(0), _wpos(0) ByteBuffer(size_t res): _rpos(0), _wpos(0)
{ {
_storage.reserve(res); _storage.reserve(res);
} }
// copy constructor // copy constructor
ByteBuffer(const ByteBuffer &buf): _rpos(buf._rpos), _wpos(buf._wpos), _storage(buf._storage) { } ByteBuffer(const ByteBuffer &buf): _rpos(buf._rpos), _wpos(buf._wpos), _storage(buf._storage) { }
@ -65,16 +67,19 @@ class ByteBuffer
append<uint8>(value); append<uint8>(value);
return *this; return *this;
} }
ByteBuffer &operator<<(uint16 value) ByteBuffer &operator<<(uint16 value)
{ {
append<uint16>(value); append<uint16>(value);
return *this; return *this;
} }
ByteBuffer &operator<<(uint32 value) ByteBuffer &operator<<(uint32 value)
{ {
append<uint32>(value); append<uint32>(value);
return *this; return *this;
} }
ByteBuffer &operator<<(uint64 value) ByteBuffer &operator<<(uint64 value)
{ {
append<uint64>(value); append<uint64>(value);
@ -87,16 +92,19 @@ class ByteBuffer
append<int8>(value); append<int8>(value);
return *this; return *this;
} }
ByteBuffer &operator<<(int16 value) ByteBuffer &operator<<(int16 value)
{ {
append<int16>(value); append<int16>(value);
return *this; return *this;
} }
ByteBuffer &operator<<(int32 value) ByteBuffer &operator<<(int32 value)
{ {
append<int32>(value); append<int32>(value);
return *this; return *this;
} }
ByteBuffer &operator<<(int64 value) ByteBuffer &operator<<(int64 value)
{ {
append<int64>(value); append<int64>(value);
@ -109,17 +117,20 @@ class ByteBuffer
append<float>(value); append<float>(value);
return *this; return *this;
} }
ByteBuffer &operator<<(double value) ByteBuffer &operator<<(double value)
{ {
append<double>(value); append<double>(value);
return *this; return *this;
} }
ByteBuffer &operator<<(const std::string &value) ByteBuffer &operator<<(const std::string &value)
{ {
append((uint8 const *)value.c_str(), value.length()); append((uint8 const *)value.c_str(), value.length());
append((uint8)0); append((uint8)0);
return *this; return *this;
} }
ByteBuffer &operator<<(const char *str) ByteBuffer &operator<<(const char *str)
{ {
append((uint8 const *)str, str ? strlen(str) : 0); append((uint8 const *)str, str ? strlen(str) : 0);
@ -138,16 +149,19 @@ class ByteBuffer
value = read<uint8>(); value = read<uint8>();
return *this; return *this;
} }
ByteBuffer &operator>>(uint16 &value) ByteBuffer &operator>>(uint16 &value)
{ {
value = read<uint16>(); value = read<uint16>();
return *this; return *this;
} }
ByteBuffer &operator>>(uint32 &value) ByteBuffer &operator>>(uint32 &value)
{ {
value = read<uint32>(); value = read<uint32>();
return *this; return *this;
} }
ByteBuffer &operator>>(uint64 &value) ByteBuffer &operator>>(uint64 &value)
{ {
value = read<uint64>(); value = read<uint64>();
@ -160,16 +174,19 @@ class ByteBuffer
value = read<int8>(); value = read<int8>();
return *this; return *this;
} }
ByteBuffer &operator>>(int16 &value) ByteBuffer &operator>>(int16 &value)
{ {
value = read<int16>(); value = read<int16>();
return *this; return *this;
} }
ByteBuffer &operator>>(int32 &value) ByteBuffer &operator>>(int32 &value)
{ {
value = read<int32>(); value = read<int32>();
return *this; return *this;
} }
ByteBuffer &operator>>(int64 &value) ByteBuffer &operator>>(int64 &value)
{ {
value = read<int64>(); value = read<int64>();
@ -181,20 +198,22 @@ class ByteBuffer
value = read<float>(); value = read<float>();
return *this; return *this;
} }
ByteBuffer &operator>>(double &value) ByteBuffer &operator>>(double &value)
{ {
value = read<double>(); value = read<double>();
return *this; return *this;
} }
ByteBuffer &operator>>(std::string& value) ByteBuffer &operator>>(std::string& value)
{ {
value.clear(); value.clear();
while (rpos() < size()) // prevent crash at wrong string format in packet while (rpos() < size()) // prevent crash at wrong string format in packet
{ {
char c=read<char>(); char c = read<char>();
if (c==0) if (c == 0)
break; break;
value+=c; value += c;
} }
return *this; return *this;
} }
@ -210,7 +229,7 @@ class ByteBuffer
{ {
_rpos = rpos_; _rpos = rpos_;
return _rpos; return _rpos;
}; }
size_t wpos() const { return _wpos; } size_t wpos() const { return _wpos; }
@ -222,13 +241,14 @@ class ByteBuffer
template <typename T> T read() template <typename T> T read()
{ {
T r=read<T>(_rpos); T r = read<T>(_rpos);
_rpos += sizeof(T); _rpos += sizeof(T);
return r; return r;
}; }
template <typename T> T read(size_t pos) const 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]); T val = *((T const*)&_storage[pos]);
EndianConvert(val); EndianConvert(val);
return val; return val;
@ -236,31 +256,31 @@ class ByteBuffer
void read(uint8 *dest, size_t len) 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); memcpy(dest, &_storage[_rpos], len);
_rpos += len; _rpos += len;
} }
bool readPackGUID(uint64& guid) bool readPackGUID(uint64& guid)
{ {
if(rpos()+1 > size()) if(rpos() + 1 > size())
return false; return false;
guid = 0; guid = 0;
uint8 guidmark=0; uint8 guidmark = 0;
(*this) >> guidmark; (*this) >> guidmark;
for(int i=0;i<8;i++) for(int i = 0; i < 8; ++i)
{ {
if(guidmark & (uint8(1) << i)) if(guidmark & (uint8(1) << i))
{ {
if(rpos()+1 > size()) if(rpos() + 1 > size())
return false; return false;
uint8 bit; uint8 bit;
(*this) >> bit; (*this) >> bit;
guid |= (uint64(bit) << (i*8)); guid |= (uint64(bit) << (i * 8));
} }
} }
@ -277,28 +297,33 @@ class ByteBuffer
_storage.resize(newsize); _storage.resize(newsize);
_rpos = 0; _rpos = 0;
_wpos = size(); _wpos = size();
}; }
void reserve(size_t ressize) void reserve(size_t ressize)
{ {
if (ressize > size()) _storage.reserve(ressize); if (ressize > size())
}; _storage.reserve(ressize);
}
void append(const std::string& str) 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) void append(const char *src, size_t cnt)
{ {
return append((const uint8 *)src, 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) void append(const uint8 *src, size_t cnt)
{ {
if (!cnt) return; if (!cnt)
return;
ASSERT(size() < 10000000); ASSERT(size() < 10000000);
@ -307,9 +332,21 @@ class ByteBuffer
memcpy(&_storage[_wpos], src, cnt); memcpy(&_storage[_wpos], src, cnt);
_wpos += cnt; _wpos += cnt;
} }
void append(const ByteBuffer& buffer) 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) void appendPackGUID(uint64 guid)
@ -319,11 +356,11 @@ class ByteBuffer
size_t mask_position = wpos(); size_t mask_position = wpos();
*this << uint8(0); *this << uint8(0);
for(uint8 i = 0; i < 8; i++) for(uint8 i = 0; i < 8; ++i)
{ {
if(guid & 0xFF) if(guid & 0xFF)
{ {
_storage[mask_position] |= uint8(1<<i); _storage[mask_position] |= uint8(1 << i);
*this << uint8(guid & 0xFF); *this << uint8(guid & 0xFF);
} }
@ -333,16 +370,17 @@ class ByteBuffer
void put(size_t pos, const uint8 *src, size_t cnt) 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); memcpy(&_storage[pos], src, cnt);
} }
void print_storage() const void print_storage() const
{ {
if(!sLog.IsOutDebug()) // optimize disabled debug output if(!sLog.IsOutDebug()) // optimize disabled debug output
return; return;
sLog.outDebug("STORAGE_SIZE: %lu", (unsigned long)size() ); 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.outDebugInLine("%u - ", read<uint8>(i) );
sLog.outDebug(" "); sLog.outDebug(" ");
} }
@ -353,7 +391,7 @@ class ByteBuffer
return; return;
sLog.outDebug("STORAGE_SIZE: %lu", (unsigned long)size() ); 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.outDebugInLine("%c", read<uint8>(i) );
sLog.outDebug(" "); sLog.outDebug(" ");
} }
@ -369,9 +407,9 @@ class ByteBuffer
if(sLog.IsIncludeTime()) if(sLog.IsIncludeTime())
sLog.outDebugInLine(" "); 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) if (read<uint8>(i) < 0x10)
{ {
@ -383,7 +421,7 @@ class ByteBuffer
} }
++j; ++j;
} }
else if (i == (k*16)) else if (i == (k * 16))
{ {
if (read<uint8>(i) < 0x10) if (read<uint8>(i) < 0x10)
{ {
@ -436,7 +474,7 @@ class ByteBuffer
template <typename T> ByteBuffer &operator<<(ByteBuffer &b, std::vector<T> v) template <typename T> ByteBuffer &operator<<(ByteBuffer &b, std::vector<T> v)
{ {
b << (uint32)v.size(); 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; 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) template <typename T> ByteBuffer &operator<<(ByteBuffer &b, std::list<T> v)
{ {
b << (uint32)v.size(); 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; 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) template <typename K, typename V> ByteBuffer &operator<<(ByteBuffer &b, std::map<K, V> &m)
{ {
b << (uint32)m.size(); 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; b << i->first << i->second;
} }

View file

@ -1,4 +1,4 @@
#ifndef __REVISION_NR_H__ #ifndef __REVISION_NR_H__
#define __REVISION_NR_H__ #define __REVISION_NR_H__
#define REVISION_NR "7998" #define REVISION_NR "7999"
#endif // __REVISION_NR_H__ #endif // __REVISION_NR_H__