forked from Uncled1023/Combot
Browse Source
Added save config command for bot session access. Added packages/ to git ignore. Added Json.NET to nuget package list.master
15 changed files with 522 additions and 118 deletions
@ -1,63 +0,0 @@
@@ -1,63 +0,0 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Text; |
||||
using System.Threading.Tasks; |
||||
using System.Net; |
||||
|
||||
namespace Combot |
||||
{ |
||||
public class Config |
||||
{ |
||||
private Server _server; |
||||
public Server Server |
||||
{ |
||||
get |
||||
{ |
||||
return _server; |
||||
} |
||||
|
||||
set |
||||
{ |
||||
if (value != _server) |
||||
{ |
||||
_server = value; |
||||
} |
||||
} |
||||
} |
||||
|
||||
private string _realname; |
||||
public string Realname |
||||
{ |
||||
get |
||||
{ |
||||
return _realname; |
||||
} |
||||
|
||||
set |
||||
{ |
||||
if (value != _realname) |
||||
{ |
||||
_realname = value; |
||||
} |
||||
} |
||||
} |
||||
|
||||
private string _nick; |
||||
public string Nick |
||||
{ |
||||
get |
||||
{ |
||||
return _nick; |
||||
} |
||||
|
||||
set |
||||
{ |
||||
if (value != _nick) |
||||
{ |
||||
_nick = value; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,51 @@
@@ -0,0 +1,51 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Text; |
||||
using System.Threading.Tasks; |
||||
|
||||
namespace Combot.Configurations |
||||
{ |
||||
public class ChannelConfig |
||||
{ |
||||
public ChannelConfig() |
||||
{ |
||||
Name = string.Empty; |
||||
Key = string.Empty; |
||||
} |
||||
|
||||
private string _Name; |
||||
public string Name |
||||
{ |
||||
get |
||||
{ |
||||
return _Name; |
||||
} |
||||
|
||||
set |
||||
{ |
||||
if (_Name != value) |
||||
{ |
||||
_Name = value; |
||||
} |
||||
} |
||||
} |
||||
|
||||
private string _Key; |
||||
public string Key |
||||
{ |
||||
get |
||||
{ |
||||
return _Key; |
||||
} |
||||
|
||||
set |
||||
{ |
||||
if (_Key != value) |
||||
{ |
||||
_Key = value; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,102 @@
@@ -0,0 +1,102 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Text; |
||||
using System.Threading; |
||||
using System.Threading.Tasks; |
||||
using System.Net; |
||||
using System.IO; |
||||
using Newtonsoft.Json; |
||||
|
||||
namespace Combot.Configurations |
||||
{ |
||||
public class Config |
||||
{ |
||||
private ReaderWriterLockSlim ConfigRWLock; |
||||
private ReaderWriterLockSlim ConfigFileRWLock; |
||||
private JsonSerializerSettings JsonSettings; |
||||
|
||||
public Config() |
||||
{ |
||||
ConfigRWLock = new ReaderWriterLockSlim(); |
||||
ConfigFileRWLock = new ReaderWriterLockSlim(); |
||||
Servers = new List<ServerConfig>(); |
||||
JsonSettings = new JsonSerializerSettings(); |
||||
JsonSettings.Converters.Add(new IPAddressConverter()); |
||||
JsonSettings.Converters.Add(new IPEndPointConverter()); |
||||
JsonSettings.Formatting = Formatting.Indented; |
||||
} |
||||
|
||||
private List<ServerConfig> _servers; |
||||
public List<ServerConfig> Servers |
||||
{ |
||||
get |
||||
{ |
||||
return _servers; |
||||
} |
||||
|
||||
private set |
||||
{ |
||||
if (value != _servers) |
||||
{ |
||||
_servers = value; |
||||
} |
||||
} |
||||
} |
||||
|
||||
public void AddServer(ServerConfig config) |
||||
{ |
||||
ConfigRWLock.EnterWriteLock(); |
||||
if (!Servers.Exists(server => server.Name == config.Name)) |
||||
{ |
||||
config.ModifyEvent += SaveServers; |
||||
Servers.Add(config); |
||||
} |
||||
ConfigRWLock.ExitWriteLock(); |
||||
} |
||||
|
||||
public void SaveServers() |
||||
{ |
||||
ConfigFileRWLock.EnterWriteLock(); |
||||
|
||||
// Serialize Config
|
||||
ConfigRWLock.EnterReadLock(); |
||||
string configContents = JsonConvert.SerializeObject(Servers, JsonSettings); |
||||
ConfigRWLock.ExitReadLock(); |
||||
|
||||
// Save config to file
|
||||
string ConfigPath = Path.Combine(Directory.GetCurrentDirectory(), @"Combot.Servers.config"); |
||||
using (StreamWriter streamWriter = new StreamWriter(ConfigPath, false)) |
||||
{ |
||||
streamWriter.Write(configContents); |
||||
} |
||||
|
||||
ConfigFileRWLock.ExitWriteLock(); |
||||
} |
||||
|
||||
public void LoadServers() |
||||
{ |
||||
ConfigFileRWLock.EnterReadLock(); |
||||
string ConfigPath = Path.Combine(Directory.GetCurrentDirectory(), @"Combot.Servers.config"); |
||||
if (File.Exists(ConfigPath)) |
||||
{ |
||||
string configContents; |
||||
using (StreamReader streamReader = new StreamReader(ConfigPath, Encoding.UTF8)) |
||||
{ |
||||
configContents = streamReader.ReadToEnd(); |
||||
} |
||||
|
||||
// Load the deserialized file into the config
|
||||
ConfigRWLock.EnterWriteLock(); |
||||
Servers = JsonConvert.DeserializeObject<List<ServerConfig>>(configContents, JsonSettings); |
||||
|
||||
for (int i = 0; i < Servers.Count; i++) |
||||
{ |
||||
Servers[i].ModifyEvent += SaveServers; |
||||
} |
||||
ConfigRWLock.ExitWriteLock(); |
||||
} |
||||
ConfigFileRWLock.ExitReadLock(); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,51 @@
@@ -0,0 +1,51 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Text; |
||||
using System.Threading.Tasks; |
||||
|
||||
namespace Combot.Configurations |
||||
{ |
||||
public class HostConfig |
||||
{ |
||||
public HostConfig() |
||||
{ |
||||
Host = string.Empty; |
||||
Port = 0; |
||||
} |
||||
|
||||
private string _Host; |
||||
public string Host |
||||
{ |
||||
get |
||||
{ |
||||
return _Host; |
||||
} |
||||
|
||||
set |
||||
{ |
||||
if (_Host != value) |
||||
{ |
||||
_Host = value; |
||||
} |
||||
} |
||||
} |
||||
|
||||
private int _Port; |
||||
public int Port |
||||
{ |
||||
get |
||||
{ |
||||
return _Port; |
||||
} |
||||
|
||||
set |
||||
{ |
||||
if (_Port != value) |
||||
{ |
||||
_Port = value; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,58 @@
@@ -0,0 +1,58 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Text; |
||||
using System.Threading.Tasks; |
||||
using System.Net; |
||||
using Newtonsoft.Json; |
||||
using Newtonsoft.Json.Linq; |
||||
|
||||
namespace Combot.Configurations |
||||
{ |
||||
public class IPAddressConverter : JsonConverter |
||||
{ |
||||
public override bool CanConvert(Type objectType) |
||||
{ |
||||
return (objectType == typeof(IPAddress)); |
||||
} |
||||
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) |
||||
{ |
||||
IPAddress ip = (IPAddress)value; |
||||
writer.WriteValue(ip.ToString()); |
||||
} |
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) |
||||
{ |
||||
JToken token = JToken.Load(reader); |
||||
return IPAddress.Parse(token.Value<string>()); |
||||
} |
||||
} |
||||
|
||||
public class IPEndPointConverter : JsonConverter |
||||
{ |
||||
public override bool CanConvert(Type objectType) |
||||
{ |
||||
return (objectType == typeof(IPEndPoint)); |
||||
} |
||||
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) |
||||
{ |
||||
IPEndPoint ep = (IPEndPoint)value; |
||||
writer.WriteStartObject(); |
||||
writer.WritePropertyName("Address"); |
||||
serializer.Serialize(writer, ep.Address); |
||||
writer.WritePropertyName("Port"); |
||||
writer.WriteValue(ep.Port); |
||||
writer.WriteEndObject(); |
||||
} |
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) |
||||
{ |
||||
JObject jo = JObject.Load(reader); |
||||
IPAddress address = jo["Address"].ToObject<IPAddress>(serializer); |
||||
int port = jo["Port"].Value<int>(); |
||||
return new IPEndPoint(address, port); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,151 @@
@@ -0,0 +1,151 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Text; |
||||
using System.Net; |
||||
using System.Threading.Tasks; |
||||
|
||||
namespace Combot.Configurations |
||||
{ |
||||
public class ServerConfig |
||||
{ |
||||
public event Action ModifyEvent; |
||||
|
||||
public ServerConfig() |
||||
{ |
||||
AutoConnect = false; |
||||
Channels = new List<ChannelConfig>(); |
||||
Hosts = new List<HostConfig>(); |
||||
Nickname = string.Empty; |
||||
Realname = string.Empty; |
||||
Username = string.Empty; |
||||
} |
||||
|
||||
public void Save() |
||||
{ |
||||
if (ModifyEvent != null) |
||||
{ |
||||
ModifyEvent(); |
||||
} |
||||
} |
||||
|
||||
private string _Name; |
||||
public string Name |
||||
{ |
||||
get |
||||
{ |
||||
return _Name; |
||||
} |
||||
|
||||
set |
||||
{ |
||||
if (_Name != value) |
||||
{ |
||||
_Name = value; |
||||
} |
||||
} |
||||
} |
||||
|
||||
private string _Nickname; |
||||
public string Nickname |
||||
{ |
||||
get |
||||
{ |
||||
return _Nickname; |
||||
} |
||||
|
||||
set |
||||
{ |
||||
if (_Nickname != value) |
||||
{ |
||||
_Nickname = value; |
||||
} |
||||
} |
||||
} |
||||
|
||||
private string _Username; |
||||
public string Username |
||||
{ |
||||
get |
||||
{ |
||||
return _Username; |
||||
} |
||||
|
||||
set |
||||
{ |
||||
if (_Username != value) |
||||
{ |
||||
_Username = value; |
||||
} |
||||
} |
||||
} |
||||
|
||||
private string _Realname; |
||||
public string Realname |
||||
{ |
||||
get |
||||
{ |
||||
return _Realname; |
||||
} |
||||
|
||||
set |
||||
{ |
||||
if (_Realname != value) |
||||
{ |
||||
_Realname = value; |
||||
} |
||||
} |
||||
} |
||||
|
||||
private List<HostConfig> _Hosts; |
||||
public List<HostConfig> Hosts |
||||
{ |
||||
get |
||||
{ |
||||
return _Hosts; |
||||
} |
||||
|
||||
set |
||||
{ |
||||
if (_Hosts != value) |
||||
{ |
||||
_Hosts = value; |
||||
} |
||||
} |
||||
} |
||||
|
||||
private List<ChannelConfig> _Channels; |
||||
public List<ChannelConfig> Channels |
||||
{ |
||||
get |
||||
{ |
||||
return _Channels; |
||||
} |
||||
|
||||
set |
||||
{ |
||||
if (_Channels != value) |
||||
{ |
||||
_Channels = value; |
||||
} |
||||
} |
||||
} |
||||
|
||||
private bool _AutoConnect; |
||||
public bool AutoConnect |
||||
{ |
||||
get |
||||
{ |
||||
return _AutoConnect; |
||||
} |
||||
|
||||
set |
||||
{ |
||||
if (_AutoConnect != value) |
||||
{ |
||||
_AutoConnect = value; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,4 @@
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<packages> |
||||
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net451" /> |
||||
</packages> |
Loading…
Reference in new issue