| 1 | /* |
|---|
| 2 | * Copyright (C) 2008 Trinity <http://www.trinitycore.org/> |
|---|
| 3 | * |
|---|
| 4 | * Thanks to the original authors: MaNGOS <http://www.mangosproject.org/> |
|---|
| 5 | * |
|---|
| 6 | * This program is free software; you can redistribute it and/or modify |
|---|
| 7 | * it under the terms of the GNU General Public License as published by |
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 9 | * (at your option) any later version. |
|---|
| 10 | * |
|---|
| 11 | * This program is distributed in the hope that it will be useful, |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | * GNU General Public License for more details. |
|---|
| 15 | * |
|---|
| 16 | * You should have received a copy of the GNU General Public License |
|---|
| 17 | * along with this program; if not, write to the Free Software |
|---|
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | #include "Common.h" |
|---|
| 22 | #include "ByteBuffer.h" |
|---|
| 23 | #include "WorldPacket.h" |
|---|
| 24 | #include "UpdateData.h" |
|---|
| 25 | #include "Log.h" |
|---|
| 26 | #include "Opcodes.h" |
|---|
| 27 | #include "World.h" |
|---|
| 28 | #include <zlib/zlib.h> |
|---|
| 29 | |
|---|
| 30 | UpdateData::UpdateData() : m_blockCount(0) |
|---|
| 31 | { |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | void UpdateData::AddOutOfRangeGUID(std::set<uint64>& guids) |
|---|
| 35 | { |
|---|
| 36 | m_outOfRangeGUIDs.insert(guids.begin(),guids.end()); |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | void UpdateData::AddOutOfRangeGUID(const uint64 &guid) |
|---|
| 40 | { |
|---|
| 41 | m_outOfRangeGUIDs.insert(guid); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | void UpdateData::AddUpdateBlock(const ByteBuffer &block) |
|---|
| 45 | { |
|---|
| 46 | m_data.append(block); |
|---|
| 47 | ++m_blockCount; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | void UpdateData::Compress(void* dst, uint32 *dst_size, void* src, int src_size) |
|---|
| 51 | { |
|---|
| 52 | z_stream c_stream; |
|---|
| 53 | |
|---|
| 54 | c_stream.zalloc = (alloc_func)0; |
|---|
| 55 | c_stream.zfree = (free_func)0; |
|---|
| 56 | c_stream.opaque = (voidpf)0; |
|---|
| 57 | |
|---|
| 58 | // default Z_BEST_SPEED (1) |
|---|
| 59 | int z_res = deflateInit(&c_stream, sWorld.getConfig(CONFIG_COMPRESSION)); |
|---|
| 60 | if (z_res != Z_OK) |
|---|
| 61 | { |
|---|
| 62 | sLog.outError("Can't compress update packet (zlib: deflateInit) Error code: %i (%s)",z_res,zError(z_res)); |
|---|
| 63 | *dst_size = 0; |
|---|
| 64 | return; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | c_stream.next_out = (Bytef*)dst; |
|---|
| 68 | c_stream.avail_out = *dst_size; |
|---|
| 69 | c_stream.next_in = (Bytef*)src; |
|---|
| 70 | c_stream.avail_in = (uInt)src_size; |
|---|
| 71 | |
|---|
| 72 | z_res = deflate(&c_stream, Z_NO_FLUSH); |
|---|
| 73 | if (z_res != Z_OK) |
|---|
| 74 | { |
|---|
| 75 | sLog.outError("Can't compress update packet (zlib: deflate) Error code: %i (%s)",z_res,zError(z_res)); |
|---|
| 76 | *dst_size = 0; |
|---|
| 77 | return; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | if (c_stream.avail_in != 0) |
|---|
| 81 | { |
|---|
| 82 | sLog.outError("Can't compress update packet (zlib: deflate not greedy)"); |
|---|
| 83 | *dst_size = 0; |
|---|
| 84 | return; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | z_res = deflate(&c_stream, Z_FINISH); |
|---|
| 88 | if (z_res != Z_STREAM_END) |
|---|
| 89 | { |
|---|
| 90 | sLog.outError("Can't compress update packet (zlib: deflate should report Z_STREAM_END instead %i (%s)",z_res,zError(z_res)); |
|---|
| 91 | *dst_size = 0; |
|---|
| 92 | return; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | z_res = deflateEnd(&c_stream); |
|---|
| 96 | if (z_res != Z_OK) |
|---|
| 97 | { |
|---|
| 98 | sLog.outError("Can't compress update packet (zlib: deflateEnd) Error code: %i (%s)",z_res,zError(z_res)); |
|---|
| 99 | *dst_size = 0; |
|---|
| 100 | return; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | *dst_size = c_stream.total_out; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | bool UpdateData::BuildPacket(WorldPacket *packet, bool hasTransport) |
|---|
| 107 | { |
|---|
| 108 | ByteBuffer buf(m_data.size() + 10 + m_outOfRangeGUIDs.size()*8); |
|---|
| 109 | |
|---|
| 110 | buf << (uint32) (!m_outOfRangeGUIDs.empty() ? m_blockCount + 1 : m_blockCount); |
|---|
| 111 | buf << (uint8) (hasTransport ? 1 : 0); |
|---|
| 112 | |
|---|
| 113 | if(!m_outOfRangeGUIDs.empty()) |
|---|
| 114 | { |
|---|
| 115 | buf << (uint8) UPDATETYPE_OUT_OF_RANGE_OBJECTS; |
|---|
| 116 | buf << (uint32) m_outOfRangeGUIDs.size(); |
|---|
| 117 | |
|---|
| 118 | for(std::set<uint64>::const_iterator i = m_outOfRangeGUIDs.begin(); |
|---|
| 119 | i != m_outOfRangeGUIDs.end(); i++) |
|---|
| 120 | { |
|---|
| 121 | //buf.appendPackGUID(*i); |
|---|
| 122 | buf << (uint8)0xFF; |
|---|
| 123 | buf << (uint64) *i; |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | buf.append(m_data); |
|---|
| 128 | |
|---|
| 129 | packet->clear(); |
|---|
| 130 | |
|---|
| 131 | if (m_data.size() > 50 ) |
|---|
| 132 | { |
|---|
| 133 | uint32 destsize = buf.size() + buf.size()/10 + 16; |
|---|
| 134 | packet->resize( destsize ); |
|---|
| 135 | |
|---|
| 136 | packet->put(0, (uint32)buf.size()); |
|---|
| 137 | |
|---|
| 138 | Compress(const_cast<uint8*>(packet->contents()) + sizeof(uint32), |
|---|
| 139 | &destsize, |
|---|
| 140 | (void*)buf.contents(), |
|---|
| 141 | buf.size()); |
|---|
| 142 | if (destsize == 0) |
|---|
| 143 | return false; |
|---|
| 144 | |
|---|
| 145 | packet->resize( destsize + sizeof(uint32) ); |
|---|
| 146 | packet->SetOpcode( SMSG_COMPRESSED_UPDATE_OBJECT ); |
|---|
| 147 | } |
|---|
| 148 | else |
|---|
| 149 | { |
|---|
| 150 | packet->append( buf ); |
|---|
| 151 | packet->SetOpcode( SMSG_UPDATE_OBJECT ); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | return true; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | void UpdateData::Clear() |
|---|
| 158 | { |
|---|
| 159 | m_data.clear(); |
|---|
| 160 | m_outOfRangeGUIDs.clear(); |
|---|
| 161 | m_blockCount = 0; |
|---|
| 162 | } |
|---|