aboutsummaryrefslogtreecommitdiff
path: root/src/shared/ByteBuffer.h
diff options
context:
space:
mode:
authormegamage <none@none>2009-06-13 09:44:35 -0500
committermegamage <none@none>2009-06-13 09:44:35 -0500
commite3b748206e9f0f4c8998ac85a39e32cf1d8580b1 (patch)
tree420228128361fb1bd0e241efe0ae5d931f6eb8bb /src/shared/ByteBuffer.h
parent599595ad1060c8b7d1ae8186ffb86e567e7eee46 (diff)
[8001] Fixed display id checks for creature case. Author: VladimirMangos
--HG-- branch : trunk
Diffstat (limited to 'src/shared/ByteBuffer.h')
-rw-r--r--src/shared/ByteBuffer.h104
1 files changed, 71 insertions, 33 deletions
diff --git a/src/shared/ByteBuffer.h b/src/shared/ByteBuffer.h
index 7ff6d32fd12..75c33dcf3ca 100644
--- a/src/shared/ByteBuffer.h
+++ b/src/shared/ByteBuffer.h
@@ -36,11 +36,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) { }
@@ -67,16 +69,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);
@@ -89,16 +94,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);
@@ -111,17 +119,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);
@@ -140,16 +151,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>();
@@ -162,16 +176,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>();
@@ -183,20 +200,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;
}
@@ -212,7 +231,7 @@ class ByteBuffer
{
_rpos = rpos_;
return _rpos;
- };
+ }
size_t wpos() const { return _wpos; }
@@ -224,13 +243,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;
@@ -238,31 +258,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));
}
}
@@ -279,28 +299,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);
@@ -309,9 +334,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)
@@ -321,11 +358,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);
}
@@ -335,16 +372,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(" ");
}
@@ -355,7 +393,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(" ");
}
@@ -368,9 +406,9 @@ class ByteBuffer
uint32 j = 1, k = 1;
sLog.outDebug("STORAGE_SIZE: %lu", (unsigned long)size() );
- 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)
{
@@ -382,7 +420,7 @@ class ByteBuffer
}
++j;
}
- else if (i == (k*16))
+ else if (i == (k * 16))
{
if (read<uint8>(i) < 0x10)
{
@@ -431,7 +469,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;
}
@@ -455,7 +493,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;
}
@@ -479,7 +517,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;
}