Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Xml;
- using System.Xml.Serialization;
- namespace SeedKiln
- {
- public class Config
- {
- [NonSerialized]
- private static Config _cfg;
- public static string GetConfigFileName()
- {
- string location = typeof(SeedKiln).Assembly.Location;
- return Path.Combine(Path.GetDirectoryName(location), "SeedKiln.xml");
- }
- public static Config Cfg
- {
- get
- {
- if (Config._cfg != null)
- {
- return Config._cfg;
- }
- else
- {
- if (!File.Exists(Config.GetConfigFileName()))
- {
- Debug.Log("[SeedKiln] Config file not found (" + Config.GetConfigFileName() + "). Creating new one with default values.");
- Config._cfg = new Config();
- try
- {
- using (StreamWriter streamWriter = new StreamWriter(Config.GetConfigFileName()))
- {
- XmlSerializer xmlSerializer = new XmlSerializer(typeof(Config));
- xmlSerializer.Serialize(streamWriter, Config._cfg);
- streamWriter.Flush();
- }
- }
- catch (Exception ex)
- {
- Debug.Log("[SeedKiln] Something goes wrong, can not save config (" + Config.GetConfigFileName() + "), Exception: " + ex.ToString());
- }
- return Config._cfg;
- }
- else
- {
- try
- {
- using (FileStream fileStream = File.OpenRead(Config.GetConfigFileName()))
- {
- XmlSerializer xmlSerializer2 = new XmlSerializer(typeof(Config));
- Config._cfg = (xmlSerializer2.Deserialize(fileStream) as Config);
- }
- }
- catch (Exception ex2)
- {
- Debug.Log("[SeedKiln] Something goes wrong, can not load config (" + Config.GetConfigFileName() + "), Exception: " + ex2.ToString());
- Config._cfg = new Config();
- }
- return Config._cfg;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment