Add config for server.

This commit is contained in:
Ray Koopa 2019-01-04 19:14:55 +01:00
parent 1d02d3834b
commit 1a5bbe714f
8 changed files with 90 additions and 33 deletions

View File

@ -35,9 +35,9 @@ namespace Syroot.Worms.OnlineWorms.Server
{ {
SendPacket(new ConnectReply SendPacket(new ConnectReply
{ {
Unknown = _server.Name, Unknown = _server.Config.Name,
Unknown2 = _server.RegionName, Unknown2 = _server.Config.Region,
Version = _server.Version Version = _server.Config.Version
}); });
} }
@ -53,7 +53,6 @@ namespace Syroot.Worms.OnlineWorms.Server
} }
SendPacket(new LoginReply SendPacket(new LoginReply
{ {
Unknown1 = 1,
Result = LoginResult.Success, Result = LoginResult.Success,
PlayerInfos = playerInfos PlayerInfos = playerInfos
}); });
@ -72,7 +71,7 @@ namespace Syroot.Worms.OnlineWorms.Server
new ChannelInfo new ChannelInfo
{ {
Name = "Test Channel", Name = "Test Channel",
EndPoint = new IPEndPoint(IPAddress.Loopback, 1), EndPoint = new IPEndPoint(_server.Config.IPAddress, 1),
Type = ChannelType.Normal, Type = ChannelType.Normal,
Color = Color.LightGreen, Color = Color.LightGreen,
Load = ChannelLoad.Highest Load = ChannelLoad.Highest
@ -80,20 +79,20 @@ namespace Syroot.Worms.OnlineWorms.Server
new ChannelInfo new ChannelInfo
{ {
Name = "Real Channel", Name = "Real Channel",
EndPoint = new IPEndPoint(IPAddress.Loopback, 2), EndPoint = new IPEndPoint(_server.Config.IPAddress, 2),
Type = ChannelType.Normal Type = ChannelType.Normal
}, },
new ChannelInfo new ChannelInfo
{ {
Name = "Boredom Time", Name = "Boredom Time",
EndPoint = new IPEndPoint(IPAddress.Loopback, 3), EndPoint = new IPEndPoint(_server.Config.IPAddress, 3),
Type = ChannelType.Normal, Type = ChannelType.Normal,
Load = ChannelLoad.Medium Load = ChannelLoad.Medium
}, },
new ChannelInfo new ChannelInfo
{ {
Name = "Nothing Goes", Name = "Nothing Goes",
EndPoint = new IPEndPoint(IPAddress.Loopback, 4), EndPoint = new IPEndPoint(_server.Config.IPAddress, 4),
Type = ChannelType.Roping, Type = ChannelType.Roping,
Color = Color.Orange, Color = Color.Orange,
Coins = 2, Coins = 2,
@ -102,7 +101,7 @@ namespace Syroot.Worms.OnlineWorms.Server
new ChannelInfo new ChannelInfo
{ {
Name = "Doper's Heaven", Name = "Doper's Heaven",
EndPoint = new IPEndPoint(IPAddress.Loopback, 5), EndPoint = new IPEndPoint(_server.Config.IPAddress, 5),
Type = ChannelType.Roping, Type = ChannelType.Roping,
Color = Color.Orange, Color = Color.Orange,
Coins = 1, Coins = 1,
@ -111,7 +110,7 @@ namespace Syroot.Worms.OnlineWorms.Server
new ChannelInfo new ChannelInfo
{ {
Name = "Unhelpful Channel", Name = "Unhelpful Channel",
EndPoint = new IPEndPoint(IPAddress.Loopback, 6), EndPoint = new IPEndPoint(_server.Config.IPAddress, 6),
Type = ChannelType.Special Type = ChannelType.Special
} }
} }
@ -123,7 +122,7 @@ namespace Syroot.Worms.OnlineWorms.Server
// Simply allow joining a channel running on the same server port. // Simply allow joining a channel running on the same server port.
SendPacket(new ChannelEnterReply SendPacket(new ChannelEnterReply
{ {
EndPoint = new IPEndPoint(IPAddress.Loopback, _server.Port) EndPoint = _server.Config.EndPoint
}); });
} }
@ -158,6 +157,8 @@ namespace Syroot.Worms.OnlineWorms.Server
}); });
} }
SendPacket(reply); SendPacket(reply);
SendPacket(new RawPacket(PacketType.Channel, 0x44, (byte)0x00));
} }
#if DEBUG #if DEBUG

View File

@ -0,0 +1,26 @@
using System.Net;
namespace Syroot.Worms.OnlineWorms.Server
{
internal class Config
{
// ---- PROPERTIES ---------------------------------------------------------------------------------------------
/// <summary>
/// Gets or sets the external IP sent to clients to connect to.
/// </summary>
public string IP { get; set; }
/// <summary>
/// Gets or sets the port under which the server listens for new connections.
/// </summary>
public ushort Port { get; set; }
public string Name { get; set; }
public string Region { get; set; }
public ushort Version { get; set; }
internal IPAddress IPAddress => IPAddress.Parse(IP);
internal IPEndPoint EndPoint => new IPEndPoint(IPAddress, Port);
}
}

View File

@ -13,7 +13,11 @@ namespace Syroot.Worms.OnlineWorms.Server.Net
internal LoginResult Result { get; set; } internal LoginResult Result { get; set; }
internal ushort Unknown1 { get; set; } /// <summary>
/// Gets or sets a value indicating whether the user can send any messages. Apparently also affects possibility
/// of even joining a channel.
/// </summary>
internal bool HasMessageRights { get; set; } = true;
internal LoginPlayerInfo[] PlayerInfos { get; set; } internal LoginPlayerInfo[] PlayerInfos { get; set; }
@ -27,7 +31,7 @@ namespace Syroot.Worms.OnlineWorms.Server.Net
stream.WriteBoolean(loginSuccessful); stream.WriteBoolean(loginSuccessful);
if (loginSuccessful) if (loginSuccessful)
{ {
stream.WriteUInt16(Unknown1); stream.WriteBoolean(!HasMessageRights, BooleanCoding.Word);
stream.WriteUInt16((ushort)PlayerInfos.Length); stream.WriteUInt16((ushort)PlayerInfos.Length);
foreach (LoginPlayerInfo playerInfo in PlayerInfos) foreach (LoginPlayerInfo playerInfo in PlayerInfos)
{ {

View File

@ -10,10 +10,11 @@ namespace Syroot.Worms.OnlineWorms.Server.Net
{ {
// ---- CONSTRUCTORS & DESTRUCTOR ------------------------------------------------------------------------------ // ---- CONSTRUCTORS & DESTRUCTOR ------------------------------------------------------------------------------
internal RawPacket(PacketType type, int id) internal RawPacket(PacketType type, int id, params byte[] data)
{ {
Type = type; Type = type;
ID = id; ID = id;
Data = data;
} }
// ---- PROPERTIES --------------------------------------------------------------------------------------------- // ---- PROPERTIES ---------------------------------------------------------------------------------------------
@ -22,12 +23,19 @@ namespace Syroot.Worms.OnlineWorms.Server.Net
internal int ID { get; } internal int ID { get; }
internal byte[] Data { get; set; } internal byte[] Data { get; private set; }
// ---- METHODS (INTERNAL) ------------------------------------------------------------------------------------- // ---- METHODS (INTERNAL) -------------------------------------------------------------------------------------
internal override void Deserialize(PacketStream stream) => Data = stream.ReadBytes((int)stream.Length); internal override void Deserialize(PacketStream stream)
internal override void Serialize(PacketStream stream) => stream.WriteBytes(Data); {
Data = stream.ReadBytes((int)stream.Length);
}
internal override void Serialize(PacketStream stream)
{
stream.WriteBytes(Data);
}
} }
} }
#endif #endif

View File

@ -0,0 +1,7 @@
{
"IP": "87.123.189.212", // external IP sent to clients to connect to
"Port": 17022,
"Name": "Online Worms Private Server",
"Region": "Global",
"Version": 114
}

View File

@ -13,7 +13,8 @@ namespace Syroot.Worms.OnlineWorms.Server
{ {
try try
{ {
new Server().Listen(17022); // Start a new server.
new Server().Listen();
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@ -2,6 +2,7 @@
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
namespace Syroot.Worms.OnlineWorms.Server namespace Syroot.Worms.OnlineWorms.Server
{ {
@ -15,16 +16,20 @@ namespace Syroot.Worms.OnlineWorms.Server
private readonly List<Client> _clients = new List<Client>(); private readonly List<Client> _clients = new List<Client>();
// ---- CONSTRUCTORS & DESTRUCTOR ------------------------------------------------------------------------------
internal Server()
{
// Create and read the configuration.
Config = new ConfigurationBuilder()
.AddJsonFile("OWServerConfig.json", true)
.Build()
.Get<Config>();
}
// ---- PROPERTIES --------------------------------------------------------------------------------------------- // ---- PROPERTIES ---------------------------------------------------------------------------------------------
internal string Name => "Online Worms Private Server"; internal Config Config { get; }
internal string RegionName => "Global";
internal ushort Version => 114;
/// <summary>
/// Gets the port under which the server listens for new connections.
/// </summary>
internal int Port { get; private set; }
internal Log Log { get; } = new Log(); internal Log Log { get; } = new Log();
@ -35,12 +40,11 @@ namespace Syroot.Worms.OnlineWorms.Server
/// dispatching them into asynchronous handling threads. This call is blocking. /// dispatching them into asynchronous handling threads. This call is blocking.
/// </summary> /// </summary>
/// <param name="port">The port on which to listen for new client connections.</param> /// <param name="port">The port on which to listen for new client connections.</param>
internal void Listen(int port) internal void Listen()
{ {
Port = port; TcpListener tcpListener = new TcpListener(IPAddress.Any, Config.Port);
TcpListener tcpListener = new TcpListener(IPAddress.Any, Port);
tcpListener.Start(); tcpListener.Start();
Log.Write(LogCategory.Server, $"Listening on port {Port}..."); Log.Write(LogCategory.Server, $"Listening on {Config.EndPoint}...");
while (true) while (true)
{ {

View File

@ -1,13 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFrameworks>net452;netcoreapp2.1</TargetFrameworks> <TargetFrameworks>netcoreapp2.1</TargetFrameworks>
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Syroot.BinaryData" Version="5.0.0" /> <PackageReference Include="Syroot.BinaryData" Version="5.0.0" />
</ItemGroup> <PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<ItemGroup Condition="'$(TargetFramework)'=='netcoreapp2.1'"> <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.2.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.5.0" /> <PackageReference Include="System.Text.Encoding.CodePages" Version="4.5.0" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Update="OWServerConfig.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project> </Project>