예제 #1
0
        private object DeserializeToFields(Type t, object o, DeserializationFlags flags, BindingFlags bindFlags)
        {
            // TODO: there /has/ to be some way to generalize this process
            //       for both fields and properties...

            FieldInfo[] allFields;
            Type        fieldType;
            bool        isArray;
            Type        elemType;
            bool        isGeneric;
            Type        genType;
            object      val;

            if (!flags.HasFlag(DeserializationFlags.Fields))
            {
                return(o);
            }

            allFields = t.GetFields(bindFlags);

            foreach (FieldInfo f in allFields)
            {
                fieldType = f.FieldType;
                isArray   = fieldType.IsArray;
                elemType  = fieldType.GetElementType();
                isGeneric = fieldType.IsGenericType;

                if (isGeneric && fieldType.GetGenericTypeDefinition() == typeof(Primitive <>))
                {
                    genType = fieldType.GetGenericArguments()[0];
                    val     = DeserializePrimitive(f.Name, genType, o, flags);
                }
                else if (isArray)
                {
                    if (PrimitiveTypeUtils.IsPrimitiveType(elemType))
                    {
                        val = DeserializeValueArray(f.Name, elemType, o, flags);
                    }
                    else
                    {
                        val = DeserializeStructureArray(f.Name, elemType, o, flags);
                    }
                }
                else if (PrimitiveTypeUtils.IsPrimitiveType(fieldType))
                {
                    val = DeserializeValue(f.Name, fieldType, o, flags);
                }
                else
                {
                    val = DeserializeStructure(f.Name, fieldType, o, flags);
                }

                if (val != null)
                {
                    f.SetValue(o, val);
                }
            }

            return(o);
        }
예제 #2
0
        private object DeserializeValueArray(string name, Type t, object o, DeserializationFlags flags)
        {
            bool   ignoreCase = flags.HasFlag(DeserializationFlags.IgnoreCase);
            object prim       = GetPrimitive(t, name, ignoreCase);

            if (prim == null)
            {
                return(null);
            }

            PropertyInfo elemCountProp = prim.GetType().GetProperty(nameof(Primitive <byte> .ElementCount));
            PropertyInfo valueProp     = prim.GetType().GetProperty(nameof(Primitive <byte> .Value));
            MethodInfo   indexerMeth   = prim.GetType().GetMethod("get_Item"); // 'this[int i]' property

            int   elemCount = (int)elemCountProp.GetValue(prim, null);
            Array arr       = Array.CreateInstance(t, elemCount);

            for (int i = 0; i < elemCount; i++)
            {
                object elem = indexerMeth.Invoke(prim, new object[] { i });
                object val  = valueProp.GetValue(elem);
                arr.SetValue(val, i);
            }

            return(arr);
        }
예제 #3
0
        /// <summary>
        /// Deserializes an object of the specified type.
        /// </summary>
        /// <param name="reader">The <see cref="EventReader" /> where to deserialize the object.</param>
        /// <param name="type">The static type of the object to deserialize.</param>
        /// <param name="options">Options that control how the deserialization is to be performed.</param>
        /// <returns>Returns the deserialized object.</returns>
        public object Deserialize(EventReader reader, Type type, DeserializationFlags options = DeserializationFlags.None)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            var hasStreamStart = reader.Allow <StreamStart>() != null;

            var hasDocumentStart = reader.Allow <DocumentStart>() != null;

            object result = DeserializeValue(reader, type, null);

            if (hasDocumentStart)
            {
                reader.Expect <DocumentEnd>();
            }

            if (hasStreamStart)
            {
                reader.Expect <StreamEnd>();
            }

            return(result);
        }
예제 #4
0
        /// <summary>
        /// Converts the data in this <see cref="Structure"/> into an object
        /// by setting properties or fields using the names specified in a
        /// <see cref="LayoutScript"/>.
        /// <see cref="DeserializationFlags"/> for this method are set to
        /// <see cref="DeserializationFlags.Public"/> and
        /// <see cref="DeserializationFlags.Properties"/>.
        /// </summary>
        public T Deserialize <T>() where T : new()
        {
            DeserializationFlags flags =
                DeserializationFlags.Public | DeserializationFlags.Properties;

            return(Deserialize <T>(flags));
        }
예제 #5
0
        private object DeserializePrimitive(string name, Type t, object o, DeserializationFlags flags)
        {
            bool   ignoreCase = flags.HasFlag(DeserializationFlags.IgnoreCase);
            object prim       = GetPrimitive(t, name, ignoreCase);

            return(prim);
        }
예제 #6
0
        private object DeserializeStructure(string name, Type t, object o, DeserializationFlags flags)
        {
            bool      ignoreCase = flags.HasFlag(DeserializationFlags.IgnoreCase);
            Structure s          = GetStructure(name, ignoreCase);

            if (s == null)
            {
                return(null);
            }

            return(s.Deserialize(t, flags));
        }
예제 #7
0
        private object DeserializeValue(string name, Type t, object o, DeserializationFlags flags)
        {
            bool   ignoreCase = flags.HasFlag(DeserializationFlags.IgnoreCase);
            object prim       = GetPrimitive(t, name, ignoreCase);

            if (prim == null)
            {
                return(null);
            }

            PropertyInfo valueProp = prim.GetType().GetProperty(nameof(Primitive <byte> .Value));

            return(valueProp.GetValue(prim, null));
        }
예제 #8
0
        private object DeserializeStructureArray(string name, Type t, object o, DeserializationFlags flags)
        {
            bool      ignoreCase = flags.HasFlag(DeserializationFlags.IgnoreCase);
            Structure s          = GetStructure(name, ignoreCase);

            if (s == null)
            {
                return(null);
            }

            Array arr = Array.CreateInstance(t, s.ElementCount);

            for (int i = 0; i < s.ElementCount; i++)
            {
                object elem = s[i].Deserialize(t, flags);
                arr.SetValue(elem, i);
            }

            return(arr);
        }
예제 #9
0
        private object Deserialize(Type t, DeserializationFlags flags)
        {
            object       o;
            BindingFlags bindFlags;

            o = Activator.CreateInstance(t);

            bindFlags = BindingFlags.Instance;
            if (flags.HasFlag(DeserializationFlags.NonPublic))
            {
                bindFlags |= BindingFlags.NonPublic;
            }
            if (flags.HasFlag(DeserializationFlags.Public))
            {
                bindFlags |= BindingFlags.Public;
            }

            o = DeserializeToProperties(t, o, flags, bindFlags);
            o = DeserializeToFields(t, o, flags, bindFlags);

            return(o);
        }
예제 #10
0
 public override EventData DeserializeEventData(StreamBuffer din, EventData target = null, DeserializationFlags flags = DeserializationFlags.None /* Metadata: 0x0064D696 */) => default;                          // 0x0000000180A05070-0x0000000180A05150
 private Dictionary <byte, object> ReadParameterTable(StreamBuffer stream, Dictionary <byte, object> target = null, DeserializationFlags flags = DeserializationFlags.None /* Metadata: 0x0064D69A */) => default; // 0x0000000180A08250-0x0000000180A08360
예제 #11
0
 public object ReadCustomType(StreamBuffer stream, byte gpType = 0 /* Metadata: 0x0064D695 */) => default;                                                                                                         // 0x0000000180A06C60-0x0000000180A06E30
 public override EventData DeserializeEventData(StreamBuffer din, EventData target = null, DeserializationFlags flags = DeserializationFlags.None /* Metadata: 0x0064D696 */) => default;                          // 0x0000000180A05070-0x0000000180A05150
예제 #12
0
        private object DeserializeToProperties(Type t, object o, DeserializationFlags flags, BindingFlags bindFlags)
        {
            PropertyInfo[] allProperties;
            Type           propType;
            bool           isArray;
            Type           elemType;
            bool           isGeneric;
            Type           genType;
            object         val;

            if (!flags.HasFlag(DeserializationFlags.Properties))
            {
                return(o);
            }

            allProperties = t.GetProperties(bindFlags);

            foreach (PropertyInfo p in allProperties)
            {
                propType  = p.PropertyType;
                isArray   = propType.IsArray;
                elemType  = propType.GetElementType();
                isGeneric = propType.IsGenericType;

                if (p.GetSetMethod() == null)
                {
                    continue;
                }

                if (isGeneric && propType.GetGenericTypeDefinition() == typeof(Primitive <>))
                {
                    genType = propType.GetGenericArguments()[0];
                    val     = DeserializePrimitive(p.Name, genType, o, flags);
                }
                else if (isArray)
                {
                    if (PrimitiveTypeUtils.IsPrimitiveType(elemType))
                    {
                        val = DeserializeValueArray(p.Name, elemType, o, flags);
                    }
                    else
                    {
                        val = DeserializeStructureArray(p.Name, elemType, o, flags);
                    }
                }
                else if (PrimitiveTypeUtils.IsPrimitiveType(propType))
                {
                    val = DeserializeValue(p.Name, propType, o, flags);
                }
                else
                {
                    val = DeserializeStructure(p.Name, propType, o, flags);
                }

                if (val != null)
                {
                    p.SetValue(o, val);
                }
            }

            return(o);
        }
예제 #13
0
 /// <summary>
 /// Converts the data in this <see cref="BinaryData"/> into an object
 /// by setting properties or fields using the names specified in a
 /// <see cref="LayoutScript"/>.
 /// </summary>
 /// <param name="flags">
 /// A <see cref="DeserializationFlags"/> bitfield that controls the
 /// behavior of deserialization.
 /// </param>
 public T Deserialize <T>(DeserializationFlags flags) where T : new()
 {
     return((T)Deserialize(typeof(T), flags));
 }
예제 #14
0
 /// <summary>
 /// Converts the data in this <see cref="BinaryData"/> into an object
 /// by setting properties or fields using the names specified in a
 /// <see cref="LayoutScript"/>. The <see cref="RunLayoutScript(LayoutScript)"/>
 /// method must have been called prior for this to work properly.
 /// </summary>
 /// <param name="flags">
 /// A <see cref="DeserializationFlags"/> bitfield that controls the
 /// behavior of deserialization.
 /// </param>
 public T Deserialize <T>(DeserializationFlags flags) where T : new()
 {
     return(fileStructure.Deserialize <T>(flags));
 }
예제 #15
0
        /// <summary>
        /// Deserializes an object of the specified type.
        /// </summary>
        /// <param name="reader">The <see cref="EventReader" /> where to deserialize the object.</param>
        /// <param name="type">The static type of the object to deserialize.</param>
        /// <param name="options">Options that control how the deserialization is to be performed.</param>
        /// <returns>Returns the deserialized object.</returns>
        public object Deserialize(EventReader reader, Type type, DeserializationFlags options = DeserializationFlags.None)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            var hasStreamStart = reader.Allow<StreamStart>() != null;

            var hasDocumentStart = reader.Allow<DocumentStart>() != null;

            object result = DeserializeValue(reader, type, null);

            if (hasDocumentStart)
            {
                reader.Expect<DocumentEnd>();
            }

            if (hasStreamStart)
            {
                reader.Expect<StreamEnd>();
            }

            return result;
        }
예제 #16
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);
        }
예제 #17
0
 public abstract EventData DeserializeEventData(StreamBuffer din, EventData target = null, DeserializationFlags flags = DeserializationFlags.None /* Metadata: 0x0064D5FA */);