-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathProfile.CompactInfo.cs
More file actions
293 lines (238 loc) · 10.8 KB
/
Copy pathProfile.CompactInfo.cs
File metadata and controls
293 lines (238 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
using CommunicationData.URLHelpers;
using DCL.Diagnostics;
using DCL.FeatureFlags;
using DCL.Utilities;
using DCL.Web3;
using ECS.StreamableLoading.Common;
using ECS.StreamableLoading.Common.Components;
using ECS.StreamableLoading.Textures;
using System;
using UnityEngine;
namespace DCL.Profiles
{
public partial class Profile
{
/// <summary>
/// Use it for internal assignments only
/// </summary>
/// <returns></returns>
internal ref CompactInfo GetCompact() =>
ref compact;
public Color UserNameColor => compact.UserNameColor;
public string UserId
{
get => compact.UserId;
set => compact.UserId = value;
}
public string Name
{
get => compact.Name;
set => compact.Name = value;
}
public bool HasClaimedName
{
get => compact.HasClaimedName;
set => compact.HasClaimedName = value;
}
public Color? ClaimedNameColor
{
get => compact.ClaimedNameColor;
set => compact.ClaimedNameColor = value;
}
public string? WalletId => compact.WalletId;
public string ValidatedName => compact.ValidatedName;
public string UnclaimedName => compact.UnclaimedName;
public string DisplayName => compact.DisplayName;
public string MentionName => compact.MentionName;
/// <summary>
/// A small slice of the profile info used when the full information is not required <br />
/// CompactInfo is never requested with a specific version/timestamp
/// </summary>
public struct CompactInfo : IEquatable<CompactInfo>, IDisposable
{
private string name;
private string userId;
private bool hasClaimedName;
/// <summary>
/// The custom color selected by users with claimed names for their username display
/// </summary>
private Color? claimedNameColor;
// TODO it's not unified with SpriteCache from where UI requests profile thumbnails
public StreamableLoadingResult<SpriteData>.WithFallback? ProfilePicture { get; set; }
// Tracks the in-flight face snapshot download; O(1) cancellation on update and ownership transfer on profile replacement.
public AssetPromise<TextureData, GetTextureIntention>? PicturePromise { get; set; }
public CompactInfo(string userId) : this(userId, "", false, "", null) { }
public CompactInfo(string userId, string name, bool hasClaimedName = false, string faceUrl = "", Color? claimedNameColor = null) : this()
{
this.name = name;
UpdateUserId(userId, true);
HasClaimedName = hasClaimedName;
FaceSnapshotUrl = string.IsNullOrEmpty(faceUrl) ? default(URLAddress) : URLAddress.FromString(faceUrl);
this.claimedNameColor = claimedNameColor;
}
/// <summary>
/// Is the complete wallet address of the user
/// </summary>
public string UserId
{
get => userId;
set => UpdateUserId(value, true);
}
/// <summary>
/// Lowercase of <see cref="UserId" />
/// </summary>
public Web3Address Address { get; private set; }
public string Name
{
get => name;
set
{
name = value;
GenerateAndValidateName();
}
}
/// <summary>
/// The custom color for users with claimed names. When set, overrides the default username color.
/// Can only be set if the user has a claimed name.
/// </summary>
public Color? ClaimedNameColor
{
get => claimedNameColor;
set
{
if (!hasClaimedName)
{
ReportHub.LogWarning(ReportCategory.PROFILE, "Cannot set claimed name color for a user without a claimed name");
return;
}
claimedNameColor = value;
if (FeaturesRegistry.Instance.IsEnabled(FeatureId.NAME_COLOR_CHANGE) && value is { a: > 0 })
UserNameColor = value.Value;
}
}
/// <summary>
/// The color calculated for this username
/// </summary>
public Color UserNameColor { get; internal set; }
/// <summary>
/// The name of the user after passing character validation, without the # part
/// For users with claimed names would be the same as DisplayName
/// </summary>
public string ValidatedName { get; private set; }
/// <summary>
/// The # part of the name for users without claimed name, will be null for users with a claimed name, includes the # character at the beginning
/// </summary>
public string? WalletId { get; private set; }
/// <summary>
/// The name of the user after passing character validation, including the
/// Wallet Id (if the user doesnt have a claimed name)
/// </summary>
public string DisplayName { get; private set; }
public string UnclaimedName { get; internal set; }
public URLAddress FaceSnapshotUrl { get; internal set; }
public bool HasClaimedName
{
get => hasClaimedName;
internal set
{
hasClaimedName = value;
GenerateAndValidateName();
}
}
/// <summary>
/// The Display Name with @ before it. Cached here to avoid re-allocations.
/// </summary>
public string MentionName { get; private set; }
private void UpdateUserId(string value, bool generateAndValidateName = true)
{
userId = value;
Address = new Web3Address(value);
if (generateAndValidateName)
GenerateAndValidateName();
}
private void GenerateAndValidateName()
{
ValidatedName = string.Empty;
DisplayName = string.Empty;
WalletId = null;
if (string.IsNullOrEmpty(Name)) return;
// Use stackalloc for reasonable-sized names, fallback to heap for very long names
const int MAX_STACK_SIZE = 256;
Span<char> buffer = Name.Length <= MAX_STACK_SIZE
? stackalloc char[Name.Length]
: new char[Name.Length];
int validLength = 0;
// Filter valid alphanumeric characters without allocations
foreach (char c in Name)
{
if (char.IsLetterOrDigit(c))
buffer[validLength++] = c;
}
if (validLength == 0) return;
ReadOnlySpan<char> validatedSpan = buffer[..validLength];
ValidatedName = new string(validatedSpan);
if (!HasClaimedName && !string.IsNullOrEmpty(UserId) && UserId.Length > 4)
{
ReadOnlySpan<char> lastFourChars = UserId.AsSpan(UserId.Length - 4, 4);
// Build WalletId: #XXXX
Span<char> walletBuffer = stackalloc char[5];
walletBuffer[0] = '#';
lastFourChars.CopyTo(walletBuffer[1..]);
WalletId = new string(walletBuffer);
// Build DisplayName: {validated}#XXXX
Span<char> displayBuffer = validLength + 5 <= MAX_STACK_SIZE
? stackalloc char[validLength + 5]
: new char[validLength + 5];
validatedSpan.CopyTo(displayBuffer);
displayBuffer[validLength] = '#';
lastFourChars.CopyTo(displayBuffer[(validLength + 1)..]);
DisplayName = new string(displayBuffer);
// Build MentionName: @{validated}#XXXX
Span<char> mentionBuffer = validLength + 6 <= MAX_STACK_SIZE
? stackalloc char[validLength + 6]
: new char[validLength + 6];
mentionBuffer[0] = '@';
validatedSpan.CopyTo(mentionBuffer[1..]);
mentionBuffer[validLength + 1] = '#';
lastFourChars.CopyTo(mentionBuffer[(validLength + 2)..]);
MentionName = new string(mentionBuffer);
}
else
{
DisplayName = ValidatedName;
// Build MentionName: @{validated}
Span<char> mentionBuffer = validLength + 1 <= MAX_STACK_SIZE
? stackalloc char[validLength + 1]
: new char[validLength + 1];
mentionBuffer[0] = '@';
validatedSpan.CopyTo(mentionBuffer[1..]);
MentionName = new string(mentionBuffer);
}
if (FeaturesRegistry.Instance.IsEnabled(FeatureId.NAME_COLOR_CHANGE) && claimedNameColor is { a: > 0 })
UserNameColor = claimedNameColor.Value;
else
UserNameColor = NameColorHelper.GetNameColor(DisplayName);
}
public bool Equals(CompactInfo other) =>
name == other.name && userId == other.userId && hasClaimedName == other.hasClaimedName;
public override bool Equals(object? obj)
{
if (obj is null) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((CompactInfo)obj);
}
public override int GetHashCode() =>
HashCode.Combine(name, userId, hasClaimedName);
public void Dispose()
{
// Cancel the in-flight download if any; LoadSystemBase will clean up the loading entity on its own when it observes cancellation.
PicturePromise?.LoadingIntention.CancellationTokenSource.Cancel();
PicturePromise = null;
// Nulling the reference keeps Dispose idempotent for any caller that reaches it twice.
ProfilePicture.TryDereference();
ProfilePicture = null;
}
}
}
}