/// <summary> /// Restore the configuration settings from nova.conf. /// </summary> /// <remarks> /// If there is no configuration file yet it will be created when the /// settings are saved for the first time. /// </remarks> public void Restore() { string fileName = FileSearcher.GetConfigFile(); if (File.Exists(fileName)) { bool waitForFile = false; double waitTime = 0; // seconds do { try { using (FileStream confFile = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { // Data = Serializer.Deserialize(state) as GameSettings; XmlSerializer s = new XmlSerializer(typeof(Config)); Config data = new Config(); data = (Config)s.Deserialize(confFile); foreach (KeyValuePair <string, string> setting in data.settings) { if (this.settings.ContainsKey(setting.Key)) { this.settings[setting.Key] = setting.Value.ToString(); } else { this.settings.Add(setting.Key, setting.Value); } } waitForFile = false; } } catch (System.IO.IOException) { // IOException. Is the file locked? Try waiting. if (waitTime < Global.TotalFileWaitTime) { waitForFile = true; System.Threading.Thread.Sleep(Global.FileWaitRetryTime); waitTime += 0.1; } else { // Give up, maybe something else is wrong? throw; } } }while (waitForFile); } this.initialized = true; }
/// <summary> /// Save the console persistent data. /// </summary> public void Save() { string fileName = FileSearcher.GetConfigFile(); if (fileName == null) { // TODO (priority 5) add the nicities. Update the config files location. SaveFileDialog fd = new SaveFileDialog(); fd.Title = "Choose a location to save the nova.config file."; DialogResult result = fd.ShowDialog(); if (result == DialogResult.OK) { fileName = fd.FileName; } else { throw new System.IO.IOException("File dialog cancelled."); } } bool waitForFile = false; double waitTime = 0.0; // seconds do { try { using (Stream stream = new FileStream(fileName, FileMode.Create)) { XmlSerializer s = new XmlSerializer(typeof(Config)); s.Serialize(stream, this); } waitForFile = false; } catch (System.IO.IOException) { // IOException. Is the file locked? Try waiting. if (waitTime < Global.TotalFileWaitTime) { waitForFile = true; System.Threading.Thread.Sleep(Global.FileWaitRetryTime); waitTime += 0.1; } else { // Give up, maybe something else is wrong? throw; } } }while (waitForFile); }