Client LuaCsForBarotrauma
BarotraumaClient/ClientSource/Networking/BanList.cs
1 #nullable enable
2 using Microsoft.Xna.Framework;
3 using System;
4 using System.Collections.Generic;
5 
6 namespace Barotrauma.Networking
7 {
8  partial class BannedPlayer
9  {
10  public BannedPlayer(
11  UInt32 uniqueIdentifier,
12  string name,
13  Either<Address, AccountId> addressOrAccountId,
14  string reason,
15  Option<SerializableDateTime> expiration)
16  {
17  this.Name = name;
18  this.AddressOrAccountId = addressOrAccountId;
19  this.UniqueIdentifier = uniqueIdentifier;
20  this.Reason = reason;
21  this.ExpirationTime = expiration;
22  }
23  }
24 
25  partial class BanList
26  {
27  public GUIComponent? BanFrame { get; private set; }
28 
29  public List<UInt32> localRemovedBans = new List<UInt32>();
30 
31  private void RecreateBanFrame()
32  {
33  if (BanFrame != null)
34  {
35  var parent = BanFrame.Parent;
36  parent.RemoveChild(BanFrame);
37  CreateBanFrame(parent);
38  }
39  }
40 
42  {
43  BanFrame = new GUIListBox(new RectTransform(Vector2.One, parent.RectTransform, Anchor.Center));
44 
45  foreach (BannedPlayer bannedPlayer in bannedPlayers)
46  {
47  if (localRemovedBans.Contains(bannedPlayer.UniqueIdentifier)) { continue; }
48 
49  var playerFrame = new GUIFrame(new RectTransform(new Vector2(1.0f, 0.2f), ((GUIListBox)BanFrame).Content.RectTransform) { MinSize = new Point(0, 70) }, style: "InnerFrame")
50  {
51  UserData = BanFrame
52  };
53 
54  var paddedPlayerFrame = new GUILayoutGroup(new RectTransform(new Vector2(0.95f, 0.85f), playerFrame.RectTransform, Anchor.Center))
55  {
56  Stretch = true,
57  RelativeSpacing = 0.05f,
58  CanBeFocused = true
59  };
60 
61  var topArea = new GUILayoutGroup(new RectTransform(new Vector2(1.0f, 0.0f), paddedPlayerFrame.RectTransform),
62  isHorizontal: true, childAnchor: Anchor.CenterLeft)
63  {
64  Stretch = true,
65  RelativeSpacing = 0.02f
66  };
67 
68  var addressOrAccountId = bannedPlayer.AddressOrAccountId;
69 
70  string nameText = bannedPlayer.Name;
71  if (addressOrAccountId.TryCast(out Address address))
72  {
73  nameText += $" ({address.StringRepresentation})";
74  }
75  else if (addressOrAccountId.TryCast(out AccountId accountId))
76  {
77  nameText += $" ({accountId.StringRepresentation})";
78  }
79  GUITextBlock textBlock = new GUITextBlock(new RectTransform(new Vector2(0.5f, 1.0f), topArea.RectTransform), nameText)
80  {
81  CanBeFocused = true
82  };
83  textBlock.RectTransform.MinSize = new Point(0, (int)textBlock.Font.MeasureString(textBlock.Text.SanitizedValue).Y);
84 
85  var removeButton = new GUIButton(new RectTransform(new Vector2(0.2f, 0.4f), topArea.RectTransform, Anchor.CenterRight),
86  TextManager.Get("BanListRemove"), style: "GUIButtonSmall")
87  {
88  IgnoreLayoutGroups = true,
89  UserData = bannedPlayer,
90  OnClicked = RemoveBan,
91  Enabled = false
92  };
93  removeButton.OnAddedToGUIUpdateList += (component) =>
94  {
95  component.Enabled = GameMain.Client?.HasPermission(ClientPermissions.Unban) ?? false;
96  };
97  topArea.RectTransform.MinSize = new Point(0, Math.Max(textBlock.RectTransform.MinSize.Y, removeButton.RectTransform.MinSize.Y));
98  topArea.RectTransform.IsFixedSize = true;
99 
100  new GUITextBlock(new RectTransform(new Vector2(1.0f, 0.0f), paddedPlayerFrame.RectTransform),
101  bannedPlayer.ExpirationTime.TryUnwrap(out var expirationTime)
102  ? TextManager.GetWithVariable("BanExpires", "[time]", expirationTime.ToLocalUserString())
103  : TextManager.Get("BanPermanent"),
104  font: GUIStyle.SmallFont);
105 
106  LocalizedString reason = TextManager.GetServerMessage(bannedPlayer.Reason).Fallback(bannedPlayer.Reason);
107  var reasonText = new GUITextBlock(new RectTransform(new Vector2(1.0f, 0.0f), paddedPlayerFrame.RectTransform),
108  TextManager.Get("BanReason") + " " +
109  (string.IsNullOrEmpty(bannedPlayer.Reason) ? TextManager.Get("None") : reason),
110  font: GUIStyle.SmallFont, wrap: true)
111  {
112  ToolTip = reason
113  };
114 
115  paddedPlayerFrame.Recalculate();
116 
117  new GUIFrame(new RectTransform(new Vector2(1.0f, 0.01f), ((GUIListBox)BanFrame).Content.RectTransform), style: "HorizontalLine");
118  }
119 
120  return BanFrame;
121  }
122 
123  private bool RemoveBan(GUIButton button, object obj)
124  {
125  if (obj is not BannedPlayer banned) { return false; }
126 
127  localRemovedBans.Add(banned.UniqueIdentifier);
128  RecreateBanFrame();
129 
130  GameMain.Client?.ServerSettings?.ClientAdminWrite(ServerSettings.NetFlags.Properties);
131 
132  return true;
133  }
134 
135  public void ClientAdminRead(IReadMessage incMsg)
136  {
137  bool hasPermission = incMsg.ReadBoolean();
138  if (!hasPermission)
139  {
140  incMsg.ReadPadBits();
141  return;
142  }
143 
144  bool isOwner = incMsg.ReadBoolean();
145  incMsg.ReadPadBits();
146 
147  bannedPlayers.Clear();
148  UInt32 bannedPlayerCount = incMsg.ReadVariableUInt32();
149 
150  for (int i = 0; i < (int)bannedPlayerCount; i++)
151  {
152  string name = incMsg.ReadString();
153  UInt32 uniqueIdentifier = incMsg.ReadUInt32();
154  bool includesExpiration = incMsg.ReadBoolean();
155  incMsg.ReadPadBits();
156 
157  Option<SerializableDateTime> expiration = Option<SerializableDateTime>.None();
158  if (includesExpiration)
159  {
160  double hoursFromNow = incMsg.ReadDouble();
161  expiration = Option<SerializableDateTime>.Some(SerializableDateTime.LocalNow + TimeSpan.FromHours(hoursFromNow));
162  }
163 
164  string reason = incMsg.ReadString();
165 
166  Either<Address, AccountId> addressOrAccountId;
167  if (isOwner)
168  {
169  bool isAddress = incMsg.ReadBoolean();
170  incMsg.ReadPadBits();
171  string str = incMsg.ReadString();
172  if (isAddress && Address.Parse(str).TryUnwrap(out var address))
173  {
174  addressOrAccountId = address;
175  }
176  else if (AccountId.Parse(str).TryUnwrap(out var accountId))
177  {
178  addressOrAccountId = accountId;
179  }
180  else
181  {
182  continue;
183  }
184  }
185  else
186  {
187  addressOrAccountId = new UnknownAddress();
188  }
189  bannedPlayers.Add(new BannedPlayer(uniqueIdentifier, name, addressOrAccountId, reason, expiration));
190  }
191 
192  if (BanFrame != null)
193  {
194  var parent = BanFrame.Parent;
195  parent.RemoveChild(BanFrame);
196  CreateBanFrame(parent);
197  }
198  }
199 
200  public void ClientAdminWrite(IWriteMessage outMsg)
201  {
202  outMsg.WriteVariableUInt32((UInt32)localRemovedBans.Count);
203  foreach (UInt32 uniqueId in localRemovedBans)
204  {
205  outMsg.WriteUInt32(uniqueId);
206  }
207 
208  localRemovedBans.Clear();
209  }
210  }
211 }
virtual void RemoveChild(GUIComponent child)
Definition: GUIComponent.cs:87
RectTransform RectTransform
Vector2 MeasureString(LocalizedString str, bool removeExtraSpacing=false)
Definition: GUIPrefab.cs:284
override GUIFont Font
Definition: GUITextBlock.cs:66
static GameClient Client
Definition: GameMain.cs:188
LocalizedString Fallback(LocalizedString fallback, bool useDefaultLanguageIfFound=true)
Use this text instead if the original text cannot be found.
BannedPlayer(UInt32 uniqueIdentifier, string name, Either< Address, AccountId > addressOrAccountId, string reason, Option< SerializableDateTime > expiration)
bool HasPermission(ClientPermissions permission)
Definition: GameClient.cs:2620
Point?? MinSize
Min size in pixels. Does not affect scaling.
DateTime wrapper that tries to offer a reliable string representation that's also human-friendly
static SerializableDateTime LocalNow