示例#1
0
        public static NMSTemplate DeserializeEXml(EXmlData xmlData)
        {
            NMSTemplate template       = TemplateFromName(xmlData.Template);
            Type        templateType   = template.GetType();
            var         templateFields = templateType.GetFields().OrderBy(field => field.MetadataToken); // hack to get fields in order of declaration (todo: use something less hacky, this might break mono?)

            foreach (var templateField in templateFields)
            {
                NMSAttribute settings = templateField.GetCustomAttribute <NMSAttribute>();
                if (settings?.DefaultValue != null)
                {
                    templateField.SetValue(template, settings.DefaultValue);
                }
            }

            foreach (var xmlProperty in xmlData.Elements.OfType <EXmlProperty>())
            {
                FieldInfo    field      = templateType.GetField(xmlProperty.Name);
                Type         fieldType  = field.FieldType;
                NMSAttribute settings   = field.GetCustomAttribute <NMSAttribute>();
                object       fieldValue = DeserializeEXmlValue(template, fieldType, field, xmlProperty, templateType, settings);
                field.SetValue(template, fieldValue);
            }

            foreach (EXmlData innerXmlData in xmlData.Elements.OfType <EXmlData>())
            {
                FieldInfo   field         = templateType.GetField(innerXmlData.Name);
                NMSTemplate innerTemplate = DeserializeEXml(innerXmlData);
                field.SetValue(template, innerTemplate);
            }

            return(template);
        }
示例#2
0
        public EXmlData SerializeEXml()
        {
            Type     type    = GetType();
            EXmlData xmlData = new EXmlData
            {
                Template = type.Name
            };

            var fields = type.GetFields().OrderBy(field => field.MetadataToken); // hack to get fields in order of declaration (todo: use something less hacky, this might break mono?)

            foreach (var field in fields)
            {
                NMSAttribute settings = field.GetCustomAttribute <NMSAttribute>();
                if (settings == null)
                {
                    settings = new NMSAttribute();
                }
                if (settings.Ignore)
                {
                    continue;
                }
                xmlData.Elements.Add(SerializeEXmlValue(field.FieldType, field, settings, field.GetValue(this)));
            }

            return(xmlData);
        }
示例#3
0
        public static NMSTemplate DeserializeEXml(EXmlData xmlData)
        {
            NMSTemplate template     = TemplateFromName(xmlData.Template);
            Type        templateType = template.GetType();

            foreach (var xmlProperty in xmlData.Elements.OfType <EXmlProperty>())
            {
                FieldInfo field     = templateType.GetField(xmlProperty.Name);
                Type      fieldType = field.FieldType;
                object    fieldValue;
                switch (fieldType.Name)
                {
                case "String":
                    fieldValue = xmlProperty.Value;
                    break;

                case "Single":
                    fieldValue = float.Parse(xmlProperty.Value);
                    break;

                case "Boolean":
                    fieldValue = bool.Parse(xmlProperty.Value);
                    break;

                case "Int16":
                    fieldValue = short.Parse(xmlProperty.Value);
                    break;

                case "Int32":
                    var valuesMethod = templateType.GetMethod(field.Name + "Values");
                    if (valuesMethod != null)
                    {
                        if (String.IsNullOrEmpty(xmlProperty.Value))
                        {
                            fieldValue = (int)-1;
                        }
                        else
                        {
                            string[] values = (string[])valuesMethod.Invoke(template, null);
                            fieldValue = Array.FindIndex(values, v => v == xmlProperty.Value);
                        }
                    }
                    else
                    {
                        fieldValue = int.Parse(xmlProperty.Value);
                    }
                    break;

                case "Int64":
                    fieldValue = long.Parse(xmlProperty.Value);
                    break;

                case "Byte[]":
                    fieldValue = xmlProperty.Value == null ? null : Convert.FromBase64String(xmlProperty.Value);
                    break;

                case "List`1":
                    Type  elementType = fieldType.GetGenericArguments()[0];
                    Type  listType    = typeof(List <>).MakeGenericType(elementType);
                    IList list        = (IList)Activator.CreateInstance(listType);
                    foreach (EXmlData innerXmlData in xmlProperty.Elements.OfType <EXmlData>())
                    {
                        NMSTemplate element = DeserializeEXml(innerXmlData);
                        list.Add(element);
                    }

                    fieldValue = list;
                    break;

                default:
                    fieldValue = fieldType.IsValueType ? Activator.CreateInstance(fieldType) : null;
                    break;
                }

                field.SetValue(template, fieldValue);
            }

            foreach (EXmlData innerXmlData in xmlData.Elements.OfType <EXmlData>())
            {
                FieldInfo   field         = templateType.GetField(innerXmlData.Name);
                NMSTemplate innerTemplate = DeserializeEXml(innerXmlData);
                field.SetValue(template, innerTemplate);
            }

            return(template);
        }
示例#4
0
        public EXmlData SerializeEXml()
        {
            Type     type    = GetType();
            EXmlData xmlData = new EXmlData
            {
                Template = type.Name
            };

            var fields = type.GetFields().OrderBy(field => field.MetadataToken); // hack to get fields in order of declaration (todo: use something less hacky, this might break mono?)

            foreach (var field in fields)
            {
                if (field.Name.StartsWith("Padding"))
                {
                    continue;
                }
                var fieldName = field.Name;
                var fieldType = field.FieldType.Name;

                switch (fieldType)
                {
                case "String":
                case "Single":
                case "Boolean":
                case "Int16":
                case "UInt16":
                case "Int32":
                case "UInt32":
                case "Int64":
                case "UInt64":
                    var value        = field.GetValue(this);
                    var valueStr     = value.ToString();
                    var valuesMethod = type.GetMethod(field.Name + "Values");     // if we have an "xxxValues()" method in the struct, use that to get the value name
                    if (valuesMethod != null)
                    {
                        if (((int)value) == -1)
                        {
                            valueStr = "";
                        }
                        else
                        {
                            string[] values = (string[])valuesMethod.Invoke(this, null);
                            valueStr = values[(int)value];
                        }
                    }

                    xmlData.Elements.Add(new EXmlProperty
                    {
                        Name  = fieldName,
                        Value = valueStr
                    });
                    break;

                case "Byte[]":
                    byte[] bytes       = (byte[])field.GetValue(this);
                    string base64Value = bytes == null ? null : Convert.ToBase64String(bytes);

                    xmlData.Elements.Add(new EXmlProperty
                    {
                        Name  = fieldName,
                        Value = base64Value
                    });
                    break;

                case "List`1":
                    EXmlProperty listProperty = new EXmlProperty
                    {
                        Name = fieldName
                    };

                    IList templates = (IList)field.GetValue(this);
                    foreach (var template in templates.Cast <NMSTemplate>())
                    {
                        listProperty.Elements.Add(template.SerializeEXml());
                    }

                    xmlData.Elements.Add(listProperty);
                    break;

                case "NMSTemplate":
                    if (field.GetValue(this) != null)
                    {
                        NMSTemplate template = (NMSTemplate)field.GetValue(this);

                        EXmlData templateXmlData = template.SerializeEXml();
                        templateXmlData.Name = fieldName;

                        xmlData.Elements.Add(templateXmlData);
                    }
                    break;

                default:
                    if (field.FieldType.BaseType.Name == "NMSTemplate")
                    {
                        NMSTemplate template = (NMSTemplate)field.GetValue(this);

                        EXmlData templateXmlData = template.SerializeEXml();
                        templateXmlData.Name = fieldName;

                        xmlData.Elements.Add(templateXmlData);
                    }

                    break;
                }
            }

            return(xmlData);
        }
示例#5
0
        public EXmlBase SerializeEXmlValue(Type fieldType, FieldInfo field, NMSAttribute settings, object value)
        {
            string t = fieldType.Name;
            int    i = 0;

            switch (fieldType.Name)
            {
            case "String":
            case "Single":
            case "Boolean":
            case "Int16":
            case "UInt16":
            case "Int32":
            case "UInt32":
            case "Int64":
            case "UInt64":
                var valueStr = value.ToString();
                if (fieldType.Name == "Int32")
                {
                    var valuesMethod = GetType().GetMethod(field.Name + "Values");     // if we have an "xxxValues()" method in the struct, use that to get the value name
                    if (valuesMethod != null)
                    {
                        if (((int)value) == -1)
                        {
                            valueStr = "";
                        }
                        else
                        {
                            string[] values = (string[])valuesMethod.Invoke(this, null);
                            valueStr = values[(int)value];
                        }
                    }
                    else if (settings?.EnumValue != null)
                    {
                        if (((int)value) == -1)
                        {
                            valueStr = "";
                        }
                        else
                        {
                            valueStr = settings.EnumValue[(int)value];
                        }
                    }
                }

                return(new EXmlProperty
                {
                    Name = field.Name,
                    Value = valueStr
                });

            case "Byte[]":
                byte[] bytes       = (byte[])value;
                string base64Value = bytes == null ? null : Convert.ToBase64String(bytes);

                return(new EXmlProperty
                {
                    Name = field.Name,
                    Value = base64Value
                });

            case "List`1":
                var          listType     = field.FieldType.GetGenericArguments()[0];
                EXmlProperty listProperty = new EXmlProperty
                {
                    Name = field.Name
                };

                IList templates = (IList)value;
                i = 0;
                foreach (var template in templates)
                {
                    EXmlBase data = SerializeEXmlValue(listType, field, settings, template);
                    if (settings?.EnumValue != null)
                    {
                        data.Name = settings.EnumValue[i];
                        i++;
                    }
                    else
                    {
                        data.Name = null;
                    }

                    listProperty.Elements.Add(data);
                }

                return(listProperty);

            case "NMSTemplate":
                if (value != null)
                {
                    NMSTemplate template = (NMSTemplate)value;

                    EXmlData templateXmlData = template.SerializeEXml();
                    templateXmlData.Name = field.Name;

                    return(templateXmlData);
                }
                return(null);

            default:
                if (fieldType.BaseType.Name == "NMSTemplate")
                {
                    NMSTemplate template = (NMSTemplate)value;

                    EXmlData templateXmlData = template.SerializeEXml();
                    templateXmlData.Name = field.Name;

                    return(templateXmlData);
                }
                else if (fieldType.IsArray)
                {
                    var          arrayType     = field.FieldType.GetElementType();
                    EXmlProperty arrayProperty = new EXmlProperty
                    {
                        Name = field.Name
                    };

                    Array array = (Array)value;
                    i = 0;
                    foreach (var template in array)
                    {
                        EXmlBase data = SerializeEXmlValue(arrayType, field, settings, template);
                        if (settings?.EnumValue != null)
                        {
                            data.Name = settings.EnumValue[i];
                            i++;
                        }
                        else
                        {
                            data.Name = null;
                        }

                        arrayProperty.Elements.Add(data);
                    }

                    return(arrayProperty);
                }
                else
                {
                    throw new Exception(string.Format("Unable to encode {0} to EXml!", field));
                }
            }
        }
示例#6
0
        public static NMSTemplate DeserializeEXml(EXmlData xmlData)
        {
            NMSTemplate template       = TemplateFromName(xmlData.Template);
            Type        templateType   = template.GetType();
            var         templateFields = templateType.GetFields().OrderBy(field => field.MetadataToken); // hack to get fields in order of declaration (todo: use something less hacky, this might break mono?)

            foreach (var templateField in templateFields)
            {
                NMSAttribute settings = templateField.GetCustomAttribute <NMSAttribute>();
                if (settings == null)
                {
                    break;
                }
                if (settings.DefaultValue != null)
                {
                    templateField.SetValue(template, settings.DefaultValue);
                }
            }

            foreach (var xmlProperty in xmlData.Elements.OfType <EXmlProperty>())
            {
                FieldInfo    field     = templateType.GetField(xmlProperty.Name);
                Type         fieldType = field.FieldType;
                NMSAttribute settings  = field.GetCustomAttribute <NMSAttribute>();
                if (settings == null)
                {
                    settings = new NMSAttribute();
                }
                object fieldValue;
                switch (fieldType.Name)
                {
                case "String":
                    fieldValue = xmlProperty.Value;
                    break;

                case "Single":
                    fieldValue = float.Parse(xmlProperty.Value);
                    break;

                case "Boolean":
                    fieldValue = bool.Parse(xmlProperty.Value);
                    break;

                case "Int16":
                    fieldValue = short.Parse(xmlProperty.Value);
                    break;

                case "Int32":
                    var valuesMethod = templateType.GetMethod(field.Name + "Values");
                    if (valuesMethod != null)
                    {
                        if (String.IsNullOrEmpty(xmlProperty.Value))
                        {
                            fieldValue = (int)-1;
                        }
                        else
                        {
                            string[] values = (string[])valuesMethod.Invoke(template, null);
                            fieldValue = Array.FindIndex(values, v => v == xmlProperty.Value);
                        }
                    }
                    else if (settings.EnumValue != null)
                    {
                        if (String.IsNullOrEmpty(xmlProperty.Value))
                        {
                            fieldValue = (int)-1;
                        }
                        else
                        {
                            fieldValue = Array.FindIndex(settings.EnumValue, v => v == xmlProperty.Value);
                        }
                    }
                    else
                    {
                        fieldValue = int.Parse(xmlProperty.Value);
                    }
                    break;

                case "Int64":
                    fieldValue = long.Parse(xmlProperty.Value);
                    break;

                case "Byte[]":
                    fieldValue = xmlProperty.Value == null ? null : Convert.FromBase64String(xmlProperty.Value);
                    break;

                case "List`1":
                    Type  elementType = fieldType.GetGenericArguments()[0];
                    Type  listType    = typeof(List <>).MakeGenericType(elementType);
                    IList list        = (IList)Activator.CreateInstance(listType);
                    foreach (EXmlData innerXmlData in xmlProperty.Elements.OfType <EXmlData>())
                    {
                        NMSTemplate element = DeserializeEXml(innerXmlData);
                        list.Add(element);
                    }

                    fieldValue = list;
                    break;

                default:
                    if (field.FieldType.IsArray && field.FieldType.GetElementType().BaseType.Name == "NMSTemplate")
                    {
                        Array           array = Array.CreateInstance(field.FieldType.GetElementType(), settings.Size);
                        List <EXmlData> data  = xmlProperty.Elements.OfType <EXmlData>().ToList();
                        for (int i = 0; i < data.Count; ++i)
                        {
                            NMSTemplate element = DeserializeEXml(data[i]);
                            array.SetValue(element, i);
                        }

                        fieldValue = array;
                    }
                    else
                    {
                        fieldValue = fieldType.IsValueType ? Activator.CreateInstance(fieldType) : null;
                    }
                    break;
                }

                field.SetValue(template, fieldValue);
            }

            foreach (EXmlData innerXmlData in xmlData.Elements.OfType <EXmlData>())
            {
                FieldInfo   field         = templateType.GetField(innerXmlData.Name);
                NMSTemplate innerTemplate = DeserializeEXml(innerXmlData);
                field.SetValue(template, innerTemplate);
            }

            return(template);
        }
示例#7
0
        public EXmlData SerializeEXml()
        {
            Type     type    = GetType();
            EXmlData xmlData = new EXmlData
            {
                Template = type.Name
            };

            var fields = type.GetFields().OrderBy(field => field.MetadataToken); // hack to get fields in order of declaration (todo: use something less hacky, this might break mono?)

            foreach (var field in fields)
            {
                NMSAttribute settings = field.GetCustomAttribute <NMSAttribute>();
                if (settings == null)
                {
                    settings = new NMSAttribute();
                }
                if (settings.Ignore)
                {
                    continue;
                }
                var fieldName = field.Name;
                var fieldType = field.FieldType.Name;

                switch (fieldType)
                {
                case "String":
                case "Single":
                case "Boolean":
                case "Int16":
                case "UInt16":
                case "Int32":
                case "UInt32":
                case "Int64":
                case "UInt64":
                    var value        = field.GetValue(this);
                    var valueStr     = value.ToString();
                    var valuesMethod = type.GetMethod(field.Name + "Values");     // if we have an "xxxValues()" method in the struct, use that to get the value name
                    if (valuesMethod != null)
                    {
                        if (((int)value) == -1)
                        {
                            valueStr = "";
                        }
                        else
                        {
                            string[] values = (string[])valuesMethod.Invoke(this, null);
                            valueStr = values[(int)value];
                        }
                    }
                    else if (settings.EnumValue != null)
                    {
                        if (((int)value) == -1)
                        {
                            valueStr = "";
                        }
                        else
                        {
                            valueStr = settings.EnumValue[(int)value];
                        }
                    }

                    xmlData.Elements.Add(new EXmlProperty
                    {
                        Name  = fieldName,
                        Value = valueStr
                    });
                    break;

                case "Byte[]":
                    byte[] bytes       = (byte[])field.GetValue(this);
                    string base64Value = bytes == null ? null : Convert.ToBase64String(bytes);

                    xmlData.Elements.Add(new EXmlProperty
                    {
                        Name  = fieldName,
                        Value = base64Value
                    });
                    break;

                case "List`1":
                    EXmlProperty listProperty = new EXmlProperty
                    {
                        Name = fieldName
                    };

                    IList templates = (IList)field.GetValue(this);
                    foreach (var template in templates.Cast <NMSTemplate>())
                    {
                        listProperty.Elements.Add(template.SerializeEXml());
                    }

                    xmlData.Elements.Add(listProperty);
                    break;

                case "NMSTemplate":
                    if (field.GetValue(this) != null)
                    {
                        NMSTemplate template = (NMSTemplate)field.GetValue(this);

                        EXmlData templateXmlData = template.SerializeEXml();
                        templateXmlData.Name = fieldName;

                        xmlData.Elements.Add(templateXmlData);
                    }
                    break;

                default:
                    if (field.FieldType.BaseType.Name == "NMSTemplate")
                    {
                        NMSTemplate template = (NMSTemplate)field.GetValue(this);

                        EXmlData templateXmlData = template.SerializeEXml();
                        templateXmlData.Name = fieldName;

                        xmlData.Elements.Add(templateXmlData);
                    }
                    else if (field.FieldType.IsArray && field.FieldType.GetElementType().BaseType.Name == "NMSTemplate")
                    {
                        var          arrayType     = field.FieldType.GetElementType();
                        EXmlProperty arrayProperty = new EXmlProperty
                        {
                            Name = fieldName
                        };

                        Array array = (Array)field.GetValue(this);
                        int   i     = 0;
                        foreach (var template in array)
                        {
                            EXmlData data = ((NMSTemplate)template).SerializeEXml();
                            if (settings.EnumValue != null)
                            {
                                data.Name = settings.EnumValue[i];
                                i++;
                            }
                            arrayProperty.Elements.Add(data);
                        }

                        xmlData.Elements.Add(arrayProperty);
                    }
                    else
                    {
                        throw new Exception(string.Format("Unable to encode {0} to EXml!", field));
                    }

                    break;
                }
            }

            return(xmlData);
        }