コード例 #1
0
ファイル: Packet.cs プロジェクト: R3DIANCE/MuEmu
        public byte[] Encode(object message, ref short serial)
        {
            var factory = _factories.First(f => f.ContainsType(message.GetType()));

            ushort opCode = factory.GetOpCode(message.GetType());

            var att = message
                      .GetType()
                      .GetCustomAttributes(false)
                      .Where(x => x.GetType() == typeof(WZContractAttribute))
                      .FirstOrDefault() as WZContractAttribute;

            if (att == null)
            {
                throw new InvalidOperationException("Invalid message format");
            }

            byte[] res;

            using (var data = new MemoryStream())
            {
                var opCodeSize = (opCode & 0xFF00) == 0xFF00 ? 1 : 2;
                if (att.LongMessage)
                {
                    Serializer.Serialize(data, new WZWPacket(0xC2, (ushort)data.Length, opCode));
                }
                else
                {
                    Serializer.Serialize(data, new WZBPacket(0xC1, (byte)data.Length, opCode));
                }

                try
                {
                    data.Position = (att.LongMessage ? 3 : 2) + opCodeSize;
                    Serializer.Serialize(data, message);
                }
                catch (Exception e)
                {
                    Logger.Error(e, "");
                }

                if (att.Serialized)
                {
                    data.Position = (att.LongMessage ? 3 : 2);
                    if (_packetRijndael == true)
                    {
                        PacketEncrypt.Encrypt(data, data);
                    }
                    else
                    {
                        SimpleModulus.Encrypt(data, (byte)serial, data);
                    }

                    serial++;
                    data.Position = 0;
                    data.WriteByte((byte)(att.LongMessage ? 0xC4 : 0xC3));
                }

                data.Position = 1;
                if (att.LongMessage)
                {
                    data.Write(BitConverter.GetBytes(((ushort)data.Length).ShufleEnding()), 0, 2);
                }
                else
                {
                    data.Write(BitConverter.GetBytes((byte)data.Length), 0, 1);
                }
                res = data.ToArray();
                //if(!att.Serialized)
                return(res);
            }

            /*byte[] result;
            *  using (var h = new MemoryStream())
            *  using (var b = new MemoryStream())
            *  {
            *   try
            *   {
            *       Serializer.Serialize(b, message);
            *   }
            *   catch (Exception e)
            *   {
            *       Logger.Error(e, "");
            *   }
            *   var body = b.ToArray();
            *   var length = body.Length;
            *   var sizeFix = (((opCode & 0xFF00) == 0xFF00) ? 1 : 0);
            *   var sizeFix2 = 0;
            *
            *   if (att.LongMessage)
            *   {
            *       var header = new WZWPacket
            *       {
            *           Type = (byte)0xC2,
            *           Size = (ushort)(length + 5 - sizeFix),
            *           Operation = opCode
            *       };
            *       Serializer.Serialize(h, header);
            *
            *       sizeFix2 = 3;
            *   }
            *   else
            *   {
            *       var header = new WZBPacket
            *       {
            *           Type = (byte)0xC1,
            *           Size = (byte)(length + 4 - sizeFix),
            *           Operation = opCode
            *       };
            *       Serializer.Serialize(h, header);
            *
            *       sizeFix2 = 2;
            *   }
            *
            *   var head = h.ToArray();
            *   var headLen = head.Length - sizeFix;
            *   result = new byte[headLen + body.Length];
            *   Array.Copy(head, result, headLen);
            *   Array.Copy(body, 0, result, headLen, body.Length);
            *
            *   if (att.Serialized)
            *   {
            *       result[0] += 2;
            *
            *       var temp = new byte[result.Length - sizeFix2];
            *       Array.Copy(result, sizeFix2, temp, 0, temp.Length);
            *
            *       //temp[0] = (byte)serial;
            *
            *       byte[] enc;// = SimpleModulus.Encoder(temp);
            *       using (var ms = new MemoryStream())
            *       {
            *           if (_packetRijndael == true)
            *               PacketEncrypt.Encrypt(ms, new MemoryStream(temp));
            *           else
            *               SimpleModulus.Encrypt(ms, (byte)serial, new MemoryStream(temp));
            *
            *           enc = new byte[ms.Length];
            *           ms.Seek(0, SeekOrigin.Begin);
            *           ms.Read(enc, 0, (int)ms.Length);
            *       }
            *
            *       var resultTemp = new byte[sizeFix2 + enc.Length + 1];
            *       Array.Copy(result, resultTemp, sizeFix2 + 1);
            *       if (resultTemp[0] == 0xC3)
            *       {
            *           resultTemp[1] = (byte)(resultTemp.Length);
            *       }
            *       else
            *       {
            *           resultTemp[1] = (byte)(resultTemp.Length >> 8);
            *           resultTemp[2] = (byte)(resultTemp.Length & 0xff);
            *       }
            *       Array.Copy(enc, 0, resultTemp, sizeFix2 + 1, enc.Length);
            *       serial++;
            *
            *       Logger.Debug("\nA:{0}\nB{1}", result.GetHex(), res.GetHex());
            *       return res;
            *   }
            *  }
            *
            *  return result;*/
        }
コード例 #2
0
ファイル: Packet.cs プロジェクト: Yomalex/MuEmu
        public byte[] Encode(object message, ref short serial)
        {
            MessageFactory factory = null;

            try
            {
                factory = _factories.First(f => f.ContainsType(message.GetType()));
            }catch (Exception ex)
            {
                throw new Exception("Unregisted message " + message.GetType().ToString(), ex);
            }

            ushort opCode = factory.GetOpCode(message.GetType());

            var att = message
                      .GetType()
                      .GetCustomAttributes(false)
                      .Where(x => x.GetType() == typeof(WZContractAttribute))
                      .FirstOrDefault() as WZContractAttribute;

            if (att == null)
            {
                throw new InvalidOperationException("Invalid message format");
            }

            byte[] res;

            using (var data = new MemoryStream())
            {
                var opCodeSize = (opCode & 0xFF00) == 0xFF00 ? 1 : 2;
                if (att.LongMessage)
                {
                    Serializer.Serialize(data, new WZWPacket(0xC2, (ushort)data.Length, opCode));
                }
                else
                {
                    Serializer.Serialize(data, new WZBPacket(0xC1, (byte)data.Length, opCode));
                }

                try
                {
                    data.Position = (att.LongMessage ? 3 : 2) + opCodeSize;
                    Serializer.Serialize(data, message);
                }
                catch (Exception e)
                {
                    Logger.Error(e, "");
                }

                if (att.Serialized)
                {
                    data.Position = (att.LongMessage ? 3 : 2);
                    if (_packetRijndael == true)
                    {
                        PacketEncrypt.Encrypt(data, data);
                        //PacketPrint(data);
                    }
                    else
                    {
                        SimpleModulus.Encrypt(data, (byte)serial, data);
                    }

                    serial++;
                    data.Position = 0;
                    data.WriteByte((byte)(att.LongMessage ? 0xC4 : 0xC3));
                }

                data.Position = 1;
                if (att.LongMessage)
                {
                    data.Write(BitConverter.GetBytes(((ushort)data.Length).ShufleEnding()), 0, 2);
                }
                else
                {
                    data.Write(BitConverter.GetBytes((byte)data.Length), 0, 1);
                }
                res = data.ToArray();

                //if(!att.Serialized)
                return(res);
            }
        }