Exemple #1
0
        public object ConvertProperty(cTSOProperty property)
        {
            var _struct = DataDefinition.GetStruct(property.StructType);

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

            if (!ModelTypeById.ContainsKey(_struct.ID))
            {
                return(null);
            }

            var type     = ModelTypeById[_struct.ID];
            var instance = ModelActivator.NewInstance(type);

            foreach (var field in property.StructFields)
            {
                var _field = _struct.Fields.FirstOrDefault(x => x.ID == field.StructFieldID);
                if (_field == null)
                {
                    continue;
                }

                SetFieldValue(instance, _field.Name, GetUpdateValue(field.Value));
            }

            return(instance);
        }
Exemple #2
0
        protected cTSOProperty ConvertToProperty(Struct _struct, object value, ISerializationContext context)
        {
            //Convert the struct to a cTSOProperty
            var property = new cTSOProperty();

            property.StructType   = _struct.ID;
            property.StructFields = new List <cTSOPropertyField>();

            foreach (var field in _struct.Fields)
            {
                var fieldValue = GetFieldValue(value, field.Name);
                if (fieldValue == null)
                {
                    continue;
                }

                if (context.ModelSerializer.GetClsid(fieldValue) == null)
                {
                    //Cant serialize this, sorry
                    continue;
                }

                property.StructFields.Add(new cTSOPropertyField
                {
                    StructFieldID = field.ID,
                    Value         = fieldValue
                });
            }

            return(property);
        }
Exemple #3
0
        protected object ConvertFromProperty(cTSOProperty property, ISerializationContext context)
        {
            var _struct = GetStruct(property.StructType);

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

            if (!ModelsByName.ContainsKey(_struct.Name))
            {
                return(null);
            }

            var modelType = ModelsByName[_struct.Name];
            var instance  = ModelActivator.NewInstance(modelType);

            foreach (var field in property.StructFields)
            {
                var fieldDef = _struct.Fields.FirstOrDefault(x => x.ID == field.StructFieldID);
                if (fieldDef == null)
                {
                    continue;
                }

                var clazzProperty = modelType.GetProperty(fieldDef.Name);
                if (clazzProperty == null)
                {
                    continue;
                }
                clazzProperty.SetValue(instance, field.Value);
            }

            return(instance);
        }
Exemple #4
0
        public virtual object Deserialize(uint clsid, IoBuffer input, ISerializationContext context)
        {
            var property = new cTSOProperty();

            property.Deserialize(input, context);
            //Convert to instance
            var result = ConvertFromProperty(property, context);

            return(result);
        }
Exemple #5
0
        public override object Deserialize(uint clsid, IoBuffer input, ISerializationContext serializer)
        {
            //note: currently an array full with null values will just return null.
            object result       = null;
            var    count        = input.GetUInt32();
            int    leadingNulls = 0;

            for (int i = 0; i < count; i++)
            {
                var property = new cTSOProperty();
                property.Deserialize(input, serializer);
                //Convert to instance
                var item = ConvertFromProperty(property, serializer);
                if (item == null)
                {
                    leadingNulls++;
                }
                else if (result == null)
                {
                    //still need to make the list!
                    var listType   = typeof(ImmutableList);
                    var consMethod = listType.GetMethods().Where(method => method.IsGenericMethod).FirstOrDefault();
                    if (consMethod == null)
                    {
                        Console.WriteLine("No Generic Methods!");
                        return(null);
                    }
                    else
                    {
                        var generic = consMethod.MakeGenericMethod(item.GetType());
                        if (generic == null)
                        {
                            Console.WriteLine("No Generic: " + item.GetType().ToString());
                            return(null);
                        }
                        result = generic.Invoke(null, new object[] { }); //listType.MakeGenericType(item.GetType());
                    }

                    //result = Activator.CreateInstance(constructedListType);
                    for (int j = 0; j < leadingNulls; j++)
                    {
                        result = result.GetType().GetMethod("Add").Invoke(result, new object[] { null });
                    }
                }
                if (result != null)
                {
                    result = result.GetType().GetMethod("Add").Invoke(result, new object[] { item });
                }
            }

            return(result);
        }