コード例 #1
0
        public static int AsVarShort(this ReadOnlySpan <byte> span, out ReadOnlySpan <byte> outSpan)
        {
            int result = VarShort.Read(span, out int offset);

            outSpan = span.Slice(offset);
            return(result);
        }
コード例 #2
0
        public static int ReadVarShort(List <byte> cache, int offset, out int count, bool readOnly = false)
        {
            int result = VarShort.Read(cache, offset, out int length);

            count = length + offset;
            if (!readOnly)
            {
                cache.RemoveRange(offset, length);
            }
            return(result);
        }
コード例 #3
0
        public static bool Verify(ReadOnlyPacket packet, int protocolVersion, Bound bound, bool hasForge, out PluginChannelPacket pcp)
        {
            if (packet is null)
            {
                throw new ArgumentNullException(nameof(packet));
            }
            if (protocolVersion < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(protocolVersion), "协议版本不能使用负数");
            }

            pcp = null;
            if ((bound.HasFlag(Bound.Client) && packet.ID != GetPacketID(protocolVersion, Bound.Client)) ||
                (bound.HasFlag(Bound.Server) && packet.ID != GetPacketID(protocolVersion, Bound.Server)))
            {
                return(false);
            }

            try
            {
                ReadOnlySpan <byte> buffer = packet.Data.ToSpan();
                pcp = new PluginChannelPacket(
                    packetID: packet.ID,
                    protocolVersion: protocolVersion,
                    hasForge: hasForge,
                    channel: buffer.AsString(out buffer));

                if (protocolVersion <= ProtocolVersionNumbers.V14w31a && hasForge)
                {
                    pcp.Data.AddRange(buffer.Slice(VarShort.GetLength(buffer)).ToArray());
                }
                else if (protocolVersion <= ProtocolVersionNumbers.V14w31a)
                {
                    pcp.Data.AddRange(buffer.Slice(2).ToArray());
                }
                else
                {
                    pcp.Data.AddRange(buffer.ToArray());
                }

                return(true);
            }
            catch (ArgumentOutOfRangeException) { return(false); }
            catch (IndexOutOfRangeException) { return(false); }
            catch (OverflowException) { return(false); }
        }
コード例 #4
0
 public static ReadOnlySpan <byte> ReadVarShort(this ReadOnlySpan <byte> span, out int varShort)
 {
     varShort = VarShort.Read(span, out int offset);
     return(span.Slice(offset));
 }
コード例 #5
0
 public static int AsVarShort(this ReadOnlySpan <byte> span) => VarShort.Read(span);
コード例 #6
0
 public virtual void WriteVarShort(int value)
 {
     WriteBytes(VarShort.GetBytes(value));
 }
コード例 #7
0
 public virtual int ReadVarShort()
 {
     return(VarShort.Read(this));
 }
コード例 #8
0
        public override byte[] ToBytes(int compress = -1)
        {
            byte[] channel = Encoding.UTF8.GetBytes(Channel);
            byte[] PacketData;
            //14w31a: The length prefix on the payload of Plugin Message has been removed (Both ways)
            if (_protocolVersion <= ProtocolVersionNumbers.V14w31a)
            {
                if (HasForge)
                {
                    PacketData = ProtocolHandler.ConcatBytes(VarInt.GetBytes(channel.Length), channel, VarShort.GetBytes(Data.Count), Data.ToArray());
                }
                else
                {
                    PacketData = ProtocolHandler.ConcatBytes(VarInt.GetBytes(channel.Length), channel, ProtocolHandler.GetBytes((short)Data.Count), Data.ToArray());
                }
            }
            else
            {
                PacketData = ProtocolHandler.ConcatBytes(VarInt.GetBytes(channel.Length), channel, Data.ToArray());
            }
            int DataLength = PacketData.Length;

            PacketData = ProtocolHandler.ConcatBytes(VarInt.GetBytes(ID), PacketData);

            if (compress > 0)
            {
                if (DataLength >= compress)
                {
                    PacketData = ProtocolHandler.ConcatBytes(VarInt.GetBytes(PacketData.Length), ZlibUtils.Compress(PacketData));
                }
                else
                {
                    PacketData = ProtocolHandler.ConcatBytes(new byte[] { 0 }, PacketData);
                }
            }
            return(ProtocolHandler.ConcatBytes(VarInt.GetBytes(PacketData.Length), PacketData));
        }