The easy to use and full featured Irc Bot everyone is talking about!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Config.cs 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 += LoadServers;
  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.config");
  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.config");
  72. if (File.Exists(ConfigPath))
  73. {
  74. string configContents;
  75. using (StreamReader streamReader = new StreamReader(ConfigPath, Encoding.UTF8))
  76. {
  77. configContents = streamReader.ReadToEnd();
  78. }
  79. // Load the deserialized file into the config
  80. ConfigRWLock.EnterWriteLock();
  81. Servers = JsonConvert.DeserializeObject<List<ServerConfig>>(configContents, JsonSettings);
  82. for (int i = 0; i < Servers.Count; i++)
  83. {
  84. Servers[i].ModifyEvent += SaveServers;
  85. }
  86. ConfigRWLock.ExitWriteLock();
  87. }
  88. ConfigFileRWLock.ExitReadLock();
  89. }
  90. public void UpdateServers()
  91. {
  92. ConfigFileRWLock.EnterReadLock();
  93. string ConfigPath = Path.Combine(Directory.GetCurrentDirectory(), @"Combot.Servers.config");
  94. if (File.Exists(ConfigPath))
  95. {
  96. string configContents;
  97. using (StreamReader streamReader = new StreamReader(ConfigPath, Encoding.UTF8))
  98. {
  99. configContents = streamReader.ReadToEnd();
  100. }
  101. // Load the deserialized file into the config
  102. ConfigRWLock.EnterWriteLock();
  103. List<ServerConfig> newConfigs = JsonConvert.DeserializeObject<List<ServerConfig>>(configContents, JsonSettings);
  104. for (int i = 0; i < newConfigs.Count; i++)
  105. {
  106. if (Servers.Count > i)
  107. {
  108. Servers[i].Copy(newConfigs[i]);
  109. }
  110. else
  111. {
  112. Servers.Add(newConfigs[i]);
  113. }
  114. }
  115. ConfigRWLock.ExitWriteLock();
  116. }
  117. ConfigFileRWLock.ExitReadLock();
  118. }
  119. }
  120. }