/* * Copyright (C) 2012-2020 CypherCore * * 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 3 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 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 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, see . */ using Framework.IO; using Game.Networking; using Game.Networking.Packets; using System.Collections.Generic; namespace Game.Entities { public class UpdateData { uint MapId; uint BlockCount; List destroyGUIDs = new(); List outOfRangeGUIDs = new(); ByteBuffer data = new(); public UpdateData(uint mapId) { MapId = mapId; } public void AddDestroyObject(ObjectGuid guid) { destroyGUIDs.Add(guid); } public void AddOutOfRangeGUID(List guids) { outOfRangeGUIDs.AddRange(guids); } public void AddOutOfRangeGUID(ObjectGuid guid) { outOfRangeGUIDs.Add(guid); } public void AddUpdateBlock(ByteBuffer block) { data.WriteBytes(block.GetData()); ++BlockCount; } public bool BuildPacket(out UpdateObject packet) { packet = new UpdateObject(); packet.NumObjUpdates = BlockCount; packet.MapID = (ushort)MapId; WorldPacket buffer = new(); if (buffer.WriteBit(!outOfRangeGUIDs.Empty() || !destroyGUIDs.Empty())) { buffer.WriteUInt16((ushort)destroyGUIDs.Count); buffer.WriteInt32(destroyGUIDs.Count + outOfRangeGUIDs.Count); foreach (var destroyGuid in destroyGUIDs) buffer.WritePackedGuid(destroyGuid); foreach (var outOfRangeGuid in outOfRangeGUIDs) buffer.WritePackedGuid(outOfRangeGuid); } var bytes = data.GetData(); buffer.WriteInt32(bytes.Length); buffer.WriteBytes(bytes); packet.Data = buffer.GetData(); return true; } public void Clear() { data.Clear(); destroyGUIDs.Clear(); outOfRangeGUIDs.Clear(); BlockCount = 0; MapId = 0; } public bool HasData() { return BlockCount > 0 || outOfRangeGUIDs.Count != 0; } public List GetOutOfRangeGUIDs() { return outOfRangeGUIDs; } public void SetMapId(ushort mapId) { MapId = mapId; } } }