Removed old black/whitelist implementations and laid groundwork for new implementation
This commit is contained in:
parent
ba8f473d20
commit
b7aa5f22df
12
TrueCraft.API/IAccessConfiguration.cs
Normal file
12
TrueCraft.API/IAccessConfiguration.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using System.Collections.Generic;
|
||||
using TrueCraft.API.Networking;
|
||||
|
||||
namespace TrueCraft.API
|
||||
{
|
||||
public interface IAccessConfiguration
|
||||
{
|
||||
IList<string> Blacklist { get; }
|
||||
IList<string> Whitelist { get; }
|
||||
IList<string> Oplist { get; }
|
||||
}
|
||||
}
|
@ -33,6 +33,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Coordinates3D.cs" />
|
||||
<Compile Include="IAccessConfiguration.cs" />
|
||||
<Compile Include="IPlayerList.cs" />
|
||||
<Compile Include="PlantSpecies.cs" />
|
||||
<Compile Include="OreTypes.cs" />
|
||||
|
40
TrueCraft/AccessConfiguration.cs
Normal file
40
TrueCraft/AccessConfiguration.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using TrueCraft.API;
|
||||
using TrueCraft.API.Networking;
|
||||
using TrueCraft.API.Server;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace TrueCraft
|
||||
{
|
||||
public class AccessConfiguration : IAccessConfiguration
|
||||
{
|
||||
private IList<string> _blacklist, _whitelist, _oplist;
|
||||
|
||||
public AccessConfiguration()
|
||||
{
|
||||
_blacklist = new List<string>();
|
||||
_whitelist = new List<string>();
|
||||
_oplist = new List<string>();
|
||||
}
|
||||
|
||||
[YamlMember(Alias = "blacklist")]
|
||||
public IList<string> Blacklist
|
||||
{
|
||||
get { return _blacklist; }
|
||||
}
|
||||
|
||||
[YamlMember(Alias = "whitelist")]
|
||||
public IList<string> Whitelist
|
||||
{
|
||||
get { return _whitelist; }
|
||||
}
|
||||
|
||||
[YamlMember(Alias = "ops")]
|
||||
public IList<string> Oplist
|
||||
{
|
||||
get { return _oplist; }
|
||||
}
|
||||
}
|
||||
}
|
@ -1,69 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using TrueCraft.Api;
|
||||
using TrueCraft.API.Logging;
|
||||
using TrueCraft.API.Server;
|
||||
|
||||
namespace TrueCraft
|
||||
{
|
||||
public class PlayerBlackList : PlayerList
|
||||
{
|
||||
public PlayerBlackList(IMultiplayerServer server)
|
||||
: base(server)
|
||||
{
|
||||
ConfigFileName = @"blacklist.txt";
|
||||
}
|
||||
}
|
||||
|
||||
public class PlayerWhiteList : PlayerList
|
||||
{
|
||||
public PlayerWhiteList(IMultiplayerServer server)
|
||||
: base(server)
|
||||
{
|
||||
ConfigFileName = @"whitelist.txt";
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class PlayerList : IPlayerList
|
||||
{
|
||||
private IList<string> _players;
|
||||
|
||||
protected PlayerList(IMultiplayerServer server)
|
||||
{
|
||||
Server = server;
|
||||
}
|
||||
|
||||
public string ConfigFileName { get; set; }
|
||||
public IMultiplayerServer Server { get; set; }
|
||||
public IList<string> Players
|
||||
{
|
||||
get { return _players ?? (_players = LoadPlayersFromFile(ConfigFileName)); }
|
||||
}
|
||||
|
||||
private IList<string> LoadPlayersFromFile(string configFileName)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!File.Exists(configFileName))
|
||||
File.Create(configFileName).Close();
|
||||
|
||||
using (var sr = new StreamReader(configFileName))
|
||||
{
|
||||
return sr.ReadToEnd().Replace("\r", "").Split('\n').ToList();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Server.Log(LogCategory.Error, "The player list file could not be read: " + e.Message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format("Players: {0}", _players);
|
||||
}
|
||||
}
|
||||
}
|
@ -17,6 +17,7 @@ namespace TrueCraft
|
||||
public class Program
|
||||
{
|
||||
public static Configuration Configuration;
|
||||
public static AccessConfiguration AccessConfiguration;
|
||||
|
||||
public static CommandManager CommandManager;
|
||||
|
||||
@ -24,17 +25,8 @@ namespace TrueCraft
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
if (File.Exists("config.yaml"))
|
||||
{
|
||||
var deserializer = new Deserializer(ignoreUnmatched: true);
|
||||
using (var file = File.OpenText("config.yaml"))
|
||||
Configuration = deserializer.Deserialize<Configuration>(file);
|
||||
}
|
||||
else
|
||||
Configuration = new Configuration();
|
||||
var serializer = new Serializer();
|
||||
using (var writer = new StreamWriter("config.yaml"))
|
||||
serializer.Serialize(writer, Configuration);
|
||||
Configuration = LoadConfiguration<Configuration>("config.yaml");
|
||||
AccessConfiguration = LoadConfiguration<AccessConfiguration>("access.yaml");
|
||||
|
||||
Server = new MultiplayerServer();
|
||||
Server.AddLogProvider(new ConsoleLogProvider(LogCategory.Notice | LogCategory.Warning | LogCategory.Error | LogCategory.Debug));
|
||||
@ -111,6 +103,25 @@ namespace TrueCraft
|
||||
}
|
||||
}
|
||||
|
||||
private static T LoadConfiguration<T>(string configFileName) where T : new()
|
||||
{
|
||||
T config;
|
||||
|
||||
if (File.Exists(configFileName))
|
||||
{
|
||||
var deserializer = new Deserializer(ignoreUnmatched: true);
|
||||
using (var file = File.OpenText("config.yaml"))
|
||||
config = deserializer.Deserialize<T>(file);
|
||||
}
|
||||
else
|
||||
config = new T();
|
||||
var serializer = new Serializer();
|
||||
using (var writer = new StreamWriter("config.yaml"))
|
||||
serializer.Serialize(writer, config);
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
static void HandleCancelKeyPress(object sender, ConsoleCancelEventArgs e)
|
||||
{
|
||||
Server.Stop();
|
||||
|
@ -40,7 +40,7 @@
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="BlackWhiteList.cs" />
|
||||
<Compile Include="AccessConfiguration.cs" />
|
||||
<Compile Include="Commands\Command.cs" />
|
||||
<Compile Include="Commands\CommandManager.cs" />
|
||||
<Compile Include="Commands\GiveCommand.cs" />
|
||||
|
Reference in New Issue
Block a user