Example #1
0
        public static string Serialize(Unity.PackageManager.Ivy.ModuleRepository repo)
        {
            HasErrors      = false;
            ErrorMessage   = null;
            ErrorException = null;
            Unity.PackageManager.IvyInternal.ModuleRepository repository = Cloner.CloneObject <Unity.PackageManager.IvyInternal.ModuleRepository>(repo);
            IvyRoot o = new IvyRoot {
                Repository = repository
            };
            StringBuilder builder = new StringBuilder();

            using (UTF8StringWriter writer = new UTF8StringWriter(builder))
            {
                XmlSerializable.GetSerializer(o.GetType()).Serialize(writer, o, Namespaces);
            }
            return(builder.ToString().Replace("<root>", "").Replace("</root>", ""));
        }
Example #2
0
        public static string Serialize(IvyModule module)
        {
            IvyParser.HasErrors      = false;
            IvyParser.ErrorMessage   = null;
            IvyParser.ErrorException = null;
            Unity.PackageManager.IvyInternal.IvyModule ivyModule = Cloner.CloneObject <Unity.PackageManager.IvyInternal.IvyModule>(module);
            IvyRoot ivyRoot = new IvyRoot();

            ivyRoot.Module = ivyModule;
            StringBuilder stringBuilder = new StringBuilder();

            using (UTF8StringWriter uTF8StringWriter = new UTF8StringWriter(stringBuilder))
            {
                XmlSerializer serializer = XmlSerializable.GetSerializer(ivyModule.GetType());
                serializer.Serialize(uTF8StringWriter, ivyModule, IvyParser.Namespaces);
            }
            return(stringBuilder.ToString().Replace("<root>", string.Empty).Replace("</root>", string.Empty));
        }
Example #3
0
        public static T Deserialize <T>(string xml) where T : class
        {
            IvyParser.HasErrors      = false;
            IvyParser.ErrorMessage   = null;
            IvyParser.ErrorException = null;
            if (xml.Length <= 0)
            {
                IvyParser.HasErrors    = true;
                IvyParser.ErrorMessage = "Cannot deserialize empty xml document.";
                return((T)((object)null));
            }
            Type typeFromHandle = typeof(T);
            int  startIndex     = 0;
            int  num            = xml.IndexOf("<ivy-module");
            int  num2           = xml.IndexOf("<ivy-repository");

            if (typeof(T) == typeof(IvyModule))
            {
                if (num < 0)
                {
                    return((T)((object)null));
                }
                startIndex     = num;
                typeFromHandle = typeof(IvyModule);
            }
            else if (typeof(T) == typeof(ModuleRepository))
            {
                if (num2 < 0)
                {
                    return((T)((object)null));
                }
                startIndex     = num2;
                typeFromHandle = typeof(ModuleRepository);
            }
            else if (typeof(T) == typeof(object))
            {
                if (num2 >= 0)
                {
                    startIndex     = num2;
                    typeFromHandle = typeof(ModuleRepository);
                }
                else if (num >= 0)
                {
                    startIndex     = num;
                    typeFromHandle = typeof(IvyModule);
                }
            }
            xml = xml.Insert(startIndex, "<root>") + "</root>";
            try
            {
                XmlTextReader xmlReader  = new XmlTextReader(xml, XmlNodeType.Document, null);
                XmlSerializer serializer = XmlSerializable.GetSerializer(typeof(IvyRoot));
                IvyRoot       ivyRoot    = serializer.Deserialize(xmlReader) as IvyRoot;
                T             result;
                if (typeFromHandle == typeof(IvyModule))
                {
                    result = (Cloner.CloneObject(ivyRoot.Module, typeFromHandle) as T);
                    return(result);
                }
                result = (Cloner.CloneObject(ivyRoot.Repository, typeFromHandle) as T);
                return(result);
            }
            catch (Exception errorException)
            {
                IvyParser.HasErrors      = true;
                IvyParser.ErrorMessage   = "Deserialization failed.";
                IvyParser.ErrorException = errorException;
            }
            return((T)((object)null));
        }
Example #4
0
        public void ReadXml(XmlReader reader)
        {
            this.ProcessTypes();
            this.ProcessType();
            bool isEmptyElement = reader.IsEmptyElement;

            reader.MoveToContent();
            if (reader.HasAttributes)
            {
                string str = base.GetType().FullName + ".";
                for (int i = 0; i < reader.AttributeCount; i++)
                {
                    reader.MoveToAttribute(i);
                    string localName = reader.LocalName;
                    string text      = reader.Value;
                    if (localName == "packageType")
                    {
                        if (text == "PlaybackEngines")
                        {
                            text = "PlaybackEngine";
                        }
                        if (text == "EditorExtension")
                        {
                            text = "UnityExtension";
                        }
                    }
                    KeyValuePair <Func <object, object>, Action <object, object> > keyValuePair;
                    if (XmlSerializable.attributeMappings.TryGetValue(str + localName, out keyValuePair))
                    {
                        keyValuePair.Value(this, text);
                    }
                }
            }
            if (reader.IsStartElement() && !reader.IsEmptyElement)
            {
                reader.ReadStartElement();
            }
            while (reader.IsStartElement() && !isEmptyElement)
            {
                string localName2 = reader.LocalName;
                string text2      = localName2;
                Type   type;
                if (!XmlSerializable.typeMappings.TryGetValue(text2, out type))
                {
                    text2 = base.GetType().Name + "." + text2;
                    if (!XmlSerializable.typeMappings.TryGetValue(text2, out type))
                    {
                        reader.Skip();
                        continue;
                    }
                }
                XmlSerializer serializer = XmlSerializable.GetSerializer(type);
                object        arg        = serializer.Deserialize(reader);
                text2 = base.GetType().FullName + "." + localName2;
                KeyValuePair <Func <object, object>, Action <object, object> > keyValuePair2;
                if (XmlSerializable.attributeMappings.TryGetValue(text2, out keyValuePair2))
                {
                    keyValuePair2.Value(this, arg);
                }
            }
            if (!isEmptyElement)
            {
                reader.ReadEndElement();
            }
            else
            {
                reader.ReadStartElement();
            }
        }
Example #5
0
        public void WriteXml(XmlWriter writer)
        {
            this.ProcessTypes();
            this.ProcessType();
            Type   type = base.GetType();
            string str  = type.FullName + ".";

            MemberInfo[]  members = type.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            List <string> list    = new List <string>();
            Dictionary <string, XmlAttributeAttribute> dictionary = new Dictionary <string, XmlAttributeAttribute>();
            Dictionary <string, string> dictionary2 = new Dictionary <string, string>();
            Dictionary <string, object> dictionary3 = new Dictionary <string, object>();

            MemberInfo[] array = members;
            for (int i = 0; i < array.Length; i++)
            {
                MemberInfo memberInfo = array[i];
                if (memberInfo.MemberType == MemberTypes.Field || memberInfo.MemberType == MemberTypes.Property)
                {
                    object[] customAttributes = memberInfo.GetCustomAttributes(false);
                    object[] array2           = customAttributes;
                    for (int j = 0; j < array2.Length; j++)
                    {
                        object obj = array2[j];
                        if (!(obj is XmlIgnoreAttribute))
                        {
                            if (obj is XmlElementAttribute)
                            {
                                XmlElementAttribute xmlElementAttribute = (XmlElementAttribute)obj;
                                string elementName = xmlElementAttribute.ElementName;
                                if (xmlElementAttribute.Order > 0)
                                {
                                    if (list.Count < xmlElementAttribute.Order)
                                    {
                                        list.AddRange(new string[xmlElementAttribute.Order - list.Count]);
                                    }
                                    list[xmlElementAttribute.Order - 1] = str + elementName;
                                }
                                else
                                {
                                    list.Add(str + elementName);
                                }
                                dictionary2.Add(str + elementName, elementName);
                                break;
                            }
                            if (obj is XmlAttributeAttribute)
                            {
                                XmlAttributeAttribute xmlAttributeAttribute = (XmlAttributeAttribute)obj;
                                dictionary.Add(str + xmlAttributeAttribute.AttributeName, xmlAttributeAttribute);
                                object[] customAttributes2 = memberInfo.GetCustomAttributes(typeof(DefaultValueAttribute), false);
                                if (customAttributes2.Length > 0)
                                {
                                    dictionary3.Add(str + xmlAttributeAttribute.AttributeName, ((DefaultValueAttribute)customAttributes2[0]).Value);
                                }
                                break;
                            }
                        }
                    }
                }
            }
            foreach (KeyValuePair <string, XmlAttributeAttribute> current in dictionary)
            {
                if (XmlSerializable.attributeMappings.ContainsKey(current.Key))
                {
                    object obj2 = XmlSerializable.attributeMappings[current.Key].Key(this);
                    if (obj2 != null)
                    {
                        string text = obj2.ToString();
                        object obj3 = null;
                        if (!dictionary3.TryGetValue(current.Key, out obj3) || obj2 != obj3)
                        {
                            if (obj2 is bool)
                            {
                                if (!(bool)obj2)
                                {
                                    continue;
                                }
                                text = text.ToLower();
                            }
                            if (obj2 is IvyPackageType && this is Unity.PackageManager.IvyInternal.IvyInfo)
                            {
                                PackageVersion a = new PackageVersion(((Unity.PackageManager.IvyInternal.IvyInfo) this).UnityVersion);
                                if (a <= XmlSerializable.compatibilityVersion)
                                {
                                    IvyPackageType ivyPackageType = (IvyPackageType)((int)obj2);
                                    if (ivyPackageType != IvyPackageType.PlaybackEngine)
                                    {
                                        if (ivyPackageType == IvyPackageType.UnityExtension)
                                        {
                                            text = "EditorExtension";
                                        }
                                    }
                                    else
                                    {
                                        text = "PlaybackEngines";
                                    }
                                }
                            }
                            writer.WriteAttributeString((current.Value.Namespace == null) ? string.Empty : "e", current.Value.AttributeName, current.Value.Namespace, text);
                        }
                    }
                }
            }
            foreach (string current2 in list)
            {
                if (XmlSerializable.attributeMappings.ContainsKey(current2))
                {
                    object obj4 = XmlSerializable.attributeMappings[current2].Key(this);
                    if (obj4 != null)
                    {
                        if (obj4 is List <Unity.PackageManager.IvyInternal.IvyRepository> )
                        {
                            IList list2 = obj4 as IList;
                            foreach (object current3 in list2)
                            {
                                XmlSerializer serializer = XmlSerializable.GetSerializer(current3.GetType());
                                serializer.Serialize(writer, current3, IvyParser.Namespaces);
                            }
                        }
                        else
                        {
                            XmlSerializer serializer2 = XmlSerializable.GetSerializer(obj4.GetType());
                            serializer2.Serialize(writer, obj4, IvyParser.Namespaces);
                        }
                    }
                }
            }
        }
Example #6
0
        public static T Deserialize <T>(string xml) where T : class
        {
            HasErrors      = false;
            ErrorMessage   = null;
            ErrorException = null;
            if (xml.Length <= 0)
            {
                HasErrors    = true;
                ErrorMessage = "Cannot deserialize empty xml document.";
                return(null);
            }
            Type targetType = typeof(T);
            int  startIndex = 0;
            int  index      = xml.IndexOf("<ivy-module");
            int  num3       = xml.IndexOf("<ivy-repository");

            if (typeof(T) == typeof(Unity.PackageManager.Ivy.IvyModule))
            {
                if (index < 0)
                {
                    return(null);
                }
                startIndex = index;
                targetType = typeof(Unity.PackageManager.Ivy.IvyModule);
            }
            else if (typeof(T) == typeof(Unity.PackageManager.Ivy.ModuleRepository))
            {
                if (num3 < 0)
                {
                    return(null);
                }
                startIndex = num3;
                targetType = typeof(Unity.PackageManager.Ivy.ModuleRepository);
            }
            else if (typeof(T) == typeof(object))
            {
                if (num3 >= 0)
                {
                    startIndex = num3;
                    targetType = typeof(Unity.PackageManager.Ivy.ModuleRepository);
                }
                else if (index >= 0)
                {
                    startIndex = index;
                    targetType = typeof(Unity.PackageManager.Ivy.IvyModule);
                }
            }
            xml = xml.Insert(startIndex, "<root>") + "</root>";
            try
            {
                XmlTextReader xmlReader = new XmlTextReader(xml, XmlNodeType.Document, null);
                IvyRoot       root      = XmlSerializable.GetSerializer(typeof(IvyRoot)).Deserialize(xmlReader) as IvyRoot;
                if (targetType == typeof(Unity.PackageManager.Ivy.IvyModule))
                {
                    return(Cloner.CloneObject(root.Module, targetType) as T);
                }
                return(Cloner.CloneObject(root.Repository, targetType) as T);
            }
            catch (Exception exception)
            {
                HasErrors      = true;
                ErrorMessage   = "Deserialization failed.";
                ErrorException = exception;
            }
            return(null);
        }