示例#1
0
        //[Fact(Skip = "Skipped")]
        public void PrimitiveConverterTest()
        {
            Util.Log("VariantBitConverterShortTest");

            Random random = new Random();

            byte[]     buffer = new byte[256];
            BytePacker packer;

            //Bool
            packer = new BytePacker(buffer);
            bool boolSrc = true;
            bool boolDst = false;

            DataSerializer.Serialize(packer, boolSrc);
            packer.Position = 0;
            boolDst         = DataSerializer.Deserialize <bool>(packer);

            if (boolSrc != boolDst)
            {
                throw new InvalidProgramException("bool");
            }

            //char
            packer = new BytePacker(buffer);
            char charSrc = (char)random.Next(char.MinValue, char.MaxValue);
            char charDst = '0';

            DataSerializer.Serialize(packer, charSrc);
            packer.Position = 0;
            charDst         = DataSerializer.Deserialize <char>(packer);

            if (charSrc != charDst)
            {
                throw new InvalidProgramException("char");
            }

            //sbyte
            packer = new BytePacker(buffer);
            sbyte sbyteSrc = (sbyte)random.Next(sbyte.MinValue, sbyte.MaxValue);
            sbyte sbyteDst = 0;

            DataSerializer.Serialize(packer, sbyteSrc);
            packer.Position = 0;
            sbyteDst        = DataSerializer.Deserialize <sbyte>(packer);

            if (sbyteSrc != sbyteDst)
            {
                throw new InvalidProgramException("sbyte");
            }

            //byte
            packer = new BytePacker(buffer);
            byte byteSrc = (byte)random.Next(byte.MinValue, byte.MaxValue);
            byte byteDst = 0;

            DataSerializer.Serialize(packer, byteSrc);
            packer.Position = 0;
            byteDst         = DataSerializer.Deserialize <byte>(packer);

            if (byteSrc != byteDst)
            {
                throw new InvalidProgramException("byte");
            }

            //short
            packer = new BytePacker(buffer);
            short shortSrc = (short)random.Next(short.MinValue, short.MaxValue);
            short shortDst = 0;

            DataSerializer.Serialize(packer, shortSrc);
            packer.Position = 0;
            shortDst        = DataSerializer.Deserialize <short>(packer);

            if (shortSrc != shortDst)
            {
                throw new InvalidProgramException("short");
            }

            //ushort
            packer = new BytePacker(buffer);
            ushort ushortSrc = (ushort)random.Next(ushort.MinValue, ushort.MaxValue);
            ushort ushortDst = 0;

            DataSerializer.Serialize(packer, ushortSrc);
            packer.Position = 0;
            ushortDst       = DataSerializer.Deserialize <ushort>(packer);

            if (ushortSrc != ushortDst)
            {
                throw new InvalidProgramException("ushort");
            }

            //int
            packer = new BytePacker(buffer);
            int intSrc = random.Next();
            int intDst = 0;

            DataSerializer.Serialize(packer, intSrc);
            packer.Position = 0;
            intDst          = DataSerializer.Deserialize <int>(packer);

            if (intSrc != intDst)
            {
                throw new InvalidProgramException("int");
            }

            //uint
            packer = new BytePacker(buffer);
            uint uintSrc = (uint)random.Next(0, int.MaxValue) * 2;
            uint uintDst = 0;

            DataSerializer.Serialize(packer, uintSrc);
            packer.Position = 0;
            uintDst         = DataSerializer.Deserialize <uint>(packer);

            if (uintSrc != uintDst)
            {
                throw new InvalidProgramException("uint");
            }

            //long
            packer = new BytePacker(buffer);
            long longSrc = (long)random.Next();
            long longDst = 0;

            DataSerializer.Serialize(packer, longSrc);
            packer.Position = 0;
            longDst         = DataSerializer.Deserialize <long>(packer);

            if (longSrc != longDst)
            {
                throw new InvalidProgramException("long");
            }

            //ulong
            packer = new BytePacker(buffer);
            ulong ulongSrc = (ulong)random.Next(0, int.MaxValue) * 2;
            ulong ulongDst = 0;

            DataSerializer.Serialize(packer, ulongSrc);
            packer.Position = 0;
            ulongDst        = DataSerializer.Deserialize <ulong>(packer);

            if (ulongSrc != ulongDst)
            {
                throw new InvalidProgramException("ulong");
            }

            //float
            packer = new BytePacker(buffer);
            float floatSrc = (float)random.NextDouble();
            float floatDst = 0;

            DataSerializer.Serialize(packer, floatSrc);
            packer.Position = 0;
            floatDst        = DataSerializer.Deserialize <float>(packer);

            if (floatSrc != floatDst)
            {
                throw new InvalidProgramException("float");
            }

            //double
            packer = new BytePacker(buffer);
            double doubleSrc = random.NextDouble();
            double doubleDst = 0;

            DataSerializer.Serialize(packer, doubleSrc);
            packer.Position = 0;
            doubleDst       = DataSerializer.Deserialize <double>(packer);

            if (doubleSrc != doubleDst)
            {
                throw new InvalidProgramException("double");
            }


            //string
            packer = new BytePacker(buffer);
            string stringSrc = "こんにちはasdf";
            //string stringSrc = "sehiillhg";
            string stringDst = "";

            DataSerializer.Serialize(packer, stringSrc);
            packer.Position = 0;
            stringDst       = DataSerializer.Deserialize <string>(packer);

            if (stringSrc != stringDst)
            {
                throw new InvalidProgramException("string");
            }

            //enum
            packer = new BytePacker(buffer);
            TestEnum enumSrc = TestEnum.B;
            TestEnum enumDst = TestEnum.A;

            DataSerializer.Serialize(packer, enumSrc);
            packer.Position = 0;
            enumDst         = DataSerializer.Deserialize <TestEnum>(packer);

            if (enumSrc != enumDst)
            {
                throw new InvalidProgramException("enum");
            }

            //DateTime
            packer = new BytePacker(buffer);
            DateTime dateTimeSrc = DateTime.Now;
            DateTime dateTimeDst;

            DataSerializer.Serialize(packer, dateTimeSrc);
            packer.Position = 0;
            dateTimeDst     = DataSerializer.Deserialize <DateTime>(packer);

            if (dateTimeSrc != dateTimeDst)
            {
                throw new InvalidProgramException("DateTime");
            }

            //TimeSpan
            packer = new BytePacker(buffer);
            TimeSpan timeSpanSrc = new TimeSpan(98271983172893);
            TimeSpan timeSpanDst;

            DataSerializer.Serialize(packer, timeSpanSrc);
            packer.Position = 0;
            timeSpanDst     = DataSerializer.Deserialize <TimeSpan>(packer);

            if (timeSpanSrc != timeSpanDst)
            {
                throw new InvalidProgramException("TimeSpan");
            }

            //class
            packer = new BytePacker(buffer);
            TestConvertClass classSrc = new TestConvertClass();

            classSrc.IntData              = random.Next();
            classSrc.StringData           = "asfeaefaw";
            classSrc.FlaotData            = (float)random.NextDouble();
            classSrc.classData            = new TestParamClass();
            classSrc.classData.IntData    = random.Next();
            classSrc.classData.StringData = "igi3u8z2";

            TestConvertClass classDst;

            DataSerializer.Serialize(packer, classSrc);
            packer.Position = 0;
            classDst        = DataSerializer.Deserialize <TestConvertClass>(packer);

            if (!classSrc.Equals(classDst))
            {
                throw new InvalidProgramException("TestConvertClass");
            }


            //array primitive
            packer = new BytePacker(buffer);
            byte[] bArraySrc = { 1, 2, 3, 4, 5 };
            byte[] bArrayDst = null;
            DataSerializer.Serialize(packer, bArraySrc);
            packer.Position = 0;
            bArrayDst       = DataSerializer.Deserialize <byte[]>(packer);

            if (bArraySrc.Length != bArrayDst.Length)
            {
                throw new InvalidProgramException("byte[]");
            }
            for (int i = 0; i < bArraySrc.Length; i++)
            {
                if (bArraySrc[i] != bArrayDst[i])
                {
                    throw new InvalidProgramException("byte[]");
                }
            }

            //array class
            packer = new BytePacker(buffer);
            TestConvertClass[] cArraySrc = { new TestConvertClass(), new TestConvertClass(), new TestConvertClass(), new TestConvertClass(), new TestConvertClass() };
            cArraySrc[1].IntData = 90;
            TestConvertClass[] cArrayDst = null;
            DataSerializer.Serialize(packer, cArraySrc);
            packer.Position = 0;
            cArrayDst       = DataSerializer.Deserialize <TestConvertClass[]>(packer);

            if (cArraySrc.Length != cArrayDst.Length)
            {
                throw new InvalidProgramException("TestConvertClass[]");
            }
            for (int i = 0; i < bArraySrc.Length; i++)
            {
                if (!cArraySrc[i].Equals(cArrayDst[i]))
                {
                    throw new InvalidProgramException("TestConvertClass[]");
                }
            }

            //list class
            packer = new BytePacker(buffer);
            List <TestConvertClass> cListSrc = new List <TestConvertClass> {
                new TestConvertClass(), new TestConvertClass(), new TestConvertClass(), new TestConvertClass(), new TestConvertClass()
            };

            cListSrc[1].IntData = 90;
            List <TestConvertClass> cListDst = null;

            DataSerializer.Serialize(packer, cArraySrc);
            packer.Position = 0;
            cListDst        = DataSerializer.Deserialize <List <TestConvertClass> >(packer);

            if (cListSrc.Count != cListDst.Count)
            {
                throw new InvalidProgramException("List<TestConvertClass>");
            }
            for (int i = 0; i < bArraySrc.Length; i++)
            {
                if (!cArraySrc[i].Equals(cListDst[i]))
                {
                    throw new InvalidProgramException("List<TestConvertClass>");
                }
            }

            //Dictionary class
            packer = new BytePacker(buffer);
            Dictionary <string, TestConvertClass> cDictSrc = new Dictionary <string, TestConvertClass>();

            TestConvertClass tcc = new TestConvertClass();

            tcc.IntData = 90;
            cDictSrc.Add("aaaa", tcc);
            tcc         = new TestConvertClass();
            tcc.IntData = 270;
            cDictSrc.Add("bbbb", tcc);

            Dictionary <string, TestConvertClass> cDictDst = null;

            DataSerializer.Serialize(packer, cDictSrc);
            packer.Position = 0;
            cDictDst        = DataSerializer.Deserialize <Dictionary <string, TestConvertClass> >(packer);

            if (cDictSrc.Count != cDictDst.Count)
            {
                throw new InvalidProgramException("Dictionary<string, TestConvertClass>");
            }
            foreach (var pair in cDictSrc)
            {
                if (!pair.Value.Equals(cDictDst[pair.Key]))
                {
                    throw new InvalidProgramException("Dictionary<string, TestConvertClass>");
                }
            }
        }
 public TcpSocket()
 {
     packer = new BytePacker(OnPacked);
 }
示例#3
0
        //[Fact(Skip = "Skipped")]
        public void BytePackerTest()
        {
            Random random = new Random();

            byte[] buffer = new byte[64];

            BytePacker packer = new BytePacker(buffer);

            //Bool
            bool boolVal = true;

            packer.Write(boolVal);

            sbyte sbyteVal = (sbyte)random.Next(sbyte.MinValue, sbyte.MaxValue);

            packer.Write(sbyteVal);

            byte byteVal = (byte)random.Next(byte.MinValue, byte.MaxValue);

            packer.Write(byteVal);

            char charVal = (char)random.Next(char.MinValue, char.MaxValue);

            packer.Write(charVal);

            short shortVal = (short)random.Next(short.MinValue, short.MaxValue);

            packer.Write(shortVal);

            ushort ushortVal = (ushort)random.Next(ushort.MinValue, ushort.MaxValue);

            packer.Write(ushortVal);

            int intVal = random.Next();

            packer.Write(intVal);

            uint uintVal = (uint)random.Next(0, int.MaxValue) * 2;

            packer.Write(uintVal);

            long longVal = (long)random.Next() * 2;

            packer.Write(longVal);

            ulong ulongVal = (uint)random.Next(0, int.MaxValue) * 2;

            packer.Write(ulongVal);

            float floatVal = (float)random.NextDouble();

            packer.Write(floatVal);

            double doubleVal = random.NextDouble();

            packer.Write(doubleVal);

            packer.Position = 0;

            if (boolVal != packer.ReadBool())
            {
                throw new InvalidProgramException("bool");
            }
            else if (sbyteVal != packer.ReadSByte())
            {
                throw new InvalidProgramException("sbyte");
            }
            else if (byteVal != packer.ReadByte())
            {
                throw new InvalidProgramException("byte");
            }
            else if (charVal != packer.ReadChar())
            {
                throw new InvalidProgramException("char");
            }
            else if (shortVal != packer.ReadShort())
            {
                throw new InvalidProgramException("short");
            }
            else if (ushortVal != packer.ReadUShort())
            {
                throw new InvalidProgramException("ushort");
            }
            else if (intVal != packer.ReadInt())
            {
                throw new InvalidProgramException("int");
            }
            else if (uintVal != packer.ReadUInt())
            {
                throw new InvalidProgramException("uint");
            }
            else if (longVal != packer.ReadLong())
            {
                throw new InvalidProgramException("long");
            }
            else if (ulongVal != packer.ReadULong())
            {
                throw new InvalidProgramException("ulong");
            }
            else if (floatVal != packer.ReadFloat())
            {
                throw new InvalidProgramException("float");
            }
            else if (doubleVal != packer.ReadDouble())
            {
                throw new InvalidProgramException("double");
            }
        }
        public static void NpcMove(PacketHitList packetList, IMap map, int mapNpcNum, Enums.Direction direction, Enums.Speed movement)
        {
            // Check for subscript out of range
            if (mapNpcNum < 0 || mapNpcNum > Constants.MAX_MAP_NPCS || direction < Enums.Direction.Up || direction > Enums.Direction.Right || movement < Enums.Speed.Standing || movement > Enums.Speed.Running) {
                return;
            }

            //if (map.ActiveNpc[mapNpcNum].StatusAilment == Enums.StatusAilment.Paralyze && movement > Enums.Speed.Walking) {
            //    movement = Enums.Speed.Walking;
            //}

            if (map.ActiveNpc[mapNpcNum].SpeedLimit < movement) {
                movement = map.ActiveNpc[mapNpcNum].SpeedLimit;
            }

            if (movement == Enums.Speed.Standing) return;

            int dataByte = 0;
            BytePacker packer = new BytePacker();
            packer.AddItem(Constants.MAX_MAP_NPCS, mapNpcNum);
            packer.AddItem(4, (int)direction);
            packer.AddItem(7, (int)movement);
            dataByte = packer.PackItems();

            switch (direction) {
                case Enums.Direction.Up:
                    map.ActiveNpc[mapNpcNum].Y--;
                    break;
                case Enums.Direction.Down:
                    map.ActiveNpc[mapNpcNum].Y++;
                    break;
                case Enums.Direction.Left:
                    map.ActiveNpc[mapNpcNum].X--;
                    break;
                case Enums.Direction.Right:
                    map.ActiveNpc[mapNpcNum].X++;
                    break;
            }
            //packetList.AddPacketToMap(map, TcpPacket.CreatePacket("nm", dataByte));
            PacketBuilder.AppendNpcMove(map, map.ActiveNpc[mapNpcNum], packetList, dataByte);

            map.ActiveNpc[mapNpcNum].AttackTimer = new TickCount(map.ActiveNpc[mapNpcNum].AttackTimer.Tick + 1000 * DetermineSpeed(movement) / 30 + 1);

            //map.ActiveNpc[mapNpcNum].HPStepCounter++;
            //if (map.ActiveNpc[mapNpcNum].HPStepCounter >= 5) {
            //    map.ActiveNpc[mapNpcNum].HPStepCounter = 0;

            Scripting.ScriptManager.InvokeSub("OnStep", map, map.ActiveNpc[mapNpcNum], movement, packetList);

            //}
            //Npcs can step on scripted tiles (which means TRAPS)
            if (map.Tile[map.ActiveNpc[mapNpcNum].X, map.ActiveNpc[mapNpcNum].Y].Type == Enums.TileType.Scripted) {
                Scripting.ScriptManager.InvokeSub("ScriptedTile", map, map.ActiveNpc[mapNpcNum], map.Tile[map.ActiveNpc[mapNpcNum].X, map.ActiveNpc[mapNpcNum].Y].Data1, map.Tile[map.ActiveNpc[mapNpcNum].X, map.ActiveNpc[mapNpcNum].Y].String1, map.Tile[map.ActiveNpc[mapNpcNum].X, map.ActiveNpc[mapNpcNum].Y].String2, map.Tile[map.ActiveNpc[mapNpcNum].X, map.ActiveNpc[mapNpcNum].Y].String3, packetList);
                return;
            }
        }