IW4M-Admin/SharedLibrary/Helpers/ConfigurationManager.cs

56 lines
2.0 KiB
C#
Raw Normal View History

2017-06-12 20:24:12 -04:00
using System;
using System.Collections.Concurrent;
2017-06-12 20:24:12 -04:00
using System.Collections.Generic;
namespace SharedLibrary.Helpers
{
public class ConfigurationManager
{
ConcurrentDictionary<string, Dictionary<string, object>> ConfigurationSet;
2017-06-12 20:24:12 -04:00
Type PluginType;
public ConfigurationManager(Type PluginType)
{
ConfigurationSet = new ConcurrentDictionary<string, Dictionary<string, object>>();
2017-06-12 20:24:12 -04:00
this.PluginType = PluginType;
}
public void AddConfiguration(Server S)
{
/* 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");
ConfigurationSet.TryAdd(S.ToString(), Config);
2017-06-12 20:24:12 -04:00
}
catch (Exceptions.SerializeException)
2017-06-12 20:24:12 -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()]);
}
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()]);
}
public IDictionary<string, object> GetConfiguration(Server S)
{
return ConfigurationSet[S.ToString()];
}
}
}