Files
TCPlayerbotCore/src/game/UpdateData.cpp
T

158 lines
4.5 KiB
C++
Raw Normal View History

2008-10-02 16:23:55 -05:00
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
2008-10-14 11:57:03 -05:00
*
2010-01-16 20:19:18 +03:00
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/>
2008-10-02 16:23:55 -05:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2008-11-10 17:19:35 -06:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2008-10-02 16:23:55 -05:00
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
2008-11-10 17:19:35 -06:00
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2008-10-02 16:23:55 -05:00
*/
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
#include "Common.h"
#include "ByteBuffer.h"
#include "WorldPacket.h"
#include "UpdateData.h"
#include "Log.h"
#include "Opcodes.h"
#include "World.h"
#include <zlib/zlib.h>
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
UpdateData::UpdateData() : m_blockCount(0)
{
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void UpdateData::AddOutOfRangeGUID(std::set<uint64>& guids)
{
m_outOfRangeGUIDs.insert(guids.begin(),guids.end());
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void UpdateData::AddOutOfRangeGUID(const uint64 &guid)
{
m_outOfRangeGUIDs.insert(guid);
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void UpdateData::AddUpdateBlock(const ByteBuffer &block)
{
m_data.append(block);
++m_blockCount;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void UpdateData::Compress(void* dst, uint32 *dst_size, void* src, int src_size)
{
z_stream c_stream;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
c_stream.zalloc = (alloc_func)0;
c_stream.zfree = (free_func)0;
c_stream.opaque = (voidpf)0;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// default Z_BEST_SPEED (1)
int z_res = deflateInit(&c_stream, sWorld.getConfig(CONFIG_COMPRESSION));
if (z_res != Z_OK)
{
sLog.outError("Can't compress update packet (zlib: deflateInit) Error code: %i (%s)",z_res,zError(z_res));
*dst_size = 0;
return;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
c_stream.next_out = (Bytef*)dst;
c_stream.avail_out = *dst_size;
c_stream.next_in = (Bytef*)src;
c_stream.avail_in = (uInt)src_size;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
z_res = deflate(&c_stream, Z_NO_FLUSH);
if (z_res != Z_OK)
{
sLog.outError("Can't compress update packet (zlib: deflate) Error code: %i (%s)",z_res,zError(z_res));
*dst_size = 0;
return;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
if (c_stream.avail_in != 0)
{
sLog.outError("Can't compress update packet (zlib: deflate not greedy)");
*dst_size = 0;
return;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
z_res = deflate(&c_stream, Z_FINISH);
if (z_res != Z_STREAM_END)
{
sLog.outError("Can't compress update packet (zlib: deflate should report Z_STREAM_END instead %i (%s)",z_res,zError(z_res));
*dst_size = 0;
return;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
z_res = deflateEnd(&c_stream);
if (z_res != Z_OK)
{
sLog.outError("Can't compress update packet (zlib: deflateEnd) Error code: %i (%s)",z_res,zError(z_res));
*dst_size = 0;
return;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
*dst_size = c_stream.total_out;
}
2009-10-17 15:51:44 -07:00
2009-05-31 15:56:51 -05:00
bool UpdateData::BuildPacket(WorldPacket *packet)
2008-10-02 16:23:55 -05:00
{
2009-06-11 00:45:59 -05:00
ASSERT(packet->empty()); // shouldn't happen
2009-10-17 15:51:44 -07:00
2009-06-11 00:45:59 -05:00
ByteBuffer buf(4 + (m_outOfRangeGUIDs.empty() ? 0 : 1 + 4 + 9 * m_outOfRangeGUIDs.size()) + m_data.wpos());
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
buf << (uint32) (!m_outOfRangeGUIDs.empty() ? m_blockCount + 1 : m_blockCount);
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if (!m_outOfRangeGUIDs.empty())
2008-10-02 16:23:55 -05:00
{
buf << (uint8) UPDATETYPE_OUT_OF_RANGE_OBJECTS;
buf << (uint32) m_outOfRangeGUIDs.size();
2009-10-17 15:51:44 -07:00
2009-10-17 16:20:24 -07:00
for (std::set<uint64>::const_iterator i = m_outOfRangeGUIDs.begin(); i != m_outOfRangeGUIDs.end(); ++i)
2008-10-02 16:23:55 -05:00
{
2009-06-05 17:44:27 -05:00
buf.appendPackGUID(*i);
2008-10-02 16:23:55 -05:00
}
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
buf.append(m_data);
2009-10-17 15:51:44 -07:00
size_t pSize = buf.wpos(); // use real used data size
2009-10-17 15:51:44 -07:00
2010-04-07 22:59:46 +02:00
if (pSize > 100) // compress large packets
2008-10-02 16:23:55 -05:00
{
uint32 destsize = compressBound(pSize);
2010-04-07 22:59:46 +02:00
packet->resize(destsize + sizeof(uint32));
2009-10-17 15:51:44 -07:00
2009-05-31 15:56:51 -05:00
packet->put<uint32>(0, pSize);
Compress(const_cast<uint8*>(packet->contents()) + sizeof(uint32), &destsize, (void*)buf.contents(), pSize);
2008-10-02 16:23:55 -05:00
if (destsize == 0)
return false;
2009-10-17 15:51:44 -07:00
2010-04-07 22:59:46 +02:00
packet->resize(destsize + sizeof(uint32));
packet->SetOpcode(SMSG_COMPRESSED_UPDATE_OBJECT);
2008-10-02 16:23:55 -05:00
}
2009-05-31 15:56:51 -05:00
else // send small packets without compression
2008-10-02 16:23:55 -05:00
{
2010-04-07 22:59:46 +02:00
packet->append(buf);
packet->SetOpcode(SMSG_UPDATE_OBJECT);
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
return true;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void UpdateData::Clear()
{
m_data.clear();
m_outOfRangeGUIDs.clear();
m_blockCount = 0;
}
2009-02-17 20:07:49 -06:00