Client LuaCsForBarotrauma
ServerListFilters.cs
1 #nullable enable
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Xml.Linq;
6 
7 namespace Barotrauma
8 {
9  public class ServerListFilters
10  {
11  private readonly Dictionary<Identifier, string> attributes = new Dictionary<Identifier, string>();
12 
13  private ServerListFilters(XElement? elem)
14  {
15  if (elem is null) { return; }
16  foreach (var attr in elem.Attributes())
17  {
18  attributes.Add(attr.NameAsIdentifier(), attr.Value);
19  }
20  }
21 
22  public static void Init(XElement? elem)
23  {
24  Instance = new ServerListFilters(elem);
25  }
26 
27  public void SaveTo(XElement elem)
28  {
29  foreach (var kvp in attributes)
30  {
31  elem.Add(new XAttribute(kvp.Key.Value, kvp.Value));
32  }
33  }
34 
35  public bool GetAttributeBool(Identifier key, bool def)
36  {
37  if (attributes.TryGetValue(key, out string? val))
38  {
39  if (bool.TryParse(val, out bool result)) { return result; }
40  }
41 
42  return def;
43  }
44 
45  public T GetAttributeEnum<T>(Identifier key, T def) where T : struct, Enum
46  {
47  if (attributes.TryGetValue(key, out string? val))
48  {
49  if (Enum.TryParse(val, ignoreCase: true, out T result)) { return result; }
50  }
51 
52  return def;
53  }
54 
56  {
57  return attributes.TryGetValue(key, out string? val)
58  ? val.Split(",")
59  .Select(static s => s.Trim())
60  .Where(static s => !s.IsNullOrWhiteSpace())
61  .Select(static s => s.ToLanguageIdentifier()).ToArray()
62  : def;
63  }
64 
65  public void SetAttribute(Identifier key, string val)
66  {
67  attributes[key] = val;
68  }
69 
70  public static ServerListFilters Instance { get; private set; } = new ServerListFilters(null);
71  }
72 }
LanguageIdentifier[] GetAttributeLanguageIdentifierArray(Identifier key, LanguageIdentifier[] def)
T GetAttributeEnum< T >(Identifier key, T def)
static ServerListFilters Instance
static void Init(XElement? elem)
bool GetAttributeBool(Identifier key, bool def)
void SetAttribute(Identifier key, string val)