Exemplo n.º 1
0
        public void SerializeHelper(object obj, SirenClass sirenClass)
        {
            Type type = obj.GetType();

            if (sirenClass == null)
            {
                if (type.IsValueType)
                {
                    Writer.OnValue(obj);
                }
                else if (type == typeof(string))
                {
                    Writer.OnString(obj as string);
                }
                else if (type == typeof(byte[]))
                {
                    Writer.OnMemoryData(obj as byte[]);
                }
                else
                {
                    Console.WriteLine("Unknown type:{0}", type);
                    Writer.OnError();
                }
            }
            else if (sirenClass.SerializeMethodInfo != null)
            {
                sirenClass.SerializeMethodInfo.Invoke(obj, new object[] { Writer });
            }
            else
            {
                SerializeStruct(obj, sirenClass);
            }
        }
Exemplo n.º 2
0
 public void DeserializeHelper(Type type, ref object obj, SirenClass sirenClass)
 {
     if (sirenClass == null)
     {
         if (type.IsValueType)
         {
             obj = Reader.OnValue(type);
         }
         else if (type == typeof(string))
         {
             obj = Reader.OnString();
         }
         else if (type == typeof(byte[]))
         {
             obj = Reader.OnMemoryData();
         }
         else
         {
             Console.WriteLine("Unknown type:{0}", type);
             Reader.OnError();
         }
     }
     else if (sirenClass.DeserializeMethodInfo != null)
     {
         sirenClass.DeserializeMethodInfo.Invoke(obj, new object[] { Reader });
     }
     else
     {
         DeserializeStruct(ref obj, sirenClass);
     }
 }
Exemplo n.º 3
0
        public static SirenClass FindClass(Type type)
        {
            if (type.IsPrimitive || type == typeof(string) || type == typeof(byte[]))
            {
                return(null);
            }

            SirenClass sirenClass;

            if (AllSirenClasses.TryGetValue(type, out sirenClass))
            {
                return(sirenClass);
            }

            if (GenerictSirenClasses.TryGetValue(type, out sirenClass))
            {
                return(sirenClass);
            }


            if (type.IsGenericType)
            {
                Type testType = null;
                foreach (var temp in AllSirenClasses)
                {
                    var oldType = temp.Key;
                    if (oldType.IsGenericTypeDefinition)
                    {
                        testType = oldType.MakeGenericType(type.GenericTypeArguments);
                        if (testType == type)
                        {
                            var attr2 = type.GetCustomAttribute <SirenClassAttribute>();
                            if (attr2 == null)
                            {
                                return(null);
                            }


                            sirenClass = new SirenClass(type, attr2);
                            sirenClass.Initialzie();
                            break;
                        }
                        testType = null;
                    }
                }

                if (testType != null)
                {
                    GenerictSirenClasses.Add(testType, sirenClass);
                }
            }

            return(sirenClass);
        }
Exemplo n.º 4
0
        public void SerializeBase(object obj, SirenClass sirenClass)
        {
            if (sirenClass.BaseSirenClass != null)
            {
                SerializeBase(obj, sirenClass.BaseSirenClass);
            }

            foreach (var sirenProperty in sirenClass.Properties)
            {
                if (sirenProperty.Attribute.Modifier == SirenFieldModifier.Required)
                {
                    if (!sirenProperty.HasValue(obj))
                    {
                        Console.WriteLine("Forget to set {0}.{1}", sirenClass.Name, sirenProperty.Name);
                        Writer.OnError();
                        return;
                    }
                }
                else
                {
                    if (!sirenProperty.HasValue(obj))
                    {
                        //skip
                        return;
                    }
                }

                Writer.OnPropertyBegin(sirenProperty.Name, sirenProperty.Id, sirenProperty.DataType);

                switch (sirenProperty.FieldType)
                {
                case SirenPropertyFieldType.Value:
                    Writer.OnValue(sirenProperty.Info.GetValue(obj));
                    break;

                case SirenPropertyFieldType.String:
                    Writer.OnString(sirenProperty.Info.GetValue(obj) as string);
                    break;

                case SirenPropertyFieldType.Blob:
                    Writer.OnMemoryData(sirenProperty.Info.GetValue(obj) as byte[]);
                    break;

                case SirenPropertyFieldType.List:
                {
                    object items    = sirenProperty.Info.GetValue(obj);
                    var    listType = sirenProperty.Type;
                    int    count    = Convert.ToInt32(listType.GetProperty("Count").GetValue(items));

                    var            itemType     = sirenProperty.Type.GenericTypeArguments[0];
                    SirenFieldType itemDataType = SirenFactory.GetPropertyType(itemType);
                    Writer.OnListBegin(itemDataType, count);
                    var itemsProperty = listType.GetProperty("Item");
                    for (int i = 0; i < count; i++)
                    {
                        object listItem = itemsProperty.GetValue(items, new object[] { i });
                        SerializeHelper(listItem, sirenProperty.ValueSirenClass);
                    }
                    Writer.OnListEnd();
                }

                break;

                case SirenPropertyFieldType.Dictionary:
                {
                    object items    = sirenProperty.Info.GetValue(obj);
                    var    dictType = sirenProperty.Type;
                    int    count    = Convert.ToInt32(dictType.GetProperty("Count").GetValue(items));

                    var keyType   = sirenProperty.Type.GenericTypeArguments[0];
                    var valueType = sirenProperty.Type.GenericTypeArguments[1];

                    SirenFieldType keyDataType   = SirenFactory.GetPropertyType(keyType);
                    SirenFieldType valueDataType = SirenFactory.GetPropertyType(valueType);


                    Writer.OnDictionaryBegin(keyDataType, valueDataType, count);

                    foreach (var o in (items as IEnumerable))
                    {
                        var itemKey = o.GetType().GetProperty("Key").GetValue(o, null);
                        SerializeHelper(itemKey, sirenProperty.KeySirenClass);
                        var itemValue = o.GetType().GetProperty("Value").GetValue(o, null);
                        SerializeHelper(itemValue, sirenProperty.ValueSirenClass);
                    }

                    Writer.OnDictionaryEnd();
                }
                break;

                case SirenPropertyFieldType.Pointer:
                case SirenPropertyFieldType.Struct:
                {
                    object val = sirenProperty.Info.GetValue(obj);
                    SerializeHelper(val, sirenProperty.ValueSirenClass);
                }

                break;
                }

                Writer.OnPropertyEnd();
            }
        }
Exemplo n.º 5
0
 public void SerializeStruct(object obj, SirenClass sirenClass)
 {
     Writer.OnStructBegin();
     SerializeBase(obj, sirenClass);
     Writer.OnStructEnd();
 }
Exemplo n.º 6
0
        static SirenFactory()
        {
            //register types
            var allAssemblies = GetAllAssemblies();

            foreach (var assemblyName in allAssemblies)
            {
                var assembly = Assembly.Load(assemblyName.Value);
                var types    = assembly.GetTypes();
                foreach (var type in types)
                {
                    var attr2 = type.GetCustomAttribute <SirenClassAttribute>();
                    if (attr2 == null)
                    {
                        continue;
                    }


                    SirenClass sirenClass = new SirenClass(type, attr2);
                    AllSirenClasses.Add(type, sirenClass);
                }
            }

            foreach (var sirenClass in AllSirenClasses)
            {
                sirenClass.Value.Initialzie();
            }


            //only generate current assembly types
            var assemblies = AppDomain.CurrentDomain.GetAssemblies();

            foreach (var assembly in assemblies)
            {
                var attr = assembly.GetCustomAttribute <SirenAssemblyAttribute>();
                if (attr == null)
                {
                    continue;
                }
                if (assembly != Assembly.GetEntryAssembly())
                {
                    continue;
                }

                var types = assembly.GetTypes();
                foreach (var type in types)
                {
                    var attr2 = type.GetCustomAttribute <SirenClassAttribute>();
                    if (attr2 == null)
                    {
                        continue;
                    }


                    SirenClass sirenClass = new SirenClass(type, attr2);
                    SirenClasses.Add(type, sirenClass);
                }
            }

            List <SirenClass> invalidTypes = new List <SirenClass>();

            foreach (var sirenClass in SirenClasses)
            {
                if (!sirenClass.Value.Initialzie())
                {
                    invalidTypes.Add(sirenClass.Value);
                }
            }

            foreach (var invalidType in invalidTypes)
            {
                SirenClasses.Remove(invalidType.Type);
            }

            foreach (var generictSirenClass in GenerictSirenClasses)
            {
                SirenClasses.Add(generictSirenClass.Key, generictSirenClass.Value);
            }
            GenerictSirenClasses.Clear();
        }
Exemplo n.º 7
0
        public SirenProperty(SirenClass parentSirenClass, PropertyInfo info, SirenFieldAttribute attribute, uint index, ushort id)
        {
            ParentSirenClass = parentSirenClass;
            Info = info;
            Id = id;
            Attribute = attribute;
            Index = index;
            FieldType = SirenPropertyFieldType.Struct;
            DataType = SirenFactory.GetPropertyType(Type);
            MethodType = SirenPropertyMethodType.Unsupported;

            if (Type.IsGenericType)
            {
                if (Type.Name.StartsWith("List"))
                {
                    MethodType = SirenPropertyMethodType.List;
                    FieldType = SirenPropertyFieldType.List;

                    ValueType = Type.GenericTypeArguments[0];
                    ValueSirenClass = SirenFactory.FindClass(ValueType);

                }
                else if (Type.Name.StartsWith("Dictionary"))
                {
                    MethodType = SirenPropertyMethodType.Dictionary;
                    FieldType = SirenPropertyFieldType.Dictionary;

                    KeyType = Type.GenericTypeArguments[0];
                    ValueType = Type.GenericTypeArguments[1];
                    KeySirenClass = SirenFactory.FindClass(KeyType);
                    ValueSirenClass = SirenFactory.FindClass(ValueType);
                }
                else
                {
                    ValueSirenClass = SirenFactory.FindClass(Type);

                    if (Attribute.Modifier == SirenFieldModifier.Required)
                    {
                        MethodType = SirenPropertyMethodType.Value;
                        FieldType = SirenPropertyFieldType.Struct;
                    }
                    else
                    {
                        MethodType = SirenPropertyMethodType.Pointer;
                        FieldType = SirenPropertyFieldType.Pointer;
                    }

                }

                DefaultValueString = String.Empty;
            }
            else
            {
                if (Type.IsValueType || Type == typeof(string) || Type == typeof(byte[]))
                {
                    MethodType = SirenPropertyMethodType.Value;

                    if (Type == typeof(string))
                    {
                        FieldType = SirenPropertyFieldType.String;

                    }
                    else if (Type == typeof(byte[]))
                    {
                        FieldType = SirenPropertyFieldType.Blob;

                    }
                    else
                    {
                        FieldType = SirenPropertyFieldType.Value;
                    }
                }
                else
                {
                    if (Attribute.Modifier == SirenFieldModifier.Required)
                    {
                        MethodType = SirenPropertyMethodType.Value;
                        FieldType = SirenPropertyFieldType.Struct;
                    }
                    else
                    {
                        MethodType = SirenPropertyMethodType.Pointer;
                        FieldType = SirenPropertyFieldType.Pointer;
                    }

                    ValueSirenClass = SirenFactory.FindClass(Type);
                }

                DefaultValueString = String.Empty;
                if (Type.IsValueType)
                {

                    if (Attribute.DefaultValue != null)
                    {
                        if (Type.IsEnum)
                        {
                            DefaultValueString = String.Format("({0}){1}",Type.Name, Convert.ToUInt32(Attribute.DefaultValue));

                        }
                        else
                        {
                            DefaultValueString = Attribute.DefaultValue.ToString();
                        }
                    }
                    else
                    {
                        if (Type == typeof(bool))
                        {
                            DefaultValueString = "false";
                        }
                        else if (Type == typeof(float))
                        {
                            DefaultValueString = "0.f";
                        }
                        else if (Type == typeof(double))
                        {
                            DefaultValueString = "0.0";
                        }
                        else
                        {
                            if (Type.IsEnum)
                            {
                                DefaultValueString = String.Format("({0})0", Type.Name);
                            }
                            else
                            {
                                DefaultValueString = "0";
                            }
                        }
                    }

                }
            }
        }
Exemplo n.º 8
0
        public SirenProperty(SirenClass parentSirenClass, PropertyInfo info, SirenPropertyAttribute attribute, uint index, ushort id)
        {
            ParentSirenClass = parentSirenClass;
            Info             = info;
            Id         = id;
            Attribute  = attribute;
            Index      = index;
            FieldType  = SirenPropertyFieldType.Struct;
            DataType   = SirenFactory.GetDataType(Type);
            MethodType = SirenPropertyMethodType.Unsupported;


            if (Type.IsGenericType)
            {
                if (Type.Name.StartsWith("List"))
                {
                    MethodType = SirenPropertyMethodType.List;
                    FieldType  = SirenPropertyFieldType.List;

                    ValueType       = Type.GenericTypeArguments[0];
                    ValueSirenClass = SirenFactory.FindClass(ValueType);
                }
                else if (Type.Name.StartsWith("Dictionary"))
                {
                    MethodType = SirenPropertyMethodType.Dictionary;
                    FieldType  = SirenPropertyFieldType.Dictionary;

                    KeyType         = Type.GenericTypeArguments[0];
                    ValueType       = Type.GenericTypeArguments[1];
                    KeySirenClass   = SirenFactory.FindClass(KeyType);
                    ValueSirenClass = SirenFactory.FindClass(ValueType);
                }
                else
                {
                    ValueSirenClass = SirenFactory.FindClass(Type);

                    if (Attribute.Modifier == SirenPropertyModifier.Required)
                    {
                        MethodType = SirenPropertyMethodType.Value;
                        FieldType  = SirenPropertyFieldType.Struct;
                    }
                    else
                    {
                        MethodType = SirenPropertyMethodType.Pointer;
                        FieldType  = SirenPropertyFieldType.Pointer;
                    }
                }

                DefaultValueString = String.Empty;
            }
            else
            {
                if (Type.IsValueType || Type == typeof(string) || Type == typeof(byte[]))
                {
                    MethodType = SirenPropertyMethodType.Value;

                    if (Type == typeof(string))
                    {
                        FieldType = SirenPropertyFieldType.String;
                    }
                    else if (Type == typeof(byte[]))
                    {
                        FieldType = SirenPropertyFieldType.Blob;
                    }
                    else
                    {
                        FieldType = SirenPropertyFieldType.Value;
                    }
                }
                else
                {
                    if (Attribute.Modifier == SirenPropertyModifier.Required)
                    {
                        MethodType = SirenPropertyMethodType.Value;
                        FieldType  = SirenPropertyFieldType.Struct;
                    }
                    else
                    {
                        MethodType = SirenPropertyMethodType.Pointer;
                        FieldType  = SirenPropertyFieldType.Pointer;
                    }

                    ValueSirenClass = SirenFactory.FindClass(Type);
                }

                DefaultValueString = String.Empty;
                if (Type.IsValueType)
                {
                    if (Attribute.DefaultValue != null)
                    {
                        if (Type.IsEnum)
                        {
                            DefaultValueString = String.Format("({0}){1}", Type.Name, Convert.ToUInt32(Attribute.DefaultValue));
                        }
                        else
                        {
                            DefaultValueString = Attribute.DefaultValue.ToString();
                        }
                    }
                    else
                    {
                        if (Type == typeof(bool))
                        {
                            DefaultValueString = "false";
                        }
                        else if (Type == typeof(float))
                        {
                            DefaultValueString = "0.f";
                        }
                        else if (Type == typeof(double))
                        {
                            DefaultValueString = "0.0";
                        }
                        else
                        {
                            if (Type.IsEnum)
                            {
                                DefaultValueString = String.Format("({0})0", Type.Name);
                            }
                            else
                            {
                                DefaultValueString = "0";
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 9
0
 public void DeserializeStruct(ref object obj, SirenClass sirenClass)
 {
     Reader.OnStructBegin();
     DeserializeBase(ref obj, sirenClass);
     Reader.OnStructEnd();
 }
Exemplo n.º 10
0
        public void DeserializeBase(ref object obj, SirenClass sirenClass)
        {
            if (sirenClass.BaseSirenClass != null)
            {
                DeserializeBase(ref obj, sirenClass.BaseSirenClass);
            }

            foreach (var sirenProperty in sirenClass.Properties)
            {
                while (true)
                {
                    if (Reader.IsEnd())
                    {
                        Console.WriteLine("Reach end of file.Cannot find property {0}.{1}", sirenClass.Name, sirenProperty.Name);
                        Reader.OnError();
                        return;
                    }

                    ushort         outId;
                    SirenFieldType outDataType;
                    int            result = Reader.OnPropertyBegin(sirenProperty.Name, sirenProperty.Id, sirenProperty.DataType, out outId, out outDataType);
                    if (result == 0)
                    {
                        object val = null;

                        switch (sirenProperty.FieldType)
                        {
                        case SirenPropertyFieldType.Value:
                        case SirenPropertyFieldType.String:
                        case SirenPropertyFieldType.Blob:
                            DeserializeHelper(sirenProperty.Type, ref val, sirenProperty.ValueSirenClass);
                            break;

                        case SirenPropertyFieldType.Struct:
                        case SirenPropertyFieldType.Pointer:
                            val = SirenFactory.Create(sirenProperty.Type);
                            DeserializeHelper(sirenProperty.Type, ref val, sirenProperty.ValueSirenClass);
                            break;

                        case SirenPropertyFieldType.List:
                        {
                            val = SirenFactory.Create(sirenProperty.Type);
                            SirenFieldType valueDataType;
                            int            count;
                            Reader.OnListBegin(out valueDataType, out count);        //get count and type
                            var addMethod = sirenProperty.Type.GetMethod("Add");
                            for (int i = 0; i < count; i++)
                            {
                                var listItem = SirenFactory.Create(sirenProperty.ValueType);
                                DeserializeHelper(sirenProperty.ValueType, ref listItem, sirenProperty.ValueSirenClass);
                                addMethod.Invoke(val, new[] { listItem });
                            }
                            Reader.OnListEnd();
                        }

                        break;

                        case SirenPropertyFieldType.Dictionary:
                        {
                            val = SirenFactory.Create(sirenProperty.Type);

                            SirenFieldType keyDataType;
                            SirenFieldType valueDataType;
                            int            count;
                            Reader.OnDictionaryBegin(out keyDataType, out valueDataType, out count);        //get count and type
                            var addMethod = sirenProperty.Type.GetMethod("Add");
                            for (int i = 0; i < count; i++)
                            {
                                var dictKey   = SirenFactory.Create(sirenProperty.KeyType);
                                var dictValue = SirenFactory.Create(sirenProperty.ValueType);

                                DeserializeHelper(sirenProperty.KeyType, ref dictKey, sirenProperty.KeySirenClass);
                                DeserializeHelper(sirenProperty.ValueType, ref dictValue, sirenProperty.ValueSirenClass);
                                addMethod.Invoke(val, new[] { dictKey, dictValue });
                            }

                            Reader.OnDictionaryEnd();
                        }
                        break;
                        }
                        sirenProperty.Info.SetValue(obj, val);
                        Reader.OnPropertyEnd();
                        break;
                    }
                    else if (result < 0)
                    {
                        //Current filed missed
                        if (sirenProperty.Attribute.Modifier == SirenFieldModifier.Required)
                        {
                            //cannot find this property
                            Console.WriteLine("Cannot find property {0}.{1}", sirenClass.Name, sirenProperty.Name);
                            Reader.OnError();
                            return;
                        }
                        else
                        {
                            sirenProperty.SetToDefault(obj);
                            break;
                        }
                    }
                    else
                    {
                        //Read unknown field
                        //skip current field
                        Reader.OnPropertySkip(sirenProperty.DataType);
                        //read next field
                    }
                }
            }
        }