示例#1
0
        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);
        }