Guest User

Untitled

a guest
Sep 16th, 2019
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Xml;
  4. using System.Xml.Serialization;
  5.  
  6. namespace SeedKiln
  7. {
  8. public class Config
  9. {
  10. [NonSerialized]
  11. private static Config _cfg;
  12.  
  13.  
  14. public static string GetConfigFileName()
  15. {
  16. string location = typeof(SeedKiln).Assembly.Location;
  17. return Path.Combine(Path.GetDirectoryName(location), "SeedKiln.xml");
  18. }
  19.  
  20. public static Config Cfg
  21. {
  22. get
  23. {
  24. if (Config._cfg != null)
  25. {
  26. return Config._cfg;
  27. }
  28. else
  29. {
  30. if (!File.Exists(Config.GetConfigFileName()))
  31. {
  32. Debug.Log("[SeedKiln] Config file not found (" + Config.GetConfigFileName() + "). Creating new one with default values.");
  33. Config._cfg = new Config();
  34. try
  35. {
  36. using (StreamWriter streamWriter = new StreamWriter(Config.GetConfigFileName()))
  37. {
  38. XmlSerializer xmlSerializer = new XmlSerializer(typeof(Config));
  39. xmlSerializer.Serialize(streamWriter, Config._cfg);
  40. streamWriter.Flush();
  41. }
  42. }
  43. catch (Exception ex)
  44. {
  45. Debug.Log("[SeedKiln] Something goes wrong, can not save config (" + Config.GetConfigFileName() + "), Exception: " + ex.ToString());
  46. }
  47. return Config._cfg;
  48. }
  49. else
  50. {
  51. try
  52. {
  53. using (FileStream fileStream = File.OpenRead(Config.GetConfigFileName()))
  54. {
  55. XmlSerializer xmlSerializer2 = new XmlSerializer(typeof(Config));
  56. Config._cfg = (xmlSerializer2.Deserialize(fileStream) as Config);
  57. }
  58. }
  59. catch (Exception ex2)
  60. {
  61. Debug.Log("[SeedKiln] Something goes wrong, can not load config (" + Config.GetConfigFileName() + "), Exception: " + ex2.ToString());
  62. Config._cfg = new Config();
  63. }
  64.  
  65.  
  66. return Config._cfg;
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment