Exemplo n.º 1
0
        public SirenField FindField(String name)
        {
            SirenField outProperty;

            FieldNameDict.TryGetValue(name, out outProperty);
            return(outProperty);
        }
Exemplo n.º 2
0
        public override bool LoadFrom(Stream stream)
        {
            base.LoadFrom(stream);
            Attribute.LoadFrom(stream);
            BaseTypeName = stream.ReadString();

            //types
            uint typeCount = stream.ReadUInt();

            for (int i = 0; i < typeCount; i++)
            {
                byte isClass = (byte)stream.ReadByte();
                if (isClass == 1)
                {
                    SirenCustomClass type = new SirenCustomClass();
                    type.LoadFrom(stream);
                    type.Parent = this;
                    Types.Add(type.Name, type);
                }
                else
                {
                    SirenCustomEnum type = new SirenCustomEnum();
                    type.LoadFrom(stream);
                    type.Parent = this;
                    Types.Add(type.Name, type);
                }
            }
            //fields
            uint fieldCount = stream.ReadUInt();

            for (int i = 0; i < fieldCount; i++)
            {
                SirenField field = new SirenField();
                field.LoadFrom(stream);
                field.ParentType = this;
                FieldNameDict.Add(field.Name, field);
                field.Index = (ushort)(FieldNameDict.Count - 1);
            }

            return(true);
        }
Exemplo n.º 3
0
        public bool Initialize()
        {
            if (Type.IsEnum)
            {
                return(true);
            }

            //get methods
            SerializeMethodInfo   = Type.GetMethod("Serialize", BindingFlags.Public | BindingFlags.Instance);
            DeserializeMethodInfo = Type.GetMethod("Deserialize", BindingFlags.Public | BindingFlags.Instance);
            RegisterMethodInfo    = Type.GetMethod("Register", BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
            if (RegisterMethodInfo != null && !Type.ContainsGenericParameters)
            {
                RegisterMethodInfo.Invoke(null, null);
            }


            //get properties
            uint   index      = 0;
            ushort id         = GetBasePropertyCount();
            var    properties = Type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);

            foreach (var propertyInfo in properties)
            {
                var attrs = propertyInfo.GetCustomAttributes(typeof(SirenFieldAttribute), false);
                if (attrs.Length > 0)
                {
                    SirenFieldAttribute propertyAttribute = attrs[0] as SirenFieldAttribute;
                    if (propertyAttribute != null)
                    {
                        SirenField property = new SirenField(this, propertyInfo, propertyAttribute, index++, id++);
                        Fields.Add(property);
                        FieldIdDict.Add(property.Id, property);
                        FieldNameDict.Add(property.Name, property);
                    }
                }
            }

            if (Fields.Count == 0)
            {
                if (Type.BaseType == typeof(object) || Type.BaseType.IsValueType)
                {
                    return(false);
                }
            }

            if (!Type.IsValueType && Type.BaseType != typeof(object) && !Type.BaseType.IsValueType)
            {
                BaseType = SirenMachine.GetClass(Type.BaseType);
                if (BaseType != null)
                {
                    //add base properties
                    foreach (var sirenProperty in BaseType.FieldIdDict)
                    {
                        FieldIdDict.Add(sirenProperty.Key, sirenProperty.Value);
                    }

                    foreach (var sirenProperty in BaseType.FieldNameDict)
                    {
                        FieldNameDict.Add(sirenProperty.Key, sirenProperty.Value);
                    }
                }
            }


            return(true);
        }