2018-02-11 20:17:20 -05:00
|
|
|
|
using Microsoft.CSharp.RuntimeBinder;
|
|
|
|
|
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
|
|
|
|
|
{
|
2018-02-10 23:33:42 -05:00
|
|
|
|
ConcurrentDictionary<string, dynamic> ConfigSet;
|
2017-11-02 12:49:45 -04:00
|
|
|
|
Server ServerInstance;
|
2017-06-12 20:24:12 -04:00
|
|
|
|
|
2017-11-02 12:49:45 -04:00
|
|
|
|
public ConfigurationManager(Server S)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-02-10 23:33:42 -05:00
|
|
|
|
ConfigSet = Interfaces.Serialize<ConcurrentDictionary<string, dynamic>>.Read($"config/plugins_{S.ToString()}.cfg");
|
2017-11-02 12:49:45 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
S.Logger.WriteInfo("ConfigurationManager could not deserialize configuration file, so initializing default config set");
|
2018-02-10 23:33:42 -05:00
|
|
|
|
ConfigSet = new ConcurrentDictionary<string, dynamic>();
|
2017-11-02 12:49:45 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ServerInstance = S;
|
2018-02-10 23:33:42 -05:00
|
|
|
|
SaveChanges();
|
2017-11-02 12:49:45 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SaveChanges()
|
|
|
|
|
{
|
2018-02-10 23:33:42 -05:00
|
|
|
|
Interfaces.Serialize<ConcurrentDictionary<string, dynamic>>.Write($"config/plugins_{ServerInstance.ToString()}.cfg", ConfigSet);
|
2017-11-02 12:49:45 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-10 23:33:42 -05:00
|
|
|
|
public void AddProperty(KeyValuePair<string, dynamic> prop)
|
2017-11-02 12:49:45 -04:00
|
|
|
|
{
|
|
|
|
|
if (!ConfigSet.ContainsKey(prop.Key))
|
|
|
|
|
ConfigSet.TryAdd(prop.Key, prop.Value);
|
|
|
|
|
|
|
|
|
|
SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-10 23:33:42 -05:00
|
|
|
|
public void UpdateProperty(KeyValuePair<string, dynamic> prop)
|
2017-11-02 12:49:45 -04:00
|
|
|
|
{
|
|
|
|
|
if (ConfigSet.ContainsKey(prop.Key))
|
|
|
|
|
ConfigSet[prop.Key] = prop.Value;
|
|
|
|
|
|
|
|
|
|
SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-10 23:33:42 -05:00
|
|
|
|
public T GetProperty<T>(string prop)
|
2017-11-02 12:49:45 -04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-02-10 23:33:42 -05:00
|
|
|
|
return ConfigSet[prop].ToObject<T>();
|
2017-11-02 12:49:45 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-11 20:17:20 -05:00
|
|
|
|
catch (RuntimeBinderException)
|
|
|
|
|
{
|
|
|
|
|
return ConfigSet[prop];
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-02 12:49:45 -04:00
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
2018-02-10 23:33:42 -05:00
|
|
|
|
return default(T);
|
2017-11-02 12:49:45 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-12 20:24:12 -04:00
|
|
|
|
}
|
|
|
|
|
}
|