Added Configuration manager class

This commit is contained in:
RaidMax 2017-06-12 19:24:12 -05:00
parent 5d1c9bd218
commit 45cb985701
8 changed files with 68 additions and 68 deletions

View File

@ -75,6 +75,9 @@ namespace IW4MAdmin
{ {
try try
{ {
foreach (var Plugin in SharedLibrary.Plugins.PluginImporter.ActivePlugins)
await Plugin.OnLoadAsync(ServerInstance);
await ServerInstance.Initialize(); await ServerInstance.Initialize();
Servers.Add(ServerInstance); Servers.Add(ServerInstance);
@ -82,9 +85,6 @@ namespace IW4MAdmin
var Status = new SharedLibrary.Helpers.AsyncStatus(ServerInstance, UPDATE_FREQUENCY); var Status = new SharedLibrary.Helpers.AsyncStatus(ServerInstance, UPDATE_FREQUENCY);
TaskStatuses.Add(Status); TaskStatuses.Add(Status);
foreach (var Plugin in SharedLibrary.Plugins.PluginImporter.ActivePlugins)
await Plugin.OnLoadAsync(ServerInstance);
Logger.WriteVerbose($"Now monitoring {ServerInstance.Hostname}"); Logger.WriteVerbose($"Now monitoring {ServerInstance.Hostname}");
} }

Binary file not shown.

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Collections.Generic;
using SharedLibrary; using SharedLibrary;
using SharedLibrary.Interfaces; using SharedLibrary.Interfaces;
@ -19,11 +20,8 @@ namespace Plugin
public override async Task ExecuteAsync(Event E) public override async Task ExecuteAsync(Event E)
{ {
var Config = new FastRestartConfig() { Enabled = true }; FastRestartPlugin.ConfigManager.UpdateProperty(E.Owner, new KeyValuePair<string, object>("Enabled", true));
if (!new Configuration<FastRestartConfig>(E.Owner).Write(Config)) await E.Origin.Tell("Fast restarting is now enabled for this server");
await E.Origin.Tell("Failed to save the configuration file for fast restart");
else
await E.Origin.Tell("Fast restarting is now enabled for this server");
} }
} }
@ -33,11 +31,8 @@ namespace Plugin
public override async Task ExecuteAsync(Event E) public override async Task ExecuteAsync(Event E)
{ {
var Config = new FastRestartConfig() { Enabled = false }; FastRestartPlugin.ConfigManager.UpdateProperty(E.Owner, new KeyValuePair<string, object>("Enabled", false));
if (!new Configuration<FastRestartConfig>(E.Owner).Write(Config)) await E.Origin.Tell("Fast restarting is now disabled for this server");
await E.Origin.Tell("Failed to save the configuration file for fast restart");
else
await E.Origin.Tell("Fast restarting is now disabled for this server");
} }
} }
@ -46,6 +41,8 @@ namespace Plugin
bool MatchEnded; bool MatchEnded;
DateTime MatchEndTime; DateTime MatchEndTime;
public static ConfigurationManager ConfigManager { get; private set; }
public string Name { get { return "Fast Restarter"; } } public string Name { get { return "Fast Restarter"; } }
public float Version { get { return 1.0f; } } public float Version { get { return 1.0f; } }
@ -70,13 +67,16 @@ namespace Plugin
public async Task OnLoadAsync(Server S) public async Task OnLoadAsync(Server S)
{ {
// this initializes the file if it doesn't exist already ConfigManager = new ConfigurationManager(typeof(FastRestartPlugin));
new Configuration<FastRestartConfig>(S).Read(); ConfigManager.AddConfiguration(S);
if (ConfigManager.GetConfiguration(S).Keys.Count == 0)
ConfigManager.AddProperty(S, new KeyValuePair<string, object>("Enabled", false));
} }
public async Task OnTickAsync(Server S) public async Task OnTickAsync(Server S)
{ {
if (!new Configuration<FastRestartConfig>(S).Read().Enabled) if ((bool)ConfigManager.GetConfiguration(S)["Enabled"] == false)
return; return;
MatchEnded = (await S.GetDvarAsync<int>("scr_gameended")).Value == 1; MatchEnded = (await S.GetDvarAsync<int>("scr_gameended")).Value == 1;

View File

@ -222,6 +222,7 @@ namespace Votemap_Plugin
/// <param name="S"></param> /// <param name="S"></param>
public async Task OnTickAsync(Server S) public async Task OnTickAsync(Server S)
{ {
return;
var serverVotes = getServerVotes(S.GetPort()); var serverVotes = getServerVotes(S.GetPort());
if (serverVotes != null) if (serverVotes != null)
@ -280,6 +281,7 @@ namespace Votemap_Plugin
public async Task OnEventAsync(Event E, Server S) public async Task OnEventAsync(Event E, Server S)
{ {
return;
if (E.Type == Event.GType.Start) if (E.Type == Event.GType.Start)
{ {
serverVotingList.Add(new ServerVoting(S.GetPort())); serverVotingList.Add(new ServerVoting(S.GetPort()));

View File

@ -1,50 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SharedLibrary.Helpers
{
public class Configuration<T> : Interfaces.Serialize<T>
{
string FilePostfix;
public Configuration(Server S)
{
FilePostfix = $"_{S.GetIP()}_{S.GetPort()}.cfg";
}
public T Read()
{
try
{
return Read();
}
catch (Exceptions.SerializeException)
{
return default(T);
}
}
public bool Write(T Data)
{
try
{
Write(Filename(), Data);
return true;
}
catch(Exceptions.SerializeException)
{
return false;
}
}
public override string Filename()
{
return $"config/{typeof(T).ToString()}_{FilePostfix}";
}
}
}

View File

@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
namespace SharedLibrary.Helpers
{
public class ConfigurationManager
{
Dictionary<string, Dictionary<string, object>> ConfigurationSet;
Type PluginType;
public ConfigurationManager(Type PluginType)
{
ConfigurationSet = new Dictionary<string, Dictionary<string, object>>();
this.PluginType = PluginType;
}
public void AddConfiguration(Server S)
{
try
{
var Config = Interfaces.Serialize<Dictionary<string, object>>.Read($"config/{PluginType.ToString()}_{S.ToString()}.cfg");
ConfigurationSet.Add(S.ToString(), Config);
}
catch(Exceptions.SerializeException)
{
ConfigurationSet.Add(S.ToString(), new Dictionary<string, object>());
}
}
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()];
}
}
}

View File

@ -23,7 +23,7 @@ namespace SharedLibrary.Interfaces
catch (Exception e) catch (Exception e)
{ {
throw new Exceptions.SerializeException($"Could not desialize file {filename}: {e.Message}"); throw new Exceptions.SerializeException($"Could not deserialize file {filename}: {e.Message}");
} }
} }

View File

@ -73,7 +73,7 @@
<Compile Include="Exceptions\NetworkException.cs" /> <Compile Include="Exceptions\NetworkException.cs" />
<Compile Include="Exceptions\SerializationException.cs" /> <Compile Include="Exceptions\SerializationException.cs" />
<Compile Include="Exceptions\ServerException.cs" /> <Compile Include="Exceptions\ServerException.cs" />
<Compile Include="Helpers\Configuration.cs" /> <Compile Include="Helpers\ConfigurationManager.cs" />
<Compile Include="Interfaces\ILogger.cs" /> <Compile Include="Interfaces\ILogger.cs" />
<Compile Include="Interfaces\IManager.cs" /> <Compile Include="Interfaces\IManager.cs" />
<Compile Include="Interfaces\IPenaltyList.cs" /> <Compile Include="Interfaces\IPenaltyList.cs" />