mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-06-10 15:20:48 -05:00
Added Configuration manager class
This commit is contained in:
@ -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}";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
48
SharedLibrary/Helpers/ConfigurationManager.cs
Normal file
48
SharedLibrary/Helpers/ConfigurationManager.cs
Normal 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()];
|
||||
}
|
||||
}
|
||||
}
|
@ -23,7 +23,7 @@ namespace SharedLibrary.Interfaces
|
||||
|
||||
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}");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -73,7 +73,7 @@
|
||||
<Compile Include="Exceptions\NetworkException.cs" />
|
||||
<Compile Include="Exceptions\SerializationException.cs" />
|
||||
<Compile Include="Exceptions\ServerException.cs" />
|
||||
<Compile Include="Helpers\Configuration.cs" />
|
||||
<Compile Include="Helpers\ConfigurationManager.cs" />
|
||||
<Compile Include="Interfaces\ILogger.cs" />
|
||||
<Compile Include="Interfaces\IManager.cs" />
|
||||
<Compile Include="Interfaces\IPenaltyList.cs" />
|
||||
|
Reference in New Issue
Block a user