Client LuaCsForBarotrauma
ClientPermissions.cs
1 using System;
2 using System.Collections.Generic;
3 using Barotrauma.IO;
4 using System.Linq;
5 using System.Xml.Linq;
6 
7 namespace Barotrauma.Networking
8 {
9  [Flags]
10  public enum ClientPermissions
11  {
12  None = 0x0,
13  ManageRound = 0x1,
14  Kick = 0x2,
15  Ban = 0x4,
16  Unban = 0x8,
17  SelectSub = 0x10,
18  SelectMode = 0x20,
19  ManageCampaign = 0x40,
20  ConsoleCommands = 0x80,
21  ServerLog = 0x100,
22  ManageSettings = 0x200,
23  ManagePermissions = 0x400,
24  KarmaImmunity = 0x800,
25  ManageMoney = 0x1000,
26  SellInventoryItems = 0x2000,
27  SellSubItems = 0x4000,
28  ManageMap = 0x8000,
29  ManageHires = 0x10000,
30  ManageBotTalents = 0x20000,
31  SpamImmunity = 0x40000,
32  All = 0x7FFFF
33  }
34 
36  {
37  public static readonly List<PermissionPreset> List = new List<PermissionPreset>();
38 
39  public readonly Identifier Identifier;
40  public readonly LocalizedString DisplayName;
41  public readonly LocalizedString Description;
42  public readonly ClientPermissions Permissions;
43  public readonly HashSet<DebugConsole.Command> PermittedCommands;
44 
45  public PermissionPreset(XElement element)
46  {
47  Identifier = element.GetAttributeIdentifier("name", Identifier.Empty);
48  DisplayName = TextManager.Get("permissionpresetname." + Identifier).Fallback(Identifier.ToString());
49  Description = TextManager.Get("permissionpresetdescription." + Identifier) .Fallback(element.GetAttributeString("description", ""));
50 
51  string permissionsStr = element.GetAttributeString("permissions", "");
52  if (!Enum.TryParse(permissionsStr, out Permissions))
53  {
54  DebugConsole.ThrowErrorLocalized("Error in permission preset \"" + DisplayName + "\" - " + permissionsStr + " is not a valid permission!");
55  }
56 
57  PermittedCommands = new HashSet<DebugConsole.Command>();
58  if (Permissions.HasFlag(ClientPermissions.ConsoleCommands))
59  {
60  foreach (var subElement in element.Elements())
61  {
62  if (!subElement.Name.ToString().Equals("command", StringComparison.OrdinalIgnoreCase)) { continue; }
63  string commandName = subElement.GetAttributeString("name", "");
64 
65  DebugConsole.Command command = DebugConsole.FindCommand(commandName);
66  if (command == null)
67  {
68 #if SERVER
69  DebugConsole.ThrowErrorLocalized("Error in permission preset \"" + DisplayName + "\" - " + commandName + "\" is not a valid console command.");
70 #endif
71  continue;
72  }
73 
74  PermittedCommands.Add(command);
75  }
76  }
77  }
78 
79  public static void LoadAll(string file)
80  {
81  if (!File.Exists(file)) { return; }
82 
83  XDocument doc = XMLExtensions.TryLoadXml(file);
84  if (doc == null) { return; }
85 
86  List.Clear();
87  foreach (XElement element in doc.Root.Elements())
88  {
89  List.Add(new PermissionPreset(element));
90  }
91  }
92 
93  public bool MatchesPermissions(ClientPermissions permissions, ISet<DebugConsole.Command> permittedConsoleCommands)
94  {
95  return permissions == Permissions
96  && PermittedCommands.All(permittedConsoleCommands.Contains)
97  && permittedConsoleCommands.All(PermittedCommands.Contains);
98  }
99  }
100 }
LocalizedString Fallback(LocalizedString fallback, bool useDefaultLanguageIfFound=true)
Use this text instead if the original text cannot be found.
static readonly List< PermissionPreset > List
readonly HashSet< DebugConsole.Command > PermittedCommands
readonly ClientPermissions Permissions
bool MatchesPermissions(ClientPermissions permissions, ISet< DebugConsole.Command > permittedConsoleCommands)