System.IO.BinaryWriter.Write(byte[])

Here are the examples of the csharp api System.IO.BinaryWriter.Write(byte[]) taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

1715 Examples 7

19 View Source File : UEStringProperty.cs
License : MIT License
Project Creator : 13xforever

public override void Serialize(BinaryWriter writer)
        {
            if (Value == null)
            {
                writer.Write(0L);
                writer.Write((byte)0);
            }
            else
            {
                var bytes = Utf8.GetBytes(Value);
                writer.Write(bytes.Length + 6L);
                writer.Write((byte)0);
                writer.Write(bytes.Length+1);
                if (bytes.Length > 0)
                    writer.Write(bytes);
                writer.Write((byte)0);
            }
        }

19 View Source File : BinaryReaderEx.cs
License : MIT License
Project Creator : 13xforever

public static void WriteUEString(this BinaryWriter writer, string value)
        {
            if (value == null)
            {
                writer.Write(0);
                return;
            }

            var valueBytes = Utf8.GetBytes(value);
            writer.Write(valueBytes.Length + 1);
            if (valueBytes.Length > 0)
                writer.Write(valueBytes);
            writer.Write((byte)0);
        }

19 View Source File : TLV_0102.cs
License : MIT License
Project Creator : 499116344

public byte[] Get_Tlv(QQUser user)
        {
            var data = new BinaryWriter(new MemoryStream());
            if (WSubVer == 0x0001)
            {
                data.BeWrite(WSubVer);
                //OfficialKey
                data.Write(new byte[]
                    { 0x9e, 0x9b, 0x03, 0x23, 0x6d, 0x7f, 0xa8, 0x81, 0xa8, 0x10, 0x72, 0xec, 0x50, 0x97, 0x96, 0x8e });
                var bufSigPic = user.TXProtocol.BufSigPic ?? Util.RandomKey(56);
                data.WriteKey(bufSigPic);
                //Official
                data.WriteKey(new byte[]
                {
                    0x60, 0x6f, 0x27, 0xd7, 0xdc, 0x40, 0x46, 0x33, 0xa6, 0xc4, 0xb9, 0x05, 0x7e, 0x60, 0xfb, 0x64,
                    0x1e, 0x75, 0x65, 0x6
                });
            }
            else
            {
                throw new Exception($"{Name} 无法识别的版本号 {WSubVer}");
            }

            FillHead(Command);
            FillBody(data.BaseStream.ToBytesArray(), data.BaseStream.Length);
            SetLength();
            return GetBuffer();
        }

19 View Source File : Send_0x0360.cs
License : MIT License
Project Creator : 499116344

protected override void PutBody()
        {
            BodyWriter.Write(new byte[] { 0x00, 0x00, 0x00, 0x07 });
            var data = new BinaryWriter(new MemoryStream());
            data.Write(new byte[] { 0x03, 0x08, 0x03, 0x22, 0x46, 0x08 });
            data.Write(Util.HexStringToByteArray(Util.PB_toLength(_recvGroupQQ)));
            data.Write((byte) 0x10);
            data.Write(
                Util.HexStringToByteArray(
                    Util.PB_toLength(Convert.ToInt64(Util.ToHex(MessageTime).Replace(" ", ""), 16))));
            data.Write(new byte[] { 0x20, 0x00 });
            //数据长度
            BodyWriter.BeWrite(data.BaseStream.Length);
            BodyWriter.Write(new byte[] { 0x08, 0x01, 0x12, 0x03, 0x98, 0x01, 0x00 });
            //数据
            BodyWriter.Write(data.BaseStream.ToBytesArray());
        }

19 View Source File : Send_0x03FC.cs
License : MIT License
Project Creator : 499116344

protected override void PutBody()
        {
            BodyWriter.Write(new byte[] { 0x00, 0x00, 0x00, 0x07 });
            var data = new BinaryWriter(new MemoryStream());
            var data_0A = new BinaryWriter(new MemoryStream());
            data_0A.Write((byte) 0x08);
            data_0A.Write(Util.HexStringToByteArray(Util.PB_toLength(User.QQ)));
            data_0A.Write((byte) 0x10);
            data_0A.Write(Util.HexStringToByteArray(Util.PB_toLength(_recvQQ)));
            data_0A.Write((byte) 0x18);
            data_0A.Write(Util.HexStringToByteArray(Util.PB_toLength(_msgSequence))); //消息包序
            data_0A.Write((byte) 0x28);
            data_0A.Write(
                Util.HexStringToByteArray(
                    Util.PB_toLength(Convert.ToInt64(Util.ToHex(MessageTime).Replace(" ", ""), 16))));
            data_0A.Write(new byte[] { 0x30 });
            data_0A.Write(
                Util.HexStringToByteArray(
                    Util.PB_toLength(Convert.ToInt64(Util.ToHex(MessageId).Replace(" ", ""), 16)))); //消息Id
            data_0A.Write(new byte[] { 0x38, 0x01, 0x40, 0x00, 0x48, 0x00 });
            data.Write((byte) 0x0A);
            data.Write((byte) data_0A.BaseStream.Length); //length
            data.Write(data_0A.BaseStream.ToBytesArray());
            data.Write(new byte[] { 0x10, 0x01, 0x18, 0x01, 0x20, 0x00, 0x2a, 0x04, 0x08, 0x00, 0x10, 0x00 });
            //数据长度
            BodyWriter.BeWrite(data.BaseStream.Length);
            BodyWriter.Write(new byte[] { 0x08, 0x01, 0x12, 0x03, 0x98, 0x01, 0x00 });
            //数据
            BodyWriter.Write(data.BaseStream.ToBytesArray());
        }

19 View Source File : SendPacket.cs
License : MIT License
Project Creator : 499116344

public byte[] WriteData()
        {
            //保存当前pos
            var pos = (int) Writer.BaseStream.Position;
            //填充头部
            PutHeader();
            //填充包体
            BodyStream = new MemoryStream();
            BodyWriter = new BinaryWriter(BodyStream);
            PutBody();
            //需要加密的包体
            BodyDecrypted = BodyStream.ToArray();
            var enc = EncryptBody(BodyDecrypted, 0, BodyDecrypted.Length);
            // 加密内容写入最终buf
            Writer.Write(enc);
            // 填充尾部
            PutTail();
            // 回填
            PostFill(pos);
            return Writer.BaseStream.ToBytesArray();
        }

19 View Source File : SendPacket.cs
License : MIT License
Project Creator : 499116344

[Obsolete("请使用BinaryWriter.Write(Richtext)方法。")]
        public static byte[] ConstructMessage(string message)
        {
            var bw = new BinaryWriter(new MemoryStream());
            var r = new Regex(@"([^\[]+)*(\[face\d+\.gif\])([^\[]+)*");
            if (r.IsMatch(message))
            {
                var faces = r.Matches(message);
                for (var i = 0; i < faces.Count; i++)
                {
                    var face = faces[i];
                    for (var j = 1; j < face.Groups.Count; j++)
                    {
                        var group = face.Groups[j].Value;
                        if (group.Contains("[face") && group.Contains(".gif]"))
                        {
                            var faceIndex =
                                Convert.ToByte(group.Substring(5, group.Length - group.LastIndexOf(".") - 4));
                            if (faceIndex > 199)
                            {
                                faceIndex = 0;
                            }

                            //表情
                            bw.Write(new byte[] { 0x02, 0x00, 0x14, 0x01, 0x00, 0x01 });
                            bw.Write(faceIndex);
                            bw.Write(new byte[] { 0xFF, 0x00, 0x02, 0x14 });
                            bw.Write((byte) (faceIndex + 65));
                            bw.Write(new byte[] { 0x0B, 0x00, 0x08, 0x00, 0x01, 0x00, 0x04, 0x52, 0xCC, 0x85, 0x50 });
                        }
                        else if (!string.IsNullOrEmpty(group))
                        {
                            var groupMsg = Encoding.UTF8.GetBytes(group);
                            //普通消息
                            ConstructMessage(bw, groupMsg);
                        }
                    }
                }
            }

            return bw.BaseStream.ToBytesArray();
        }

19 View Source File : EncodingUtil.cs
License : MIT License
Project Creator : 404Lcc

public static byte[] LengthEncode(byte[] bytes)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(stream))
                {
                    writer.Write(bytes.Length);
                    writer.Write(bytes);
                    bytes = stream.ToArray();
                }
            }
            return bytes;
        }

19 View Source File : EncodingUtil.cs
License : MIT License
Project Creator : 404Lcc

public static byte[] SocketModelEncode(SocketModel model)
        {
            byte[] bytes;
            using (MemoryStream stream = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(stream))
                {
                    writer.Write(model.type);
                    writer.Write(model.area);
                    writer.Write(model.command);
                    if (model.message != null)
                    {
                        writer.Write(SerializationEncode(model.message));
                    }
                    bytes = stream.ToArray();
                }
            }
            return bytes;

        }

19 View Source File : Util.cs
License : MIT License
Project Creator : 499116344

public static void BeWrite(this BinaryWriter bw, ushort v)
        {
            bw.Write(BitConverter.GetBytes(v).Reverse().ToArray());
        }

19 View Source File : Util.cs
License : MIT License
Project Creator : 499116344

public static void BeWrite(this BinaryWriter bw, char v)
        {
            bw.Write(BitConverter.GetBytes((ushort) v).Reverse().ToArray());
        }

19 View Source File : Util.cs
License : MIT License
Project Creator : 499116344

public static void BeWrite(this BinaryWriter bw, int v)
        {
            bw.Write(BitConverter.GetBytes(v).Reverse().ToArray());
        }

19 View Source File : Util.cs
License : MIT License
Project Creator : 499116344

public static void BeWrite(this BinaryWriter bw, long v)
        {
            bw.Write(BitConverter.GetBytes((uint) v).Reverse().ToArray());
        }

19 View Source File : Util.cs
License : MIT License
Project Creator : 499116344

public static void BeWrite(this BinaryWriter bw, ulong v)
        {
            bw.Write(BitConverter.GetBytes((uint) v).Reverse().ToArray());
        }

19 View Source File : Util.cs
License : MIT License
Project Creator : 499116344

public static void WriteKey(this BinaryWriter bw, byte[] v)
        {
            bw.BeWrite((ushort) v.Length);
            bw.Write(v);
        }

19 View Source File : Util.cs
License : MIT License
Project Creator : 499116344

public static List<byte[]> WriteSnippet(TextSnippet snippet, int length)
        {
            // TODO: 富文本支持
            var ret = new List<byte[]>();
            var bw = new BinaryWriter(new MemoryStream());
            switch (snippet.Type)
            {
                case MessageType.Normal:
                {
                    if (length + 6 >= 699) // 数字应该稍大点,但是我不清楚具体是多少
                    {
                        length = 0;
                        ret.Add(new byte[0]);
                    }

                    bw.BaseStream.Position = 6;
                    foreach (var chr in snippet.Content)
                    {
                        var bytes = Encoding.UTF8.GetBytes(chr.ToString());
                        // 705 = 699 + 6个byte: (byte + short + byte + short)
                        if (length + bw.BaseStream.Length + bytes.Length > 705)
                        {
                            var pos = bw.BaseStream.Position;
                            bw.BaseStream.Position = 0;
                            bw.Write(new byte[]
                            {
                                0x01
                            });
                            bw.BeWrite((ushort) (pos - 3)); // 本来是+3和0的,但是提前预留了6个byte给它们,所以变成了-3和-6。下同理。
                            bw.Write(new byte[]
                            {
                                0x01
                            });
                            bw.BeWrite((ushort) (pos - 6));
                            bw.BaseStream.Position = pos;
                            ret.Add(bw.BaseStream.ToBytesArray());
                            bw = new BinaryWriter(new MemoryStream());
                            bw.BaseStream.Position = 6;
                            length = 0;
                        }

                        bw.Write(bytes);
                    }

                    // 在最后一段的开头补充结构 
                    {
                        var pos = bw.BaseStream.Position;
                        bw.BaseStream.Position = 0;
                        bw.Write(new byte[]
                        {
                            0x01
                        });
                        bw.BeWrite((ushort) (pos - 3));
                        bw.Write(new byte[]
                        {
                            0x01
                        });
                        bw.BeWrite((ushort) (pos - 6));
                        bw.BaseStream.Position = pos;
                    }
                    break;
                }
                case MessageType.At:
                    break;
                case MessageType.Emoji:
                {
                    if (length + 12 > 699)
                    {
                        ret.Add(new byte[0]);
                    }

                    var faceIndex = Convert.ToByte(snippet.Content);
                    if (faceIndex > 199)
                    {
                        faceIndex = 0;
                    }

                    bw.Write(new byte[]
                    {
                        0x02,
                        0x00,
                        0x14,
                        0x01,
                        0x00,
                        0x01
                    });
                    bw.Write(faceIndex);
                    bw.Write(new byte[]
                    {
                        0xFF,
                        0x00,
                        0x02,
                        0x14
                    });
                    bw.Write((byte) (faceIndex + 65));
                    bw.Write(new byte[]
                    {
                        0x0B,
                        0x00,
                        0x08,
                        0x00,
                        0x01,
                        0x00,
                        0x04,
                        0x52,
                        0xCC,
                        0x85,
                        0x50
                    });
                    break;
                }
                case MessageType.Picture:
                    break;
                case MessageType.Xml:
                    break;
                case MessageType.Json:
                    break;
                case MessageType.Shake:
                    break;
                case MessageType.Audio:
                    break;
                case MessageType.Video:
                    break;
                case MessageType.ExitGroup:
                    break;
                case MessageType.GetGroupImformation:
                    break;
                case MessageType.AddGroup:
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            if (bw.BaseStream.Position != 0)
            {
                ret.Add(bw.BaseStream.ToBytesArray());
            }

            return ret;
        }

19 View Source File : BaseTLV.cs
License : MIT License
Project Creator : 499116344

protected void FillBody(byte[] bufdata, long length)
        {
            _buffer.BeWrite((ushort) length);
            _buffer.Write(bufdata);
        }

19 View Source File : TLV_0006.cs
License : MIT License
Project Creator : 499116344

public byte[] Get_Tlv(QQUser user)
        {
            if (WSubVer == 0x0002)
            {
                if (user.TXProtocol.BufTgtgt == null)
                {
                    var data = new BinaryWriter(new MemoryStream());
                    data.BeWrite(new Random(Guid.NewGuid().GetHashCode()).Next()); //随机4字节??
                    data.BeWrite(WSubVer); //wSubVer
                    data.BeWrite(user.QQ); //QQ号码
                    data.BeWrite(user.TXProtocol.DwSsoVersion);
                    data.BeWrite(user.TXProtocol.DwServiceId);
                    data.BeWrite(user.TXProtocol.DwClientVer);
                    data.BeWrite((ushort) 0);
                    data.Write(user.TXProtocol.BRememberPwdLogin);
                    data.Write(user.MD51); //密码的一次MD5值,服务器用该MD5值验证用户密码是否正确
                    data.BeWrite(user.TXProtocol.DwServerTime); //登录时间
                    data.Write(new byte[13]); //固定13字节
                    data.Write(Util.IPStringToByteArray(user.TXProtocol.DwClientIP)); //IP地址
                    data.BeWrite(user.TXProtocol.DwIsp); //dwISP
                    data.BeWrite(user.TXProtocol.DwIdc); //dwIDC
                    data.WriteKey(user.TXProtocol.BufComputerIdEx); //机器码
                    data.Write(user.TXProtocol.BufTgtgtKey); //00DD临时密钥(通过验证时客户端用该密钥解密服务端发送回来的数据)

                    user.TXProtocol.BufTgtgt =
                        QQTea.Encrypt(data.BaseStream.ToBytesArray(), user.Md52());
                }
            }
            else
            {
                throw new Exception($"{Name} 无法识别的版本号 {WSubVer}");
            }

            var tlv = new BinaryWriter(new MemoryStream());
            tlv.Write(user.TXProtocol.BufTgtgt);
            FillHead(Command);
            FillBody(tlv.BaseStream.ToBytesArray(), tlv.BaseStream.Length);
            SetLength();
            return GetBuffer();
        }

19 View Source File : TLV_050C.cs
License : MIT License
Project Creator : 499116344

public byte[] Get_Tlv(QQUser user)
        {
            var buf = new BinaryWriter(new MemoryStream());
            var dataTime = DateTime.Now;
            buf.BeWrite(0);
            buf.BeWrite(user.QQ);
            buf.Write(new byte[] { 0x76, 0x71, 0x01, 0x9d });
            buf.BeWrite(Util.GetTimeMillis(dataTime));
            buf.BeWrite(user.TXProtocol.DwServiceId);
            buf.Write(new byte[]
                { 0x77, 0x69, 0x6e, 0x64, 0x6f, 0x77, 0x73, 0x00, 0x04, 0x5f, 0x80, 0x33, 0x01, 0x01 });
            buf.BeWrite(user.TXProtocol.DwClientVer);
            buf.Write(new byte[]
                { 0x66, 0x35, 0x4d, 0xf1, 0xab, 0xdc, 0x98, 0xf0, 0x70, 0x69, 0xfc, 0x2a, 0x2b, 0x86, 0x06, 0x1b });
            buf.BeWrite(user.TXProtocol.SubVer);

            var data = new BinaryWriter(new MemoryStream());
            data.BeWrite(0);
            data.BeWrite(user.QQ);
            data.Write(new byte[] { 0x76, 0x71, 0x01, 0x9d });
            data.BeWrite(Util.GetTimeMillis(dataTime));
            data.Write(user.TXProtocol.DwPubNo);

            buf.Write((byte) data.BaseStream.Length * 3);
            buf.Write(data.BaseStream.ToBytesArray());
            buf.Write(data.BaseStream.ToBytesArray());
            buf.Write(data.BaseStream.ToBytesArray());


            FillHead(Command);
            FillBody(buf.BaseStream.ToBytesArray(), buf.BaseStream.Length);
            SetLength();
            return GetBuffer();
        }

19 View Source File : Send_0x0319.cs
License : MIT License
Project Creator : 499116344

protected override void PutBody()
        {
            BodyWriter.Write(new byte[] { 0x00, 0x00, 0x00, 0x07 });
            var data = new BinaryWriter(new MemoryStream());
            data.Write(new byte[] { 0x0A, 0x0C, 0x08 });
            data.Write(Util.HexStringToByteArray(Util.PB_toLength(_recvQQ)));
            data.Write((byte) 0x10);
            data.Write(
                Util.HexStringToByteArray(
                    Util.PB_toLength(Convert.ToInt64(Util.ToHex(MessageTime).Replace(" ", ""), 16))));
            data.Write(new byte[] { 0x20, 0x00 });
            //数据长度
            BodyWriter.BeWrite(data.BaseStream.Length);
            BodyWriter.Write(new byte[] { 0x08, 0x01, 0x12, 0x03, 0x98, 0x01, 0x00 });
            //数据
            BodyWriter.Write(data.BaseStream.ToBytesArray());
        }

19 View Source File : Send_0x0352.cs
License : MIT License
Project Creator : 499116344

protected override void PutBody()
        {
            var pic = new Bitmap(FileName);
            var width = pic.Size.Width; // 图片的宽度
            var height = pic.Size.Height; // 图片的高度
            var picBytes = ImageHelper.ImageToBytes(pic);
            var md5 = QQTea.MD5(picBytes);

            var data = new BinaryWriter(new MemoryStream());
            data.Write(new byte[]
            {
                0x01, 0x08, 0x01, 0x12
            });
            data.Write((byte) 0x5A);
            data.Write((byte) 0x08);
            data.Write(Util.HexStringToByteArray(Util.PB_toLength(User.QQ)));
            data.Write((byte) 0x10);
            data.Write(Util.HexStringToByteArray(Util.PB_toLength(ToQQ)));
            data.BeWrite((ushort) 0x1800);
            data.Write((byte) 0x22);
            data.Write((byte) 0x10);
            data.Write(md5);
            data.Write((byte) 0x28);
            data.Write(Util.HexStringToByteArray(Util.PB_toLength(picBytes.Length)));
            data.Write((byte) 0x32);
            data.Write((byte) 0x1A);
            data.Write(new byte[]
            {
                0x57, 0x00, 0x53, 0x00, 0x4E, 0x00, 0x53, 0x00, 0x4C, 0x00, 0x54, 0x00,
                0x31, 0x00, 0x36, 0x00, 0x47, 0x00, 0x45, 0x00, 0x4F, 0x00, 0x5B, 0x00,
                0x5F, 0x00
            });
            data.BeWrite((ushort) 0x3801);
            data.BeWrite((ushort) 0x4801);
            data.Write((byte) 0x70);
            data.Write(Util.HexStringToByteArray(Util.PB_toLength(width)));
            data.Write((byte) 0x78);
            data.Write(Util.HexStringToByteArray(Util.PB_toLength(height)));

            BodyWriter.Write(new byte[]
            {
                0x00, 0x00, 0x00, 0x07
            });
            //数据长度
            BodyWriter.BeWrite(data.BaseStream.Length);
            BodyWriter.Write(new byte[]
            {
                0x08, 0x01, 0x12, 0x03, 0x98, 0x01
            });
            //数据
            BodyWriter.Write(data.BaseStream.ToBytesArray());
        }

19 View Source File : Send_0x0391.cs
License : MIT License
Project Creator : 499116344

protected override void PutBody()
        {
            BodyWriter.Write(new byte[] { 0x00, 0x00, 0x00, 0x0D });
            var data = new BinaryWriter(new MemoryStream());
            data.Write(new byte[] { 0x0A, 0x12, 0x08 });
            data.Write(Util.HexStringToByteArray(Util.PB_toLength(GroupQQ)));
            data.Write(new byte[] { 0x12, 0x0a, 0x38, 0x00, 0x40, 0x00, 0x4a, 0x04, 0x08, 0x00, 0x10, 0x00 });
            //数据长度
            BodyWriter.BeWrite(data.BaseStream.Length);
            BodyWriter.Write(new byte[] { 0x08, 0x01, 0x12, 0x09 });

            var data12 = new BinaryWriter(new MemoryStream());
            data12.Write(
                Util.HexStringToByteArray(
                    Util.PB_toLength(Convert.ToInt64(Util.ToHex(MessageIndex).Replace(" ", ""), 16))));
            data12.Write(new byte[] { 0x88, 0x01, 0x04, 0x98, 0x01, 0x00 });

            BodyWriter.Write((byte) data12.BaseStream.Length);
            BodyWriter.Write(data12.BaseStream.ToBytesArray());

            //数据
            BodyWriter.Write(data.BaseStream.ToBytesArray());
        }

19 View Source File : Send_0x03F7.cs
License : MIT License
Project Creator : 499116344

protected override void PutBody()
        {
            var pbGroupId = Util.HexStringToByteArray(Util.PB_toLength(_groupId));
            var pbMessageId =
                Util.HexStringToByteArray(
                    Util.PB_toLength(Convert.ToInt64(Util.ToHex(_messageId).Replace(" ", ""), 16)));
            var pbMessageIndex =
                Util.HexStringToByteArray(
                    Util.PB_toLength(Convert.ToInt64(Util.ToHex(_messageIndex).Replace(" ", ""), 16)));
            BodyWriter.Write(new byte[] { 0x00, 0x00, 0x00, 0x07 });
            var data = new BinaryWriter(new MemoryStream());
            data.Write(new byte[] { 0x08, 0x01, 0x10, 0x00, 0x18 });
            data.Write(pbGroupId);
            data.Write(new byte[] { 0x22, 0x09, 0x08 });
            data.Write(pbMessageIndex); //消息索引
            data.Write((byte) 0x10);
            data.Write(pbMessageId); //消息Id
            var data_2A = new BinaryWriter(new MemoryStream());
            data_2A.Write(new byte[] { 0x08, 0x00 });
            var data12 = new BinaryWriter(new MemoryStream());
            data12.Write((byte) 0x08);
            data12.Write(pbMessageIndex); //消息索引
            data12.Write((byte) 0x10);
            data12.Write(new byte[] { 0x00, 0x18, 0x01, 0x20, 0x00 });
            data_2A.Write((byte) 0x12);
            data_2A.Write((byte) data12.BaseStream.Length);
            data_2A.Write(data12.BaseStream.ToBytesArray());
            data.Write((byte) 0x2a);
            data.Write((byte) data_2A.BaseStream.Length);
            data.Write(data_2A.BaseStream.ToBytesArray());
            //数据长度
            BodyWriter.BeWrite(data.BaseStream.Length);
            BodyWriter.Write(new byte[] { 0x08, 0x01, 0x12, 0x03, 0x98, 0x01, 0x00 });
            //数据
            BodyWriter.Write(data.BaseStream.ToBytesArray());
        }

19 View Source File : SendPacket.cs
License : MIT License
Project Creator : 499116344

[Obsolete("请使用BinaryWriter.Write(Richtext)方法。")]
        public static void ConstructMessage(BinaryWriter writer, byte[] groupMsg)
        {
            writer.Write(new byte[] { 0x01 });
            writer.BeWrite((ushort) (groupMsg.Length + 3));
            writer.Write(new byte[] { 0x01 });
            writer.BeWrite((ushort) groupMsg.Length);
            writer.Write(groupMsg);
        }

19 View Source File : SendPacket.cs
License : MIT License
Project Creator : 499116344

[Obsolete("请使用BinaryWriter.Write(Richtext)方法。")]
        public static byte[] SendXml(byte[] compressMsg)
        {
            var bw = new BinaryWriter(new MemoryStream());
            bw.Write(Util.RandomKey(4));
            bw.Write(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x86, 0x00 });
            bw.Write(new byte[] { 0x00, 0x0C });
            bw.Write(new byte[] { 0xE5, 0xBE, 0xAE, 0xE8, 0xBD, 0xAF, 0xE9, 0x9B, 0x85, 0xE9, 0xBB, 0x91 });
            bw.Write(new byte[] { 0x00, 0x00, 0x14 });
            bw.BeWrite((ushort) (compressMsg.Length + 11));
            bw.Write((byte) 0x01);
            bw.BeWrite((ushort) (compressMsg.Length + 1));
            bw.Write((byte) 0x01);
            bw.Write(compressMsg);
            bw.Write(new byte[] { 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, 0x4D });
            return bw.BaseStream.ToBytesArray();
        }

19 View Source File : QdData.cs
License : MIT License
Project Creator : 499116344

public static byte[] GetQdData(QQUser user)
        {
            try
            {
                var data = new BinaryWriter(new MemoryStream());
                data.Write(user.TXProtocol.DwServerIP);

                var qddata = new BinaryWriter(new MemoryStream());
                qddata.Write(user.TXProtocol.DwQdVerion);
                qddata.Write(user.TXProtocol.DwPubNo);
                qddata.BeWrite(user.QQ);
                qddata.BeWrite((ushort) data.BaseStream.Length);

                data = new BinaryWriter(new MemoryStream());
                data.Write(user.TXProtocol.QdPreFix);
                data.BeWrite(user.TXProtocol.CQdProtocolVer);
                data.BeWrite(user.TXProtocol.DwQdVerion);
                data.Write((byte) 0);
                data.BeWrite(user.TXProtocol.WQdCsCmdNo);
                data.Write(user.TXProtocol.CQdCcSubNo);
                data.Write(new byte[] { 0x0E, 0x88 }); //xrand(0xFFFF) + 1
                data.BeWrite(0); //四个0
                data.Write(user.TXProtocol.BufComputerIdEx);
                data.Write(user.TXProtocol.COsType);
                data.Write(user.TXProtocol.BIsWow64);
                data.Write(user.TXProtocol.DwPubNo);
                data.BeWrite((ushort) user.TXProtocol.DwClientVer);
                data.BeWrite(user.TXProtocol.DwDrvVersionInfo / 0x10000);
                data.BeWrite(user.TXProtocol.DwDrvVersionInfo % 0x10000);
                data.Write(user.TXProtocol.BufVersionTsSafeEditDat);
                data.Write(user.TXProtocol.BufVersionQScanEngineDll);
                data.Write((byte) 0);

                data.Write(new TeaCrypter().Encrypt(qddata.BaseStream.ToBytesArray(), user.TXProtocol.BufQdKey));

                data.Write(user.TXProtocol.QdSufFix);

                var size = data.BaseStream.Length + 3;
                qddata = new BinaryWriter(new MemoryStream());
                qddata.Write(user.TXProtocol.QdPreFix);
                qddata.Write(size);
                qddata.Write(data.BaseStream.Length);

                var result = data.BaseStream.ToBytesArray();
                user.TXProtocol.QdData = result;
                return result;
            }
            catch
            {
                return new byte[] { };
            }
        }

19 View Source File : Util.cs
License : MIT License
Project Creator : 499116344

public static List<byte[]> WriteRichtext(Richtext richtext)
        {
            if (richtext.Snippets.Count > 1)
            {
                if (!richtext.Snippets.TrueForAll(s =>
                    s.Type == MessageType.Normal || s.Type == MessageType.At || s.Type == MessageType.Emoji ||
                    s.Type == MessageType.Picture))
                {
                    throw new NotSupportedException("富文本中包含多个非聊天代码");
                }
            }

            // TODO: 富文本支持
            var ret = new List<byte[]>();
            var bw = new BinaryWriter(new MemoryStream());
            foreach (var snippet in richtext.Snippets)
            {
                var list = WriteSnippet(snippet, (int) bw.BaseStream.Position);
                for (var i = 0; i < list.Count; i++)
                {
                    bw.Write(list[i]);
                    // 除最后一个以外别的都开新的包
                    //   如果有多个,那前几个一定是太长了被分段了,所以开新的包
                    //   如果只有一个/是最后一个,那就不开
                    if (i == list.Count - 1)
                    {
                        break;
                    }

                    ret.Add(bw.BaseStream.ToBytesArray());
                    bw = new BinaryWriter(new MemoryStream());
                }
            }

            ret.Add(bw.BaseStream.ToBytesArray());
            return ret;
        }

19 View Source File : QQUser.cs
License : MIT License
Project Creator : 499116344

public byte[] Md52()
        {
            var byteBuffer = new BinaryWriter(new MemoryStream());
            byteBuffer.Write(MD51);
            byteBuffer.BeWrite(0);
            byteBuffer.BeWrite(QQ);
            return MD5.Create().ComputeHash(((MemoryStream) byteBuffer.BaseStream).ToArray());
        }

19 View Source File : BaiduMap_main.cs
License : MIT License
Project Creator : 734843327

void saveData(WWW www, string name)//没用
    {

        //以下代码作用为:将数据保存到本地生成json文件
        string filename = name +"南开内"+UnityEngine.Random.Range(0.0f, 5.0f).ToString();
       
        byte[] stream = www.bytes;
        FileStream fs = new FileStream(filename, FileMode.CreateNew);
        // Create the writer for data.
        BinaryWriter w = new BinaryWriter(fs);
        // Write data to Test.data.
        w.Write(stream);
        w.Close();
        fs.Close();
    }

19 View Source File : IPCSupport.cs
License : GNU General Public License v3.0
Project Creator : 9E4ECDDE

public byte[] ToByteArray()
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    using (BinaryWriter binaryWriter = new BinaryWriter(ms))
                    {
                        binaryWriter.Write(name);
                        binaryWriter.Write(boneCount);
                        List<byte[]> bonesBytes = new List<byte[]>(bones.Select((b) => b.ToByteArray()));
                        foreach (byte[] boneByteData in bonesBytes)
                        {
                            binaryWriter.Write(boneByteData.Length);
                            binaryWriter.Write(boneByteData);
                        }
                        return ms.ToArray();
                    }
                }
            }

19 View Source File : IPCSupport.cs
License : GNU General Public License v3.0
Project Creator : 9E4ECDDE

public void Send(Message messageType, string boneName, object data)
            {
                if (!IsConnected) throw new InvalidOperationException("Tried to send a message but the pipe is disconnected.");
                switch (messageType)
                {
                    case Message.SetBoneData:
                        {
                            if (!IsServer) throw new InvalidOperationException("GetBoneData can only be called from the server.");
                            pipeWriter.Write((int)Message.SetBoneData);
                            pipeWriter.Write((byte[])data);
                            break;
                        }
                    case Message.SetBoneDamping:
                        {
                            if (IsServer) throw new InvalidOperationException("SetBoneDamping can only be called from the client.");
                            pipeWriter.Write((int)Message.SetBoneDamping);
                            pipeWriter.Write(boneName);
                            pipeWriter.Write((float)data);
                            break;
                        }
                    case Message.SetBoneElasticity:
                        {
                            if (IsServer) throw new InvalidOperationException("SetBoneElasticity can only be called from the client.");
                            pipeWriter.Write((int)Message.SetBoneElasticity);
                            pipeWriter.Write(boneName);
                            pipeWriter.Write((float)data);
                            break;
                        }
                    case Message.SetBoneStiffness:
                        {
                            if (IsServer) throw new InvalidOperationException("SetBoneStiffness can only be called from the client.");
                            pipeWriter.Write((int)Message.SetBoneStiffness);
                            pipeWriter.Write(boneName);
                            pipeWriter.Write((float)data);
                            break;
                        }
                    case Message.SetBoneInert:
                        {
                            if (IsServer) throw new InvalidOperationException("SetBoneInert can only be called from the client.");
                            pipeWriter.Write((int)Message.SetBoneInert);
                            pipeWriter.Write(boneName);
                            pipeWriter.Write((float)data);
                            break;
                        }
                    case Message.SetBoneRadius:
                        {
                            if (IsServer) throw new InvalidOperationException("SetBoneRadius can only be called from the client.");
                            pipeWriter.Write((int)Message.SetBoneRadius);
                            pipeWriter.Write(boneName);
                            pipeWriter.Write((float)data);
                            break;
                        }
                    case Message.SetBoneEndLength:
                        {
                            if (IsServer) throw new InvalidOperationException("SetBoneEndLength can only be called from the client.");
                            pipeWriter.Write((int)Message.SetBoneEndLength);
                            pipeWriter.Write(boneName);
                            pipeWriter.Write((float)data);
                            break;
                        }
                    case Message.SetBoneEndOffset:
                        {
                            if (IsServer) throw new InvalidOperationException("SetBoneEndOffset can only be called from the client.");
                            pipeWriter.Write((int)Message.SetBoneEndOffset);
                            pipeWriter.Write(boneName);
                            pipeWriter.Write(((float3)data).x);
                            pipeWriter.Write(((float3)data).y);
                            pipeWriter.Write(((float3)data).z);
                            break;
                        }
                    case Message.SetBoneGravity:
                        {
                            if (IsServer) throw new InvalidOperationException("SetBoneGravity can only be called from the client.");
                            pipeWriter.Write((int)Message.SetBoneGravity);
                            pipeWriter.Write(boneName);
                            pipeWriter.Write(((float3)data).x);
                            pipeWriter.Write(((float3)data).y);
                            pipeWriter.Write(((float3)data).z);
                            break;
                        }
                    case Message.SetBoneForce:
                        {
                            if (IsServer) throw new InvalidOperationException("SetBoneForce can only be called from the client.");
                            pipeWriter.Write((int)Message.SetBoneForce);
                            pipeWriter.Write(boneName);
                            pipeWriter.Write(((float3)data).x);
                            pipeWriter.Write(((float3)data).y);
                            pipeWriter.Write(((float3)data).z);
                            break;
                        }
                }
                pipe.WaitForPipeDrain();
            }

19 View Source File : IPCSupport.cs
License : GNU General Public License v3.0
Project Creator : 9E4ECDDE

public void Send(Message messageType, string boneName, object data)
            {
                if (!IsConnected) throw new InvalidOperationException("Tried to send a message but the pipe is disconnected.");
                switch (messageType)
                {
                    case Message.SetBoneData:
                        {
                            if (!IsServer) throw new InvalidOperationException("GetBoneData can only be called from the server.");
                            pipeWriter.Write((int)Message.SetBoneData);
                            pipeWriter.Write((byte[])data);
                            break;
                        }
                    case Message.SetBoneDamping:
                        {
                            if (IsServer) throw new InvalidOperationException("SetBoneDamping can only be called from the client.");
                            pipeWriter.Write((int)Message.SetBoneDamping);
                            pipeWriter.Write(boneName);
                            pipeWriter.Write((float)data);
                            break;
                        }
                    case Message.SetBoneElasticity:
                        {
                            if (IsServer) throw new InvalidOperationException("SetBoneElasticity can only be called from the client.");
                            pipeWriter.Write((int)Message.SetBoneElasticity);
                            pipeWriter.Write(boneName);
                            pipeWriter.Write((float)data);
                            break;
                        }
                    case Message.SetBoneStiffness:
                        {
                            if (IsServer) throw new InvalidOperationException("SetBoneStiffness can only be called from the client.");
                            pipeWriter.Write((int)Message.SetBoneStiffness);
                            pipeWriter.Write(boneName);
                            pipeWriter.Write((float)data);
                            break;
                        }
                    case Message.SetBoneInert:
                        {
                            if (IsServer) throw new InvalidOperationException("SetBoneInert can only be called from the client.");
                            pipeWriter.Write((int)Message.SetBoneInert);
                            pipeWriter.Write(boneName);
                            pipeWriter.Write((float)data);
                            break;
                        }
                    case Message.SetBoneRadius:
                        {
                            if (IsServer) throw new InvalidOperationException("SetBoneRadius can only be called from the client.");
                            pipeWriter.Write((int)Message.SetBoneRadius);
                            pipeWriter.Write(boneName);
                            pipeWriter.Write((float)data);
                            break;
                        }
                    case Message.SetBoneEndLength:
                        {
                            if (IsServer) throw new InvalidOperationException("SetBoneEndLength can only be called from the client.");
                            pipeWriter.Write((int)Message.SetBoneEndLength);
                            pipeWriter.Write(boneName);
                            pipeWriter.Write((float)data);
                            break;
                        }
                    case Message.SetBoneEndOffset:
                        {
                            if (IsServer) throw new InvalidOperationException("SetBoneEndOffset can only be called from the client.");
                            pipeWriter.Write((int)Message.SetBoneInert);
                            pipeWriter.Write(boneName);
                            pipeWriter.Write(((float3)data).x);
                            pipeWriter.Write(((float3)data).y);
                            pipeWriter.Write(((float3)data).z);
                            break;
                        }
                    case Message.SetBoneGravity:
                        {
                            if (IsServer) throw new InvalidOperationException("SetBoneGravity can only be called from the client.");
                            pipeWriter.Write((int)Message.SetBoneGravity);
                            pipeWriter.Write(boneName);
                            pipeWriter.Write(((float3)data).x);
                            pipeWriter.Write(((float3)data).y);
                            pipeWriter.Write(((float3)data).z);
                            break;
                        }
                    case Message.SetBoneForce:
                        {
                            if (IsServer) throw new InvalidOperationException("SetBoneForce can only be called from the client.");
                            pipeWriter.Write((int)Message.SetBoneInert);
                            pipeWriter.Write(boneName);
                            pipeWriter.Write(((float3)data).x);
                            pipeWriter.Write(((float3)data).y);
                            pipeWriter.Write(((float3)data).z);
                            break;
                        }
                }

            }

19 View Source File : OvrAvatarPacket.cs
License : MIT License
Project Creator : absurd-joy

public void Write(Stream stream)
    {
        BinaryWriter writer = new BinaryWriter(stream);

        // Write all of the frames
        int frameCount = frameTimes.Count;
        writer.Write(frameCount);
        for (int i = 0; i < frameCount; ++i)
        {
            writer.Write(frameTimes[i]);
        }
        for (int i = 0; i < frameCount; ++i)
        {
            OvrAvatarDriver.PoseFrame frame = frames[i];
            writer.Write(frame);
        }

        // Write all of the encoded audio packets
        int audioPacketCount = encodedAudioPackets.Count;
        writer.Write(audioPacketCount);
        for (int i = 0; i < audioPacketCount; ++i)
        {
            byte[] packet = encodedAudioPackets[i];
            writer.Write(packet.Length);
            writer.Write(packet);
        }
    }

19 View Source File : RemoteLoopbackManager.cs
License : MIT License
Project Creator : absurd-joy

void OnLocalAvatarPacketRecorded(object sender, OvrAvatar.PacketEventArgs args)
    {
        using (MemoryStream outputStream = new MemoryStream())
        {
            BinaryWriter writer = new BinaryWriter(outputStream);

            if (LocalAvatar.UseSDKPackets)
            {
                var size = CAPI.ovrAvatarPacket_GetSize(args.Packet.ovrNativePacket);
                byte[] data = new byte[size];
                CAPI.ovrAvatarPacket_Write(args.Packet.ovrNativePacket, size, data);

                writer.Write(PacketSequence++);
                writer.Write(size);
                writer.Write(data);
            }
            else
            {
                writer.Write(PacketSequence++);
                args.Packet.Write(outputStream);
            }

            SendPacketData(outputStream.ToArray());
        }
    }

19 View Source File : FileCacheManager.cs
License : Apache License 2.0
Project Creator : acarteas

public virtual long WriteFile(FileCache.PayloadMode mode, string key, FileCachePayload data, string regionName = null, bool policyUpdateOnly = false)
        {
            string cachedPolicy = GetPolicyPath(key, regionName);
            string cachedItemPath = GetCachePath(key, regionName);
            long cacheSizeDelta = 0;

            //ensure that the cache policy contains the correct key
            data.Policy.Key = key;

            if (!policyUpdateOnly)
            {
                long oldBlobSize = 0;
                if (File.Exists(cachedItemPath))
                {
                    oldBlobSize = new FileInfo(cachedItemPath).Length;
                }

                switch (mode)
                {
                    case FileCache.PayloadMode.Serializable:
                        using (FileStream stream = GetStream(cachedItemPath, FileMode.Create, FileAccess.Write, FileShare.None))
                        {
                            BinaryFormatter formatter = new BinaryFormatter();
                            formatter.Serialize(stream, data.Payload);
                        }
                        break;
                    case FileCache.PayloadMode.RawBytes:
                        using (FileStream stream = GetStream(cachedItemPath, FileMode.Create, FileAccess.Write, FileShare.None))
                        {
                            using (BinaryWriter writer = new BinaryWriter(stream))
                            {
                                if (data.Payload is byte[])
                                {
                                    byte[] dataPayload = (byte[])data.Payload;
                                    writer.Write(dataPayload);
                                }
                                else if (data.Payload is Stream)
                                {
                                    Stream dataPayload = (Stream)data.Payload;
                                    byte[] bytePayload = new byte[dataPayload.Length - dataPayload.Position];
                                    dataPayload.Read(bytePayload, (int)dataPayload.Position, bytePayload.Length);
                                    // no close or the like for data.Payload - we are not the owner
                                }
                            }
                        }
                        break;

                    case FileCache.PayloadMode.Filename:
                        File.Copy((string)data.Payload, cachedItemPath, true);
                        break;
                }

                //adjust cache size (while we have the file to ourselves)
                cacheSizeDelta += new FileInfo(cachedItemPath).Length - oldBlobSize;
            }

            //remove current policy file from cache size calculations
            if (File.Exists(cachedPolicy))
            {
                cacheSizeDelta -= new FileInfo(cachedPolicy).Length;
            }

            //write the cache policy
            using (FileStream stream = GetStream(cachedPolicy, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (BinaryWriter writer = new BinaryWriter(stream))
                {
                    data.Policy.Serialize(writer);
                }
            }

            // Adjust cache size outside of the using blocks to ensure it's after the data is written.
            cacheSizeDelta += new FileInfo(cachedPolicy).Length;

            return cacheSizeDelta;
        }

19 View Source File : Texture.cs
License : GNU Affero General Public License v3.0
Project Creator : ACEmulator

public void ExportTexture(string directory)
        {
            if (Length == 0) return;

            switch (Format)
            {
                case SurfacePixelFormat.PFID_CUSTOM_RAW_JPEG:
                    {
                        string filename = Path.Combine(directory, Id.ToString("X8") + ".jpg");
                        using (BinaryWriter writer = new BinaryWriter(File.Open(filename, FileMode.Create)))
                        {
                            writer.Write(SourceData);
                        }
                    }
                    break;

                default:
                    {
                        var bitmapImage = GetBitmap();
                        string filename = Path.Combine(directory, Id.ToString("X8") + ".png");
                        bitmapImage.Save(filename, ImageFormat.Png);
                    }
                    break;
            }
        }

19 View Source File : Wave.cs
License : GNU Affero General Public License v3.0
Project Creator : ACEmulator

public void ReadData(Stream stream)
        {
            var binaryWriter = new BinaryWriter(stream);

            binaryWriter.Write(System.Text.Encoding.ASCII.GetBytes("RIFF"));

            uint filesize = (uint)(Data.Length + 36); // 36 is added for all the extra we're adding for the WAV header format
            binaryWriter.Write(filesize);

            binaryWriter.Write(System.Text.Encoding.ASCII.GetBytes("WAVE"));

            binaryWriter.Write(System.Text.Encoding.ASCII.GetBytes("fmt"));
            binaryWriter.Write((byte)0x20); // Null ending to the fmt

            binaryWriter.Write((int)0x10); // 16 ... length of all the above

            // AC audio headers start at Format Type,
            // and are usually 18 bytes, with some exceptions
            // notably objectID A000393 which is 30 bytes

            // WAV headers are always 16 bytes from Format Type to end of header,
            // so this extra data is truncated here.
            binaryWriter.Write(Header.Take(16).ToArray());

            binaryWriter.Write(System.Text.Encoding.ASCII.GetBytes("data"));
            binaryWriter.Write((uint)Data.Length);
            binaryWriter.Write(Data);
        }

19 View Source File : Extensions.cs
License : GNU Affero General Public License v3.0
Project Creator : ACEmulator

public static void WriteString16L(this BinaryWriter writer, string data)
        {
            if (data == null) data = "";

            writer.Write((ushort)data.Length);
            writer.Write(System.Text.Encoding.GetEncoding(1252).GetBytes(data));

            // client expects string length to be a multiple of 4 including the 2 bytes for length
            writer.Pad(CalculatePadMultiple(sizeof(ushort) + (uint)data.Length, 4u));
        }

19 View Source File : Extensions.cs
License : GNU Affero General Public License v3.0
Project Creator : ACEmulator

public static void WritePackedDword(this BinaryWriter writer, uint value)
        {
            if (value <= 32767)
            {
                ushort networkValue = Convert.ToUInt16(value);
                writer.Write(BitConverter.GetBytes(networkValue));
            }
            else
            {
                uint packedValue = (value << 16) | ((value >> 16) | 0x8000);
                writer.Write(BitConverter.GetBytes(packedValue));
            }
        }

19 View Source File : Extensions.cs
License : GNU Affero General Public License v3.0
Project Creator : ACEmulator

public static void Pad(this BinaryWriter writer, uint pad) { writer.Write(new byte[pad]); }

19 View Source File : PacketHeaderOptional.cs
License : GNU Affero General Public License v3.0
Project Creator : ACEmulator

public void Unpack(BinaryReader reader, PacketHeader header)
        {
            Header = header;
            Size = (uint)reader.BaseStream.Position;
            BinaryWriter writer = new BinaryWriter(headerBytes);

            if (header.HasFlag(PacketHeaderFlags.ServerSwitch)) // 0x100
            {
                writer.Write(reader.ReadBytes(8));
            }

            if (header.HasFlag(PacketHeaderFlags.RequestRetransmit)) // 0x1000
            {
                if (reader.BaseStream.Length < reader.BaseStream.Position + 4) { IsValid = false; return; }
                uint retransmitCount = reader.ReadUInt32();
                writer.Write(retransmitCount);
                RetransmitData = new List<uint>();
                for (uint i = 0u; i < retransmitCount; i++)
                {
                    if (reader.BaseStream.Length < reader.BaseStream.Position + 4) { IsValid = false; return; }
                    uint sequence = reader.ReadUInt32();
                    writer.Write(sequence);
                    RetransmitData.Add(sequence);
                }
            }

            if (header.HasFlag(PacketHeaderFlags.RejectRetransmit)) // 0x2000
            {
                if (reader.BaseStream.Length < reader.BaseStream.Position + 4) { IsValid = false; return; }
                uint count = reader.ReadUInt32();
                writer.Write(count);
                for (int i = 0; i < count; i++)
                {
                    if (reader.BaseStream.Length < reader.BaseStream.Position + 4) { IsValid = false; return; }
                    writer.Write(reader.ReadBytes(4));
                }
            }

            if (header.HasFlag(PacketHeaderFlags.AckSequence)) // 0x4000
            {
                if (reader.BaseStream.Length < reader.BaseStream.Position + 4) { IsValid = false; return; }
                AckSequence = reader.ReadUInt32();
                writer.Write(AckSequence);
            }

            if (header.HasFlag(PacketHeaderFlags.LoginRequest)) // 0x10000
            {
                long position = reader.BaseStream.Position;
                long length = reader.BaseStream.Length - position;
                if (length < 1) { IsValid = false; return; }
                byte[] loginBytes = new byte[length];
                reader.BaseStream.Read(loginBytes, (int)position, (int)length);
                writer.Write(loginBytes);
                reader.BaseStream.Position = position;
            }

            if (header.HasFlag(PacketHeaderFlags.WorldLoginRequest)) // 0x20000
            {
                if (reader.BaseStream.Length < reader.BaseStream.Position + 8) { IsValid = false; return; }
                long position = reader.BaseStream.Position;
                writer.Write(reader.ReadBytes(8));
                reader.BaseStream.Position = position;
            }

            if (header.HasFlag(PacketHeaderFlags.ConnectResponse)) // 0x80000
            {
                long position = reader.BaseStream.Position;
                if (reader.BaseStream.Length < reader.BaseStream.Position + 8) { IsValid = false; return; }
                writer.Write(reader.ReadBytes(8));
                reader.BaseStream.Position = position;
            }

            if (header.HasFlag(PacketHeaderFlags.CICMDCommand)) // 0x400000
            {
                if (reader.BaseStream.Length < reader.BaseStream.Position + 8) { IsValid = false; return; }
                writer.Write(reader.ReadBytes(8));
            }

            if (header.HasFlag(PacketHeaderFlags.TimeSync)) // 0x1000000
            {
                if (reader.BaseStream.Length < reader.BaseStream.Position + 8) { IsValid = false; return; }
                TimeSynch = reader.ReadDouble();
                writer.Write(TimeSynch);
            }

            if (header.HasFlag(PacketHeaderFlags.Ecreplacedquest)) // 0x2000000
            {
                if (reader.BaseStream.Length < reader.BaseStream.Position + 4) { IsValid = false; return; }
                EcreplacedquestClientTime = reader.ReadSingle();
                writer.Write(EcreplacedquestClientTime);
            }

            if (header.HasFlag(PacketHeaderFlags.Flow)) // 0x8000000
            {
                if (reader.BaseStream.Length < reader.BaseStream.Position + 6) { IsValid = false; return; }
                FlowBytes = reader.ReadUInt32();
                FlowInterval = reader.ReadUInt16();
                writer.Write(FlowBytes);
                writer.Write(FlowInterval);
            }

            Size = (uint)reader.BaseStream.Position - Size;
        }

19 View Source File : PositionPack.cs
License : GNU Affero General Public License v3.0
Project Creator : ACEmulator

public static void Write(this BinaryWriter writer, PositionPack position)
        {
            writer.Write((uint)position.Flags);
            writer.Write(position.Origin);

            // choose valid sections by masking against flags
            if ((position.Flags & PositionFlags.OrientationHasNoW) == 0)
                writer.Write(position.Rotation.W);
            if ((position.Flags & PositionFlags.OrientationHasNoX) == 0)
                writer.Write(position.Rotation.X);
            if ((position.Flags & PositionFlags.OrientationHasNoY) == 0)
                writer.Write(position.Rotation.Y);
            if ((position.Flags & PositionFlags.OrientationHasNoZ) == 0)
                writer.Write(position.Rotation.Z);

            if ((position.Flags & PositionFlags.HasVelocity) != 0)
                writer.Write(position.Velocity);

            if ((position.Flags & PositionFlags.HasPlacementID) != 0)
                writer.Write((uint)position.PlacementID);

            writer.Write(position.InstanceSequence);
            writer.Write(position.PositionSequence);
            writer.Write(position.TeleportSequence);
            writer.Write(position.ForcePositionSequence);
        }

19 View Source File : MainWindow.xaml.cs
License : GNU General Public License v2.0
Project Creator : adrifcastr

private static void Extract(string nameSpace, string outDirectory, string internalFilePath, string resourceName)
        {
            replacedembly replacedembly = replacedembly.GetCallingreplacedembly();

            using (Stream s = replacedembly.GetManifestResourceStream(nameSpace + "." + (internalFilePath == "" ? "" : internalFilePath + ".") + resourceName))
            using (BinaryReader r = new BinaryReader(s))
            using (FileStream fs = new FileStream(outDirectory + "\\" + resourceName, FileMode.OpenOrCreate))
            using (BinaryWriter w = new BinaryWriter(fs))
                w.Write(r.ReadBytes((int)s.Length));
        }

19 View Source File : KoiHeap.cs
License : GNU General Public License v3.0
Project Creator : Aekras1a

protected override void WriteToImpl(BinaryWriter writer)
        {
            foreach(var chunk in chunks)
                writer.Write(chunk);
        }

19 View Source File : Pack.cs
License : Apache License 2.0
Project Creator : aequabit

public byte[] Serialize(params object[] data)
    {
        MemoryStream Stream = new MemoryStream();
        BinaryWriter Writer = new BinaryWriter(Stream, Encoding.UTF8);
        byte Current = 0;

        Writer.Write(Convert.ToByte(data.Length));

        for (int I = 0; I <= data.Length - 1; I++)
        {
            Current = Table[data[I].GetType()];
            Writer.Write(Current);

            switch (Current)
            {
                case 0:
                    Writer.Write((bool)data[I]);
                    break;
                case 1:
                    Writer.Write((byte)data[I]);
                    break;
                case 2:
                    Writer.Write(((byte[])data[I]).Length);
                    Writer.Write((byte[])data[I]);
                    break;
                case 3:
                    Writer.Write((char)data[I]);
                    break;
                case 4:
                    Writer.Write(((char[])data[I]).ToString());
                    break;
                case 5:
                    Writer.Write((decimal)data[I]);
                    break;
                case 6:
                    Writer.Write((double)data[I]);
                    break;
                case 7:
                    Writer.Write((int)data[I]);
                    break;
                case 8:
                    Writer.Write((long)data[I]);
                    break;
                case 9:
                    Writer.Write((sbyte)data[I]);
                    break;
                case 10:
                    Writer.Write((short)data[I]);
                    break;
                case 11:
                    Writer.Write((float)data[I]);
                    break;
                case 12:
                    Writer.Write((string)data[I]);
                    break;
                case 13:
                    Writer.Write((uint)data[I]);
                    break;
                case 14:
                    Writer.Write((ulong)data[I]);
                    break;
                case 15:
                    Writer.Write((ushort)data[I]);
                    break;
                case 16:
                    Writer.Write(((System.DateTime)data[I]).ToBinary());
                    break;
            }
        }

        Writer.Close();
        return Stream.ToArray();
    }

19 View Source File : ImageHelper.cs
License : GNU General Public License v3.0
Project Creator : affederaffe

internal static void WriteNullableSprite(this BinaryWriter binaryWriter, Sprite? sprite)
        {
            if (sprite is null)
            {
                binaryWriter.Write(false);
                return;
            }

            byte[] textureBytes = BytesFromTexture2D(sprite.texture);
            binaryWriter.Write(true);
            binaryWriter.Write(textureBytes.Length);
            binaryWriter.Write(textureBytes);
        }

19 View Source File : File.cs
License : GNU General Public License v3.0
Project Creator : ahmed605

public void Save()
        {
            if (Header.EntryCount > 0)
            {
                _stream.Position = 0;

                var bw = new BinaryWriter(_stream);

                Header.Write(bw);

                // Recalculate the offset/sizes of the TOC entries
                var tocOffset = _stream.Position;

                // Find the max value here
                var dataOffset = int.MinValue;
                foreach (var entry in TOC)
                {
                    var offset = entry.OffsetBlock + entry.UsedBlocks;
                    if (offset > dataOffset)
                    {
                        dataOffset = offset;
                    }
                }

                foreach (var entry in TOC)
                {
                    if (entry.CustomData != null)
                    {
                        var blockCount = (int)Math.Ceiling((float)entry.CustomData.Length / TOCEntry.BlockSize);
                        if (blockCount <= entry.UsedBlocks)
                        {
                            // Clear up the old data
                            _stream.Seek(entry.OffsetBlock * TOCEntry.BlockSize, SeekOrigin.Begin);
                            bw.Write(new byte[entry.UsedBlocks * TOCEntry.BlockSize]);

                            // We can fit it in the existing block... so lets do that.
                            _stream.Seek(entry.OffsetBlock * TOCEntry.BlockSize, SeekOrigin.Begin);
                        }
                        else
                        {
                            // Clear up the old data
                            _stream.Seek(entry.OffsetBlock * TOCEntry.BlockSize, SeekOrigin.Begin);
                            bw.Write(new byte[entry.UsedBlocks * TOCEntry.BlockSize]);

                            // Fit it at the end of the stream
                            entry.OffsetBlock = dataOffset;
                            _stream.Seek(dataOffset*TOCEntry.BlockSize, SeekOrigin.Begin);
                            dataOffset += blockCount;
                        }

                        entry.UsedBlocks = (short)blockCount;

                        bw.Write(entry.CustomData);

                        if ((entry.CustomData.Length % TOCEntry.BlockSize) != 0)
                        {
                            var padding = new byte[blockCount * TOCEntry.BlockSize - entry.CustomData.Length];
                            bw.Write(padding);
                        }

                        entry.SetCustomData(null);
                    }
                }

                _stream.Seek(tocOffset, SeekOrigin.Begin);

                TOC.Write(bw);
            }
        }

19 View Source File : File.cs
License : GNU General Public License v3.0
Project Creator : ahmed605

public void Save()
        {
            if (Header.EntryCount > 0)
            {
                _stream.Position = 0;

                var bw = new BinaryWriter(_stream);

                Header.Write(bw);

                // Recalculate the offset/sizes of the TOC entries
                var tocOffset = 0x800;

                // Find the max value here
                var dataOffset = int.MinValue;
                foreach (var entry in TOC)
                {
                    var fileEntry = entry as FileEntry;
                    if (fileEntry != null)
                    {
                        var offset = fileEntry.Offset + fileEntry.SizeUsed;
                        if (offset > dataOffset)
                        {
                            dataOffset = offset;
                        }                        
                    }
                }

                foreach (var entry in TOC)
                {
                    var fileEntry = entry as FileEntry;
                    if (fileEntry != null && fileEntry.CustomData != null)
                    {
                        var blockCount = (int)Math.Ceiling((float)fileEntry.CustomData.Length / FileEntry.BlockSize);
                        var blockSize = blockCount*FileEntry.BlockSize;

                        if (blockSize <= fileEntry.SizeUsed)
                        {
                            // Clear up the old data
                            _stream.Seek(fileEntry.Offset, SeekOrigin.Begin);
                            bw.Write(new byte[fileEntry.SizeUsed]);

                            // We can fit it in the existing block... so lets do that.
                            _stream.Seek(fileEntry.Offset, SeekOrigin.Begin);
                        }
                        else
                        {
                            // Clear up the old data
                            _stream.Seek(fileEntry.Offset, SeekOrigin.Begin);
                            bw.Write(new byte[fileEntry.SizeUsed]);

                            // Fit it at the end of the stream
                            fileEntry.Offset = dataOffset;
                            _stream.Seek(dataOffset, SeekOrigin.Begin);
                            dataOffset += blockSize;
                        }

                        fileEntry.SizeUsed = blockSize;

                        bw.Write(fileEntry.CustomData);

                        if ((fileEntry.CustomData.Length % FileEntry.BlockSize) != 0)
                        {
                            var padding = new byte[blockSize - fileEntry.CustomData.Length];
                            bw.Write(padding);
                        }

                        fileEntry.SetCustomData(null);
                    }
                }

                _stream.Seek(tocOffset, SeekOrigin.Begin);

                TOC.Write(bw);
            }
        }

19 View Source File : TOC.cs
License : GNU General Public License v3.0
Project Creator : ahmed605

public void Write(BinaryWriter bw)
        {
            foreach (var entry in _entries)
            {
                entry.Write(bw);
            }

            foreach (var s in _nameTable)
            {
                byte[] nameData = Encoding.ASCII.GetBytes(s);
                bw.Write( nameData );
                bw.Write( (byte)0 );
            }
        }

19 View Source File : TOC.cs
License : GNU General Public License v3.0
Project Creator : ahmed605

public void Write(BinaryWriter bw)
        {
            foreach (var entry in _entries)
            {
                entry.Write(bw);
            }

            byte[] stringData = Encoding.ASCII.GetBytes(_nameStringTable);
            bw.Write(stringData);
        }

See More Examples