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;
+24 -18
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";
@@ -40,23 +52,26 @@ namespace Barotrauma.Networking
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)
@@ -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)
@@ -130,18 +149,5 @@ namespace Barotrauma.Networking
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;
}
} }
} }