예제 #1
0
        public bool Save(FileInfo fileInfo)
        {
            if (fileInfo == null)
            {
                return(false);
            }

            //Save the definitions container out to the file
            try
            {
                using (XmlTextWriter xmlTextWriter = new XmlTextWriter(fileInfo.FullName, null))
                {
                    xmlTextWriter.Formatting  = Formatting.Indented;
                    xmlTextWriter.Indentation = 2;
                    xmlTextWriter.IndentChar  = ' ';
                    MyConfigDedicatedDataSerializer serializer = (MyConfigDedicatedDataSerializer)Activator.CreateInstance(typeof(MyConfigDedicatedDataSerializer));
                    serializer.Serialize(xmlTextWriter, _definition);
                }
            }
            catch
            {
                throw new GameInstallationInfoException(GameInstallationInfoExceptionState.ConfigFileCorrupted, fileInfo.FullName);
            }

            if (_definition == null)
            {
                throw new GameInstallationInfoException(GameInstallationInfoExceptionState.ConfigFileEmpty, fileInfo.FullName);
            }

            return(true);
        }
        /// <summary>
        /// Load the dedicated server configuration file
        /// </summary>
        /// <param name="fileInfo">Path to the configuration file</param>
        /// <returns></returns>
        public static MyConfigDedicatedData Load(FileInfo fileInfo)
        {
            object fileContent;

            string filePath = fileInfo.FullName;

            if (!File.Exists(filePath))
            {
                throw new GameInstallationInfoException(GameInstallationInfoExceptionState.ConfigFileMissing, filePath);
            }

            try
            {
                XmlReaderSettings settings = new XmlReaderSettings
                {
                    IgnoreComments   = true,
                    IgnoreWhitespace = true,
                };

                using (XmlReader xmlReader = XmlReader.Create(filePath, settings))
                {
                    MyConfigDedicatedDataSerializer serializer = (MyConfigDedicatedDataSerializer)Activator.CreateInstance(typeof(MyConfigDedicatedDataSerializer));
                    fileContent = serializer.Deserialize(xmlReader);
                }
            }
            catch
            {
                throw new GameInstallationInfoException(GameInstallationInfoExceptionState.ConfigFileCorrupted, filePath);
            }

            if (fileContent == null)
            {
                throw new GameInstallationInfoException(GameInstallationInfoExceptionState.ConfigFileEmpty, filePath);
            }

            return((MyConfigDedicatedData)fileContent);
        }
예제 #3
0
        /// <summary>
        /// Load the dedicated server configuration file
        /// </summary>
        /// <param name="fileInfo">Path to the configuration file</param>
        /// <exception cref="FileNotFoundException">Thrown if configuration file cannot be found at the path specified.</exception>
        /// <returns></returns>
        /// <exception cref="ConfigurationErrorsException">Configuration file not understood. See inner exception for details. Ignore configuration file line number in outer exception.</exception>
        public static MyConfigDedicatedData Load(FileInfo fileInfo)
        {
            object fileContent;

            string filePath = fileInfo.FullName;

            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException("Game configuration file not found.", filePath);
            }

            try
            {
                XmlReaderSettings settings = new XmlReaderSettings
                {
                    IgnoreComments   = true,
                    IgnoreWhitespace = true,
                };

                using (XmlReader xmlReader = XmlReader.Create(filePath, settings))
                {
                    MyConfigDedicatedDataSerializer serializer = (MyConfigDedicatedDataSerializer)Activator.CreateInstance(typeof(MyConfigDedicatedDataSerializer));
                    fileContent = serializer.Deserialize(xmlReader);
                }
            }
            catch (Exception ex)
            {
                throw new ConfigurationErrorsException("Configuration file not understood. See inner exception for details. Ignore configuration file line number in outer exception.", ex, filePath, -1);
            }

            if (fileContent == null)
            {
                throw new ConfigurationErrorsException("Configuration file empty.");
            }

            return((MyConfigDedicatedData)fileContent);
        }