The easy to use and full featured Irc Bot everyone is talking about!
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Config.cs 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using System.Net;
  8. using System.IO;
  9. using Newtonsoft.Json;
  10. namespace Combot.Configurations
  11. {
  12. public class Config
  13. {
  14. private ReaderWriterLockSlim ConfigRWLock;
  15. private ReaderWriterLockSlim ConfigFileRWLock;
  16. private JsonSerializerSettings JsonSettings;
  17. public Config()
  18. {
  19. ConfigRWLock = new ReaderWriterLockSlim();
  20. ConfigFileRWLock = new ReaderWriterLockSlim();
  21. Servers = new List<ServerConfig>();
  22. JsonSettings = new JsonSerializerSettings();
  23. JsonSettings.Converters.Add(new IPAddressConverter());
  24. JsonSettings.Converters.Add(new IPEndPointConverter());
  25. JsonSettings.Formatting = Formatting.Indented;
  26. }
  27. private List<ServerConfig> _servers;
  28. public List<ServerConfig> Servers
  29. {
  30. get
  31. {
  32. return _servers;
  33. }
  34. private set
  35. {
  36. if (value != _servers)
  37. {
  38. _servers = value;
  39. }
  40. }
  41. }
  42. public void AddServer(ServerConfig config)
  43. {
  44. ConfigRWLock.EnterWriteLock();
  45. if (!Servers.Exists(server => server.Name == config.Name))
  46. {
  47. config.ModifyEvent += SaveServers;
  48. config.LoadEvent += UpdateServers;
  49. Servers.Add(config);
  50. }
  51. ConfigRWLock.ExitWriteLock();
  52. }
  53. public void SaveServers()
  54. {
  55. ConfigFileRWLock.EnterWriteLock();
  56. // Serialize Config
  57. ConfigRWLock.EnterReadLock();
  58. string configContents = JsonConvert.SerializeObject(Servers, JsonSettings);
  59. ConfigRWLock.ExitReadLock();
  60. // Save config to file
  61. string ConfigPath = Path.Combine(Directory.GetCurrentDirectory(), @"Combot.Servers.json");
  62. using (StreamWriter streamWriter = new StreamWriter(ConfigPath, false))
  63. {
  64. streamWriter.Write(configContents);
  65. }
  66. ConfigFileRWLock.ExitWriteLock();
  67. }
  68. public void LoadServers()
  69. {
  70. ConfigFileRWLock.EnterReadLock();
  71. string ConfigPath = Path.Combine(Directory.GetCurrentDirectory(), @"Combot.Servers.json");
  72. if (!File.Exists(ConfigPath))
  73. {
  74. string defaultPath = Path.Combine(Directory.GetCurrentDirectory(), @"Combot.Servers.Default.json");
  75. if (File.Exists(defaultPath))
  76. {
  77. File.Copy(defaultPath, ConfigPath);
  78. }
  79. }
  80. if (File.Exists(ConfigPath))
  81. {
  82. string configContents;
  83. using (StreamReader streamReader = new StreamReader(ConfigPath, Encoding.UTF8))
  84. {
  85. configContents = streamReader.ReadToEnd();
  86. }
  87. // Load the deserialized file into the config
  88. ConfigRWLock.EnterWriteLock();
  89. Servers = JsonConvert.DeserializeObject<List<ServerConfig>>(configContents, JsonSettings);
  90. for (int i = 0; i < Servers.Count; i++)
  91. {
  92. Servers[i].ModifyEvent += SaveServers;
  93. Servers[i].LoadEvent += UpdateServers;
  94. }
  95. ConfigRWLock.ExitWriteLock();
  96. }
  97. ConfigFileRWLock.ExitReadLock();
  98. }
  99. public void UpdateServers()
  100. {
  101. ConfigFileRWLock.EnterReadLock();
  102. string ConfigPath = Path.Combine(Directory.GetCurrentDirectory(), @"Combot.Servers.json");
  103. if (!File.Exists(ConfigPath))
  104. {
  105. string defaultPath = Path.Combine(Directory.GetCurrentDirectory(), @"Combot.Servers.Default.json");
  106. if (File.Exists(defaultPath))
  107. {
  108. File.Copy(defaultPath, ConfigPath);
  109. }
  110. }
  111. if (File.Exists(ConfigPath))
  112. {
  113. string configContents;
  114. using (StreamReader streamReader = new StreamReader(ConfigPath, Encoding.UTF8))
  115. {
  116. configContents = streamReader.ReadToEnd();
  117. }
  118. // Load the deserialized file into the config
  119. ConfigRWLock.EnterWriteLock();
  120. List<ServerConfig> newConfigs = JsonConvert.DeserializeObject<List<ServerConfig>>(configContents, JsonSettings);
  121. foreach (ServerConfig newConfig in newConfigs)
  122. {
  123. if (Servers.Exists(server => server.Name == newConfig.Name))
  124. {
  125. Servers.Find(server => server.Name == newConfig.Name).Copy(newConfig);
  126. }
  127. else
  128. {
  129. Servers.Add(newConfig);
  130. }
  131. }
  132. ConfigRWLock.ExitWriteLock();
  133. }
  134. ConfigFileRWLock.ExitReadLock();
  135. }
  136. }
  137. }