public void BinaryData_DataAccess_ReadWrite_ULong()
        {
            // Arrange
            byte[] testData = new byte[16];
            Random rand     = new Random();

            rand.NextBytes(testData);

            BinaryData bf        = new BinaryData(testData);
            int        rAddress  = 0x00;
            int        wAddress  = 0x08;
            ulong      expRValue = GetULong(rAddress, testData);
            ulong      expWValue = (ulong)(rand.Next(0, int.MaxValue) | rand.Next(0, int.MaxValue) << 32);

            // Act
            ulong rValue = bf.Get <ulong>(rAddress);
            ulong wValue = expWValue;

            bf.Set(wAddress, wValue);

            // Assert
            byte[] fileData = bf.ToArray();
            Assert.Equal(expRValue, rValue);
            Assert.Equal(expWValue, GetULong(wAddress, fileData));
        }
        public void BinaryData_DataAccess_ReadWrite_Short()
        {
            // Arrange
            byte[] testData = new byte[16];
            Random rand     = new Random();

            rand.NextBytes(testData);

            BinaryData bf        = new BinaryData(testData);
            int        rAddress  = 0x00;
            int        wAddress  = 0x08;
            short      expRValue = GetShort(rAddress, testData);
            short      expWValue = (short)rand.Next(0, ushort.MaxValue);

            // Act
            short rValue = bf.Get <short>(rAddress);
            short wValue = expWValue;

            bf.Set(wAddress, wValue);

            // Assert
            byte[] fileData = bf.ToArray();
            Assert.Equal(expRValue, rValue);
            Assert.Equal(expWValue, GetShort(wAddress, fileData));
        }
        public void BinaryData_DataAccess_ReadWrite_Float()
        {
            // Arrange
            byte[] testData = new byte[16];
            Random rand     = new Random();

            rand.NextBytes(testData);

            BinaryData bf        = new BinaryData(testData);
            int        rAddress  = 0x00;
            int        wAddress  = 0x08;
            float      expRValue = GetFloat(rAddress, testData);
            float      expWValue = (float)rand.NextDouble();

            // Act
            float rValue = bf.Get <float>(rAddress);
            float wValue = expWValue;

            bf.Set(wAddress, wValue);

            // Assert
            byte[] fileData = bf.ToArray();
            Assert.Equal(expRValue, rValue);
            Assert.Equal(expWValue, GetFloat(wAddress, fileData));
        }
        public void BinaryData_DataAccess_ReadWrite_Char8()
        {
            // Arrange
            byte[] testData = new byte[16];
            Random rand     = new Random();

            rand.NextBytes(testData);

            BinaryData bf        = new BinaryData(testData);
            int        rAddress  = 0x00;
            int        wAddress  = 0x08;
            Char8      expRValue = GetChar8(rAddress, testData);
            Char8      expWValue = (Char8)rand.Next(0, byte.MaxValue);

            // Act
            Char8 rValue = bf.Get <Char8>(rAddress);
            Char8 wValue = expWValue;

            bf.Set(wAddress, wValue);

            // Assert
            byte[] fileData = bf.ToArray();
            Assert.Equal(expRValue, rValue);
            Assert.Equal(expWValue, GetChar8(wAddress, fileData));
        }
        public void BinaryData_DataAccess_ReadWrite_Data_LittleEndian()
        {
            // Arrange
            byte[] testData = new byte[8];
            Random rand     = new Random();

            rand.NextBytes(testData);

            BinaryData bf        = new BinaryData(testData, Endianness.Little);
            int        rAddress  = 0x00;
            int        wAddress  = 0x04;
            uint       expRValue = GetUInt(rAddress, testData);
            uint       expWValue = 0xDEADBEEF;

            // Act
            uint rValue = bf.Get <uint>(rAddress);
            uint wValue = expWValue;

            bf.Set(wAddress, wValue);

            // Assert
            byte[] fileData = bf.ToArray();
            Assert.Equal(expRValue, rValue);
            Assert.Equal(expWValue, GetUInt(wAddress, fileData));
        }
        public void BinaryData_DataAccess_ReadWrite_Bool64()
        {
            // Arrange
            Random rand = new Random();

            byte[] testData = new byte[16];
            for (int i = 0; i < testData.Length; i++)
            {
                testData[i] = (byte)0x00;
            }

            BinaryData bf        = new BinaryData(testData);
            int        rAddress  = 0x00;
            int        wAddress  = 0x08;
            Bool64     expRValue = false;
            Bool64     expWValue = rand.Next(0, 2) != 0;

            // Act
            Bool64 rValue = bf.Get <Bool64>(rAddress);
            Bool64 wValue = expWValue;

            bf.Set(wAddress, wValue);

            // Assert
            byte[] fileData = bf.ToArray();
            Assert.Equal(expRValue, rValue);
            Assert.Equal(expWValue, GetBool64(wAddress, fileData));
        }
예제 #7
0
        public void BinaryData_Bounds_ReadWrite_WordArray_AcrossEnd()
        {
            BinaryData bf    = new BinaryData(16);
            int        count = 2;

            Assert.Throws <ArgumentOutOfRangeException>(() => bf.Get <uint>(bf.Length - sizeof(uint), count));
            Assert.Throws <ArgumentOutOfRangeException>(() => bf.Set <uint>(bf.Length, new uint[count]));
        }
예제 #8
0
        public void BinaryData_Bounds_ReadWrite_Word_AtEnd()
        {
            BinaryData bf = new BinaryData(16);

            // Read/write last valid address in file
            bf.Get <uint>(bf.Length - sizeof(uint));
            bf.Set <uint>(bf.Length - sizeof(uint), 0xDEADBEEF);
        }
예제 #9
0
        public void BinaryData_Bounds_ReadWrite_Byte_AtEnd()
        {
            BinaryData bf = new BinaryData(16);

            // Read/write last valid address in file
            bf.Get <byte>(bf.Length - sizeof(byte));
            bf.Set <byte>(bf.Length - sizeof(byte), 0xFF);
        }
예제 #10
0
        public void BinaryData_DataAccess_ReadWrite_DataArray()
        {
            // Arrange
            byte[] testData = new byte[64];
            Random rand     = new Random();

            rand.NextBytes(testData);

            BinaryData bf       = new BinaryData(testData);
            int        rAddress = 0x00;
            int        wAddress = 0x20;

            uint[] expRValue = new uint[8];
            for (int i = 0; i < 8; i++)
            {
                expRValue[i] = GetUInt(rAddress + i * sizeof(uint), testData);
            }
            uint[] expWValue = new uint[8];
            for (int i = 0; i < 8; i++)
            {
                expWValue[i] = (uint)rand.Next(0, int.MaxValue);
            }

            // Act
            uint[] rValue = bf.Get <uint>(rAddress, 8);
            uint[] wValue = new uint[8];
            Array.Copy(expWValue, wValue, expWValue.Length);
            bf.Set(wAddress, wValue);

            // Assert
            byte[] fileData = bf.ToArray();
            Assert.Equal(expRValue, rValue);
            for (int i = 0; i < 8; i++)
            {
                Assert.Equal(expWValue[i], GetUInt(wAddress + i * sizeof(uint), fileData));
            }
        }
예제 #11
0
파일: Program.cs 프로젝트: whampson/cascara
        static void Deserialize()
        {
            // PlayerData:
            //  0000    short       Health              1000
            //  0002    short       Armor               767
            //  0004    int         Score               564353
            //  -       Weapon[4]   Weapons
            //  0008    uint        Weapons[0].Id       0
            //  000C    uint        Weapons[0].Ammo     16116
            //  0010    uint        Weapons[1].Id       1
            //  0014    uint        Weapons[1].Ammo     85
            //  0018    uint        Weapons[2].Id       2
            //  001C    uint        Weapons[2].Ammo     77172
            //  0020    uint        Weapons[3].Id       3
            //  0024    uint        Weapons[3].Ammo     12378
            //  0028    long        Seed                547546237654745238
            //  -       Statistics  Stats
            //  0030    uint        Stats.NumKills      984
            //  0034    uint        Stats.NumDeaths     1
            //  0038    int         Stats.HighScore     564353
            //  003C    bool        Stats.FoundTreasure True
            //  003D    byte[3]     (align)
            //  0040    Char8[32]   Name                "Tiberius"
            byte[] data = new byte[]
            {
                0xE8, 0x03, 0xFF, 0x02, 0x81, 0x9C, 0x08, 0x00,     // Health, Armor, Score
                0x00, 0x00, 0x00, 0x00, 0xF4, 0x3E, 0x00, 0x00,     // Weapon[0]
                0x01, 0x00, 0x00, 0x00, 0x55, 0x00, 0x00, 0x00,     // Weapon[1]
                0x02, 0x00, 0x00, 0x00, 0x74, 0x2D, 0x01, 0x00,     // Weapon[2]
                0x03, 0x00, 0x00, 0x00, 0x5A, 0x30, 0x00, 0x00,     // Weapon[3]
                0x96, 0x40, 0x83, 0xF1, 0x66, 0x46, 0x99, 0x07,     // Seed
                0xD8, 0x03, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,     // Stats
                0x81, 0x9C, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00,
                0x54, 0x69, 0x62, 0x65, 0x72, 0x69, 0x75, 0x73,     // Name
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
            };

            string src = @"
                <layoutScript>
                    <short name='Health'/>
                    <short name='Armor'/>
                    <int name='Score'/>
                    <struct name='Weapons' count='4'>
                        <uint name='Id'/>
                        <uint name='Ammo'/>
                    </struct>
                    <long name='Seed'/>
                    <struct name='Stats'>
                        <uint name='NumKills'/>
                        <uint name='NumDeaths'/>
                        <int name='HighScore'/>
                        <bool name='FoundTreasure'/>
                        <byte count='3'/>
                    </struct>
                    <char name='Name' count='32'/>
                </layoutScript>
            ";

            BinaryData b = new BinaryData(data);

            b.RunLayoutScript(LayoutScript.Parse(src));
            DeserializationFlags flags = DeserializationFlags.Fields
                                         | DeserializationFlags.IgnoreCase
                                         | DeserializationFlags.NonPublic;
            PlayerData3 p = b.Deserialize <PlayerData3>(flags);

            Console.WriteLine(b.Get <int>(0x38));
            Console.WriteLine(p.Stats.HighScore);
            p.Stats.HighScore = 1234;
            Console.WriteLine(p.Stats.HighScore);
            Console.WriteLine(b.Get <int>(0x38));
            p.Name = "The Quick Brown Fox jumps over the Lazy Dog";
            Console.WriteLine(p.Name);
        }