All pastes #382649 Raw Edit

Something

public text v1 · immutable
·published 2007-03-05 20:28 UTC
rendered paste body
using System;
using System.Collections.Generic;

namespace WCell.RealmServer
{
    public class UpdateMask
    {
        private int m_count;
        private int m_blocks;
        private uint[] m_updateMask;
        private SortedDictionary<int, uint> m_updateValues = new SortedDictionary<int, uint>();

        public UpdateMask()
        {
        }

        public UpdateMask(int valuesCount)
        {
            SetCount(valuesCount);
        }

        public UpdateMask(UpdateFieldId valuesCount)
        {
            SetCount((int) valuesCount.RawId);
        }

        public void Clear()
        {
            m_updateMask = new uint[m_blocks];
            m_updateValues = new SortedDictionary<int, uint>();
        }

        public void SetCount(int valuesCount)
        {
            m_count = valuesCount;
            m_blocks = (valuesCount + 31)/32;
            m_updateMask = new uint[m_blocks];
        }

        public void Write(RealmPacketOut packet)
        {
            packet.WriteByte((byte) m_blocks);
            for (int i = 0; i < m_blocks; i++)
            {
                packet.Write(m_updateMask[i]);
            }
            foreach (uint value in m_updateValues.Values)
            {
                packet.Write(value);
            }
        }

        public void SetBit(int index)
        {
            m_updateMask[index/32] |= (uint) (1 << index%32);
        }

        public void SetBit(UpdateFieldId index, float value)
        {
            SetBit(index);
            m_updateValues.Add(index, BitConverter.ToUInt32(BitConverter.GetBytes(value), 0));
        }

        public void SetBit(UpdateFieldId index, uint value)
        {
            SetBit(index);
            m_updateValues.Add(index, value);
        }

        public void SetBit(UpdateFieldId index, ulong value)
        {
            SetBit(index);
            SetBit(index + 1);
            byte[] uintBytes = BitConverter.GetBytes(value);
            m_updateValues.Add(index, BitConverter.ToUInt32(uintBytes, 0));
            m_updateValues.Add(index + 1, BitConverter.ToUInt32(uintBytes, 4));
        }

        public void SetBit(UpdateFieldId index, byte[] value)
        {
            SetBit(index);
            m_updateValues.Add(index, BitConverter.ToUInt32(value, 0));
        }

        public void SetBit(int index, uint value)
        {
            SetBit(index);
            m_updateValues.Add(index, value);
        }

        public void SetBit(int index, byte[] value)
        {
            SetBit(index);
            m_updateValues.Add(index, BitConverter.ToUInt32(value, 0));
        }

        public void SetBit(int index, ulong value)
        {
            SetBit(index);
            SetBit(index + 1);
            byte[] uintBytes = BitConverter.GetBytes(value);
            m_updateValues.Add(index, BitConverter.ToUInt32(uintBytes, 0));
            m_updateValues.Add(index + 1, BitConverter.ToUInt32(uintBytes, 4));
        }

        public bool GetBit(int index)
        {
            return (m_updateMask[index/32] & (1 << index%32)) == 0;
        }
    }
}