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 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. Servers.Add(config);
  49. }
  50. ConfigRWLock.ExitWriteLock();
  51. }
  52. public void SaveServers()
  53. {
  54. ConfigFileRWLock.EnterWriteLock();
  55. // Serialize Config
  56. ConfigRWLock.EnterReadLock();
  57. string configContents = JsonConvert.SerializeObject(Servers, JsonSettings);
  58. ConfigRWLock.ExitReadLock();
  59. // Save config to file
  60. string ConfigPath = Path.Combine(Directory.GetCurrentDirectory(), @"Combot.Servers.config");
  61. using (StreamWriter streamWriter = new StreamWriter(ConfigPath, false))
  62. {
  63. streamWriter.Write(configContents);
  64. }
  65. ConfigFileRWLock.ExitWriteLock();
  66. }
  67. public void LoadServers()
  68. {
  69. ConfigFileRWLock.EnterReadLock();
  70. string ConfigPath = Path.Combine(Directory.GetCurrentDirectory(), @"Combot.Servers.config");
  71. if (File.Exists(ConfigPath))
  72. {
  73. string configContents;
  74. using (StreamReader streamReader = new StreamReader(ConfigPath, Encoding.UTF8))
  75. {
  76. configContents = streamReader.ReadToEnd();
  77. }
  78. // Load the deserialized file into the config
  79. ConfigRWLock.EnterWriteLock();
  80. Servers = JsonConvert.DeserializeObject<List<ServerConfig>>(configContents, JsonSettings);
  81. for (int i = 0; i < Servers.Count; i++)
  82. {
  83. Servers[i].ModifyEvent += SaveServers;
  84. }
  85. ConfigRWLock.ExitWriteLock();
  86. }
  87. ConfigFileRWLock.ExitReadLock();
  88. }
  89. }
  90. }