Finish Worms 2 server (so far).

This commit is contained in:
Ray Koopa 2020-07-10 21:01:09 +02:00
parent 0fee2e1e13
commit 02a644abe9
4 changed files with 207 additions and 129 deletions

View File

@ -32,7 +32,7 @@ Implementation of formats listed above as unsupported is planned for a later dat
* `Syroot.Worms.Mgame.Launcher`: Creates a fake launch configuration to start OW or WWPA clients with. * `Syroot.Worms.Mgame.Launcher`: Creates a fake launch configuration to start OW or WWPA clients with.
* `Syroot.Worms.Mgame.GameServer`: Simulates OW or WWPA networking to allow playing games. * `Syroot.Worms.Mgame.GameServer`: Simulates OW or WWPA networking to allow playing games.
* `Syroot.Worms.Worms2.GameServer`: Simulates W2 networking (WIP). * `Syroot.Worms.Worms2.GameServer`: Simulates a Worms 2 server.
## Availability ## Availability

View File

@ -140,17 +140,17 @@ namespace Syroot.Worms.Worms2.GameServer
int dataLength = 0; int dataLength = 0;
Code = (PacketCode)stream.ReadInt32(); Code = (PacketCode)stream.ReadInt32();
Flags flags = (Flags)stream.ReadInt32(); Flags flags = (Flags)stream.ReadInt32();
if (flags.HasFlag(Flags.HasValue0)) Value0 = stream.ReadInt32(); if (flags.HasFlag(Flags.Value0)) Value0 = stream.ReadInt32();
if (flags.HasFlag(Flags.HasValue1)) Value1 = stream.ReadInt32(); if (flags.HasFlag(Flags.Value1)) Value1 = stream.ReadInt32();
if (flags.HasFlag(Flags.HasValue2)) Value2 = stream.ReadInt32(); if (flags.HasFlag(Flags.Value2)) Value2 = stream.ReadInt32();
if (flags.HasFlag(Flags.HasValue3)) Value3 = stream.ReadInt32(); if (flags.HasFlag(Flags.Value3)) Value3 = stream.ReadInt32();
if (flags.HasFlag(Flags.HasValue4)) Value4 = stream.ReadInt32(); if (flags.HasFlag(Flags.Value4)) Value4 = stream.ReadInt32();
if (flags.HasFlag(Flags.HasValue10)) Value10 = stream.ReadInt32(); if (flags.HasFlag(Flags.Value10)) Value10 = stream.ReadInt32();
if (flags.HasFlag(Flags.HasDataLength)) dataLength = stream.ReadInt32(); if (flags.HasFlag(Flags.DataLength)) dataLength = stream.ReadInt32();
if (flags.HasFlag(Flags.HasData) && dataLength != 0) Data = stream.ReadFixedString(dataLength, Encodings.Windows1252); if (flags.HasFlag(Flags.Data) && dataLength != 0) Data = stream.ReadFixedString(dataLength, Encodings.Windows1252);
if (flags.HasFlag(Flags.HasError)) Error = stream.ReadInt32(); if (flags.HasFlag(Flags.Error)) Error = stream.ReadInt32();
if (flags.HasFlag(Flags.HasName)) Name = stream.ReadFixedString(20, Encodings.Windows1252); if (flags.HasFlag(Flags.Name)) Name = stream.ReadFixedString(20, Encodings.Windows1252);
if (flags.HasFlag(Flags.HasSession)) Session = stream.ReadStruct<SessionInfo>(); if (flags.HasFlag(Flags.Session)) Session = stream.ReadStruct<SessionInfo>();
} }
/// <summary> /// <summary>
@ -182,20 +182,20 @@ namespace Syroot.Worms.Worms2.GameServer
private Flags GetFlags() private Flags GetFlags()
{ {
Flags flags = Flags.None; Flags flags = Flags.None;
if (Value0.HasValue) flags |= Flags.HasValue0; if (Value0.HasValue) flags |= Flags.Value0;
if (Value1.HasValue) flags |= Flags.HasValue1; if (Value1.HasValue) flags |= Flags.Value1;
if (Value2.HasValue) flags |= Flags.HasValue2; if (Value2.HasValue) flags |= Flags.Value2;
if (Value3.HasValue) flags |= Flags.HasValue3; if (Value3.HasValue) flags |= Flags.Value3;
if (Value4.HasValue) flags |= Flags.HasValue4; if (Value4.HasValue) flags |= Flags.Value4;
if (Value10.HasValue) flags |= Flags.HasValue10; if (Value10.HasValue) flags |= Flags.Value10;
if (Data != null) if (Data != null)
{ {
flags |= Flags.HasDataLength; flags |= Flags.DataLength;
flags |= Flags.HasData; flags |= Flags.Data;
} }
if (Error.HasValue) flags |= Flags.HasError; if (Error.HasValue) flags |= Flags.Error;
if (Name != null) flags |= Flags.HasName; if (Name != null) flags |= Flags.Name;
if (Session.HasValue) flags |= Flags.HasSession; if (Session.HasValue) flags |= Flags.Session;
return flags; return flags;
} }
@ -205,17 +205,17 @@ namespace Syroot.Worms.Worms2.GameServer
private enum Flags : int private enum Flags : int
{ {
None, None,
HasValue0 = 1 << 0, Value0 = 1 << 0,
HasValue1 = 1 << 1, Value1 = 1 << 1,
HasValue2 = 1 << 2, Value2 = 1 << 2,
HasValue3 = 1 << 3, Value3 = 1 << 3,
HasValue4 = 1 << 4, Value4 = 1 << 4,
HasValue10 = 1 << 10, Value10 = 1 << 10,
HasDataLength = 1 << 5, DataLength = 1 << 5,
HasData = 1 << 6, Data = 1 << 6,
HasError = 1 << 7, Error = 1 << 7,
HasName = 1 << 8, Name = 1 << 8,
HasSession = 1 << 9 Session = 1 << 9
} }
} }
} }

View File

@ -17,11 +17,11 @@
CreateRoomReply = 701, CreateRoomReply = 701,
Join = 800, Join = 800,
JoinReply = 801, JoinReply = 801,
LeaveRoom = 900, Leave = 900,
LeaveRoomReply = 901, LeaveReply = 901,
DisconnectUser = 1000, DisconnectUser = 1000,
CloseRoom = 1100, Close = 1100,
CloseRoomReply = 1101, CloseReply = 1101,
CreateGame = 1200, CreateGame = 1200,
CreateGameReply = 1201, CreateGameReply = 1201,
ChatRoom = 1300, ChatRoom = 1300,

View File

@ -36,9 +36,9 @@ namespace Syroot.Worms.Worms2.GameServer
[PacketCode.ListGames] = OnListGames, [PacketCode.ListGames] = OnListGames,
[PacketCode.Login] = OnLogin, [PacketCode.Login] = OnLogin,
[PacketCode.CreateRoom] = OnCreateRoom, [PacketCode.CreateRoom] = OnCreateRoom,
[PacketCode.Join] = OnJoinRoom, [PacketCode.Join] = OnJoin,
[PacketCode.LeaveRoom] = OnLeaveRoom, [PacketCode.Leave] = OnLeave,
[PacketCode.CloseRoom] = OnCloseRoom, [PacketCode.Close] = OnClose,
[PacketCode.CreateGame] = OnCreateGame, [PacketCode.CreateGame] = OnCreateGame,
[PacketCode.ChatRoom] = OnChatRoom, [PacketCode.ChatRoom] = OnChatRoom,
[PacketCode.ConnectGame] = OnConnectGame, [PacketCode.ConnectGame] = OnConnectGame,
@ -128,10 +128,65 @@ namespace Syroot.Worms.Worms2.GameServer
} }
} }
private void LeaveGame(User fromUser)
{
// Close an abandoned game.
Game? game = _games.SingleOrDefault(x => x.Name == fromUser.Name);
if (game == null)
return;
_games.Remove(game);
// Notify other users.
foreach (User user in _users.Where(x => x != fromUser))
{
SendPacket(user.Connection, new Packet(PacketCode.Leave,
value2: game.ID,
value10: fromUser.ID));
SendPacket(user.Connection, new Packet(PacketCode.Close,
value10: game.ID));
}
}
private void LeaveRoom(User fromUser)
{
// Close an abandoned room.
bool roomClosed = fromUser.RoomID != 0
&& !_users.Any(x => x != fromUser && x.RoomID == fromUser.RoomID)
&& !_games.Any(x => x.RoomID == fromUser.RoomID);
if (roomClosed)
_rooms.Remove(_rooms.Single(x => x.ID == fromUser.RoomID));
// Notify other users.
foreach (User user in _users.Where(x => x != fromUser))
{
// Notify room leave, if any.
if (fromUser.RoomID != 0)
{
SendPacket(user.Connection, new Packet(PacketCode.Leave,
value2: fromUser.RoomID,
value10: fromUser.ID));
}
// Notify room close, if any.
if (roomClosed)
{
SendPacket(user.Connection, new Packet(PacketCode.Close,
value10: fromUser.RoomID));
}
}
// Update user room.
fromUser.RoomID = 0;
}
// ---- Handlers ---- // ---- Handlers ----
private void OnListRooms(PacketConnection connection, Packet packet) private void OnListRooms(PacketConnection connection, Packet packet)
{ {
User? fromUser = GetUser(connection);
if (fromUser == null || packet.Value4 != 0)
return;
foreach (Room room in _rooms) foreach (Room room in _rooms)
{ {
SendPacket(connection, new Packet(PacketCode.ListItem, SendPacket(connection, new Packet(PacketCode.ListItem,
@ -146,7 +201,7 @@ namespace Syroot.Worms.Worms2.GameServer
private void OnListUsers(PacketConnection connection, Packet packet) private void OnListUsers(PacketConnection connection, Packet packet)
{ {
User? fromUser = GetUser(connection); User? fromUser = GetUser(connection);
if (fromUser == null) if (fromUser == null || packet.Value2 != fromUser.RoomID || packet.Value4 != 0)
return; return;
foreach (User user in _users.Where(x => x.RoomID == fromUser.RoomID)) // notably includes the user itself foreach (User user in _users.Where(x => x.RoomID == fromUser.RoomID)) // notably includes the user itself
@ -162,7 +217,7 @@ namespace Syroot.Worms.Worms2.GameServer
private void OnListGames(PacketConnection connection, Packet packet) private void OnListGames(PacketConnection connection, Packet packet)
{ {
User? fromUser = GetUser(connection); User? fromUser = GetUser(connection);
if (fromUser == null) if (fromUser == null || packet.Value2 != fromUser.RoomID || packet.Value4 != 0)
return; return;
foreach (Game game in _games.Where(x => x.RoomID == fromUser.RoomID)) foreach (Game game in _games.Where(x => x.RoomID == fromUser.RoomID))
@ -178,10 +233,15 @@ namespace Syroot.Worms.Worms2.GameServer
private void OnLogin(PacketConnection connection, Packet packet) private void OnLogin(PacketConnection connection, Packet packet)
{ {
if (packet.Value1 == null || packet.Value4 == null || packet.Name == null || packet.Session == null)
return;
// Check if user name is valid and not already taken. // Check if user name is valid and not already taken.
if (!String.IsNullOrWhiteSpace(packet.Name) if (_users.Any(x => x.Name.Equals(packet.Name, StringComparison.InvariantCultureIgnoreCase)))
&& packet.Session.HasValue {
&& !_users.Any(x => x.Name.Equals(packet.Name, StringComparison.InvariantCultureIgnoreCase))) SendPacket(connection, new Packet(PacketCode.LoginReply, value1: 0, error: 1));
}
else
{ {
User newUser = new User(connection, ++_lastID, packet.Name, packet.Session.Value.Nation); User newUser = new User(connection, ++_lastID, packet.Name, packet.Session.Value.Nation);
@ -197,28 +257,19 @@ namespace Syroot.Worms.Worms2.GameServer
// Register new user and send reply to him. // Register new user and send reply to him.
_users.Add(newUser); _users.Add(newUser);
SendPacket(connection, new Packet(PacketCode.LoginReply, SendPacket(connection, new Packet(PacketCode.LoginReply, value1: newUser.ID, error: 0));
value1: newUser.ID,
error: 0));
}
else
{
SendPacket(connection, new Packet(PacketCode.LoginReply,
value1: 0,
error: 1));
} }
} }
private void OnCreateRoom(PacketConnection connection, Packet packet) private void OnCreateRoom(PacketConnection connection, Packet packet)
{ {
User? fromUser = GetUser(connection); User? fromUser = GetUser(connection);
if (fromUser == null) if (fromUser == null || packet.Value1 != 0 || packet.Value4 != 0 || packet.Data == null
|| packet.Name == null || packet.Session == null)
return; return;
// Check if room name is valid is not already taken. // Check if room name is valid is not already taken.
if (!String.IsNullOrWhiteSpace(packet.Name) if (!_rooms.Any(x => x.Name.Equals(packet.Name, StringComparison.InvariantCultureIgnoreCase)))
&& packet.Session.HasValue
&& !_rooms.Any(x => x.Name.Equals(packet.Name, StringComparison.InvariantCultureIgnoreCase)))
{ {
Room newRoom = new Room(++_lastID, packet.Name, packet.Session.Value.Nation, Room newRoom = new Room(++_lastID, packet.Name, packet.Session.Value.Nation,
connection.RemoteEndPoint.Address); connection.RemoteEndPoint.Address);
@ -243,18 +294,19 @@ namespace Syroot.Worms.Worms2.GameServer
else else
{ {
SendPacket(connection, new Packet(PacketCode.CreateRoomReply, SendPacket(connection, new Packet(PacketCode.CreateRoomReply,
value1: 0,
error: 1)); error: 1));
} }
} }
private void OnJoinRoom(PacketConnection connection, Packet packet) private void OnJoin(PacketConnection connection, Packet packet)
{ {
User? fromUser = GetUser(connection); User? fromUser = GetUser(connection);
if (fromUser == null) if (fromUser == null || packet.Value2 == null || packet.Value10 != fromUser.ID)
return; return;
// Check if room ID is valid. // Require valid room or game ID.
if (packet.Value2.HasValue && _rooms.Any(x => x.ID == packet.Value2)) if (_rooms.Any(x => x.ID == packet.Value2))
{ {
fromUser.RoomID = packet.Value2.Value; fromUser.RoomID = packet.Value2.Value;
@ -267,66 +319,47 @@ namespace Syroot.Worms.Worms2.GameServer
} }
// Send reply to joiner. // Send reply to joiner.
SendPacket(connection, new Packet(PacketCode.JoinReply, SendPacket(connection, new Packet(PacketCode.JoinReply, error: 0));
error: 0));
} }
else else if (_games.Any(x => x.ID == packet.Value2 && x.RoomID == fromUser.RoomID))
{ {
SendPacket(connection, new Packet(PacketCode.JoinReply, // Notify other users about the join.
error: 1));
}
}
private void OnLeaveRoom(PacketConnection connection, Packet packet)
{
User? fromUser = GetUser(connection);
if (fromUser == null)
return;
// Require left room ID and user ID.
if (packet.Value2.HasValue && packet.Value2 == fromUser.RoomID
&& packet.Value10.HasValue && packet.Value10 == fromUser.ID)
{
LeaveRoom(fromUser);
// Reply to leaver.
SendPacket(connection, new Packet(PacketCode.LeaveRoomReply,
error: 0));
}
else
{
// Reply to leaver.
SendPacket(connection, new Packet(PacketCode.LeaveRoomReply,
error: 1));
}
}
private void LeaveRoom(User fromUser)
{
// Close an abandoned room.
bool roomClosed = fromUser.RoomID != 0 && !_users.Any(x => x != fromUser && x.RoomID == fromUser.RoomID);
if (roomClosed)
_rooms.Remove(_rooms.Single(x => x.ID == fromUser.RoomID));
// Notify other users.
foreach (User user in _users.Where(x => x != fromUser)) foreach (User user in _users.Where(x => x != fromUser))
{ {
// Notify room leave, if any. SendPacket(user.Connection, new Packet(PacketCode.Join,
if (fromUser.RoomID != 0)
{
SendPacket(user.Connection, new Packet(PacketCode.LeaveRoom,
value2: fromUser.RoomID, value2: fromUser.RoomID,
value10: fromUser.ID)); value10: fromUser.ID));
} }
// Notify room close, if any.
if (roomClosed) // Send reply to joiner.
SendPacket(connection, new Packet(PacketCode.JoinReply, error: 0));
}
else
{ {
SendPacket(user.Connection, new Packet(PacketCode.CloseRoom, SendPacket(connection, new Packet(PacketCode.JoinReply, error: 1));
value10: fromUser.RoomID));
} }
} }
// Update user room. private void OnLeave(PacketConnection connection, Packet packet)
fromUser.RoomID = 0; {
User? fromUser = GetUser(connection);
if (fromUser == null || packet.Value2 == null || packet.Value10 != fromUser.ID)
return;
// Require valid room ID (never sent for games, users disconnect if leaving a game).
if (packet.Value2 == fromUser.RoomID)
{
LeaveRoom(fromUser);
// Reply to leaver.
SendPacket(connection, new Packet(PacketCode.LeaveReply,
error: 0));
}
else
{
// Reply to leaver.
SendPacket(connection, new Packet(PacketCode.LeaveReply,
error: 1));
}
} }
private void OnDisconnectUser(PacketConnection connection) private void OnDisconnectUser(PacketConnection connection)
@ -337,6 +370,7 @@ namespace Syroot.Worms.Worms2.GameServer
_users.Remove(fromUser); _users.Remove(fromUser);
LeaveGame(fromUser);
LeaveRoom(fromUser); LeaveRoom(fromUser);
// Notify user disconnect. // Notify user disconnect.
@ -347,31 +381,59 @@ namespace Syroot.Worms.Worms2.GameServer
} }
} }
private void OnCloseRoom(PacketConnection connection, Packet packet) private void OnClose(PacketConnection connection, Packet packet)
{ {
User? fromUser = GetUser(connection); User? fromUser = GetUser(connection);
if (fromUser == null) if (fromUser == null || packet.Value10 == null)
return; return;
// Never sent for games, users disconnect if leaving a game.
// Simply reply success to client, the server decides when to actually close rooms. // Simply reply success to client, the server decides when to actually close rooms.
SendPacket(connection, new Packet(PacketCode.CloseRoomReply, error: 0)); SendPacket(connection, new Packet(PacketCode.CloseReply, error: 0));
} }
private void OnCreateGame(PacketConnection connection, Packet packet) private void OnCreateGame(PacketConnection connection, Packet packet)
{ {
User? fromUser = GetUser(connection);
if (fromUser == null || packet.Value2 == null || packet.Data == null || packet.Session == null)
return;
// Require valid room ID.
if (packet.Value2 == fromUser.RoomID)
{
Game newGame = new Game(++_lastID, fromUser.Name, fromUser.Session.Nation, fromUser.RoomID,
connection.RemoteEndPoint.Address, // do not use bad NAT IP reported by users here
packet.Session.Value.Access);
_games.Add(newGame);
// Notify other users about new game, even those in other rooms.
foreach (User user in _users.Where(x => x != fromUser))
{
SendPacket(user.Connection, new Packet(PacketCode.CreateGame,
value2: newGame.RoomID,
value4: newGame.ID,
data: newGame.IPAddress.ToString(),
name: newGame.Name,
session: newGame.Session));
}
// Send reply to host.
SendPacket(connection, new Packet(PacketCode.CreateGameReply, SendPacket(connection, new Packet(PacketCode.CreateGameReply,
value1: 123456, value1: newGame.ID,
error: 0)); error: 0));
} }
else
{
SendPacket(connection, new Packet(PacketCode.CreateGameReply,
value1: 0,
error: 1));
}
}
private void OnChatRoom(PacketConnection connection, Packet packet) private void OnChatRoom(PacketConnection connection, Packet packet)
{ {
User? fromUser = GetUser(connection); User? fromUser = GetUser(connection);
if (fromUser == null) if (fromUser == null || packet.Value0 != fromUser.ID || packet.Value3 == null || packet.Data == null)
return;
// Requires user ID, target ID, and message data.
if (!packet.Value0.HasValue || !packet.Value3.HasValue || packet.Data == null)
return; return;
int targetID = packet.Value3.Value; int targetID = packet.Value3.Value;
@ -421,9 +483,25 @@ namespace Syroot.Worms.Worms2.GameServer
} }
private void OnConnectGame(PacketConnection connection, Packet packet) private void OnConnectGame(PacketConnection connection, Packet packet)
{
User? fromUser = GetUser(connection);
if (fromUser == null || packet.Value0 == null)
return;
// Require valid game ID and user to be in appropriate room.
Game? game = _games.FirstOrDefault(x => x.ID == packet.Value0 && x.RoomID == fromUser.RoomID);
if (game == null)
{ {
SendPacket(connection, new Packet(PacketCode.ConnectGameReply, SendPacket(connection, new Packet(PacketCode.ConnectGameReply,
data: "12.34.45.56")); data: String.Empty,
error: 1));
}
else
{
SendPacket(connection, new Packet(PacketCode.ConnectGameReply,
data: game.IPAddress.ToString(),
error: 0));
}
} }
} }
} }