Esempio n. 1
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));
                }
            }
        }
Esempio n. 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)
            {
                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);
        }
Esempio n. 3
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);
        }