// websocket数据解包
        public byte[] AnalyticData(byte[] recBytes, int recByteLength)
        {
            if (recByteLength < 2)
            {
                return(null);
            }


            bool fin = (recBytes[0] & 0x80) == 0x80; // 1bit,1表示最后一帧

            if (!fin)
            {
                return(null);// 超过一帧暂不处理
            }

            bool mask_flag = (recBytes[1] & 0x80) == 0x80; // 是否包含掩码

            if (!mask_flag)
            {
                return(null);// 不包含掩码的暂不处理
            }

            int payload_len = recBytes[1] & 0x7F; // 数据长度

            byte[] masks = new byte[4];
            byte[] messageData;

            if (payload_len == 126)
            {
                Array.Copy(recBytes, 4, masks, 0, 4);
                payload_len = (ushort)(recBytes[2] << 8 | recBytes[3]);
                messageData = new byte[payload_len];
                Array.Copy(recBytes, 8, messageData, 0, payload_len);
            }
            else if (payload_len == 127)
            {
                Array.Copy(recBytes, 10, masks, 0, 4);
                byte[] uInt64Bytes = new byte[8];
                for (int i = 0; i < 8; i++)
                {
                    uInt64Bytes[i] = recBytes[9 - i];
                }
                ulong len = BitConverter.ToUInt64(uInt64Bytes, 0);

                messageData = new byte[len];
                for (ulong i = 0; i < len; i++)
                {
                    messageData[i] = recBytes[i + 14];
                }
            }
            else
            {
                Array.Copy(recBytes, 2, masks, 0, 4);
                messageData = new byte[payload_len];
                Array.Copy(recBytes, 6, messageData, 0, payload_len);
            }

            for (var i = 0; i < payload_len; i++)
            {
                messageData[i] = (byte)(messageData[i] ^ masks[i % 4]);
            }

            return(messageData);
        }
Exemplo n.º 2
0
        private static MovementInfo ReadMovementUpdateBlock(Packet packet, WowGuid guid, object index)
        {
            var moveInfo = new MovementInfo();

            var guid1               = new byte[8];
            var transportGuid       = new byte[8];
            var goTransportGuid     = new byte[8];
            var attackingTargetGuid = new byte[8];
            var guid5               = new byte[8];
            var guid6               = new byte[8];
            var guid7               = new byte[8];

            var bit160 = false;
            var bit528 = false;
            var bit600 = false;
            var bit544 = false;
            var bit526 = false;
            var bit552 = false;
            var bit524 = false;
            var bit572 = false;
            var bit525 = false;
            var bit664 = false;
            var bit527 = false;
            var bit536 = false;
            var bit644 = false;
            var bit560 = false;

            var hasAnimKit1                   = false;
            var hasAnimKit2                   = false;
            var hasAnimKit3                   = false;
            var hasFullSpline                 = false;
            var hasSplineStartTime            = false;
            var hasSplineVerticalAcceleration = false;
            var hasMovementFlags              = false;
            var hasMoveFlagsExtra             = false;
            var hasTimestamp                  = false;
            var hasOrientation                = false;
            var hasTransportData              = false;
            var hasTransportTime2             = false;
            var hasTransportTime3             = false;
            var hasGOTransportTime2           = false;
            var hasGOTransportTime3           = false;
            var hasPitch           = false;
            var hasFallData        = false;
            var hasFallDirection   = false;
            var hasSplineElevation = false;

            var bits168 = 0u;

            var splineCount = 0u;
            var bits138     = 0u;
            var bits98      = 0u;
            var bits25C     = 0u;
            var bits26C     = 0u;
            var bits288     = 0u;
            var bits418     = 0u;
            var bits2AA     = 0u;
            var splineType  = SplineType.Stop;

            var bit676      = packet.ReadBit("bit676", index);
            var hasAnimKits = packet.ReadBit("Has Anim Kits", index); // 498
            var isLiving    = packet.ReadBit("Is Living", index);     // 368
            var bit810      = packet.ReadBit("bit810", index);

            packet.ReadBit();                                                           //fake bit
            var transportFrames = packet.ReadBits("Transport Frames Count", 22, index); // 1068
            var hasVehicleData  = packet.ReadBit("Has Vehicle Data", index);            // 488
            var bit1044         = packet.ReadBit("bit1044", index);

            packet.ReadBit();                                                             //fake bit
            var bit476 = packet.ReadBit("bit476", index);
            var hasGameObjectRotation = packet.ReadBit("Has GameObject Rotation", index); // 512

            packet.ReadBit();                                                             //fake bit
            var bit680             = packet.ReadBit("bit680", index);
            var hasAttackingTarget = packet.ReadBit("Has Attacking Target", index);       // 464
            var hasSceneObjectData = packet.ReadBit("Has Scene Object Data", index);      // 1032
            var bit1064            = packet.ReadBit("bit1064", index);

            packet.ReadBit();                                                             //fake bit
            var bit668 = packet.ReadBit("bit668", index);
            var hasTransportPosition = packet.ReadBit("Has Transport Position", index);   // 424
            var bit681 = packet.ReadBit("bit681", index);
            var hasStationaryPosition = packet.ReadBit("Has Stationary Position", index); // 448

            if (isLiving)
            {
                guid1[2] = packet.ReadBit();
                var bit140 = packet.ReadBit();
                hasPitch         = !packet.ReadBit("Has Pitch", index);         //104
                hasTransportData = packet.ReadBit("Has Transport Data", index); //96
                packet.ReadBit();                                               // 164 fake bit

                if (hasTransportData)
                {
                    transportGuid[4]  = packet.ReadBit();
                    transportGuid[2]  = packet.ReadBit();
                    hasTransportTime3 = packet.ReadBit();
                    transportGuid[0]  = packet.ReadBit();
                    transportGuid[1]  = packet.ReadBit();
                    transportGuid[3]  = packet.ReadBit();
                    transportGuid[6]  = packet.ReadBit();
                    transportGuid[7]  = packet.ReadBit();
                    hasTransportTime2 = packet.ReadBit();
                    transportGuid[5]  = packet.ReadBit();
                }
                hasTimestamp     = !packet.ReadBit();
                guid1[6]         = packet.ReadBit();
                guid1[4]         = packet.ReadBit();
                guid1[3]         = packet.ReadBit();
                hasOrientation   = !packet.ReadBit(); //40
                bit160           = !packet.ReadBit();
                guid1[5]         = packet.ReadBit();
                bits98           = packet.ReadBits("bits98", 22, index); //144
                hasMovementFlags = !packet.ReadBit();
                bits168          = packet.ReadBits(19);                  //352

                /*var bits356 = new uint[bits168];
                 * for (var i = 0; i < bits168; ++i)
                 * {
                 *  bits356[i] = packet.ReadBits(2);
                 * }*/

                hasFallData = packet.ReadBit(); //132

                if (hasMovementFlags)
                {
                    moveInfo.Flags = (uint)packet.ReadBitsE <MovementFlag>("Movement Flags", 30, index);
                }

                hasSplineElevation     = !packet.ReadBit("Has SplineElevation", index);//136
                moveInfo.HasSplineData = packet.ReadBit("Has SplineData", index);
                var bit141 = packet.ReadBit();
                guid1[0] = packet.ReadBit();
                guid1[7] = packet.ReadBit();
                guid1[1] = packet.ReadBit();

                if (moveInfo.HasSplineData)
                {
                    hasFullSpline = packet.ReadBit();
                    if (hasFullSpline)
                    {
                        hasSplineVerticalAcceleration = packet.ReadBit(); //260
                        hasSplineStartTime            = packet.ReadBit(); //252
                        var bit304 = packet.ReadBit();

                        splineCount = packet.ReadBits(20);
                        packet.ReadBitsE <SplineMode>("Spline Mode", 2, index);
                        packet.ReadBitsE <SplineFlag434>("Spline flags", 25, index);

                        if (bit304)
                        {
                            bits138 = packet.ReadBits(21);
                            packet.ReadBits(2);
                        }
                    }
                }

                hasMoveFlagsExtra = !packet.ReadBit(); //20

                if (hasFallData)
                {
                    hasFallDirection = packet.ReadBit(); //128
                }
                if (hasMoveFlagsExtra)
                {
                    moveInfo.Flags2 = (uint)packet.ReadBitsE <MovementFlag2>("Extra Movement Flags", 13, index);
                }
            }

            /*if (hasSceneObjectData)
             * {
             *
             * }*/

            if (hasTransportPosition) //424
            {
                packet.StartBitStream(goTransportGuid, 4, 1, 0);
                hasGOTransportTime2 = packet.ReadBit(); // 420
                packet.StartBitStream(goTransportGuid, 6, 5, 3, 2, 7);
                hasGOTransportTime3 = packet.ReadBit(); // 412
            }

            if (bit668)
            {
                bit528 = packet.ReadBit();
                bit600 = packet.ReadBit();
                bit544 = packet.ReadBit();
                bit526 = packet.ReadBit();
                bit552 = packet.ReadBit();
                bit524 = packet.ReadBit();
                bit572 = packet.ReadBit();
                bit525 = packet.ReadBit();
                bit664 = packet.ReadBit();
                bit527 = packet.ReadBit();

                if (bit664)
                {
                    bits288 = packet.ReadBits(20);
                }

                bit536 = packet.ReadBit();
                bit644 = packet.ReadBit();
                bit560 = packet.ReadBit();

                if (bit664)
                {
                    bits25C = packet.ReadBits(21); //604
                    bits26C = packet.ReadBits(21); //624
                }
            }

            if (hasAnimKits)
            {
                hasAnimKit2 = !packet.ReadBit();
                hasAnimKit3 = !packet.ReadBit();
                hasAnimKit1 = !packet.ReadBit();
            }

            if (hasAttackingTarget)
            {
                attackingTargetGuid = packet.StartBitStream(4, 6, 5, 2, 0, 1, 3, 7);
            }

            if (bit1064)
            {
                bits418 = packet.ReadBits(22);
            }

            if (bit810)
            {
                bits2AA = packet.ReadBits(7);
            }

            packet.ResetBitReader();

            for (var i = 0; i < transportFrames; ++i)
            {
                packet.ReadInt32("Transport frame", index, i);
            }

            /*if (hasSceneObjectData)
             * {
             *  until if ( *(_BYTE *)(v3 + 668) )
             * }*/

            if (isLiving)
            {
                if (hasTransportData) //96
                {
                    moveInfo.Transport = new MovementInfo.TransportInfo();

                    packet.ReadXORByte(transportGuid, 7);
                    moveInfo.Transport.Offset.X = packet.ReadSingle();

                    if (hasTransportTime3)
                    {
                        packet.ReadUInt32("Transport Time 3", index);
                    }

                    moveInfo.Transport.Offset.O = packet.ReadSingle();
                    moveInfo.Transport.Offset.Y = packet.ReadSingle();
                    packet.ReadXORByte(transportGuid, 4);
                    packet.ReadXORByte(transportGuid, 1);
                    packet.ReadXORByte(transportGuid, 3);
                    moveInfo.Transport.Offset.Z = packet.ReadSingle();
                    packet.ReadXORByte(transportGuid, 5);

                    if (hasTransportTime2)
                    {
                        packet.ReadUInt32("Transport Time 2", index);
                    }

                    packet.ReadXORByte(transportGuid, 0);
                    var seat = packet.ReadSByte("Transport Seat", index); //72
                    packet.ReadXORByte(transportGuid, 6);
                    packet.ReadXORByte(transportGuid, 2);
                    packet.ReadUInt32("Transport Time", index); //76

                    moveInfo.Transport.Guid = new WowGuid64(BitConverter.ToUInt64(transportGuid, 0));
                    packet.AddValue("Transport GUID", moveInfo.Transport.Guid, index);
                    packet.AddValue("Transport Position", moveInfo.Transport.Offset, index);

                    if (moveInfo.Transport.Guid.HasEntry() && moveInfo.Transport.Guid.GetHighType() == HighGuidType.Vehicle &&
                        guid.HasEntry() && guid.GetHighType() == HighGuidType.Creature)
                    {
                        VehicleTemplateAccessory vehicleAccessory = new VehicleTemplateAccessory
                        {
                            Entry          = moveInfo.Transport.Guid.GetEntry(),
                            AccessoryEntry = guid.GetEntry(),
                            SeatId         = seat
                        };

                        Storage.VehicleTemplateAccessories.Add(vehicleAccessory, packet.TimeSpan);
                    }
                }

                packet.ReadXORByte(guid1, 4);

                /*for (var i = 0; i < 10; ++i)
                 *  packet.ReadSingle("unk float");
                 * if (bits98 > 0)
                 *  packet.ReadBits((int)bits98);*/
                //for (var i = 0; i < bits98; ++i)
                //    packet.ReadInt32("Int9C", index, i);

                if (moveInfo.HasSplineData)                            //344
                {
                    if (hasFullSpline)                                 //336
                    {
                        packet.ReadUInt32("Spline Time", index);       //232
                        packet.ReadSingle("Duration Mod Next", index); //244

                        //    NYI block here
                        packet.ReadSingle("Duration Mod", index); //240

                        for (uint i = 0; i < splineCount; i++)
                        {
                            Vector3 v = new Vector3();
                            v.X = packet.ReadSingle(); //0
                            v.Z = packet.ReadSingle(); //8
                            v.Y = packet.ReadSingle(); //4

                            packet.AddValue("Spline", v, index);
                        }

                        if (hasSplineStartTime)
                        {
                            packet.ReadUInt32("Spline Start time", index); //256
                        }
                        var type = packet.ReadByte();                      // 228

                        switch (type)
                        {
                        case 1:
                            splineType = SplineType.Normal;
                            break;

                        case 2:
                            splineType = SplineType.FacingSpot;
                            break;

                        case 3:
                            splineType = SplineType.FacingTarget;
                            break;

                        case 4:
                            splineType = SplineType.FacingAngle;
                            break;
                        }

                        if (splineType == SplineType.FacingAngle) // == 4
                        {
                            packet.ReadSingle("Facing Angle", index);
                        }

                        if (splineType == SplineType.FacingSpot) // == 2
                        {
                            packet.ReadVector3("Facing spot", index);
                        }

                        if (hasSplineVerticalAcceleration) //252
                        {
                            packet.ReadSingle("Spline Vertical Acceleration", index);
                        }

                        packet.ReadUInt32("Spline FULL Time", index); //236
                    }

                    moveInfo.Position.X = packet.ReadSingle(); //212
                    moveInfo.Position.Z = packet.ReadSingle(); //220
                    packet.ReadUInt32("Spline ID", index);     //208
                    moveInfo.Position.Y = packet.ReadSingle(); //216
                }
                packet.ReadSingle("Fly Speed", index);         //188

                if (bit160)
                {
                    packet.ReadUInt32("unk160");
                }

                packet.ReadXORByte(guid1, 2);

                if (hasFallData)                                    //132
                {
                    if (hasFallDirection)                           //128
                    {
                        packet.ReadSingle("Jump Sin Angle", index); //124
                        packet.ReadSingle("Jump XY Speed", index);  //116
                        packet.ReadSingle("Jump Cos Angle", index); //120
                    }
                    packet.ReadUInt32("Jump Fall Time", index);     //108
                    packet.ReadSingle("Jump Z Speed", index);       //112
                }
                packet.ReadXORByte(guid1, 1);
                packet.ReadSingle("Turn Speed", index);

                if (hasTimestamp)
                {
                    packet.ReadUInt32("Time?", index);
                }

                packet.ReadSingle("Swim Speed", index);           //176

                if (hasSplineElevation)                           //136
                {
                    packet.ReadSingle("Spline Elevation", index); //196
                }
                packet.ReadXORByte(guid1, 7);
                packet.ReadSingle("Pitch Speed", index); //200

                for (var i = 0; i < bits98; ++i)
                {
                    packet.ReadInt32("UNK counter", index, i);
                }

                moveInfo.Position.X = packet.ReadSingle(); //28

                if (hasPitch)
                {
                    packet.ReadSingle("Pitch", index); //104
                }
                if (hasOrientation)
                {
                    moveInfo.Orientation = packet.ReadSingle("Orientation", index);  //40
                }
                moveInfo.WalkSpeed  = packet.ReadSingle("Walk Speed", index) / 2.5f; // 168
                moveInfo.Position.Y = packet.ReadSingle();                           //32
                packet.ReadSingle("Fly Back Speed", index);                          //192
                packet.ReadXORByte(guid1, 3);
                packet.ReadXORByte(guid1, 5);
                packet.ReadXORByte(guid1, 6);
                packet.ReadXORByte(guid1, 0);
                packet.ReadSingle("Run Back Speed", index);                       //184
                moveInfo.RunSpeed = packet.ReadSingle("Run Speed", index) / 7.0f; //172
                packet.ReadSingle("Swim Back Speed", index);                      //180
                moveInfo.Position.Z = packet.ReadSingle();                        //36
            }

            packet.AddValue("Position", moveInfo.Position, index);

            if (bit668)
            {
                if (bit664)
                {
                    for (var i = 0; i < bits288; ++i)
                    {
                        packet.ReadSingle("Float652+4", index, i);
                        packet.ReadSingle("Float652", index, i);
                        packet.ReadSingle("Float652+8", index, i);
                    }
                }

                if (bit600)
                {
                    packet.ReadSingle("Float584", index);
                    packet.ReadSingle("Float580", index);
                    packet.ReadSingle("Float596", index);
                    packet.ReadSingle("Float592", index);
                    packet.ReadSingle("Float576", index);
                    packet.ReadSingle("Float588", index);
                }

                if (bit644)
                {
                    for (var i = 0; i < bits25C; ++i)
                    {
                        packet.ReadSingle("Float608", index, i);
                        packet.ReadSingle("Float608+4", index, i);
                    }

                    for (var i = 0; i < bits26C; ++i)
                    {
                        packet.ReadSingle("Float260+0", index, i);
                        packet.ReadSingle("Float260+1", index, i);
                    }

                    packet.ReadSingle("Float624", index);
                    packet.ReadSingle("Float624+4", index);
                }

                packet.ReadUInt32("unk520", index);

                if (bit544)
                {
                    packet.ReadUInt32("unk544", index);
                }

                if (bit552)
                {
                    packet.ReadUInt32("unk548", index);
                }

                if (bit536)
                {
                    packet.ReadUInt32("unk532", index);
                }

                if (bit560)
                {
                    packet.ReadUInt32("unk556", index);
                }

                if (bit572)
                {
                    packet.ReadSingle("Float564", index);
                    packet.ReadSingle("Float568", index);
                }
            }

            if (hasTransportPosition)
            {
                moveInfo.Transport = new MovementInfo.TransportInfo();

                if (hasGOTransportTime2)
                {
                    packet.ReadUInt32("Transport Time 2", index);
                }

                moveInfo.Transport.Offset.Y = packet.ReadSingle(); //388
                packet.ReadSByte("Transport Seat", index);         //400
                moveInfo.Transport.Offset.X = packet.ReadSingle(); //384

                packet.ReadXORBytes(goTransportGuid, 2, 4, 1);

                if (hasGOTransportTime3)
                {
                    packet.ReadUInt32("Transport Time 3", index);  //412
                }
                packet.ReadUInt32("Transport Time", index);        //404
                moveInfo.Transport.Offset.O = packet.ReadSingle(); //396
                moveInfo.Transport.Offset.Z = packet.ReadSingle(); //392

                packet.ReadXORBytes(goTransportGuid, 6, 0, 5, 3, 7);

                moveInfo.Transport.Guid = new WowGuid64(BitConverter.ToUInt64(goTransportGuid, 0));
                packet.AddValue("Transport GUID", moveInfo.Transport.Guid, index);
                packet.AddValue("Transport Position", moveInfo.Transport.Offset, index);
            }

            if (hasAttackingTarget)
            {
                packet.ParseBitStream(attackingTargetGuid, 7, 1, 5, 2, 6, 3, 0, 4);
                packet.WriteGuid("Attacking Target GUID", attackingTargetGuid, index);
            }

            if (hasVehicleData)
            {
                moveInfo.VehicleId = packet.ReadUInt32("Vehicle Id", index);
                packet.ReadSingle("Vehicle Orientation", index);
            }

            if (hasStationaryPosition)
            {
                moveInfo.Position.Y  = packet.ReadSingle();
                moveInfo.Position.Z  = packet.ReadSingle();
                moveInfo.Orientation = packet.ReadSingle("Stationary Orientation", index);
                moveInfo.Position.X  = packet.ReadSingle();

                packet.AddValue("Stationary Position", moveInfo.Position, index);
            }

            if (bit676)
            {
                packet.ReadUInt32("unk672");
            }

            if (hasAnimKits)
            {
                if (hasAnimKit1)
                {
                    packet.ReadUInt16("Anim Kit 1", index);
                }
                if (hasAnimKit3)
                {
                    packet.ReadUInt16("Anim Kit 3", index);
                }
                if (hasAnimKit2)
                {
                    packet.ReadUInt16("Anim Kit 2", index);
                }
            }

            if (bit810)
            {
                packet.ReadBytes("Bytes", (int)bits2AA);
            }

            if (bit476)
            {
                packet.ReadUInt32("unk472");
            }

            if (bit1064)
            {
                for (var i = 0; i < bits418; ++i)
                {
                    packet.ReadInt32("unk1052+4", index, i);
                }
            }

            if (hasGameObjectRotation)
            {
                moveInfo.Rotation = packet.ReadPackedQuaternion("GameObject Rotation", index);
            }

            if (bit1044)
            {
                packet.ReadInt32("unk1040", index);
            }

            if (isLiving && moveInfo.HasSplineData && hasFullSpline && splineType == SplineType.FacingTarget)
            {
                var facingTargetGuid = new byte[8];
                packet.StartBitStream(facingTargetGuid, 4, 7, 0, 5, 1, 2, 3, 6);
                packet.ParseBitStream(facingTargetGuid, 4, 2, 0, 5, 6, 3, 1, 7);
                packet.WriteGuid("Facing Target GUID", facingTargetGuid, index);
            }
            return(moveInfo);
        }
        /// <inheritdoc />
        protected override object DeserialiseDataObjectInt(Stream inputStream, Type resultType, Dictionary <string, string> options)
        {
            if (inputStream == null)
            {
                throw new ArgumentNullException("inputStream");
            }

#if NETFX_CORE
            var constructor = (from ctor in resultType.GetTypeInfo().DeclaredConstructors
                               where ctor.GetParameters().Length == 0
                               select ctor).FirstOrDefault();

            if (constructor == null || !explicitlySerializableType.GetTypeInfo().IsAssignableFrom(resultType.GetTypeInfo()))
#else
            var constructor = resultType.GetConstructor(BindingFlags.Instance, null, new Type[] { }, null);

            if (constructor == null)
            {
                constructor = resultType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { }, null);
            }

            if (constructor == null || !explicitlySerializableType.IsAssignableFrom(resultType))
#endif
            {
                byte[] buffer = new byte[8];

                if (resultType == typeof(bool))
                {
                    inputStream.Read(buffer, 0, sizeof(bool));
                    return(BitConverter.ToBoolean(buffer, 0));
                }
                else if (resultType == typeof(byte))
                {
                    return((byte)inputStream.ReadByte());
                }
                else if (resultType == typeof(sbyte))
                {
                    return((sbyte)(inputStream.ReadByte() - 128));
                }
                else if (resultType == typeof(Int16))
                {
                    inputStream.Read(buffer, 0, sizeof(short));
                    return(BitConverter.ToInt16(buffer, 0));
                }
                else if (resultType == typeof(UInt16))
                {
                    inputStream.Read(buffer, 0, sizeof(ushort));
                    return(BitConverter.ToUInt16(buffer, 0));
                }
                else if (resultType == typeof(Int32))
                {
                    inputStream.Read(buffer, 0, sizeof(int));
                    return(BitConverter.ToInt32(buffer, 0));
                }
                else if (resultType == typeof(UInt32))
                {
                    inputStream.Read(buffer, 0, sizeof(uint));
                    return(BitConverter.ToUInt32(buffer, 0));
                }
                else if (resultType == typeof(Int64))
                {
                    inputStream.Read(buffer, 0, sizeof(long));
                    return(BitConverter.ToInt64(buffer, 0));
                }
                else if (resultType == typeof(UInt64))
                {
                    inputStream.Read(buffer, 0, sizeof(ulong));
                    return(BitConverter.ToUInt64(buffer, 0));
                }
                else if (resultType == typeof(IntPtr))
                {
                    inputStream.Read(buffer, 0, sizeof(long));
                    return(new IntPtr(BitConverter.ToInt64(buffer, 0)));
                }
                else if (resultType == typeof(UIntPtr))
                {
                    inputStream.Read(buffer, 0, sizeof(ulong));
                    return(new UIntPtr(BitConverter.ToUInt64(buffer, 0)));
                }
                else if (resultType == typeof(Char))
                {
                    inputStream.Read(buffer, 0, sizeof(char));
                    return(BitConverter.ToChar(buffer, 0));
                }
                else if (resultType == typeof(Single))
                {
                    inputStream.Read(buffer, 0, sizeof(float));
                    return(BitConverter.ToSingle(buffer, 0));
                }
                else if (resultType == typeof(Double))
                {
                    inputStream.Read(buffer, 0, sizeof(double));
                    return(BitConverter.ToDouble(buffer, 0));
                }
                else if (resultType == typeof(String))
                {
                    inputStream.Read(buffer, 0, sizeof(int));
                    byte[] objData = new byte[BitConverter.ToInt32(buffer, 0)];
                    inputStream.Read(objData, 0, objData.Length);

                    return(new String(Encoding.UTF8.GetChars(objData)));
                }
                else
                {
                    throw new ArgumentException("Provided type " + resultType.ToString() + " either does not have a parameterless constructor or does not implement IExplicitlySerialize", "resultType");
                }
            }

            var result = constructor.Invoke(new object[] { }) as IExplicitlySerialize;
            result.Deserialize(inputStream);

            return(result);
        }
Exemplo n.º 4
0
        private static PropertyInfo getProperty(ExportEntry entry)
        {
            IMEPackage pcc = entry.FileRef;

            string       reference = null;
            PropertyType type;

            switch (entry.ClassName)
            {
            case "IntProperty":
                type = PropertyType.IntProperty;
                break;

            case "StringRefProperty":
                type = PropertyType.StringRefProperty;
                break;

            case "FloatProperty":
                type = PropertyType.FloatProperty;
                break;

            case "BoolProperty":
                type = PropertyType.BoolProperty;
                break;

            case "StrProperty":
                type = PropertyType.StrProperty;
                break;

            case "NameProperty":
                type = PropertyType.NameProperty;
                break;

            case "DelegateProperty":
                type = PropertyType.DelegateProperty;
                break;

            case "ObjectProperty":
            case "ClassProperty":
            case "ComponentProperty":
                type      = PropertyType.ObjectProperty;
                reference = pcc.getObjectName(BitConverter.ToInt32(entry.Data, entry.Data.Length - 4));
                break;

            case "StructProperty":
                type      = PropertyType.StructProperty;
                reference = pcc.getObjectName(BitConverter.ToInt32(entry.Data, entry.Data.Length - 4));
                break;

            case "BioMask4Property":
            case "ByteProperty":
                type      = PropertyType.ByteProperty;
                reference = pcc.getObjectName(BitConverter.ToInt32(entry.Data, entry.Data.Length - 4));
                break;

            case "ArrayProperty":
                type = PropertyType.ArrayProperty;
                PropertyInfo arrayTypeProp = getProperty(pcc.GetUExport(BitConverter.ToInt32(entry.Data, 44)));
                if (arrayTypeProp != null)
                {
                    switch (arrayTypeProp.Type)
                    {
                    case PropertyType.ObjectProperty:
                    case PropertyType.StructProperty:
                    case PropertyType.ArrayProperty:
                        reference = arrayTypeProp.Reference;
                        break;

                    case PropertyType.ByteProperty:
                        if (arrayTypeProp.Reference == "Class")
                        {
                            reference = arrayTypeProp.Type.ToString();
                        }
                        else
                        {
                            reference = arrayTypeProp.Reference;
                        }
                        break;

                    case PropertyType.IntProperty:
                    case PropertyType.FloatProperty:
                    case PropertyType.NameProperty:
                    case PropertyType.BoolProperty:
                    case PropertyType.StrProperty:
                    case PropertyType.StringRefProperty:
                    case PropertyType.DelegateProperty:
                        reference = arrayTypeProp.Type.ToString();
                        break;

                    case PropertyType.None:
                    case PropertyType.Unknown:
                    default:
                        System.Diagnostics.Debugger.Break();
                        return(null);
                    }
                }
                else
                {
                    return(null);
                }
                break;

            case "InterfaceProperty":
            default:
                return(null);
            }

            bool transient = (BitConverter.ToUInt64(entry.Data, 24) & 0x0000000000002000) != 0;

            return(new PropertyInfo(type, reference, transient));
        }
Exemplo n.º 5
0
 /// <summary>
 /// 从流中读取一个ulong数据
 /// </summary>
 /// <returns></returns>
 public ulong ReadULong()
 {
     byte[] arr = new byte[4];
     base.Read(arr, 0, 4);
     return(BitConverter.ToUInt64(arr, 0));
 }
Exemplo n.º 6
0
 public ulong GetRandomULong()
 {
     CheckBuf(sizeof(ulong));
     return(BitConverter.ToUInt64(_buf, _idx));
 }
 /// <summary>
 ///     Returns a 64-bit unsigned integer converted from eight bytes at a specified position in a byte array.
 /// </summary>
 /// <param name="value">An array of bytes.</param>
 /// <param name="startIndex">The starting position within value.</param>
 /// <returns>A 64-bit unsigned integer formed by the eight bytes beginning at startIndex.</returns>
 /// <exception cref="System.ArgumentException">
 ///     startIndex is greater than or equal to the length of value minus 7, and is
 ///     less than or equal to the length of value minus 1.
 /// </exception>
 /// <exception cref="System.ArgumentNullException">value is null.</exception>
 /// <exception cref="System.ArgumentOutOfRangeException">
 ///     startIndex is less than zero or greater than the length of value
 ///     minus 1.
 /// </exception>
 public static ulong ToUInt64(byte[] value, int startIndex) =>
 !IsLittleEndian
         ? BitConverter.ToUInt64(value, startIndex)
         : BitConverter.ToUInt64(value.Reverse().ToArray(), value.Length - sizeof(ulong) - startIndex);
Exemplo n.º 8
0
        /// <summary>
        /// Unwrap object wrapped in <see cref="SerializedObjectWrapper"/>
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        object Unwrap(SerializedObjectWrapper item)
        {
            if (item.Data.Array == null)
            {
                return(null);
            }

            if (item.Flags == RawDataFlag)
            {
                ArraySegment <byte> tmp = item.Data;

                if (tmp.Count == tmp.Array.Length)
                {
                    return(tmp.Array);
                }

                // we should never arrive here, but it's better to be safe than sorry
                var retval = new byte[tmp.Count];

                Array.Copy(tmp.Array, tmp.Offset, retval, 0, tmp.Count);

                return(retval);
            }

            var code = (TypeCode)(item.Flags & 0x00ff);

            byte[] data   = item.Data.Array;
            int    offset = item.Data.Offset;
            int    count  = item.Data.Count;

            switch (code)
            {
            // incrementing a non-existing key then getting it
            // returns as a string, but the flag will be 0
            // so treat all 0 flagged items as string
            // this may help inter-client data management as well
            //
            // however we store 'null' as Empty + an empty array,
            // so this must special-cased for compatibilty with
            // earlier versions. we introduced DBNull as null marker in emc2.6
            case TypeCode.Empty:
                return((data == null || count == 0)
                            ? null
                            : Encoding.UTF8.GetString(data, offset, count));

            case TypeCode.DBNull:
                return(null);

            case TypeCode.String:
                return(Encoding.UTF8.GetString(data, offset, count));

            case TypeCode.Boolean:
                return(BitConverter.ToBoolean(data, offset));

            case TypeCode.Int16:
                return(BitConverter.ToInt16(data, offset));

            case TypeCode.Int32:
                return(BitConverter.ToInt32(data, offset));

            case TypeCode.Int64:
                return(BitConverter.ToInt64(data, offset));

            case TypeCode.UInt16:
                return(BitConverter.ToUInt16(data, offset));

            case TypeCode.UInt32:
                return(BitConverter.ToUInt32(data, offset));

            case TypeCode.UInt64:
                return(BitConverter.ToUInt64(data, offset));

            case TypeCode.Char:
                return(BitConverter.ToChar(data, offset));

            case TypeCode.DateTime:
                return(DateTime.FromBinary(BitConverter.ToInt64(data, offset)));

            case TypeCode.Double:
                return(BitConverter.ToDouble(data, offset));

            case TypeCode.Single:
                return(BitConverter.ToSingle(data, offset));

            case TypeCode.Object:
                using (var ms = new MemoryStream(data, offset, count))
                {
#if NETSTANDARD2_0
                    return(null);
#else
                    return(bf.Deserialize(ms));
#endif
                }

            default: throw new InvalidOperationException("Unknown TypeCode was returned: " + code);
            }
        }
Exemplo n.º 9
0
 public static ulong GetUInt64()
 {
     return(BitConverter.ToUInt64(GetBytes(sizeof(ulong)), 0));
 }
        private void ParseFields()
        {
            if (Format == null)
            {
                return;
            }
            int offset = 0;

            byte[] message = this.Blob;
            string format  = Format.FormatString;

            for (int k = 0, n = format.Length; k < n; k++)
            {
                char   ch    = format[k];
                object value = null;
                switch (ch)
                {
                case 'b':
                    value = (sbyte)message[offset];
                    offset++;
                    break;

                case 'B':
                    value = message[offset];
                    offset++;
                    break;

                case 'h':
                    value   = BitConverter.ToInt16(message, offset);
                    offset += 2;
                    break;

                case 'H':
                    value   = BitConverter.ToUInt16(message, offset);
                    offset += 2;
                    break;

                case 'i':
                    value   = BitConverter.ToInt32(message, offset);
                    offset += 4;
                    break;

                case 'I':
                    value   = BitConverter.ToUInt32(message, offset);
                    offset += 4;
                    break;

                case 'q':
                    value   = BitConverter.ToInt64(message, offset);
                    offset += 8;
                    break;

                case 'Q':
                    value   = BitConverter.ToUInt64(message, offset);
                    offset += 8;
                    break;

                case 'f':
                    value   = BitConverter.ToSingle(message, offset);
                    offset += 4;
                    break;

                case 'd':
                    value   = BitConverter.ToDouble(message, offset);
                    offset += 8;
                    break;

                case 'c':
                    value   = (BitConverter.ToInt16(message, offset) / 100.0);
                    offset += 2;
                    break;

                case 'C':
                    value   = (BitConverter.ToUInt16(message, offset) / 100.0);
                    offset += 2;
                    break;

                case 'e':
                    value   = (BitConverter.ToInt32(message, offset) / 100.0);
                    offset += 4;
                    break;

                case 'E':
                    value   = (BitConverter.ToUInt32(message, offset) / 100.0);
                    offset += 4;
                    break;

                case 'L':
                    value   = ((double)BitConverter.ToInt32(message, offset) / 10000000.0);
                    offset += 4;
                    break;

                case 'n':
                    value   = ASCIIEncoding.ASCII.GetString(message, offset, 4).Trim(NullTerminator);
                    offset += 4;
                    break;

                case 'N':
                    value   = ASCIIEncoding.ASCII.GetString(message, offset, 16).Trim(NullTerminator);
                    offset += 16;
                    break;

                case 'M':
                    int modeno = message[offset];
                    value = modeno;
                    offset++;
                    break;

                case 'Z':
                    value   = ASCIIEncoding.ASCII.GetString(message, offset, 64).Trim(NullTerminator);
                    offset += 64;
                    break;

                default:
                    throw new Exception(string.Format("Unexpected format specifier '{0}'", ch));
                }
                if (k < Format.Columns.Length)
                {
                    cache[Format.Columns[k]] = new LogField()
                    {
                        Name   = Format.Columns[k],
                        Value  = value,
                        Parent = this
                    };
                }
            }
        }
Exemplo n.º 11
0
        public static dynamic ConvertBuffer(string datatype, byte[] buffer)
        {
            try
            {
                (string type, int size) = Separate(datatype);

                if (type == "Bool")
                {
                    if (size > 1)
                    {
                        return(Array.ConvertAll(buffer, val => val == 0 ? false : true));
                    }
                    else
                    {
                        return(buffer[0] == 0 ? false : true);
                    }
                }
                else if (type == "Float")
                {
                    if (size > 1)
                    {
                        float[] temp = new float[size];
                        for (int i = 0; i < size; i++)
                        {
                            temp[i] = BitConverter.ToSingle(buffer, i * sizeof(float));
                        }
                        return(temp);
                    }
                    else
                    {
                        return(BitConverter.ToSingle(buffer, 0));
                    }
                }
                else if (type == "Double")
                {
                    if (size > 1)
                    {
                        double[] temp = new double[size];
                        for (int i = 0; i < size; i++)
                        {
                            temp[i] = BitConverter.ToDouble(buffer, i * sizeof(double));
                        }
                        return(temp);
                    }
                    else
                    {
                        return(BitConverter.ToDouble(buffer, 0));
                    }
                }
                else if (type == "Char")
                {
                    return(Encoding.ASCII.GetString(buffer));
                }
                else if (type.Contains("Int"))
                {
                    if (type == "Int8")
                    {
                        if (size > 1)
                        {
                            return(Array.ConvertAll(buffer, val => unchecked ((sbyte)val)));
                        }
                        else
                        {
                            return(unchecked ((sbyte)buffer[0]));
                        }
                    }
                    else if (type == "UInt8")
                    {
                        if (size > 1)
                        {
                            return(buffer);
                        }
                        else
                        {
                            return(buffer[0]);
                        }
                    }
                    if (type == "Int16")
                    {
                        if (size > 1)
                        {
                            short[] temp = new short[size];
                            for (int i = 0; i < size; i++)
                            {
                                temp[i] = BitConverter.ToInt16(buffer, i * sizeof(short));
                            }
                            return(temp);
                        }
                        else
                        {
                            return(BitConverter.ToInt16(buffer, 0));
                        }
                    }
                    else if (type == "UInt16")
                    {
                        if (size > 1)
                        {
                            ushort[] temp = new ushort[size];
                            for (int i = 0; i < size; i++)
                            {
                                temp[i] = BitConverter.ToUInt16(buffer, i * sizeof(ushort));
                            }
                            return(temp);
                        }
                        else
                        {
                            return(BitConverter.ToUInt16(buffer, 0));
                        }
                    }
                    if (type == "Int32")
                    {
                        if (size > 1)
                        {
                            int[] temp = new int[size];
                            for (int i = 0; i < size; i++)
                            {
                                temp[i] = BitConverter.ToInt32(buffer, i * sizeof(int));
                            }
                            return(temp);
                        }
                        else
                        {
                            return(BitConverter.ToInt32(buffer, 0));
                        }
                    }
                    else if (type == "UInt32")
                    {
                        if (size > 1)
                        {
                            uint[] temp = new uint[size];
                            for (int i = 0; i < size; i++)
                            {
                                temp[i] = BitConverter.ToUInt32(buffer, i * sizeof(uint));
                            }
                            return(temp);
                        }
                        else
                        {
                            return(BitConverter.ToUInt32(buffer, 0));
                        }
                    }
                    if (type == "Int64")
                    {
                        if (size > 1)
                        {
                            long[] temp = new long[size];
                            for (int i = 0; i < size; i++)
                            {
                                temp[i] = BitConverter.ToInt64(buffer, i * sizeof(long));
                            }
                            return(temp);
                        }
                        else
                        {
                            return(BitConverter.ToInt64(buffer, 0));
                        }
                    }
                    else if (type == "UInt64")
                    {
                        if (size > 1)
                        {
                            ulong[] temp = new ulong[size];
                            for (int i = 0; i < size; i++)
                            {
                                temp[i] = BitConverter.ToUInt64(buffer, i * sizeof(ulong));
                            }
                            return(temp);
                        }
                        else
                        {
                            return(BitConverter.ToUInt64(buffer, 0));
                        }
                    }
                }
                else
                {
                    throw new Exception(string.Format("Unknown datatype: {0}.", type));
                }

                return(null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(null);
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Accept a client's request.
        /// </summary>
        public void Accept()
        {
            if (!_client.Connected)
            {
                return;
            }
            try
            {
                var sizeBuffer = new byte[8];
                _stream.Read(sizeBuffer, 0, sizeBuffer.Length);
                var messageLength  = BitConverter.ToUInt64(sizeBuffer, 0);
                var messageBuffer  = new byte[_client.ReceiveBufferSize];
                var messageBuilder = new StringBuilder();
                var requestType    = (Request)_stream.ReadByte();
                if (!Enum.IsDefined(typeof(Request), requestType))
                {
                    Close();
                    return;
                }
                switch (requestType)
                {
                case Request.Get:
                    var read = 0uL;
                    do
                    {
                        read += (uint)_stream.Read(messageBuffer, 0, messageBuffer.Length);
                        messageBuilder.Append(Encoding.ASCII.GetString(messageBuffer));
                    } while (read < messageLength);

                    var request = JObject.Parse(messageBuilder.ToString());
                    {
                        using (var fs = LogHandler.GetLogStream(request["LogID"].ToObject <string>()))
                        {
                            if (fs == null)
                            {
                                Send(ErrorAsJSON(Error.LogNotFound));
                                break;
                            }
                            var rl = BitConverter.GetBytes(fs.Length);
                            _stream.Write(rl, 0, rl.Length);
                            fs.CopyTo(_stream);
                        }
                    }
                    break;

                case Request.Put:
                    try
                    {
                        const int readTimeout = 1000;
                        _stream.ReadTimeout = readTimeout;
                        Send(new JObject {
                            { "LogID", LogHandler.PutFromStream(_stream) }
                        }.ToString());
                    }
                    catch (JsonException)
                    {
                        Send(ErrorAsJSON(Error.InvalidLog));
                    }
                    break;
                }
            }
            catch (JsonReaderException)
            {
            }
            catch (IOException)
            {
            }
            catch (Win32Exception e)
            {
                if (e.NativeErrorCode == -2147467259)
                {
                    Console.WriteLine("Run server as admin.");
                    Environment.Exit(1);
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                Close();
            }
        }
Exemplo n.º 13
0
        private static MovementInfo ReadMovementUpdateBlock547(ref Packet packet, Guid guid, int index)
        {
            var moveInfo = new MovementInfo();

            var hasUnkDword676        = packet.ReadBit();
            var hasVehicleData        = packet.ReadBit("Has Vehicle Data", index);
            var hasUnkDword1044       = packet.ReadBit();
            var hasGameObjectRotation = packet.ReadBit("Has GameObject Rotation", index);

            packet.ReadBit("unk byte0", index);
            var living           = packet.ReadBit("Living", index);
            var hasUnkLargeBlock = packet.ReadBit("Has Unk Large Block", index);

            packet.ReadBit("Unk Byte2", index);
            var hasUnkLargeBlock2 = packet.ReadBit("Has Unk Large Block2", index);

            packet.ReadBit("Self", index);
            packet.ReadBit("Unk Byte681", index);
            packet.ReadBit("Unk Byte1", index);
            var hasGameObjectPosition = packet.ReadBit("Has GameObject Position", index);
            var transport             = packet.ReadBit("Has Transport Data", index);
            var hasAnimKits           = packet.ReadBit("Has AnimKits", index);
            var hasStationaryPosition = packet.ReadBit("Has Stationary Position", index);
            var hasAttackingTarget    = packet.ReadBit("Has Attacking Target", index);
            var unkLoopCounter        = packet.ReadBits("Unknown array size", 22, index);
            var hasUnkString          = packet.ReadBit("Has Unknown String", index);
            var hasTransportFrames    = packet.ReadBit("Has Transport Frames", index);

            uint transportFramesCount = 0;
            uint UnkStringLen         = 0;
            //int IsLivingUnkCountLoop = 0;
            uint IsLivingUnkCounter = 0;
            var  hasOrientation     = false;
            var  guid2                         = new byte[8];
            var  hasPitch                      = false;
            var  hasFallData                   = false;
            var  hasSplineElevation            = false;
            var  hasTransportData              = false;
            var  hasTimestamp                  = false;
            var  hasMovementCounter            = false;
            var  transportGuid                 = new byte[8];
            var  hasTransportTime2             = false;
            var  hasTransportTime3             = false;
            var  bit216                        = false;
            var  hasSplineStartTime            = false;
            var  splineCount                   = 0u;
            var  splineType                    = SplineType.Stop;
            var  facingTargetGuid              = new byte[8];
            var  hasSplineVerticalAcceleration = false;
            var  hasSplineUnkPart              = false;
            var  hasFallDirection              = false;
            var  goTransportGuid               = new byte[8];
            var  hasGOTransportTime2           = false;
            var  hasGOTransportTime3           = false;
            var  attackingTargetGuid           = new byte[8];
            var  SplineFacingTargetGuid        = new byte[8];
            var  hasAnimKit1                   = false;
            var  hasAnimKit2                   = false;
            var  hasAnimKit3                   = false;

            bool[] UnkLargeBlockBits  = new bool[13];
            uint[] UnkLargeBlockCount = new uint[3];

            if (hasTransportFrames)
            {
                transportFramesCount = packet.ReadBits("Transport Frames Count", 22, index);
            }

            if (hasGameObjectPosition)
            {
                goTransportGuid[3]  = packet.ReadBit();
                goTransportGuid[5]  = packet.ReadBit();
                goTransportGuid[2]  = packet.ReadBit();
                goTransportGuid[1]  = packet.ReadBit();
                goTransportGuid[4]  = packet.ReadBit();
                hasGOTransportTime3 = packet.ReadBit();
                hasGOTransportTime2 = packet.ReadBit();
                goTransportGuid[0]  = packet.ReadBit();
                goTransportGuid[6]  = packet.ReadBit();
                goTransportGuid[7]  = packet.ReadBit();
            }

            if (living)
            {
                hasTransportData = packet.ReadBit("Has Transport Data", index);

                if (hasTransportData)
                {
                    transportGuid[4]  = packet.ReadBit();
                    transportGuid[0]  = packet.ReadBit();
                    transportGuid[5]  = packet.ReadBit();
                    transportGuid[2]  = packet.ReadBit();
                    transportGuid[3]  = packet.ReadBit();
                    hasTransportTime2 = packet.ReadBit();
                    transportGuid[7]  = packet.ReadBit();
                    transportGuid[6]  = packet.ReadBit();
                    transportGuid[1]  = packet.ReadBit();
                    hasTransportTime3 = packet.ReadBit();
                }

                hasPitch = !packet.ReadBit("Lacks pitch", index);
                packet.ReadBit("Has MovementInfo spline", index);
                packet.ReadBits("IsLicingUnkLoop", 19, index);
                guid2[1] = packet.ReadBit();
                var hasExtraMovementFlags = !packet.ReadBit();

                /*var v37 = IsLivingUnkCountLoop == 0;
                 *
                 * if (!v37)
                 * {
                 *  for (int i = 0; i < IsLivingUnkCountLoop; ++i)
                 *  {
                 *      packet.ReadBits("Unk DWORD24 Loop1", 2, index, (int)i);
                 *  }
                 * }*/

                packet.ReadBit("Unk Bit from movementInfo", index);
                hasSplineElevation = !packet.ReadBit("Lacks spline elevation", index);

                if (hasExtraMovementFlags)
                {
                    moveInfo.FlagsExtra = packet.ReadEnum <MovementFlagExtra>("Extra Movement Flags", 13, index);
                }

                hasOrientation = !packet.ReadBit("Lacks orientation", index);
                hasTimestamp   = !packet.ReadBit("Has Timestamp", index);
                var hasMovementFlags = !packet.ReadBit();
                hasMovementCounter = !packet.ReadBit("Has Movement Counter", index);

                guid2[2]    = packet.ReadBit();
                guid2[6]    = packet.ReadBit();
                hasFallData = packet.ReadBit("Has Fall Data", index);
                guid2[5]    = packet.ReadBit();
                guid2[4]    = packet.ReadBit();
                guid2[0]    = packet.ReadBit();

                if (hasMovementFlags)
                {
                    moveInfo.Flags = (WowPacketParser.Enums.MovementFlag)packet.ReadEnum <MovementFlag>("Movement Flags", 30, index);
                }

                packet.ReadBit("Unk byte164", index);

                if (hasFallData)
                {
                    hasFallDirection = packet.ReadBit("Has Fall Direction", index);
                }

                packet.ReadBits("Is Living Unk Counter", 22, index);
                guid2[7] = packet.ReadBit();
                moveInfo.HasSplineData = packet.ReadBit("Has Spline Data", index);
                guid2[3] = packet.ReadBit();

                if (moveInfo.HasSplineData)
                {
                    bit216 = packet.ReadBit("Has extended spline data", index);
                    if (bit216)
                    {
                        packet.ReadBits("Unk bits", 2, index);
                        hasSplineStartTime = packet.ReadBit("Has spline start time", index);
                        splineCount        = packet.ReadBits("Spline Waypoints", 22, index);
                        packet.ReadEnum <SplineFlag434>("Spline flags", 25, index);
                        hasSplineVerticalAcceleration = packet.ReadBit("Has spline vertical acceleration", index);

                        hasSplineUnkPart = packet.ReadBit();

                        if (hasSplineUnkPart)
                        {
                            packet.ReadBits("Unk word300", 2, index);                            //unk word300
                            packet.ReadBits("Unk dword284", 21, index);                          //unk dword284
                        }
                    }
                }
            }

            if (hasUnkLargeBlock2)
            {
                UnkLargeBlockBits[0] = packet.ReadBit();
                UnkLargeBlockBits[1] = packet.ReadBit();
                packet.ReadBit("Unk Byte256", index);
                packet.ReadBit("Unk Byte257", index);
                UnkLargeBlockBits[4] = packet.ReadBit();
                packet.ReadBit("Unk Byte254", index);
                UnkLargeBlockBits[6] = packet.ReadBit();
                packet.ReadBit("Unk Byte255", index);
                UnkLargeBlockBits[8] = packet.ReadBit();

                if (UnkLargeBlockBits[8])
                {
                    UnkLargeBlockCount[0] = packet.ReadBits(21);
                    UnkLargeBlockCount[1] = packet.ReadBits(21);
                }

                UnkLargeBlockBits[9] = packet.ReadBit();

                if (UnkLargeBlockBits[9])
                {
                    UnkLargeBlockCount[2] = packet.ReadBits(20);
                }

                UnkLargeBlockBits[10] = packet.ReadBit();
                packet.ReadBit("Unk Byte258", index);
                UnkLargeBlockBits[12] = packet.ReadBit();
            }

            if (hasAttackingTarget)
            {
                attackingTargetGuid = packet.StartBitStream(4, 6, 3, 5, 0, 2, 7, 1);
            }

            if (hasAnimKits)
            {
                hasAnimKit2 = !packet.ReadBit();
                hasAnimKit3 = !packet.ReadBit();
                hasAnimKit1 = !packet.ReadBit();
            }

            if (hasUnkString)
            {
                UnkStringLen = packet.ReadBits(7);
            }

            packet.ResetBitReader();

            // Reading data
            for (var i = 0u; i < unkLoopCounter; ++i)
            {
                packet.ReadUInt32("Unk UInt32", index, (int)i);
            }

            if (hasUnkLargeBlock2)
            {
                if (UnkLargeBlockBits[10])
                {
                    packet.ReadSingle("Unk Float 234", index);
                    packet.ReadSingle("Unk Float 238", index);
                }

                if (UnkLargeBlockBits[8])
                {
                    for (uint i = 0; i < UnkLargeBlockCount[1]; ++i)
                    {
                        packet.ReadSingle("Unk Float Loop1", index);
                        packet.ReadSingle("Unk Float1 Loop1", index);
                    }

                    packet.ReadSingle("Unk Float 27C", index);

                    for (uint i = 0; i < UnkLargeBlockCount[0]; ++i)
                    {
                        packet.ReadSingle("Unk Float Loop0", index);
                        packet.ReadSingle("Unk Float1 Loop0", index);
                    }

                    packet.ReadSingle("Unk Float 280", index);
                }

                if (UnkLargeBlockBits[1])
                {
                    packet.ReadSingle("Unk Float 244", index);
                    packet.ReadSingle("Unk Float 250", index);
                    packet.ReadSingle("Unk Float 254", index);
                    packet.ReadSingle("Unk Float 248", index);
                    packet.ReadSingle("Unk Float 240", index);
                    packet.ReadSingle("Unk Float 24C", index);
                }

                packet.ReadInt32("Unk DWORD 520", index);

                if (UnkLargeBlockBits[9])
                {
                    for (uint i = 0; i < UnkLargeBlockCount[2]; ++i)
                    {
                        packet.ReadSingle("Unk Float Loop2", index);
                        packet.ReadSingle("Unk Float1 Loop2", index);
                        packet.ReadSingle("Unk Float2 Loop2", index);
                    }
                }

                if (UnkLargeBlockBits[12])
                {
                    packet.ReadInt32("Unk DWORD 540", index);
                }

                if (UnkLargeBlockBits[6])
                {
                    packet.ReadInt32("Unk DWORD 532", index);
                }

                if (UnkLargeBlockBits[0])
                {
                    packet.ReadInt32("Unk DWORD 556", index);
                }

                if (UnkLargeBlockBits[4])
                {
                    packet.ReadInt32("Unk DWORD 548", index);
                }
            }

            if (living)
            {
                if (hasFallData)
                {
                    if (hasFallDirection)
                    {
                        packet.ReadSingle("Jump Sin", index);
                        packet.ReadSingle("Jump Cos", index);
                        packet.ReadSingle("Jump XY Speed", index);
                    }
                    packet.ReadSingle("Fall Z Speed", index);
                    packet.ReadUInt32("Time Fallen", index);
                }

                if (moveInfo.HasSplineData)
                {
                    if (bit216)
                    {
                        packet.ReadSingle("Spline Duration Multiplier Next", index);

                        for (var i = 0u; i < splineCount; ++i)
                        {
                            var wp = new Vector3
                            {
                                Z = packet.ReadSingle(),
                                X = packet.ReadSingle(),
                                Y = packet.ReadSingle(),
                            };

                            packet.WriteLine("[{0}][{1}] Spline Waypoint: {2}", index, i, wp);
                        }

                        splineType = packet.ReadEnum <SplineType>("Spline Type", TypeCode.Byte, index);

                        packet.ReadSingle("Spline Duration Multiplier", index);

                        //unk part goes here

                        if (splineType == SplineType.FacingSpot)
                        {
                            var point = new Vector3
                            {
                                X = packet.ReadSingle(),
                                Z = packet.ReadSingle(),
                                Y = packet.ReadSingle(),
                            };

                            packet.WriteLine("[{0}] Facing Spot: {1}", index, point);
                        }

                        if (hasSplineVerticalAcceleration)
                        {
                            packet.ReadSingle("Spline Vertical Acceleration", index);
                        }

                        if (splineType == SplineType.FacingAngle)
                        {
                            packet.ReadSingle("Facing Angle", index);
                        }

                        packet.ReadUInt32("Spline Full Time", index);

                        if (hasSplineStartTime)
                        {
                            packet.ReadUInt32("Spline Start time", index);
                        }

                        packet.ReadUInt32("Spline Time", index);
                    }

                    packet.ReadUInt32("Spline Id", index);

                    var endPoint = new Vector3
                    {
                        Z = packet.ReadSingle(),
                        X = packet.ReadSingle(),
                        Y = packet.ReadSingle(),
                    };


                    packet.WriteLine("[{0}] Spline Endpoint: {1}", index, endPoint);
                }

                moveInfo.Position.Z = packet.ReadSingle();

                /*if (IsLivingUnkCountLoop > 0)
                 * {
                 *  for (int i = 0; i < IsLivingUnkCountLoop; ++i)
                 *  {
                 *      packet.ReadSingle("Unk Float14", index, (int)i);
                 *      packet.ReadInt32("Unk DWORD10", index, (int)i);
                 *      packet.ReadInt32("Unk DWORD0", index, (int)i);
                 *      packet.ReadSingle("Unk FloatC", index, (int)i);
                 *      packet.ReadSingle("Unk Float4", index, (int)i);
                 *      packet.ReadSingle("Unk Float8", index, (int)i);
                 *  }
                 * }*/

                moveInfo.Position.Y = packet.ReadSingle();
                packet.ReadSingle("Fly Speed", index);
                packet.ReadXORByte(guid2, 6);
                packet.ReadSingle("FlyBack Speed", index);

                if (hasTransportData)
                {
                    packet.ReadXORByte(transportGuid, 7);
                    packet.ReadXORByte(transportGuid, 4);

                    if (hasTransportTime3)
                    {
                        packet.ReadUInt32("Transport Time 3", index);
                    }

                    packet.ReadUInt32("Transport Time", index);

                    if (hasTransportTime2)
                    {
                        packet.ReadUInt32("Transport Time 2", index);
                    }

                    moveInfo.TransportOffset.O = packet.ReadSingle();
                    moveInfo.TransportOffset.X = packet.ReadSingle();
                    packet.ReadXORByte(transportGuid, 6);
                    packet.ReadXORByte(transportGuid, 3);
                    packet.ReadXORByte(transportGuid, 2);
                    moveInfo.TransportOffset.Z = packet.ReadSingle();
                    moveInfo.TransportOffset.Y = packet.ReadSingle();
                    packet.ReadSByte("Transport Seat", index);
                    packet.ReadXORByte(transportGuid, 1);
                    packet.ReadXORByte(transportGuid, 0);
                    packet.ReadXORByte(transportGuid, 5);
                    moveInfo.TransportGuid = new Guid(BitConverter.ToUInt64(transportGuid, 0));
                    packet.WriteLine("[{0}] Transport GUID {1}", index, moveInfo.TransportGuid);
                    packet.WriteLine("[{0}] Transport Position: {1}", index, moveInfo.TransportOffset);
                }

                moveInfo.Position.X = packet.ReadSingle();
                packet.ReadXORByte(guid2, 2);

                if (hasPitch)
                {
                    packet.ReadSingle("Pitch", index);
                }

                packet.ReadSingle("Swim Speed", index);
                packet.ReadXORByte(guid2, 1);
                packet.ReadSingle("RunBack Speed", index);
                packet.ReadSingle("SwimBack Speed", index);
                packet.ReadXORByte(guid2, 5);

                if (hasSplineElevation)
                {
                    packet.ReadSingle("Spline Elevation", index);
                }

                if (hasMovementCounter)
                {
                    packet.ReadUInt32("Movement Counter", index);
                }

                moveInfo.RunSpeed = packet.ReadSingle("Run Speed", index) / 7.0f;
                packet.ReadXORByte(guid2, 7);
                moveInfo.WalkSpeed = packet.ReadSingle("Walk Speed", index) / 2.5f;
                packet.ReadSingle("Pitch Rate", index);

                if (IsLivingUnkCounter > 0)
                {
                    for (uint i = 0; i < IsLivingUnkCounter; ++i)
                    {
                        packet.ReadUInt32("Unk DWORD148", index, (int)i);
                    }
                }

                if (hasTimestamp)
                {
                    packet.ReadUInt32("Timestamp", index);
                }

                packet.ReadXORByte(guid2, 4);
                packet.ReadXORByte(guid2, 0);

                if (hasOrientation)
                {
                    moveInfo.Orientation = packet.ReadSingle();
                }

                packet.WriteLine("[{0}] GUID 2: {1}", index, new Guid(BitConverter.ToUInt64(guid2, 0)));
                packet.WriteLine("[{0}] Position: {1}", index, moveInfo.Position);
                packet.WriteLine("[{0}] Orientation: {1}", index, moveInfo.Orientation);
            }

            if (hasAttackingTarget)
            {
                packet.ParseBitStream(attackingTargetGuid, 5, 1, 2, 0, 3, 4, 6, 7);
                packet.WriteGuid("Attacking Target GUID", attackingTargetGuid, index);
            }

            if (hasStationaryPosition)
            {
                moveInfo.Orientation = packet.ReadSingle("Stationary Orientation", index);
                moveInfo.Position    = packet.ReadVector3("Stationary Position", index);
            }

            if (hasGameObjectPosition)
            {
                packet.ReadByte("GO Transport Seat", index);
                moveInfo.TransportOffset.X = packet.ReadSingle();
                packet.ReadXORByte(goTransportGuid, 1);
                packet.ReadXORByte(goTransportGuid, 0);
                packet.ReadXORByte(goTransportGuid, 2);
                packet.ReadXORByte(goTransportGuid, 6);
                packet.ReadXORByte(goTransportGuid, 5);
                packet.ReadXORByte(goTransportGuid, 4);

                if (hasGOTransportTime3)
                {
                    packet.ReadUInt32("GO Transport Time 3", index);
                }

                packet.ReadXORByte(goTransportGuid, 7);
                moveInfo.TransportOffset.O = packet.ReadSingle();
                moveInfo.TransportOffset.Z = packet.ReadSingle();
                moveInfo.TransportOffset.Y = packet.ReadSingle();

                if (hasGOTransportTime2)
                {
                    packet.ReadUInt32("GO Transport Time 2", index);
                }

                packet.ReadXORByte(goTransportGuid, 3);

                packet.ReadUInt32("GO Transport Time", index);

                moveInfo.TransportGuid = new Guid(BitConverter.ToUInt64(goTransportGuid, 0));
                packet.WriteLine("[{0}] GO Transport GUID {1}", index, moveInfo.TransportGuid);
                packet.WriteLine("[{0}] GO Transport Position: {1}", index, moveInfo.TransportOffset);
            }

            if (transport)
            {
                packet.ReadUInt32("Transport path timer", index);
            }

            if (hasUnkDword676)
            {
                packet.ReadUInt32("Unk DWORD676", index);
            }

            if (hasUnkString)
            {
                packet.ReadWoWString("Unk String", UnkStringLen, index);
            }

            if (hasGameObjectRotation)
            {
                packet.ReadPackedQuaternion("GameObject Rotation", index);
            }

            if (hasVehicleData)
            {
                moveInfo.VehicleId = packet.ReadUInt32("Vehicle Id", index);
                packet.ReadSingle("Vehicle Orientation", index);
            }

            if (hasUnkDword1044)
            {
                packet.ReadUInt32("Unk DWORD1044", index);
            }

            if (hasAnimKits)
            {
                if (hasAnimKit1)
                {
                    packet.ReadUInt16("Anim Kit 1", index);
                }
                if (hasAnimKit2)
                {
                    packet.ReadUInt16("Anim Kit 2", index);
                }
                if (hasAnimKit3)
                {
                    packet.ReadUInt16("Anim Kit 3", index);
                }
            }

            /*if (bit456)
             * {
             *  // float[] arr = new float[16];
             *  // ordering: 13, 4, 7, 15, BYTE, 10, 11, 3, 5, 14, 6, 1, 8, 12, 0, 2, 9
             *  packet.ReadBytes(4 * 16 + 1);
             * }*/

            if (living && moveInfo.HasSplineData && bit216)
            {
                if (splineType == SplineType.FacingTarget)
                {
                    SplineFacingTargetGuid = packet.StartBitStream(6, 7, 3, 0, 5, 1, 4, 2);
                    packet.ParseBitStream(SplineFacingTargetGuid, 4, 2, 5, 6, 0, 7, 1, 3);
                    packet.WriteGuid("Spline Facing Target GUID", SplineFacingTargetGuid, index);
                }
            }

            return(moveInfo);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Return a new <see cref="NetMQMessage"/> that contains the decrypted content of the give message.
        /// </summary>
        /// <param name="contentType">This identifies the type of content: ChangeCipherSpec, Handshake, or ApplicationData.</param>
        /// <param name="cipherMessage">the message to decrypt</param>
        /// <returns>a new NetMQMessage with the contents decrypted</returns>
        /// <exception cref="NetMQSecurityException"><see cref="NetMQSecurityErrorCode.InvalidFramesCount"/>: Cipher message must have at least 2 frames, iv and sequence number.</exception>
        /// <exception cref="NetMQSecurityException"><see cref="NetMQSecurityErrorCode.ReplayAttack"/>: Message already handled or very old message, might be under replay attack.</exception>
        /// <exception cref="NetMQSecurityException"><see cref="NetMQSecurityErrorCode.EncryptedFramesMissing"/>: Frames were removed from the encrypted message.</exception>
        public NetMQMessage DecryptMessage(ContentType contentType, NetMQMessage cipherMessage)
        {
            if (SecurityParameters.BulkCipherAlgorithm == BulkCipherAlgorithm.Null &&
                SecurityParameters.MACAlgorithm == MACAlgorithm.Null)
            {
                return(cipherMessage);
            }

            if (cipherMessage.FrameCount < 2)
            {
                throw new NetMQSecurityException(NetMQSecurityErrorCode.InvalidFramesCount, "cipher message should have at least 2 frames, iv and sequence number");
            }

            NetMQFrame ivFrame = cipherMessage.Pop();

            m_decryptionBulkAlgorithm.IV = ivFrame.ToByteArray();

            using (var decryptor = m_decryptionBulkAlgorithm.CreateDecryptor())
            {
                NetMQMessage plainMessage = new NetMQMessage();

                NetMQFrame seqNumFrame = cipherMessage.Pop();

                byte[] frameBytes;
                byte[] seqNumMAC;
                byte[] padding;

                DecryptBytes(decryptor, seqNumFrame.ToByteArray(), out frameBytes, out seqNumMAC, out padding);

                ulong seqNum     = BitConverter.ToUInt64(frameBytes, 0);
                int   frameCount = BitConverter.ToInt32(frameBytes, 8);

                int frameIndex = 0;

                ValidateBytes(contentType, seqNum, frameIndex, frameBytes, seqNumMAC, padding);

                if (CheckReplayAttack(seqNum))
                {
                    throw new NetMQSecurityException(NetMQSecurityErrorCode.ReplayAttack,
                                                     "Message already handled or very old message, might be under replay attack");
                }

                if (frameCount != cipherMessage.FrameCount)
                {
                    throw new NetMQSecurityException(NetMQSecurityErrorCode.EncryptedFramesMissing, "Frames was removed from the encrypted message");
                }

                frameIndex++;

                foreach (NetMQFrame cipherFrame in cipherMessage)
                {
                    byte[] data;
                    byte[] mac;

                    DecryptBytes(decryptor, cipherFrame.ToByteArray(), out data, out mac, out padding);
                    ValidateBytes(contentType, seqNum, frameIndex, data, mac, padding);

                    frameIndex++;

                    plainMessage.Append(data);
                }

                return(plainMessage);
            }
        }
Exemplo n.º 15
0
 /// <summary>Construct the data by giving the Byte Array data. This is for reading index data from storage</summary>
 public IndexCache(Byte[] Data)
 {
     Time   = BitConverter.ToUInt64(Data, 0);
     Offset = BitConverter.ToUInt64(Data, 8);
     Size   = BitConverter.ToUInt32(Data, 16);
 }
Exemplo n.º 16
0
        [Parser(Opcode.SMSG_VOID_STORAGE_TRANSFER_CHANGES)] // 4.3.4
        public static void HandleVoidStorageTransferChanges(Packet packet)
        {
            var count1 = packet.ReadBits("Count 1", 5);
            var count2 = packet.ReadBits("Count 2", 5);

            var id1  = new byte[count1][];
            var guid = new byte[count1][];

            for (int i = 0; i < count1; ++i)
            {
                id1[i]  = new byte[8];
                guid[i] = new byte[8];

                guid[i][7] = packet.ReadBit();
                id1[i][7]  = packet.ReadBit();
                id1[i][4]  = packet.ReadBit();
                guid[i][6] = packet.ReadBit();
                guid[i][5] = packet.ReadBit();
                id1[i][3]  = packet.ReadBit();
                id1[i][5]  = packet.ReadBit();
                guid[i][4] = packet.ReadBit();
                guid[i][2] = packet.ReadBit();
                guid[i][0] = packet.ReadBit();
                guid[i][3] = packet.ReadBit();
                guid[i][1] = packet.ReadBit();
                id1[i][2]  = packet.ReadBit();
                id1[i][0]  = packet.ReadBit();
                id1[i][1]  = packet.ReadBit();
                id1[i][6]  = packet.ReadBit();
            }

            var id2 = new byte[count2][];

            for (int i = 0; i < count2; ++i)
            {
                id2[i] = packet.StartBitStream(1, 7, 3, 5, 6, 2, 4, 0);
            }

            for (int i = 0; i < count2; ++i)
            {
                packet.ParseBitStream(id2[i], 3, 1, 0, 2, 7, 5, 6, 4);
                packet.WriteLine("[{1}] Item Id 2: {0}", BitConverter.ToUInt64(id2[i], 0), i);
            }

            for (int i = 0; i < count1; ++i)
            {
                packet.ReadInt32("Item Suffix Factor", i);

                packet.ReadXORByte(id1[i], 6);
                packet.ReadXORByte(id1[i], 4);
                packet.ReadXORByte(guid[i], 4);
                packet.ReadXORByte(id1[i], 2);
                packet.ReadXORByte(guid[i], 1);
                packet.ReadXORByte(guid[i], 3);
                packet.ReadXORByte(id1[i], 3);
                packet.ReadXORByte(guid[i], 0);
                packet.ReadXORByte(id1[i], 0);
                packet.ReadXORByte(guid[i], 6);
                packet.ReadXORByte(id1[i], 5);
                packet.ReadXORByte(guid[i], 5);
                packet.ReadXORByte(guid[i], 7);

                packet.ReadEntryWithName <UInt32>(StoreNameType.Item, "Item Entry", i);

                packet.ReadXORByte(id1[i], 1);

                packet.ReadInt32("Item Slot", i);

                packet.ReadXORByte(guid[i], 2);
                packet.ReadXORByte(id1[i], 7);

                packet.ReadInt32("Item Random Property ID", i);

                packet.WriteLine("[{1}] Item Id 1: {0}", BitConverter.ToUInt64(id1[i], 0), i);
                packet.WriteGuid("Item Player Creator Guid", guid[i], i);
            }
        }
 /// <summary>
 /// 从缓存中提取ulong结果
 /// </summary>
 /// <param name="buffer">缓存数据</param>
 /// <param name="index">索引位置</param>
 /// <returns>ulong对象</returns>
 public virtual ulong TransUInt64( byte[] buffer, int index )
 {
     return BitConverter.ToUInt64( ByteTransDataFormat8( buffer, index ), 0 );
 }
Exemplo n.º 18
0
        [Parser(Opcode.SMSG_VOID_STORAGE_CONTENTS)] // 4.3.4
        public static void HandleVoidStorageContents(Packet packet)
        {
            var count = packet.ReadBits("Count", 8);

            var id   = new byte[count][];
            var guid = new byte[count][];

            for (int i = 0; i < count; ++i)
            {
                id[i]   = new byte[8];
                guid[i] = new byte[8];

                guid[i][3] = packet.ReadBit();
                id[i][5]   = packet.ReadBit();
                guid[i][6] = packet.ReadBit();
                guid[i][1] = packet.ReadBit();
                id[i][1]   = packet.ReadBit();
                id[i][3]   = packet.ReadBit();
                id[i][6]   = packet.ReadBit();
                guid[i][5] = packet.ReadBit();
                guid[i][2] = packet.ReadBit();
                id[i][2]   = packet.ReadBit();
                guid[i][4] = packet.ReadBit();
                id[i][0]   = packet.ReadBit();
                id[i][4]   = packet.ReadBit();
                id[i][7]   = packet.ReadBit();
                guid[i][0] = packet.ReadBit();
                guid[i][7] = packet.ReadBit();
            }

            for (int i = 0; i < count; ++i)
            {
                packet.ReadXORByte(guid[i], 3);

                packet.ReadInt32("Item Suffix Factor", i);

                packet.ReadXORByte(guid[i], 4);

                packet.ReadInt32("Item Slot", i);

                packet.ReadXORByte(id[i], 0);
                packet.ReadXORByte(id[i], 6);
                packet.ReadXORByte(guid[i], 0);
                packet.ReadXORByte(guid[i], 1);

                packet.ReadInt32("Item Random Property ID", i);

                packet.ReadXORByte(id[i], 4);
                packet.ReadXORByte(id[i], 5);
                packet.ReadXORByte(id[i], 2);
                packet.ReadXORByte(guid[i], 2);
                packet.ReadXORByte(guid[i], 6);
                packet.ReadXORByte(id[i], 1);
                packet.ReadXORByte(id[i], 3);
                packet.ReadXORByte(guid[i], 5);
                packet.ReadXORByte(guid[i], 7);

                packet.ReadEntryWithName <UInt32>(StoreNameType.Item, "Item Entry", i);

                packet.ReadXORByte(id[i], 7);

                packet.WriteLine("[{1}] Item Id: {0}", BitConverter.ToUInt64(id[i], 0), i);
                packet.WriteGuid("Item Player Creator Guid", guid[i], i);
            }
        }
        /// <summary>
        /// Generates a cryptographically strong random <see cref="ulong" /> value that is greater than or equal to 0UL, and less than or equal to <see cref="ulong.MaxValue" />.
        /// </summary>
        /// <param name="randomNumberGenerator">The <see cref="RandomNumberGenerator" /> object to be used for random number generation.</param>
        /// <returns>
        /// A cryptographically strong random <see cref="ulong" /> value that is greater than or equal to 0UL, and less than or equal to <see cref="ulong.MaxValue" />.
        /// </returns>
        public static ulong GetUInt64(this RandomNumberGenerator randomNumberGenerator)
        {
            Check.ArgumentNull(randomNumberGenerator, nameof(randomNumberGenerator));

            return(BitConverter.ToUInt64(randomNumberGenerator.GetBytes(8), 0));
        }
Exemplo n.º 20
0
        public static UInt64 ReadUInt64(byte[] bytes, int offset)
        {
            var _bytes = CopyBytes(bytes, offset, 8);

            return(BitConverter.ToUInt64(_bytes.Reverse().ToArray(), 0));
        }
Exemplo n.º 21
0
        uint ReadHeader(byte[] buffer, uint offset, uint numBytesAvailable)
        {
            // we need at least the first 2 bytes of the message frame to be able to decode the header size

            if (numBytesAvailable < 2)
            {
                return(0);
            }


            // read first two bytes

            var codeByte    = buffer[offset + 0];
            var payloadByte = buffer[offset + 1];


            // calculate header size

            IsMasked = (byte)(payloadByte & (byte)Flag.Mask) == (byte)Flag.Mask;

            uint payload    = (uint)(payloadByte & (byte)Flag.Payload);
            uint headerSize = 2;

            if (IsMasked)
            {
                headerSize += 4;
            }

            if (payload == 126)
            {
                headerSize += 2;
            }
            else if (payload == 127)
            {
                headerSize += 8;
            }


            // wait until we have the whole header available
            // ... this saves trying to track state, but does mean we need at least enough buffer space for the whole header

            if (numBytesAvailable < headerSize)
            {
                return(0);
            }


            // we have the whole header, so get the length of the payload

            if (payload < 126)
            {
                mPayloadLen = (uint)payload;
            }
            else if (payload == 126)
            {
                var size = Endian.NetworkToHost(BitConverter.ToUInt16(buffer, (int)offset + 2));

                if (size > WebSocket.MaxPayload)
                {
                    throw new WebSocketException(WebSocketStatus.PayloadTooLarge);
                }

                mPayloadLen = (uint)size;
            }
            else // payload == 127
            {
                var size = Endian.NetworkToHost(BitConverter.ToUInt64(buffer, (int)offset + 2));

                if (size > WebSocket.MaxPayload)
                {
                    throw new WebSocketException(WebSocketStatus.PayloadTooLarge);
                }

                mPayloadLen = (uint)size;
            }


            // decode all the control bits

            IsFinal = (codeByte & (byte)Flag.Fin) == (byte)Flag.Fin;
            OpCode  = (WebSocketOpCode)(codeByte & (byte)Flag.Op);

            if ((codeByte & (byte)Flag.Rsv) != 0)
            {
                throw new WebSocketException(WebSocketStatus.ProtocolError);
            }


            // read mask key

            if (IsMasked)
            {
                Array.Copy(buffer, offset + headerSize - 4, mMask, 0, 4);
            }


            // if no payload for this message then we're done (presumably this is a control frame)

            if (mPayloadLen == 0)
            {
                mState = Reading.Complete;
                return(headerSize);
            }

            // otherwise start reading the payload body

            mState  = Reading.Payload;
            Payload = new byte[mPayloadLen];


            // return total number of bytes consumed from the buffer

            var numBytesOfBodyRead = ReadPayload(buffer, offset + headerSize, numBytesAvailable - headerSize);

            return(headerSize + numBytesOfBodyRead);
        }
Exemplo n.º 22
0
        /// <summary>
        ///     Marshals an array of byte to a managed object.
        /// </summary>
        /// <param name="byteArray">The array of bytes corresponding to a managed object.</param>
        /// <returns>A managed object.</returns>
        public unsafe static T ByteArrayToObject(byte[] byteArray)
        {
            var requiresMarshal = MarshalCache <T> .TypeRequiresMarshal;
            var size            = requiresMarshal ? MarshalCache <T> .Size : Unsafe.SizeOf <T>();

            fixed(byte *b = byteArray)
            {
                if (requiresMarshal)
                {
                    return(Marshal.PtrToStructure <T>(new IntPtr(b)));
                }

                return(Unsafe.Read <T>(b));
            }

#if false
            // We'll tried to avoid marshalling as it really slows the process
            // First, check if the type can be converted without marhsalling
            switch (TypeCode)
            {
            case TypeCode.Object:
                if (IsIntPtr)
                {
                    switch (byteArray.Length)
                    {
                    case 1:
                        return
                            ((T)
                             (object)
                             new IntPtr(BitConverter.ToInt32(new byte[] { byteArray[0], 0x0, 0x0, 0x0 }, 0)));

                    case 2:
                        return
                            ((T)
                             (object)
                             new IntPtr(
                                 BitConverter.ToInt32(new byte[] { byteArray[0], byteArray[1], 0x0, 0x0 },
                                                      0)));

                    case 4:
                        return((T)(object)new IntPtr(BitConverter.ToInt32(byteArray, 0)));

                    case 8:
                        return((T)(object)new IntPtr(BitConverter.ToInt64(byteArray, 0)));
                    }
                }
                break;

            case TypeCode.Boolean:
                return((T)(object)BitConverter.ToBoolean(byteArray, 0));

            case TypeCode.Byte:
                return((T)(object)byteArray[0]);

            case TypeCode.Char:
                return((T)(object)Encoding.UTF8.GetChars(byteArray)[0]);      //BitConverter.ToChar(byteArray, 0);

            case TypeCode.Double:
                return((T)(object)BitConverter.ToDouble(byteArray, 0));

            case TypeCode.Int16:
                return((T)(object)BitConverter.ToInt16(byteArray, 0));

            case TypeCode.Int32:
                return((T)(object)BitConverter.ToInt32(byteArray, 0));

            case TypeCode.Int64:
                return((T)(object)BitConverter.ToInt64(byteArray, 0));

            case TypeCode.Single:
                return((T)(object)BitConverter.ToSingle(byteArray, 0));

            case TypeCode.String:
                throw new InvalidCastException("This method doesn't support string conversion.");

            case TypeCode.UInt16:
                return((T)(object)BitConverter.ToUInt16(byteArray, 0));

            case TypeCode.UInt32:
                return((T)(object)BitConverter.ToUInt32(byteArray, 0));

            case TypeCode.UInt64:
                return((T)(object)BitConverter.ToUInt64(byteArray, 0));
            }
            // Check if it's not a common type
            // Allocate a block of unmanaged memory
            using (var unmanaged = new LocalUnmanagedMemory(Size))
            {
                // Write the array of bytes inside the unmanaged memory
                unmanaged.Write(byteArray);
                // Return a managed object created from the block of unmanaged memory
                return(unmanaged.Read <T>());
            }
#endif
        }
Exemplo n.º 23
0
 /// <summary>
 /// Changes the endianness of the value.
 /// </summary>
 /// <param name="value">The value to change the endianness of.</param>
 /// <returns>The value with endianness changed.</returns>
 public static ulong ChangeEndianness(this ulong value)
 {
     byte[] bytes = BitConverter.GetBytes(value);
     Array.Reverse(bytes);
     return(BitConverter.ToUInt64(bytes, 0));
 }
Exemplo n.º 24
0
        public static void HandleAllAchievementCriteriaDataAccount(Packet packet)
        {
            var count = packet.ReadBits("Criteria count", 19);

            var counter   = new byte[count][];
            var accountId = new byte[count][];
            var flags     = new byte[count];

            for (var i = 0; i < count; ++i)
            {
                counter[i]   = new byte[8];
                accountId[i] = new byte[8];

                counter[i][2]   = packet.ReadBit();
                accountId[i][0] = packet.ReadBit();
                counter[i][0]   = packet.ReadBit();
                accountId[i][1] = packet.ReadBit();

                flags[i] = (byte)(packet.ReadBits(4) & 0xFFu);

                accountId[i][5] = packet.ReadBit();
                accountId[i][7] = packet.ReadBit();
                counter[i][7]   = packet.ReadBit();
                counter[i][6]   = packet.ReadBit();
                accountId[i][4] = packet.ReadBit();
                accountId[i][2] = packet.ReadBit();
                counter[i][4]   = packet.ReadBit();
                accountId[i][3] = packet.ReadBit();
                accountId[i][6] = packet.ReadBit();
                counter[i][3]   = packet.ReadBit();
                counter[i][5]   = packet.ReadBit();
                counter[i][1]   = packet.ReadBit();
            }

            for (var i = 0; i < count; ++i)
            {
                packet.ReadXORByte(accountId[i], 0);
                packet.ReadXORByte(counter[i], 4);
                packet.ReadXORByte(accountId[i], 2);
                packet.ReadXORByte(counter[i], 0);
                packet.ReadPackedTime("Time", i);
                packet.ReadXORByte(accountId[i], 6);
                packet.ReadInt32("Criteria ID", i);
                packet.ReadXORByte(accountId[i], 5);
                packet.ReadXORByte(counter[i], 1);
                packet.ReadXORByte(counter[i], 2);
                packet.ReadXORByte(counter[i], 3);
                packet.ReadXORByte(accountId[i], 4);
                packet.ReadXORByte(accountId[i], 1);
                packet.ReadXORByte(accountId[i], 7);
                packet.ReadXORByte(counter[i], 5);
                packet.ReadUInt32("Timer 1", i);
                packet.ReadXORByte(counter[i], 7);
                packet.ReadXORByte(accountId[i], 3);
                packet.ReadUInt32("Timer 2", i);
                packet.ReadXORByte(counter[i], 6);

                packet.AddValue("Criteria Flags", flags[i], i);
                packet.AddValue("Criteria Counter", BitConverter.ToUInt64(counter[i], 0), i);
                packet.AddValue("Account", i, BitConverter.ToUInt64(accountId[i], 0), i);
            }
        }
Exemplo n.º 25
0
        public static ulong ToUnsignedLong(object value)
        {
            var resultBytes = GetBytes(value, 8, false);

            return(BitConverter.ToUInt64(resultBytes, 0));
        }
Exemplo n.º 26
0
        public static void HandleInspectAchievementDataResponse(Packet packet)
        {
            var guid = new byte[8];

            var criterias    = packet.ReadBits("Criterias", 19);
            var achievements = packet.ReadBits("Achievements", 20);

            var flags   = new uint[criterias];
            var counter = new byte[criterias][];
            var guid2   = new byte[criterias][];

            for (var i = 0; i < criterias; ++i)
            {
                counter[i] = new byte[8];
                guid2[i]   = new byte[8];

                counter[i][5] = packet.ReadBit();
                flags[i]      = packet.ReadBits(4);
                guid2[i][4]   = packet.ReadBit();
                counter[i][6] = packet.ReadBit();
                guid2[i][6]   = packet.ReadBit();
                guid2[i][1]   = packet.ReadBit();
                counter[i][0] = packet.ReadBit();
                guid2[i][5]   = packet.ReadBit();
                counter[i][7] = packet.ReadBit();
                guid2[i][2]   = packet.ReadBit();
                counter[i][1] = packet.ReadBit();
                guid2[i][7]   = packet.ReadBit();
                counter[i][2] = packet.ReadBit();
                counter[i][3] = packet.ReadBit();
                guid2[i][0]   = packet.ReadBit();
                counter[i][4] = packet.ReadBit();
                guid2[i][3]   = packet.ReadBit();
            }

            guid[1] = packet.ReadBit();
            guid[7] = packet.ReadBit();

            var guid5 = new byte[achievements][];

            for (var i = 0; i < achievements; ++i)
            {
                guid5[i] = new byte[8];

                guid5[i][4] = packet.ReadBit();
                guid5[i][3] = packet.ReadBit();
                guid5[i][0] = packet.ReadBit();
                guid5[i][6] = packet.ReadBit();
                guid5[i][2] = packet.ReadBit();
                guid5[i][5] = packet.ReadBit();
                guid5[i][1] = packet.ReadBit();
                guid5[i][7] = packet.ReadBit();
            }

            guid[3] = packet.ReadBit();
            guid[5] = packet.ReadBit();
            guid[6] = packet.ReadBit();
            guid[0] = packet.ReadBit();
            guid[4] = packet.ReadBit();
            guid[2] = packet.ReadBit();

            packet.ReadXORByte(guid, 2);

            for (var i = 0; i < achievements; ++i)
            {
                packet.ReadXORByte(guid5[i], 3);
                packet.ReadXORByte(guid5[i], 0);
                packet.ReadInt32("Realm Id", i);
                packet.ReadInt32("Realm Id", i);
                packet.ReadXORByte(guid5[i], 4);
                packet.ReadUInt32("Achievement Id", i);
                packet.ReadPackedTime("Achievement Time", i);
                packet.ReadXORByte(guid5[i], 7);
                packet.ReadXORByte(guid5[i], 5);
                packet.ReadXORByte(guid5[i], 1);
                packet.ReadXORByte(guid5[i], 6);
                packet.ReadXORByte(guid5[i], 2);

                packet.WriteGuid("Player GUID", guid2[i], i);
            }

            for (var i = 0; i < criterias; ++i)
            {
                packet.ReadUInt32("Criteria Timer 2", i);
                packet.ReadXORByte(guid2[i], 3);
                packet.ReadXORByte(counter[i], 5);
                packet.ReadXORByte(guid2[i], 6);
                packet.ReadXORByte(guid2[i], 4);
                packet.ReadXORByte(counter[i], 4);
                packet.ReadXORByte(counter[i], 7);
                packet.ReadUInt32("Criteria Id", i);
                packet.ReadXORByte(counter[i], 0);
                packet.ReadXORByte(counter[i], 1);
                packet.ReadXORByte(guid2[i], 0);
                packet.ReadXORByte(guid2[i], 1);
                packet.ReadUInt32("Criteria Timer 1", i);
                packet.ReadXORByte(guid2[i], 2);
                packet.ReadXORByte(guid2[i], 7);
                packet.ReadXORByte(guid2[i], 5);
                packet.ReadXORByte(counter[i], 2);
                packet.ReadPackedTime("Criteria Time", i);
                packet.ReadXORByte(counter[i], 6);
                packet.ReadXORByte(counter[i], 3);

                packet.AddValue("Criteria Flags", flags[i], i);
                packet.AddValue("Criteria Counter", BitConverter.ToUInt64(counter[i], 0), i);
                packet.WriteGuid("Criteria GUID", guid2[i], i);
            }

            packet.ReadXORByte(guid, 1);
            packet.ReadXORByte(guid, 4);
            packet.ReadXORByte(guid, 5);
            packet.ReadXORByte(guid, 3);
            packet.ReadXORByte(guid, 6);
            packet.ReadXORByte(guid, 0);
            packet.ReadXORByte(guid, 7);

            packet.WriteGuid("GUID", guid);
        }
Exemplo n.º 27
0
        public static void DecompressDxt5Block(byte[] Data, int Width, int Height, int pos, byte[] output, int xPos, int yPos)
        {
            if (pos >= Data.Length) return;
            Color[] c = new Color[4];
            byte[] alpha = new byte[8];

            var alpha0 = Data[pos];
            var alpha1 = Data[pos + 1];
            var alphaMask = BitConverter.ToUInt64(Data, pos) >> 16;

            var color0 = BitConverter.ToUInt16(Data, pos + 8);
            var color1 = BitConverter.ToUInt16(Data, pos + 10);
            var mask = BitConverter.ToUInt32(Data, pos + 12);

            alpha[0] = alpha0;
            alpha[1] = alpha1;
            if (alpha[0] > alpha[1])
            {
                alpha[2] = (byte)((6 * alpha[0] + 1 * alpha[1] + 3) / 7);
                alpha[3] = (byte)((5 * alpha[0] + 2 * alpha[1] + 3) / 7);
                alpha[4] = (byte)((4 * alpha[0] + 3 * alpha[1] + 3) / 7);
                alpha[5] = (byte)((3 * alpha[0] + 4 * alpha[1] + 3) / 7);
                alpha[6] = (byte)((2 * alpha[0] + 5 * alpha[1] + 3) / 7);
                alpha[7] = (byte)((1 * alpha[0] + 6 * alpha[1] + 3) / 7);
            }
            else
            {
                alpha[2] = (byte)((4 * alpha[0] + 1 * alpha[1] + 2) / 5);
                alpha[3] = (byte)((3 * alpha[0] + 2 * alpha[1] + 2) / 5);
                alpha[4] = (byte)((2 * alpha[0] + 3 * alpha[1] + 2) / 5);
                alpha[5] = (byte)((1 * alpha[0] + 4 * alpha[1] + 2) / 5);
                alpha[6] = 0;
                alpha[7] = 255;
            }

            Color(color0, ref c[0]);
            Color(color1, ref c[1]);

            c[2].R = (byte)((2 * c[0].R + c[1].R) / 3);
            c[2].G = (byte)((2 * c[0].G + c[1].G) / 3);
            c[2].B = (byte)((2 * c[0].B + c[1].B) / 3);

            c[3].R = (byte)((c[0].R + 2 * c[1].R) / 3);
            c[3].G = (byte)((c[0].G + 2 * c[1].G) / 3);
            c[3].B = (byte)((c[0].B + 2 * c[1].B) / 3);

            for (int y = 0; y < 4; y++)
            {
                for (int x = 0; x < 4; x++)
                {
                    var index = mask & 3;
                    mask >>= 2;
                    var alphaIndex = alphaMask & 7;
                    alphaMask >>= 3;

                    var col = c[index];

                    var x2 = xPos + x;
                    var y2 = yPos + y;

                    var offset = (x2 + y2 * Width) * 4;
                    output[offset] = col.B;
                    output[offset + 1] = col.G;
                    output[offset + 2] = col.R;
                    output[offset + 3] = alpha[alphaIndex];
                }
            }
        }
Exemplo n.º 28
0
        public static void HandleAllAchievementDataPlayer(Packet packet)
        {
            var bits20 = packet.ReadBits("Criteria count", 19);

            var counter = new byte[bits20][];
            var guid2   = new byte[bits20][];
            var flags   = new byte[bits20];

            for (var i = 0; i < bits20; ++i)
            {
                counter[i] = new byte[8];
                guid2[i]   = new byte[8];

                counter[i][3] = packet.ReadBit();
                counter[i][6] = packet.ReadBit();
                guid2[i][0]   = packet.ReadBit();
                guid2[i][7]   = packet.ReadBit();
                counter[i][7] = packet.ReadBit();
                counter[i][0] = packet.ReadBit();
                counter[i][5] = packet.ReadBit();

                flags[i] = (byte)(packet.ReadBits(4) & 0xFFu);

                counter[i][4] = packet.ReadBit();
                guid2[i][3]   = packet.ReadBit();
                guid2[i][6]   = packet.ReadBit();
                guid2[i][4]   = packet.ReadBit();
                guid2[i][5]   = packet.ReadBit();
                guid2[i][1]   = packet.ReadBit();
                counter[i][2] = packet.ReadBit();
                counter[i][1] = packet.ReadBit();
                guid2[i][2]   = packet.ReadBit();
            }

            var bits10 = packet.ReadBits("Achievement count", 20);

            var guid1 = new byte[bits10][];

            for (var i = 0; i < bits10; ++i)
            {
                guid1[i] = new byte[8];
                packet.StartBitStream(guid1[i], 4, 7, 2, 3, 1, 5, 6, 0);
            }

            for (var i = 0; i < bits10; ++i)
            {
                packet.ReadXORByte(guid1[i], 6);
                packet.ReadXORByte(guid1[i], 5);
                packet.ReadXORByte(guid1[i], 3);
                packet.ReadXORByte(guid1[i], 0);
                packet.ReadXORByte(guid1[i], 2);
                packet.ReadXORByte(guid1[i], 1);
                packet.ReadUInt32("Achievement Id", i);
                packet.ReadInt32("Realm Id", i);
                packet.ReadInt32("Realm Id", i);
                packet.ReadXORByte(guid1[i], 4);
                packet.ReadPackedTime("Time", i);
                packet.ReadXORByte(guid1[i], 7);

                packet.WriteGuid("Guid3", guid1[i], i);
            }

            for (var i = 0; i < bits20; ++i)
            {
                packet.ReadPackedTime("Time", i);

                packet.ReadXORByte(counter[i], 2);

                packet.ReadXORByte(guid2[i], 2);
                packet.ReadXORByte(guid2[i], 3);

                packet.ReadInt32("Criteria ID", i);

                packet.ReadXORByte(guid2[i], 5);

                packet.ReadXORByte(counter[i], 4);
                packet.ReadXORByte(counter[i], 7);
                packet.ReadXORByte(counter[i], 0);

                packet.ReadXORByte(guid2[i], 0);
                packet.ReadUInt32("Timer 1", i);
                packet.ReadXORByte(counter[i], 1);
                packet.ReadXORByte(counter[i], 3);
                packet.ReadXORByte(counter[i], 6);
                packet.ReadXORByte(counter[i], 5);

                packet.ReadXORByte(guid2[i], 4);
                packet.ReadXORByte(guid2[i], 6);
                packet.ReadXORByte(guid2[i], 1);
                packet.ReadXORByte(guid2[i], 7);

                packet.ReadUInt32("Timer 2", i);

                packet.WriteGuid("Guid2", guid2[i], i);

                packet.AddValue("Criteria Flags", flags[i], i);
                packet.AddValue("Criteria Counter", BitConverter.ToUInt64(counter[i], 0), i);
                packet.WriteGuid("Criteria GUID", guid2[i], i);
            }
        }
Exemplo n.º 29
0
 public static UInt64 ReadUInt64BE(this BinaryReader binRdr)
 {
     return(BitConverter.ToUInt64(binRdr.ReadBytesRequired(sizeof(UInt64)).Reverse(), 0));
 }
Exemplo n.º 30
0
        protected override void HandleData(byte[] data)
        {
            byte b; long len;

            for (long i = 0; i < data.LongLength;)
            {
                switch (State)
                {
                case FrameParserState.Head1:
                    b = data[i++];
                    bool fin  = Incoming.FIN = (b & 128) == 128;
                    bool rsv1 = Incoming.RSV1 = (b & 64) == 64;
                    bool rsv2 = Incoming.RSV2 = (b & 32) == 32;
                    bool rsv3 = Incoming.RSV3 = (b & 16) == 16;
                    Incoming.Opcode = (byte)(b - (fin ? 128 : 0) - (rsv1 ? 64 : 0) - (rsv2 ? 32 : 0) - (rsv3 ? 16 : 0));
                    State           = FrameParserState.Head2;
                    break;

                case FrameParserState.Head2:
                    b = data[i++];
                    bool masked = (b & 128) == 128;
                    if (masked)
                    {
                        Incoming.Mask = new byte[4];
                    }
                    long payloadLen = b - (masked ? 128 : 0);
                    if (payloadLen > 125)
                    {
                        Temp2 = new byte[payloadLen == 126 ? 2 : 8];
                        State = FrameParserState.ExtendedLen;
                    }
                    else
                    {
                        Incoming.Payload = new byte[payloadLen];
                        State            = Incoming.Masked ? FrameParserState.Mask : FrameParserState.Payload;
                    }
                    break;

                case FrameParserState.ExtendedLen:
                    len = Math.Min(Temp2.LongLength - Temp1, data.LongLength - i);
                    Frame.ArrayCopy(data, i, Temp2, Temp1, len);
                    Temp1 += len; i += len;
                    if (Temp2.LongLength != Temp1)
                    {
                        break;
                    }
                    Incoming.Payload = new byte[Temp1 == 8 ? BitConverter.ToUInt64(Temp2, 0) : BitConverter.ToUInt16(Temp2, 0)];
                    Temp1            = 0; Temp2 = null;
                    State            = Incoming.Masked ? FrameParserState.Mask : FrameParserState.Payload;
                    break;

                case FrameParserState.Mask:
                    len = Math.Min(4 - Temp1, data.LongLength - i);
                    Frame.ArrayCopy(data, i, Incoming.Mask, Temp1, len);
                    Temp1 += len; i += len;
                    if (Temp1 != 4)
                    {
                        break;
                    }
                    Temp1 = 0;
                    State = FrameParserState.Payload;
                    break;

                case FrameParserState.Payload:
                    len = Math.Min(Incoming.PayloadLength - Temp1, data.LongLength - i);
                    Frame.ArrayCopy(data, i, Incoming.Payload, Temp1, len);
                    Temp1 += len; i += len;
                    if (Temp1 != Incoming.PayloadLength)
                    {
                        break;
                    }
                    if (Incoming.Masked)
                    {
                        Incoming.FlipMask();
                    }
                    PushIncoming();
                    State = FrameParserState.Head1;
                    Temp1 = 0;
                    break;
                }
            }
        }