/// <param name="typeOverrideMap">Allows override of the type of the definition. Refer to MyDefinitionXmlSerializer</param>
        internal static bool DeserializeXML(Stream reader, out MyObjectBuilder_Base objectBuilder, Type builderType, Dictionary <string, string> typeOverrideMap)
        {
            Debug.Assert(typeof(MyObjectBuilder_Base).IsAssignableFrom(builderType));
            Debug.Assert(reader != null);
            Debug.Assert(builderType != null);

            objectBuilder = null;
            try
            {
                XmlSerializer serializer = MyXmlSerializerManager.GetSerializer(builderType);
                Debug.Assert(serializer != null);

                XmlReaderSettings settings = new XmlReaderSettings()
                {
                    CheckCharacters = true
                };
                MyXmlTextReader xmlReader = new MyXmlTextReader(reader, settings);
                xmlReader.DefinitionTypeOverrideMap = typeOverrideMap;

                objectBuilder = (MyObjectBuilder_Base)serializer.Deserialize(xmlReader);
            }
            catch (Exception e)
            {
                MyLog.Default.WriteLine("ERROR: Exception during objectbuilder read! (xml): " + builderType.Name);
                MyLog.Default.WriteLine(e);
                //if (Debugger.IsAttached)
                //    Debugger.Break();
                return(false);
            }

            return(true);
        }
Example #2
0
        /// <returns>True if it sucessfully deserialized the file.</returns>
        public static bool DeserializeXml <T>(string filename, out T objectBuilder, out string errorInformation) where T : MyObjectBuilder_Base
        {
            bool result = false;

            objectBuilder    = null;
            errorInformation = null;

            using (var fileStream = MyFileSystem.OpenRead(filename))
            {
                if (fileStream != null)
                {
                    using (var readStream = fileStream.UnwrapGZip())
                    {
                        if (readStream != null)
                        {
                            try
                            {
                                XmlSerializer serializer = MyXmlSerializerManager.GetSerializer(typeof(T));

                                XmlReaderSettings settings = new XmlReaderSettings {
                                    CheckCharacters = true
                                };
                                MyXmlTextReader xmlReader = new MyXmlTextReader(readStream, settings);

                                objectBuilder = (T)serializer.Deserialize(xmlReader);
                                result        = true;
                            }
                            catch (Exception ex)
                            {
                                objectBuilder    = null;
                                errorInformation = $@"Failed to load file: {filename}
Reason: {ex.AllMessages()}";
                            }
                        }
                    }
                }
            }

            return(result);
        }
Example #3
0
        protected override string GetTypeAttribute(XmlReader reader)
        {
            string typeAttrib = base.GetTypeAttribute(reader);

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

            MyXmlTextReader myreader = reader as MyXmlTextReader;

            if (myreader != null && myreader.DefinitionTypeOverrideMap != null)
            {
                string typeOverride;
                if (myreader.DefinitionTypeOverrideMap.TryGetValue(typeAttrib, out typeOverride))
                {
                    return(typeOverride);
                }
            }

            return(typeAttrib);
        }