(3db48532f) Give joining players "None" permissions by default (so server hosts can automatically give players some permissions if they want to). Save client permissions using the name of the permission preset instead of listing all the permissions given by the preset. Partially implements #1334

This commit is contained in:
Joonas Rikkonen
2019-04-01 14:25:33 +03:00
parent 6ffb28b6fb
commit 5cc2806185
4 changed files with 98 additions and 31 deletions
@@ -494,7 +494,15 @@ namespace Barotrauma.Networking
}
else
{
newClient.SetPermissions(ClientPermissions.None, new List<DebugConsole.Command>());
var defaultPerms = PermissionPreset.List.Find(p => p.Name == "None");
if (defaultPerms != null)
{
newClient.SetPermissions(defaultPerms.Permissions, defaultPerms.PermittedCommands);
}
else
{
newClient.SetPermissions(ClientPermissions.None, new List<DebugConsole.Command>());
}
}
}
@@ -337,37 +337,56 @@ namespace Barotrauma.Networking
continue;
}
string permissionsStr = clientElement.GetAttributeString("permissions", "");
ClientPermissions permissions = Networking.ClientPermissions.None;
if (permissionsStr.ToLowerInvariant() == "all")
List<DebugConsole.Command> permittedCommands = new List<DebugConsole.Command>();
if (clientElement.Attribute("preset") == null)
{
foreach (ClientPermissions permission in Enum.GetValues(typeof(ClientPermissions)))
string permissionsStr = clientElement.GetAttributeString("permissions", "");
if (permissionsStr.ToLowerInvariant() == "all")
{
permissions |= permission;
foreach (ClientPermissions permission in Enum.GetValues(typeof(ClientPermissions)))
{
permissions |= permission;
}
}
else if (!Enum.TryParse(permissionsStr, out permissions))
{
DebugConsole.ThrowError("Error in " + ClientPermissionsFile + " - \"" + permissionsStr + "\" is not a valid client permission.");
continue;
}
if (permissions.HasFlag(Networking.ClientPermissions.ConsoleCommands))
{
foreach (XElement commandElement in clientElement.Elements())
{
if (commandElement.Name.ToString().ToLowerInvariant() != "command") continue;
string commandName = commandElement.GetAttributeString("name", "");
DebugConsole.Command command = DebugConsole.FindCommand(commandName);
if (command == null)
{
DebugConsole.ThrowError("Error in " + ClientPermissionsFile + " - \"" + commandName + "\" is not a valid console command.");
continue;
}
permittedCommands.Add(command);
}
}
}
else if (!Enum.TryParse(permissionsStr, out permissions))
else
{
DebugConsole.ThrowError("Error in " + ClientPermissionsFile + " - \"" + permissionsStr + "\" is not a valid client permission.");
continue;
}
List<DebugConsole.Command> permittedCommands = new List<DebugConsole.Command>();
if (permissions.HasFlag(Networking.ClientPermissions.ConsoleCommands))
{
foreach (XElement commandElement in clientElement.Elements())
string presetName = clientElement.GetAttributeString("preset", "");
PermissionPreset preset = PermissionPreset.List.Find(p => p.Name == presetName);
if (preset == null)
{
if (commandElement.Name.ToString().ToLowerInvariant() != "command") continue;
string commandName = commandElement.GetAttributeString("name", "");
DebugConsole.Command command = DebugConsole.FindCommand(commandName);
if (command == null)
{
DebugConsole.ThrowError("Error in " + ClientPermissionsFile + " - \"" + commandName + "\" is not a valid console command.");
continue;
}
permittedCommands.Add(command);
DebugConsole.ThrowError("Failed to restore saved permissions to the client \"" + clientName + "\". Permission preset \"" + presetName + "\" not found.");
return;
}
else
{
permissions = preset.Permissions;
permittedCommands = preset.PermittedCommands.ToList();
}
}
@@ -440,9 +459,14 @@ namespace Barotrauma.Networking
foreach (SavedClientPermission clientPermission in ClientPermissions)
{
var matchingPreset = PermissionPreset.List.Find(p => p.MatchesPermissions(clientPermission.Permissions, clientPermission.PermittedCommands));
if (matchingPreset != null && matchingPreset.Name == "None")
{
continue;
}
XElement clientElement = new XElement("Client",
new XAttribute("name", clientPermission.Name),
new XAttribute("permissions", clientPermission.Permissions.ToString()));
new XAttribute("name", clientPermission.Name));
if (clientPermission.SteamID > 0)
{
@@ -453,14 +477,21 @@ namespace Barotrauma.Networking
clientElement.Add(new XAttribute("ip", clientPermission.IP));
}
if (clientPermission.Permissions.HasFlag(Barotrauma.Networking.ClientPermissions.ConsoleCommands))
if (matchingPreset == null)
{
foreach (DebugConsole.Command command in clientPermission.PermittedCommands)
clientElement.Add(new XAttribute("permissions", clientPermission.Permissions.ToString()));
if (clientPermission.Permissions.HasFlag(Networking.ClientPermissions.ConsoleCommands))
{
clientElement.Add(new XElement("command", new XAttribute("name", command.names[0])));
foreach (DebugConsole.Command command in clientPermission.PermittedCommands)
{
clientElement.Add(new XElement("command", new XAttribute("name", command.names[0])));
}
}
}
else
{
clientElement.Add(new XAttribute("preset", matchingPreset.Name));
}
doc.Root.Add(clientElement);
}
@@ -13,6 +13,13 @@
<Command name="autorestart"/>
<Command name="autorestartinterval"/>
<Command name="autorestarttimer"/>
<Command name="botcount"/>
<Command name="botspawnmode"/>
<Command name="gamemode"/>
<Command name="sub"/>
<Command name="shuttle"/>
<Command name="water"/>
<Command name="fire"/>
<Command name="difficulty"/>
<Command name="kick"/>
<Command name="kickid"/>
@@ -28,14 +35,21 @@
<Command name="autorestart"/>
<Command name="autorestartinterval"/>
<Command name="autorestarttimer"/>
<Command name="botcount"/>
<Command name="botspawnmode"/>
<Command name="difficulty"/>
<Command name="kick"/>
<Command name="kickid"/>
<Command name="campaigninfo"/>
<Command name="campaigndestination"/>
<Command name="servermsg"/>
<Command name="showperm"/>
<Command name="spawn"/>
<Command name="spawnitem"/>
<Command name="disablecrewai"/>
<Command name="gamemode"/>
<Command name="sub"/>
<Command name="shuttle"/>
<Command name="enablecrewai"/>
<Command name="ban"/>
<Command name="banid"/>
@@ -43,10 +57,16 @@
<Command name="unban"/>
<Command name="unbanip"/>
<Command name="teleportcharacter"/>
<Command name="mute"/>
<Command name="unmute"/>
<Command name="godmode"/>
<Command name="lock"/>
<Command name="lockx"/>
<Command name="locky"/>
<Command name="water"/>
<Command name="fire"/>
<Command name="heal"/>
<Command name="giveaffliction"/>
<Command name="revive"/>
<Command name="freeze"/>
<Command name="freecam"/>
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
namespace Barotrauma.Networking
@@ -54,7 +55,9 @@ namespace Barotrauma.Networking
DebugConsole.Command command = DebugConsole.FindCommand(commandName);
if (command == null)
{
#if SERVER
DebugConsole.ThrowError("Error in permission preset \"" + Name + "\" - " + commandName + "\" is not a valid console command.");
#endif
continue;
}
@@ -75,5 +78,10 @@ namespace Barotrauma.Networking
List.Add(new PermissionPreset(element));
}
}
public bool MatchesPermissions(ClientPermissions permissions, List<DebugConsole.Command> permittedConsoleCommands)
{
return permissions == this.Permissions && PermittedCommands.SequenceEqual(permittedConsoleCommands);
}
}
}