Client LuaCsForBarotrauma
MultiplayerPreferences.cs
1 #nullable enable
2 using System;
3 using System.Collections.Generic;
4 using System.Globalization;
5 using System.Linq;
6 using System.Xml.Linq;
8 using Microsoft.Xna.Framework;
9 
10 namespace Barotrauma
11 {
13  {
14  public readonly struct JobPreference
15  {
16  public JobPreference(Identifier jobIdentifier, int variant)
17  {
18  JobIdentifier = jobIdentifier;
19  Variant = variant;
20  }
21 
22  public JobPreference(XElement element) : this(
23  element.GetAttributeIdentifier("identifier", Identifier.Empty),
24  element.GetAttributeInt("variant", -1)) { }
25 
26  public readonly Identifier JobIdentifier;
27  public readonly int Variant;
28 
29  public static bool operator ==(JobPreference a, JobPreference b)
30  => a.JobIdentifier == b.JobIdentifier && a.Variant == b.Variant;
31 
32  public static bool operator !=(JobPreference a, JobPreference b) => !(a == b);
33 
34  public override bool Equals(object? obj)
35  => obj is JobPreference jp && jp == this;
36 
37  public bool Equals(JobPreference other) => other == this;
38 
39  public override int GetHashCode() => HashCode.Combine(JobIdentifier, Variant);
40  }
41 
42  public readonly List<JobPreference> JobPreferences = new List<JobPreference>();
44  public string PlayerName = string.Empty;
45 
46  public readonly HashSet<Identifier> TagSet = new HashSet<Identifier>();
47  public int HairIndex = -1;
48  public int BeardIndex = -1;
49  public int MoustacheIndex = -1;
50  public int FaceAttachmentIndex = -1;
51  public Color HairColor = Color.Black;
52  public Color FacialHairColor = Color.Black;
53  public Color SkinColor = Color.Black;
54 
55  public static MultiplayerPreferences Instance { get; private set; } = new MultiplayerPreferences();
56 
57  private MultiplayerPreferences() { }
58 
59  private MultiplayerPreferences(IEnumerable<XElement> elements)
60  {
61  foreach (var element in elements)
62  {
63  PlayerName = element.GetAttributeString("name", PlayerName);
64 
65  TagSet.UnionWith(element.GetAttributeIdentifierArray("tags", Array.Empty<Identifier>()));
66  HairIndex = element.GetAttributeInt(nameof(HairIndex), HairIndex);
67  BeardIndex = element.GetAttributeInt(nameof(BeardIndex), BeardIndex);
68  MoustacheIndex = element.GetAttributeInt(nameof(MoustacheIndex), MoustacheIndex);
69  FaceAttachmentIndex = element.GetAttributeInt(nameof(FaceAttachmentIndex), FaceAttachmentIndex);
70 
71  HairColor = element.GetAttributeColor(nameof(HairColor), HairColor);
72  FacialHairColor = element.GetAttributeColor(nameof(FacialHairColor), FacialHairColor);
73  SkinColor = element.GetAttributeColor(nameof(SkinColor), SkinColor);
74 
75  foreach (var subElement in element.GetChildElements("job"))
76  {
77  JobPreferences.Add(new JobPreference(subElement));
78  }
79  }
80  }
81 
82  public static void Init(params XElement?[] elements)
83  {
84  Instance = new MultiplayerPreferences(elements.Where(e => e != null)!);
85  }
86 
87  public void SaveTo(XElement element)
88  {
89  element.SetAttributeValue("name", PlayerName);
90 
91  element.SetAttributeValue("tags", string.Join(",", TagSet));
92  element.SetAttributeValue(nameof(HairIndex), HairIndex);
93  element.SetAttributeValue(nameof(BeardIndex), BeardIndex);
94  element.SetAttributeValue(nameof(MoustacheIndex), MoustacheIndex);
95  element.SetAttributeValue(nameof(FaceAttachmentIndex), FaceAttachmentIndex);
96 
97  element.SetAttributeValue(nameof(HairColor), HairColor.ToStringHex());
98  element.SetAttributeValue(nameof(FacialHairColor), FacialHairColor.ToStringHex());
99  element.SetAttributeValue(nameof(SkinColor), SkinColor.ToStringHex());
100 
101  foreach (var jobPreference in JobPreferences)
102  {
103  element.Add(new XElement("job",
104  new XAttribute("identifier", jobPreference.JobIdentifier.Value),
105  new XAttribute("variant", jobPreference.Variant.ToString(CultureInfo.InvariantCulture))));
106  }
107  }
108 
109  public bool AreJobPreferencesEqual(IReadOnlyList<JobPreference> other)
110  => JobPreferences.SequenceEqual(other);
111  }
112 }
readonly HashSet< Identifier > TagSet
bool AreJobPreferencesEqual(IReadOnlyList< JobPreference > other)
static void Init(params XElement?[] elements)
static MultiplayerPreferences Instance
readonly List< JobPreference > JobPreferences
JobPreference(Identifier jobIdentifier, int variant)
static bool operator!=(JobPreference a, JobPreference b)
static bool operator==(JobPreference a, JobPreference b)