Ban console command, clients with spaces in their name can be kicked/banned, fixed banlist not loading correctly if a client has commas in their name

This commit is contained in:
Regalis
2016-08-18 21:02:03 +03:00
parent a02f0c23e1
commit 5dc8a5f8b7
2 changed files with 43 additions and 29 deletions
+10 -2
View File
@@ -189,6 +189,9 @@ namespace Barotrauma
NewMessage(" ", Color.Cyan); NewMessage(" ", Color.Cyan);
NewMessage("kick [name]: kick a player out from the server", Color.Cyan);
NewMessage("ban [name]: kick and ban the player", Color.Cyan);
NewMessage("debugdraw: toggles the ''debug draw mode''", Color.Cyan); NewMessage("debugdraw: toggles the ''debug draw mode''", Color.Cyan);
NewMessage("netstats: toggles the visibility of the network statistics panel", Color.Cyan); NewMessage("netstats: toggles the visibility of the network statistics panel", Color.Cyan);
@@ -278,8 +281,13 @@ namespace Barotrauma
HumanAIController.DisableCrewAI = false; HumanAIController.DisableCrewAI = false;
break; break;
case "kick": case "kick":
if (GameMain.Server == null) break; if (GameMain.Server == null || commands.Length < 2) break;
GameMain.Server.KickPlayer(commands[1]); GameMain.Server.KickPlayer(string.Join(" ", commands.Skip(1)));
break;
case "ban":
if (GameMain.Server == null || commands.Length < 2) break;
GameMain.Server.KickPlayer(string.Join(" ", commands.Skip(1)), true);
break; break;
case "startclient": case "startclient":
if (commands.Length == 1) return; if (commands.Length == 1) return;
+30 -24
View File
@@ -7,6 +7,18 @@ using System.Text;
namespace Barotrauma.Networking namespace Barotrauma.Networking
{ {
class BannedPlayer
{
public string Name;
public string IP;
public BannedPlayer(string name, string ip)
{
this.Name = name;
this.IP = ip;
}
}
class BanList class BanList
{ {
const string SavePath = "Data/bannedplayers.txt"; const string SavePath = "Data/bannedplayers.txt";
@@ -33,30 +45,33 @@ namespace Barotrauma.Networking
} }
catch (Exception e) catch (Exception e)
{ {
DebugConsole.ThrowError("Failed to open the list of banned players in "+SavePath, e); DebugConsole.ThrowError("Failed to open the list of banned players in " + SavePath, e);
return; return;
} }
foreach (string line in lines) foreach (string line in lines)
{ {
string[] separatedLine = line.Split(','); string[] separatedLine = line.Split(',');
if (separatedLine.Length != 2) continue; if (separatedLine.Length < 2) continue;
bannedPlayers.Add(new BannedPlayer(separatedLine[0],separatedLine[1])); string name = String.Join(",", separatedLine.Take(separatedLine.Length - 1));
string ip = separatedLine.Last();
bannedPlayers.Add(new BannedPlayer(name, ip));
} }
} }
} }
public void BanPlayer(string name, string ip) public void BanPlayer(string name, string ip)
{ {
if (bannedPlayers.FirstOrDefault(bp => bp.IP == ip)!=null) return; if (bannedPlayers.Any(bp => bp.IP == ip)) return;
bannedPlayers.Add(new BannedPlayer(name,ip)); bannedPlayers.Add(new BannedPlayer(name, ip));
} }
public bool IsBanned(string IP) public bool IsBanned(string IP)
{ {
return bannedPlayers.FirstOrDefault(bp => bp.IP == IP)!=null; return bannedPlayers.Any(bp => bp.IP == IP);
} }
public GUIComponent CreateBanFrame(GUIComponent parent) public GUIComponent CreateBanFrame(GUIComponent parent)
@@ -64,19 +79,19 @@ namespace Barotrauma.Networking
//GUIFrame banFrame = new GUIFrame(new Rectangle(0,0,0,0), null, Alignment.Center, GUI.Style, parent); //GUIFrame banFrame = new GUIFrame(new Rectangle(0,0,0,0), null, Alignment.Center, GUI.Style, parent);
//new GUITextBlock(new Rectangle(0, -10, 0, 30), "Banned IPs:", GUI.Style, Alignment.Left, Alignment.Left, banFrame, false, GUI.LargeFont); //new GUITextBlock(new Rectangle(0, -10, 0, 30), "Banned IPs:", GUI.Style, Alignment.Left, Alignment.Left, banFrame, false, GUI.LargeFont);
banFrame = new GUIListBox(new Rectangle(0,0,0,0), GUI.Style, parent); banFrame = new GUIListBox(new Rectangle(0, 0, 0, 0), GUI.Style, parent);
foreach (BannedPlayer bannedPlayer in bannedPlayers) foreach (BannedPlayer bannedPlayer in bannedPlayers)
{ {
GUITextBlock textBlock = new GUITextBlock( GUITextBlock textBlock = new GUITextBlock(
new Rectangle(0, 0, 0, 25), new Rectangle(0, 0, 0, 25),
bannedPlayer.IP+" ("+bannedPlayer.Name+")", bannedPlayer.IP + " (" + bannedPlayer.Name + ")",
GUI.Style, GUI.Style,
Alignment.Left, Alignment.Left, banFrame); Alignment.Left, Alignment.Left, banFrame);
textBlock.Padding = new Vector4(10.0f, 0.0f, 0.0f, 0.0f); textBlock.Padding = new Vector4(10.0f, 0.0f, 0.0f, 0.0f);
textBlock.UserData = banFrame; textBlock.UserData = banFrame;
var removeButton = new GUIButton(new Rectangle(0,00,100,20), "Remove", Alignment.Right | Alignment.CenterY, GUI.Style, textBlock); var removeButton = new GUIButton(new Rectangle(0, 00, 100, 20), "Remove", Alignment.Right | Alignment.CenterY, GUI.Style, textBlock);
removeButton.UserData = bannedPlayer; removeButton.UserData = bannedPlayer;
removeButton.OnClicked = RemoveBan; removeButton.OnClicked = RemoveBan;
} }
@@ -93,8 +108,9 @@ namespace Barotrauma.Networking
BannedPlayer banned = obj as BannedPlayer; BannedPlayer banned = obj as BannedPlayer;
if (banned == null) return false; if (banned == null) return false;
bannedPlayers.Remove(banned); GameServer.Log("Removing ban from " + banned.Name, null);
bannedPlayers.Remove(banned);
if (banFrame != null) if (banFrame != null)
{ {
@@ -114,6 +130,9 @@ namespace Barotrauma.Networking
public void Save() public void Save()
{ {
GameServer.Log("Saving banlist", null);
List<string> lines = new List<string>(); List<string> lines = new List<string>();
foreach (BannedPlayer banned in bannedPlayers) foreach (BannedPlayer banned in bannedPlayers)
@@ -127,21 +146,8 @@ namespace Barotrauma.Networking
} }
catch (Exception e) catch (Exception e)
{ {
DebugConsole.ThrowError("Saving the list of banned players to "+SavePath+" failed", e); DebugConsole.ThrowError("Saving the list of banned players to " + SavePath + " failed", e);
} }
} }
}
class BannedPlayer
{
public string Name;
public string IP;
public BannedPlayer(string name, string ip)
{
this.Name = name;
this.IP = ip;
}
} }
} }