public static void SendMessage(BinaryWriter toClientWriter, string message) { var text = new { text = "[反作弊破解]", bold = true, color = "green", extra = new List <object> { new { text = message, bold = true, color = "green" } } }; string json = JsonConvert.SerializeObject(text); Package package = Package.Create((byte)0x02, ProtoBufUtil.GetVarStringBytes(json), Global.IsVersion1_12_2); toClientWriter.Write(package.OriginData); }
/// <summary> /// 读取一个package /// </summary> /// <param name="reader"></param> /// <returns></returns> public static Package ReadPackage(this BinaryReader reader) { List <byte> packageLengthBytes = new List <byte>(); int packageLength = reader.ReadVarInt(packageLengthBytes); //byte[] packageLengthBytes = ProtoBufUtil.GetVarIntBytes(packageLength); byte[] packageData = reader.ReadBytes(packageLength); List <byte> originData = new List <byte>(); originData.AddRange(packageLengthBytes); originData.AddRange(packageData); //如果压缩了, 就获取压缩后的数据 if (Global.IsCompression) { using (MemoryStream ms = new MemoryStream(packageData)) { using (BinaryReader br = new BinaryReader(ms)) { int uncompressionDataLength = br.ReadVarInt(); packageData = br.ReadBytes(Convert.ToInt32(br.BaseStream.Length - br.BaseStream.Position)); if (uncompressionDataLength != 0) { packageData = ZLibUtil.Decompress(packageData); } } } } List <byte> packageIdBytes = new List <byte>(); int packageId = ProtoBufUtil.GetVarIntFromBytes(packageData, packageIdBytes); return(new Package { PackageLength = packageLength, PackageId = (byte)packageId, OriginData = originData.ToArray(), Data = packageData.Where((x, i) => i >= packageIdBytes.Count).ToArray() }); }
/// <summary> /// 写入varint的数据 /// </summary> /// <param name="paramInt"></param> /// <returns></returns> public static void GetVarInt(this BinaryWriter writer, int paramInt) { byte[] output = ProtoBufUtil.GetVarIntBytes(paramInt); writer.Write(output); }
/// <summary> /// 写入varstring的数据 /// </summary> /// <param name="content"></param> /// <returns></returns> public static void GetString(this BinaryWriter writer, string content) { byte[] output = ProtoBufUtil.GetVarStringBytes(content); writer.Write(output); }