2017-06-12 20:24:12 -04:00
|
|
|
|
using System;
|
2017-09-27 16:07:43 -04:00
|
|
|
|
using System.Collections.Concurrent;
|
2017-06-12 20:24:12 -04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace SharedLibrary.Helpers
|
|
|
|
|
{
|
|
|
|
|
public class ConfigurationManager
|
|
|
|
|
{
|
2017-09-27 16:07:43 -04:00
|
|
|
|
ConcurrentDictionary<string, Dictionary<string, object>> ConfigurationSet;
|
2017-11-02 12:49:45 -04:00
|
|
|
|
ConcurrentDictionary<string, object> ConfigSet;
|
2017-06-12 20:24:12 -04:00
|
|
|
|
Type PluginType;
|
2017-11-02 12:49:45 -04:00
|
|
|
|
Server ServerInstance;
|
2017-06-12 20:24:12 -04:00
|
|
|
|
|
|
|
|
|
public ConfigurationManager(Type PluginType)
|
|
|
|
|
{
|
2017-09-27 16:07:43 -04:00
|
|
|
|
ConfigurationSet = new ConcurrentDictionary<string, Dictionary<string, object>>();
|
2017-06-12 20:24:12 -04:00
|
|
|
|
this.PluginType = PluginType;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-02 12:49:45 -04:00
|
|
|
|
public ConfigurationManager(Server S)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ConfigSet = Interfaces.Serialize<ConcurrentDictionary<string, object>>.Read($"config/Plugins_{S}.cfg");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
S.Logger.WriteInfo("ConfigurationManager could not deserialize configuration file, so initializing default config set");
|
|
|
|
|
ConfigSet = new ConcurrentDictionary<string, object>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ServerInstance = S;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SaveChanges()
|
|
|
|
|
{
|
|
|
|
|
Interfaces.Serialize<ConcurrentDictionary<string, object>>.Write($"config/Plugins_{ServerInstance}.cfg", ConfigSet);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-12 20:24:12 -04:00
|
|
|
|
public void AddConfiguration(Server S)
|
|
|
|
|
{
|
2017-09-27 16:07:43 -04:00
|
|
|
|
/* if (ConfigurationSet.ContainsKey(S.ToString()))
|
|
|
|
|
{
|
|
|
|
|
S.Logger.WriteWarning($"not adding server configuration for {S} as it already exists");
|
|
|
|
|
return;
|
|
|
|
|
}*/
|
|
|
|
|
|
2017-06-12 20:24:12 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var Config = Interfaces.Serialize<Dictionary<string, object>>.Read($"config/{PluginType.ToString()}_{S.ToString()}.cfg");
|
2017-09-27 16:07:43 -04:00
|
|
|
|
ConfigurationSet.TryAdd(S.ToString(), Config);
|
2017-06-12 20:24:12 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 18:33:47 -04:00
|
|
|
|
catch (Exceptions.SerializeException)
|
2017-06-12 20:24:12 -04:00
|
|
|
|
{
|
2017-09-27 16:07:43 -04:00
|
|
|
|
ConfigurationSet.TryAdd(S.ToString(), new Dictionary<string, object>());
|
2017-06-12 20:24:12 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddProperty(Server S, KeyValuePair<string, object> Property)
|
|
|
|
|
{
|
|
|
|
|
ConfigurationSet[S.ToString()].Add(Property.Key, Property.Value);
|
|
|
|
|
Interfaces.Serialize<Dictionary<string, object>>.Write($"config/{PluginType.ToString()}_{S.ToString()}.cfg", ConfigurationSet[S.ToString()]);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-02 12:49:45 -04:00
|
|
|
|
public void AddProperty(KeyValuePair<string, object> prop)
|
|
|
|
|
{
|
|
|
|
|
if (!ConfigSet.ContainsKey(prop.Key))
|
|
|
|
|
ConfigSet.TryAdd(prop.Key, prop.Value);
|
|
|
|
|
|
|
|
|
|
SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-12 20:24:12 -04:00
|
|
|
|
public void UpdateProperty(Server S, KeyValuePair<string, object> Property)
|
|
|
|
|
{
|
|
|
|
|
ConfigurationSet[S.ToString()][Property.Key] = Property.Value;
|
|
|
|
|
Interfaces.Serialize<Dictionary<string, object>>.Write($"config/{PluginType.ToString()}_{S.ToString()}.cfg", ConfigurationSet[S.ToString()]);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-02 12:49:45 -04:00
|
|
|
|
public void UpdateProperty(KeyValuePair<string, object> prop)
|
|
|
|
|
{
|
|
|
|
|
if (ConfigSet.ContainsKey(prop.Key))
|
|
|
|
|
ConfigSet[prop.Key] = prop.Value;
|
|
|
|
|
|
|
|
|
|
SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-12 20:24:12 -04:00
|
|
|
|
public IDictionary<string, object> GetConfiguration(Server S)
|
|
|
|
|
{
|
|
|
|
|
return ConfigurationSet[S.ToString()];
|
|
|
|
|
}
|
2017-11-02 12:49:45 -04:00
|
|
|
|
|
|
|
|
|
public object GetProperty(string prop)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return ConfigSet[prop];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-12 20:24:12 -04:00
|
|
|
|
}
|
|
|
|
|
}
|