Moved config loading to parent class of Serverconfig and Accessconfig
This commit is contained in:
parent
7d16f0d835
commit
c62b9cd079
30
TrueCraft.API/Configuration.cs
Normal file
30
TrueCraft.API/Configuration.cs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
using System.IO;
|
||||||
|
using YamlDotNet.Serialization;
|
||||||
|
|
||||||
|
namespace TrueCraft.API
|
||||||
|
{
|
||||||
|
public abstract class Configuration
|
||||||
|
{
|
||||||
|
public 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(configFileName))
|
||||||
|
config = deserializer.Deserialize<T>(file);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
config = new T();
|
||||||
|
}
|
||||||
|
|
||||||
|
var serializer = new Serializer();
|
||||||
|
using (var writer = new StreamWriter(configFileName))
|
||||||
|
serializer.Serialize(writer, config);
|
||||||
|
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -30,8 +30,12 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
|
<Reference Include="YamlDotNet">
|
||||||
|
<HintPath>..\packages\YamlDotNet.3.5.1\lib\net35\YamlDotNet.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="Configuration.cs" />
|
||||||
<Compile Include="Coordinates3D.cs" />
|
<Compile Include="Coordinates3D.cs" />
|
||||||
<Compile Include="IAccessConfiguration.cs" />
|
<Compile Include="IAccessConfiguration.cs" />
|
||||||
<Compile Include="PlantSpecies.cs" />
|
<Compile Include="PlantSpecies.cs" />
|
||||||
|
@ -4,7 +4,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace TrueCraft
|
namespace TrueCraft
|
||||||
{
|
{
|
||||||
public class AccessConfiguration : IAccessConfiguration
|
public class AccessConfiguration : Configuration, IAccessConfiguration
|
||||||
{
|
{
|
||||||
public AccessConfiguration()
|
public AccessConfiguration()
|
||||||
{
|
{
|
||||||
|
@ -58,8 +58,8 @@ namespace TrueCraft.Handlers
|
|||||||
server.Scheduler.ScheduleEvent(DateTime.Now.AddSeconds(10), remoteClient.SendKeepAlive);
|
server.Scheduler.ScheduleEvent(DateTime.Now.AddSeconds(10), remoteClient.SendKeepAlive);
|
||||||
server.Scheduler.ScheduleEvent(DateTime.Now.AddSeconds(1), remoteClient.ExpandChunkRadius);
|
server.Scheduler.ScheduleEvent(DateTime.Now.AddSeconds(1), remoteClient.ExpandChunkRadius);
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(Program.Configuration.MOTD))
|
if (!string.IsNullOrEmpty(Program.ServerConfiguration.MOTD))
|
||||||
remoteClient.SendMessage(Program.Configuration.MOTD);
|
remoteClient.SendMessage(Program.ServerConfiguration.MOTD);
|
||||||
server.SendMessage(ChatColor.Yellow + "{0} joined the server.", remoteClient.Username);
|
server.SendMessage(ChatColor.Yellow + "{0} joined the server.", remoteClient.Username);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -90,6 +90,8 @@ namespace TrueCraft
|
|||||||
PendingBlockUpdates = new Queue<BlockUpdate>();
|
PendingBlockUpdates = new Queue<BlockUpdate>();
|
||||||
EnableClientLogging = false;
|
EnableClientLogging = false;
|
||||||
|
|
||||||
|
AccessConfiguration = Configuration.LoadConfiguration<AccessConfiguration>("access.yaml");
|
||||||
|
|
||||||
reader.RegisterCorePackets();
|
reader.RegisterCorePackets();
|
||||||
Handlers.PacketHandlers.RegisterHandlers(this);
|
Handlers.PacketHandlers.RegisterHandlers(this);
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ namespace TrueCraft
|
|||||||
{
|
{
|
||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
public static Configuration Configuration;
|
public static ServerConfiguration ServerConfiguration;
|
||||||
|
|
||||||
public static CommandManager CommandManager;
|
public static CommandManager CommandManager;
|
||||||
|
|
||||||
@ -31,15 +31,14 @@ namespace TrueCraft
|
|||||||
Server.AddLogProvider(new FileLogProvider(new StreamWriter("packets.log", false), LogCategory.Packets));
|
Server.AddLogProvider(new FileLogProvider(new StreamWriter("packets.log", false), LogCategory.Packets));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Configuration = LoadConfiguration<Configuration>("config.yaml");
|
ServerConfiguration = Configuration.LoadConfiguration<ServerConfiguration>("config.yaml");
|
||||||
Server.AccessConfiguration = LoadConfiguration<AccessConfiguration>("access.yaml");
|
|
||||||
|
|
||||||
if (Configuration.Debug.DeleteWorldOnStartup)
|
if (ServerConfiguration.Debug.DeleteWorldOnStartup)
|
||||||
{
|
{
|
||||||
if (Directory.Exists("world"))
|
if (Directory.Exists("world"))
|
||||||
Directory.Delete("world", true);
|
Directory.Delete("world", true);
|
||||||
}
|
}
|
||||||
if (Configuration.Debug.DeletePlayersOnStartup)
|
if (ServerConfiguration.Debug.DeletePlayersOnStartup)
|
||||||
{
|
{
|
||||||
if (Directory.Exists("players"))
|
if (Directory.Exists("players"))
|
||||||
Directory.Delete("players", true);
|
Directory.Delete("players", true);
|
||||||
@ -92,11 +91,11 @@ namespace TrueCraft
|
|||||||
}
|
}
|
||||||
CommandManager = new CommandManager();
|
CommandManager = new CommandManager();
|
||||||
Server.ChatMessageReceived += HandleChatMessageReceived;
|
Server.ChatMessageReceived += HandleChatMessageReceived;
|
||||||
Server.Start(new IPEndPoint(IPAddress.Parse(Configuration.ServerAddress), Configuration.ServerPort));
|
Server.Start(new IPEndPoint(IPAddress.Parse(ServerConfiguration.ServerAddress), ServerConfiguration.ServerPort));
|
||||||
Console.CancelKeyPress += HandleCancelKeyPress;
|
Console.CancelKeyPress += HandleCancelKeyPress;
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
Thread.Sleep(1000 * Configuration.WorldSaveInterval);
|
Thread.Sleep(1000 * ServerConfiguration.WorldSaveInterval);
|
||||||
foreach (var w in Server.Worlds)
|
foreach (var w in Server.Worlds)
|
||||||
{
|
{
|
||||||
w.Save();
|
w.Save();
|
||||||
@ -104,30 +103,6 @@ namespace TrueCraft
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static T LoadConfiguration<T>(string configFileName) where T : new()
|
|
||||||
{
|
|
||||||
T config;
|
|
||||||
|
|
||||||
if (File.Exists(configFileName))
|
|
||||||
{
|
|
||||||
Server.Log(LogCategory.Warning, configFileName + @" already exists, reading it right now...");
|
|
||||||
var deserializer = new Deserializer(ignoreUnmatched: true);
|
|
||||||
using (var file = File.OpenText(configFileName))
|
|
||||||
config = deserializer.Deserialize<T>(file);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Server.Log(LogCategory.Warning, configFileName + @" didn't exist yet, creating new one");
|
|
||||||
config = new T();
|
|
||||||
}
|
|
||||||
|
|
||||||
var serializer = new Serializer();
|
|
||||||
using (var writer = new StreamWriter(configFileName))
|
|
||||||
serializer.Serialize(writer, config);
|
|
||||||
|
|
||||||
return config;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void HandleCancelKeyPress(object sender, ConsoleCancelEventArgs e)
|
static void HandleCancelKeyPress(object sender, ConsoleCancelEventArgs e)
|
||||||
{
|
{
|
||||||
Server.Stop();
|
Server.Stop();
|
||||||
|
@ -3,7 +3,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace TrueCraft
|
namespace TrueCraft
|
||||||
{
|
{
|
||||||
public class Configuration
|
public class ServerConfiguration : Configuration
|
||||||
{
|
{
|
||||||
public class DebugConfiguration
|
public class DebugConfiguration
|
||||||
{
|
{
|
||||||
@ -20,7 +20,7 @@ namespace TrueCraft
|
|||||||
public bool DeletePlayersOnStartup { get; set; }
|
public bool DeletePlayersOnStartup { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public Configuration()
|
public ServerConfiguration()
|
||||||
{
|
{
|
||||||
MOTD = ChatColor.Red + "Welcome to TrueCraft!";
|
MOTD = ChatColor.Red + "Welcome to TrueCraft!";
|
||||||
Debug = new DebugConfiguration();
|
Debug = new DebugConfiguration();
|
@ -63,7 +63,7 @@
|
|||||||
<Compile Include="Exceptions\PlayerDisconnectException.cs" />
|
<Compile Include="Exceptions\PlayerDisconnectException.cs" />
|
||||||
<Compile Include="ItemRepository.cs" />
|
<Compile Include="ItemRepository.cs" />
|
||||||
<Compile Include="CraftingRepository.cs" />
|
<Compile Include="CraftingRepository.cs" />
|
||||||
<Compile Include="Configuration.cs" />
|
<Compile Include="ServerConfiguration.cs" />
|
||||||
<Compile Include="PhysicsEngine.cs" />
|
<Compile Include="PhysicsEngine.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||||
|
Reference in New Issue
Block a user