-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAangConfiguration.cs
More file actions
379 lines (337 loc) · 12.3 KB
/
Copy pathAangConfiguration.cs
File metadata and controls
379 lines (337 loc) · 12.3 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Services;
using UnityEngine;
using Utils;
public class AangConfiguration
{
public static AangConfiguration Instance { get; private set; } = new();
private AangConfiguration()
{
}
/// <summary>
/// The mode in which the preview will run.
/// </summary>
public PreviewMode Mode { get; private set; } = PreviewMode.Profile;
/// <summary>
/// Converts the string value to the appropriate mode. Defaults to Marketplace.
/// </summary>
/// <param name="value"></param>
public void SetMode(string value)
{
Mode = value switch
{
"profile" => PreviewMode.Profile,
"jesus" => PreviewMode.Jesus,
"authentication" => PreviewMode.Authentication,
"builder" => PreviewMode.Builder,
"configurator" => PreviewMode.Configurator,
_ => PreviewMode.Marketplace
};
}
/// <summary>
/// An ethereum address of a profile to load as the base avatar.
/// It can be set to default or a numbered default profile like default15 to use a default profile.
/// </summary>
public string Profile { get; set; } = "default1";
/// <summary>
/// The emote that the avatar will play. Default value is idle, other possible values are:
/// clap, dab, dance, fashion, fashion-2, fashion-3,fashion-4, love, money, fist-pump and head-explode
/// </summary>
public string Emote { get; set; } = "idle";
/// <summary>
/// The base64 encoded GLB to load.
/// </summary>
public List<byte[]> Base64 { get; } = new();
/// <summary>
/// Converts the string base64 to a byte array and adds it to the list, adding padding if needed
/// </summary>
public void AddBase64(string value)
{
var sanitized = (value.Length % 4) switch
{
2 => value + "==",
3 => value + "=",
0 => value,
_ => throw new FormatException("Invalid Base64 string")
};
Base64.Add(Convert.FromBase64String(sanitized));
}
/// <summary>
/// A URN of a wearable or an emote to load. If it is a wearable, it will override anything loaded from a profile.
/// </summary>
public List<string> Urns { get; set; } = new();
/// <summary>
/// Sanitizes an URN and adds it to the list.
/// </summary>
public void AddURN(string urn)
{
Urns.Add(URNUtils.SanitizeURN(urn));
}
/// <summary>
/// The color of the background in HEX.
/// </summary>
public Color Background { get; private set; } = new(0, 0, 0, 0);
/// <summary>
/// Sets the background color from a hex string. The string must not contain a leading #.
/// </summary>
public void SetBackground(string value)
{
if (ColorUtility.TryParseHtmlString("#" + value, out var color))
{
Background = color;
}
else
{
Background = Color.black;
Debug.LogError("Failed to parse background color");
}
}
/// <summary>
/// The color of the skin in HEX.
/// </summary>
public Color? SkinColor { get; private set; }
/// <summary>
/// Sets the skin color from a hex string. The string must not contain a leading #.
/// </summary>
public void SetSkinColor(string value)
{
if (ColorUtility.TryParseHtmlString("#" + value, out var color))
{
SkinColor = color;
}
else
{
SkinColor = null;
Debug.LogError("Failed to parse skin color");
}
}
/// <summary>
/// The color of the hair in HEX.
/// </summary>
public Color? HairColor { get; private set; }
/// <summary>
/// Sets the skin color from a hex string. The string must not contain a leading #.
/// </summary>
public void SetHairColor(string value)
{
if (ColorUtility.TryParseHtmlString("#" + value, out var color))
{
HairColor = color;
}
else
{
HairColor = null;
Debug.LogError("Failed to parse hair color");
}
}
/// <summary>
/// The color of the eyes in HEX.
/// </summary>
public Color? EyeColor { get; private set; }
/// <summary>
/// Sets the skin color from a hex string. The string must not contain a leading #.
/// </summary>
public void SetEyeColor(string value)
{
if (ColorUtility.TryParseHtmlString("#" + value, out var color))
{
EyeColor = color;
}
else
{
EyeColor = null;
Debug.LogError("Failed to parse eye color");
}
}
/// <summary>
/// The body shape URN (urn:decentraland:off-chain:base-avatars:BaseMale or urn:decentraland:off-chain:base-avatars:BaseFemale)
/// </summary>
public string BodyShape { get; set; }
/// <summary>
/// Shows or hides the animation reference platform.
/// </summary>
public bool ShowAnimationReference { get; set; }
/// <summary>
/// If we're using orthographic projection.
/// </summary>
public string Projection { get; set; } = "perspective";
/// <summary>
/// The contract address of the wearable collection.
/// </summary>
public string Contract { get; set; }
/// <summary>
/// The id of the item in the collection.
/// </summary>
public string ItemID { get; set; }
/// <summary>
/// The id of the token (to preview a specific NFT).
/// </summary>
public string TokenID { get; set; }
/// <summary>
/// If the loading circle should be shown when loading content.
/// </summary>
public bool DisableLoader { get; set; }
/// <summary>
/// If we should instruct the browser to pre-fetch certain files. On by default.
/// </summary>
public bool UseBrowserPreload { get; set; } = true;
/// <summary>
/// The username used in the avatar creator
/// </summary>
public string Username { get; set; }
/// <summary>
/// Shows or hides the FPS counter.
/// </summary>
public bool ShowFPS { get; set; }
/// <summary>
/// If true we load individual items for an avatar one after another, instead of concurrently.
/// </summary>
public bool ConcurrentLoad { get; set; } = !Application.isMobilePlatform;
/// <summary>
/// If true we use the <see cref="GLTFast.UninterruptedDeferAgent"/> for loading GLB files. If false, the
/// default frame time limit one is used. Using the uninterrupted agent will produce faster loads but might
/// case FPS dips / crashes on mobile devices.
/// </summary>
public bool UninterruptedDeferAgent { get; set; } = !Application.isMobilePlatform;
/// <summary>
/// If true, facial features (eyes, eyebrows, mouth) will be hidden on the avatar.
/// </summary>
public bool DisableFace { get; set; }
public static void RecreateFrom(string url)
{
Instance = new AangConfiguration();
if (string.IsNullOrEmpty(url) || !url.Contains('?')) return;
var split = url[(url.IndexOf('?') + 1)..].Split('&');
if (split.Length == 0) return;
foreach (var parameter in split)
{
var keyValueSplit = parameter.Split('=');
var key = HttpUtility.UrlDecode(keyValueSplit[0]).Trim();
var value = (keyValueSplit.Length > 1 ? HttpUtility.UrlDecode(keyValueSplit[1]) : string.Empty).Trim();
switch (key)
{
case "mode":
Instance.SetMode(value);
break;
case "profile":
Instance.Profile = value;
break;
case "emote":
Instance.Emote = value;
break;
case "urn":
Instance.AddURN(value);
break;
case "background":
Instance.SetBackground(value);
break;
case "skinColor":
Instance.SetSkinColor(value);
break;
case "hairColor":
Instance.SetHairColor(value);
break;
case "eyeColor":
Instance.SetEyeColor(value);
break;
case "bodyShape":
Instance.BodyShape = value;
break;
case "showAnimationReference":
Instance.ShowAnimationReference = bool.Parse(value);
break;
case "projection":
Instance.Projection = value;
break;
case "base64":
Instance.AddBase64(value);
break;
case "contract":
Instance.Contract = value;
break;
case "item":
Instance.ItemID = value;
break;
case "token":
Instance.TokenID = value;
break;
case "env":
APIService.Environment = value == "dev" ? "zone" : "org";
Debug.Log($"Using environment {APIService.Environment} (env value=[{value}])");
break;
case "disableLoader":
Instance.DisableLoader = bool.Parse(value);
break;
case "useBrowserPreload":
Instance.UseBrowserPreload = bool.Parse(value);
break;
case "username":
Instance.Username = value;
break;
case "showFPS":
Instance.ShowFPS = bool.Parse(value);
break;
case "concurrentLoad":
Instance.ConcurrentLoad = bool.Parse(value);
break;
case "uninterruptedDeferAgent":
Instance.UninterruptedDeferAgent = bool.Parse(value);
break;
case "disableFace":
Instance.DisableFace = bool.Parse(value);
break;
default:
Debug.LogWarning($"Unknown parameter in URL: {key}");
break;
}
}
}
public override string ToString()
{
var sb = new StringBuilder("?");
sb.AppendFormat("mode={0}", Mode.ToString().ToLowerInvariant());
sb.AppendFormat("&profile={0}", Profile);
sb.AppendFormat("&emote={0}", Emote);
foreach (var urn in Urns)
sb.AppendFormat("&urn={0}", urn);
sb.AppendFormat("&background={0}", ColorUtility.ToHtmlStringRGBA(Background));
if (SkinColor.HasValue)
sb.AppendFormat("&skinColor={0}", ColorUtility.ToHtmlStringRGB(SkinColor.Value));
if (HairColor.HasValue)
sb.AppendFormat("&hairColor={0}", ColorUtility.ToHtmlStringRGB(HairColor.Value));
if (EyeColor.HasValue)
sb.AppendFormat("&eyeColor={0}", ColorUtility.ToHtmlStringRGB(EyeColor.Value));
if (!string.IsNullOrEmpty(BodyShape))
sb.AppendFormat("&bodyShape={0}", BodyShape);
sb.AppendFormat("&showAnimationReference={0}", ShowAnimationReference);
sb.AppendFormat("&projection={0}", Projection);
foreach (var bytes in Base64)
sb.AppendFormat("&base64={0}", Convert.ToBase64String(bytes));
if (!string.IsNullOrEmpty(Contract))
sb.AppendFormat("&contract={0}", Contract);
if (!string.IsNullOrEmpty(ItemID))
sb.AppendFormat("&item={0}", ItemID);
if (!string.IsNullOrEmpty(TokenID))
sb.AppendFormat("&token={0}", TokenID);
sb.AppendFormat("&env={0}", APIService.Environment == "zone" ? "dev" : "prod");
sb.AppendFormat("&disableLoader={0}", DisableLoader);
sb.AppendFormat("&useBrowserPreload={0}", UseBrowserPreload);
sb.AppendFormat("&username={0}", Username);
sb.AppendFormat("&showFPS={0}", ShowFPS);
sb.AppendFormat("&sequentialLoad={0}", ConcurrentLoad);
sb.AppendFormat("&useUninterruptedDeferAgent={0}", UninterruptedDeferAgent);
return sb.ToString();
}
}
public enum PreviewMode
{
Marketplace,
Authentication,
Profile,
Builder,
Configurator,
Jesus,
}