| | 1 | | using DCL; |
| | 2 | | using DCL.Emotes; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | | using UnityEngine; |
| | 7 | | using static WearableLiterals; |
| | 8 | |
|
| | 9 | | // TODO: We need to separate this entity into WearableItem and EmoteItem they both can inherit a EntityItem and just hav |
| | 10 | | [Serializable] |
| | 11 | | public class WearableItem |
| | 12 | | { |
| | 13 | | private const string THIRD_PARTY_COLLECTIONS_PATH = "collections-thirdparty"; |
| 1 | 14 | | public static readonly IList<string> CATEGORIES_PRIORITY = new List<string> |
| | 15 | | { |
| | 16 | | Categories.SKIN, |
| | 17 | | Categories.UPPER_BODY, |
| | 18 | | Categories.HANDS_WEAR, |
| | 19 | | Categories.LOWER_BODY, |
| | 20 | | Categories.FEET, |
| | 21 | | Categories.HELMET, |
| | 22 | | Categories.HAT, |
| | 23 | | Categories.TOP_HEAD, |
| | 24 | | Categories.MASK, |
| | 25 | | Categories.EYEWEAR, |
| | 26 | | Categories.EARRING, |
| | 27 | | Categories.TIARA, |
| | 28 | | Categories.HAIR, |
| | 29 | | Categories.EYEBROWS, |
| | 30 | | Categories.EYES, |
| | 31 | | Categories.MOUTH, |
| | 32 | | Categories.FACIAL_HAIR, |
| | 33 | | Categories.BODY_SHAPE, |
| | 34 | | }; |
| | 35 | |
|
| 1 | 36 | | public static readonly Dictionary<string, string> CATEGORIES_READABLE_MAPPING = new () |
| | 37 | | { |
| | 38 | | { Categories.SKIN, "Skin" }, |
| | 39 | | { Categories.UPPER_BODY, "Upper body" }, |
| | 40 | | { Categories.LOWER_BODY, "Lower body" }, |
| | 41 | | { Categories.FEET, "Feet" }, |
| | 42 | | { Categories.HELMET, "Helmet" }, |
| | 43 | | { Categories.HAT, "Hat" }, |
| | 44 | | { Categories.TOP_HEAD, "Top Head" }, |
| | 45 | | { Categories.MASK, "Mask" }, |
| | 46 | | { Categories.EYEWEAR, "Eyewear" }, |
| | 47 | | { Categories.EARRING, "Earring" }, |
| | 48 | | { Categories.TIARA, "Tiara" }, |
| | 49 | | { Categories.EYES, "Eyes" }, |
| | 50 | | { Categories.MOUTH, "Mouth" }, |
| | 51 | | { Categories.HAIR, "Hair" }, |
| | 52 | | { Categories.EYEBROWS, "Eyebrows" }, |
| | 53 | | { Categories.BODY_SHAPE, "Body shape" }, |
| | 54 | | { Categories.FACIAL_HAIR, "Facial hair" }, |
| | 55 | | { Categories.HANDS_WEAR, "Handwear" }, |
| | 56 | | }; |
| | 57 | |
|
| 1 | 58 | | public static readonly string[] SKIN_IMPLICIT_CATEGORIES = |
| | 59 | | { |
| | 60 | | Categories.EYES, |
| | 61 | | Categories.MOUTH, |
| | 62 | | Categories.EYEBROWS, |
| | 63 | | Categories.HAIR, |
| | 64 | | Categories.UPPER_BODY, |
| | 65 | | Categories.LOWER_BODY, |
| | 66 | | Categories.FEET, |
| | 67 | | Categories.HANDS, |
| | 68 | | Categories.HANDS_WEAR, |
| | 69 | | Categories.HEAD, |
| | 70 | | Categories.FACIAL_HAIR |
| | 71 | | }; |
| | 72 | |
|
| 1 | 73 | | public static readonly string[] UPPER_BODY_DEFAULT_HIDES = |
| | 74 | | { |
| | 75 | | Categories.HANDS, |
| | 76 | | }; |
| | 77 | |
|
| | 78 | | [Serializable] |
| | 79 | | public class MappingPair |
| | 80 | | { |
| | 81 | | public string key; |
| | 82 | | public string hash; |
| | 83 | | public string url; |
| | 84 | | } |
| | 85 | |
|
| | 86 | | [Serializable] |
| | 87 | | public class Representation |
| | 88 | | { |
| | 89 | | public string[] bodyShapes; |
| | 90 | | public string mainFile; |
| | 91 | | public MappingPair[] contents; |
| | 92 | | public string[] overrideHides; |
| | 93 | | public string[] overrideReplaces; |
| | 94 | | } |
| | 95 | |
|
| | 96 | | [Serializable] |
| | 97 | | public class Data |
| | 98 | | { |
| | 99 | | public Representation[] representations; |
| | 100 | | public string category; |
| | 101 | | public string[] tags; |
| | 102 | | public string[] replaces; |
| | 103 | | public string[] hides; |
| | 104 | | public string[] removesDefaultHiding; |
| | 105 | | public bool loop; |
| | 106 | | public bool blockVrmExport; |
| | 107 | | } |
| | 108 | |
|
| | 109 | | public Data data; |
| | 110 | | public EmoteDataV0 emoteDataV0; |
| | 111 | | public string id; // urn |
| | 112 | | public string entityId; |
| | 113 | |
|
| | 114 | | public string baseUrl; |
| | 115 | | public string baseUrlBundles; |
| | 116 | |
|
| | 117 | | public i18n[] i18n; |
| | 118 | | public string thumbnail; |
| | 119 | |
|
| 38 | 120 | | public DateTime MostRecentTransferredDate { get; set; } |
| | 121 | |
|
| | 122 | | private string thirdPartyCollectionId; |
| | 123 | |
|
| | 124 | | public string ThirdPartyCollectionId |
| | 125 | | { |
| | 126 | | get |
| | 127 | | { |
| 0 | 128 | | if (!string.IsNullOrEmpty(thirdPartyCollectionId)) |
| 0 | 129 | | return thirdPartyCollectionId; |
| | 130 | |
|
| 0 | 131 | | if (!id.Contains(THIRD_PARTY_COLLECTIONS_PATH)) |
| 0 | 132 | | return ""; |
| | 133 | |
|
| 0 | 134 | | var paths = id.Split(':'); |
| 0 | 135 | | var thirdPartyIndex = Array.IndexOf(paths, THIRD_PARTY_COLLECTIONS_PATH); |
| 0 | 136 | | thirdPartyCollectionId = string.Join(":", paths, 0, thirdPartyIndex + 2); |
| 0 | 137 | | return thirdPartyCollectionId; |
| | 138 | | } |
| | 139 | | } |
| | 140 | |
|
| 0 | 141 | | public bool IsFromThirdPartyCollection => !string.IsNullOrEmpty(ThirdPartyCollectionId); |
| | 142 | |
|
| | 143 | | public Sprite thumbnailSprite; |
| | 144 | |
|
| | 145 | | //This fields are temporary, once Kernel is finished we must move them to wherever they are placed |
| | 146 | | public int amount; |
| | 147 | | public string rarity; |
| | 148 | | public string description; |
| | 149 | | public int issuedId; |
| | 150 | |
|
| 2983 | 151 | | private Dictionary<string, string> cachedI18n = new (); |
| 2983 | 152 | | private Dictionary<string, ContentProvider> cachedContentProviers = new (); |
| | 153 | |
|
| | 154 | | public bool TryGetRepresentation(string bodyshapeId, out Representation representation) |
| | 155 | | { |
| 0 | 156 | | representation = GetRepresentation(bodyshapeId); |
| 0 | 157 | | return representation != null; |
| | 158 | | } |
| | 159 | |
|
| | 160 | | public Representation GetRepresentation(string bodyShapeType) |
| | 161 | | { |
| 74 | 162 | | if (data?.representations == null) |
| 2 | 163 | | return null; |
| | 164 | |
|
| 288 | 165 | | for (int i = 0; i < data.representations.Length; i++) |
| | 166 | | { |
| 104 | 167 | | if (data.representations[i].bodyShapes.Contains(bodyShapeType)) { return data.representations[i]; } |
| | 168 | | } |
| | 169 | |
|
| 56 | 170 | | return null; |
| | 171 | | } |
| | 172 | |
|
| | 173 | | public ContentProvider GetContentProvider(string bodyShapeType) |
| | 174 | | { |
| 2 | 175 | | var representation = GetRepresentation(bodyShapeType); |
| | 176 | |
|
| 2 | 177 | | if (representation == null) |
| 0 | 178 | | return null; |
| | 179 | |
|
| 2 | 180 | | cachedContentProviers ??= new Dictionary<string, ContentProvider>(); |
| | 181 | |
|
| 2 | 182 | | if (!cachedContentProviers.ContainsKey(bodyShapeType)) |
| | 183 | | { |
| 1 | 184 | | var contentProvider = CreateContentProvider(baseUrl, representation.contents); |
| 1 | 185 | | contentProvider.BakeHashes(); |
| 1 | 186 | | cachedContentProviers.Add(bodyShapeType, contentProvider); |
| | 187 | | } |
| | 188 | |
|
| 2 | 189 | | return cachedContentProviers[bodyShapeType]; |
| | 190 | | } |
| | 191 | |
|
| | 192 | | protected virtual ContentProvider CreateContentProvider(string baseUrl, MappingPair[] contents) |
| | 193 | | { |
| 0 | 194 | | return new ContentProvider |
| | 195 | | { |
| | 196 | | baseUrl = baseUrl, |
| | 197 | | assetBundlesBaseUrl = baseUrlBundles, |
| 0 | 198 | | contents = contents.Select(mapping => new ContentServerUtils.MappingPair() |
| | 199 | | { file = mapping.key, hash = mapping.hash }) |
| | 200 | | .ToList() |
| | 201 | | }; |
| | 202 | | } |
| | 203 | |
|
| | 204 | | public bool SupportsBodyShape(string bodyShapeType) |
| | 205 | | { |
| 19 | 206 | | if (data?.representations == null) |
| 7 | 207 | | return false; |
| | 208 | |
|
| 44 | 209 | | for (int i = 0; i < data.representations.Length; i++) |
| | 210 | | { |
| 14 | 211 | | if (data.representations[i].bodyShapes.Contains(bodyShapeType)) { return true; } |
| | 212 | | } |
| | 213 | |
|
| 10 | 214 | | return false; |
| | 215 | | } |
| | 216 | |
|
| | 217 | | public string[] GetReplacesList(string bodyShapeType) |
| | 218 | | { |
| 35 | 219 | | var representation = GetRepresentation(bodyShapeType); |
| | 220 | |
|
| 35 | 221 | | if (representation?.overrideReplaces == null || representation.overrideReplaces.Length == 0) |
| 35 | 222 | | return data.replaces; |
| | 223 | |
|
| 0 | 224 | | return representation.overrideReplaces; |
| | 225 | | } |
| | 226 | |
|
| | 227 | | public string[] GetHidesList(string bodyShapeType) |
| | 228 | | { |
| 35 | 229 | | var representation = GetRepresentation(bodyShapeType); |
| | 230 | |
|
| 35 | 231 | | HashSet<string> hides = new HashSet<string>(); |
| | 232 | |
|
| 35 | 233 | | if (representation?.overrideHides == null || representation.overrideHides.Length == 0) |
| 35 | 234 | | hides.UnionWith(data.hides ?? Enumerable.Empty<string>()); |
| | 235 | | else |
| 0 | 236 | | hides.UnionWith(representation.overrideHides); |
| | 237 | |
|
| 35 | 238 | | if (IsSkin()) |
| 0 | 239 | | hides.UnionWith(SKIN_IMPLICIT_CATEGORIES); |
| | 240 | |
|
| | 241 | | // we apply this rule to hide the hands by default if the wearable is an upper body or hides the upper body |
| 35 | 242 | | bool isOrHidesUpperBody = hides.Contains(Categories.UPPER_BODY) || data.category == Categories.UPPER_BODY; |
| | 243 | |
|
| | 244 | | // the rule is ignored if the wearable contains the removal of this default rule (newer upper bodies since the r |
| 35 | 245 | | bool removesHandDefault = data.removesDefaultHiding?.Contains(Categories.HANDS) ?? false; |
| | 246 | |
|
| | 247 | | // why we do this? because old upper bodies contains the base hand mesh, and they might clip with the new handwe |
| 35 | 248 | | if (isOrHidesUpperBody && !removesHandDefault) |
| 11 | 249 | | hides.UnionWith(UPPER_BODY_DEFAULT_HIDES); |
| | 250 | |
|
| 35 | 251 | | string[] replaces = GetReplacesList(bodyShapeType); |
| | 252 | |
|
| 35 | 253 | | if (replaces != null) |
| 35 | 254 | | hides.UnionWith(replaces); |
| | 255 | |
|
| | 256 | | // Safeguard so no wearable can hide itself |
| 35 | 257 | | hides.Remove(data.category); |
| | 258 | |
|
| 35 | 259 | | return hides.ToArray(); |
| | 260 | | } |
| | 261 | |
|
| | 262 | | public void SanitizeHidesLists() |
| | 263 | | { |
| | 264 | | //remove bodyshape from hides list |
| 28 | 265 | | if (data.hides != null) |
| 10 | 266 | | data.hides = data.hides.Except(new[] { Categories.BODY_SHAPE }).ToArray(); |
| | 267 | |
|
| 112 | 268 | | for (int i = 0; i < data.representations.Length; i++) |
| | 269 | | { |
| 28 | 270 | | Representation representation = data.representations[i]; |
| | 271 | |
|
| 28 | 272 | | if (representation.overrideHides != null) |
| 11 | 273 | | representation.overrideHides = representation.overrideHides.Except(new[] { Categories.BODY_SHAPE }).ToAr |
| | 274 | | } |
| 28 | 275 | | } |
| | 276 | |
|
| | 277 | | public bool DoesHide(string category, string bodyShape) => |
| 0 | 278 | | GetHidesList(bodyShape).Any(s => s == category); |
| | 279 | |
|
| | 280 | | public bool IsCollectible() |
| | 281 | | { |
| 0 | 282 | | if (id == null) |
| 0 | 283 | | return false; |
| | 284 | |
|
| 0 | 285 | | return !id.StartsWith("urn:decentraland:off-chain:base-avatars:"); |
| | 286 | | } |
| | 287 | |
|
| | 288 | | public bool IsSkin() => |
| 35 | 289 | | data.category == Categories.SKIN; |
| | 290 | |
|
| | 291 | | public bool IsSmart() |
| | 292 | | { |
| 21 | 293 | | if (data?.representations == null) |
| 14 | 294 | | return false; |
| | 295 | |
|
| 28 | 296 | | for (var i = 0; i < data.representations.Length; i++) |
| | 297 | | { |
| 7 | 298 | | var representation = data.representations[i]; |
| 7 | 299 | | var containsGameJs = representation.contents?.Any(pair => pair.key.EndsWith("game.js")) ?? false; |
| | 300 | |
|
| 7 | 301 | | if (containsGameJs) |
| 0 | 302 | | return true; |
| | 303 | | } |
| | 304 | |
|
| 7 | 305 | | return false; |
| | 306 | | } |
| | 307 | |
|
| | 308 | | public string GetName(string langCode = "en") |
| | 309 | | { |
| 1 | 310 | | cachedI18n ??= new Dictionary<string, string>(); |
| 2 | 311 | | cachedI18n.TryAdd(langCode, i18n.FirstOrDefault(x => x.code == langCode)?.text); |
| 1 | 312 | | return cachedI18n[langCode]; |
| | 313 | | } |
| | 314 | |
|
| | 315 | | public int GetIssuedCountFromRarity(string rarity) |
| | 316 | | { |
| | 317 | | switch (rarity) |
| | 318 | | { |
| | 319 | | case ItemRarity.RARE: |
| 0 | 320 | | return 5000; |
| | 321 | | case ItemRarity.EPIC: |
| 0 | 322 | | return 1000; |
| | 323 | | case ItemRarity.LEGENDARY: |
| 0 | 324 | | return 100; |
| | 325 | | case ItemRarity.MYTHIC: |
| 0 | 326 | | return 10; |
| | 327 | | case ItemRarity.EXOTIC: |
| 0 | 328 | | return 50; |
| | 329 | | case ItemRarity.UNIQUE: |
| 0 | 330 | | return 1; |
| | 331 | | } |
| | 332 | |
|
| 0 | 333 | | return int.MaxValue; |
| | 334 | | } |
| | 335 | |
|
| | 336 | | public string ComposeThumbnailUrl() |
| | 337 | | { |
| 22 | 338 | | return baseUrl + thumbnail; |
| | 339 | | } |
| | 340 | |
|
| | 341 | | public static HashSet<string> ComposeHiddenCategories(string bodyShapeId, List<WearableItem> wearables) |
| | 342 | | { |
| 0 | 343 | | HashSet<string> result = new HashSet<string>(); |
| | 344 | |
|
| | 345 | | //Last wearable added has priority over the rest |
| 0 | 346 | | for (int index = 0; index < wearables.Count; index++) |
| | 347 | | { |
| 0 | 348 | | WearableItem wearableItem = wearables[index]; |
| | 349 | |
|
| 0 | 350 | | if (wearableItem == null) |
| | 351 | | continue; |
| | 352 | |
|
| 0 | 353 | | if (result.Contains(wearableItem.data.category)) //Skip hidden elements to avoid two elements hiding each ot |
| | 354 | | continue; |
| | 355 | |
|
| 0 | 356 | | string[] wearableHidesList = wearableItem.GetHidesList(bodyShapeId); |
| | 357 | |
|
| 0 | 358 | | if (wearableHidesList != null) { result.UnionWith(wearableHidesList); } |
| | 359 | | } |
| | 360 | |
|
| 0 | 361 | | return result; |
| | 362 | | } |
| | 363 | |
|
| | 364 | | public static HashSet<string> ComposeHiddenCategoriesOrdered(string bodyShapeId, HashSet<string> forceRender, List<W |
| | 365 | | { |
| 0 | 366 | | var result = new HashSet<string>(); |
| 0 | 367 | | var wearablesByCategory = new Dictionary<string, WearableItem>(); |
| | 368 | |
|
| 0 | 369 | | foreach (var wearable in wearables) |
| 0 | 370 | | wearablesByCategory.TryAdd(wearable.data.category, wearable); |
| | 371 | |
|
| 0 | 372 | | var previouslyHidden = new Dictionary<string, HashSet<string>>(); |
| | 373 | |
|
| 0 | 374 | | foreach (string priorityCategory in CATEGORIES_PRIORITY) |
| | 375 | | { |
| 0 | 376 | | previouslyHidden[priorityCategory] = new HashSet<string>(); |
| | 377 | |
|
| 0 | 378 | | if (!wearablesByCategory.TryGetValue(priorityCategory, out var wearable) || wearable.GetHidesList(bodyShapeI |
| | 379 | | continue; |
| | 380 | |
|
| 0 | 381 | | foreach (string categoryToHide in wearable.GetHidesList(bodyShapeId)) |
| | 382 | | { |
| 0 | 383 | | if (previouslyHidden.TryGetValue(categoryToHide, out var hiddenCategories) && hiddenCategories.Contains( |
| | 384 | | continue; |
| | 385 | |
|
| 0 | 386 | | previouslyHidden[priorityCategory].Add(categoryToHide); |
| | 387 | |
|
| 0 | 388 | | if (forceRender != null && forceRender.Contains(categoryToHide)) |
| | 389 | | continue; |
| | 390 | |
|
| 0 | 391 | | result.Add(categoryToHide); |
| | 392 | | } |
| | 393 | | } |
| | 394 | |
|
| 0 | 395 | | return result; |
| | 396 | | } |
| | 397 | |
|
| | 398 | | //Workaround to know the net of a wearable. |
| | 399 | | //Once wearables are allowed to be moved from Ethereum to Polygon this method wont be reliable anymore |
| | 400 | | //To retrieve this properly first we need the catalyst to send the net of each wearable, not just the ID |
| | 401 | | public bool IsInL2() |
| | 402 | | { |
| 0 | 403 | | if (id.StartsWith("urn:decentraland:matic") || |
| | 404 | | id.StartsWith("urn:decentraland:mumbai") || |
| | 405 | | id.StartsWith("urn:decentraland:amoy")) |
| 0 | 406 | | return true; |
| | 407 | |
|
| 0 | 408 | | return false; |
| | 409 | | } |
| | 410 | |
|
| | 411 | | public bool IsEmote() => |
| 0 | 412 | | emoteDataV0 != null; |
| | 413 | |
|
| | 414 | | public NftInfo GetNftInfo() => |
| 0 | 415 | | new () |
| | 416 | | { |
| | 417 | | Id = id, |
| | 418 | | Category = IsEmote() ? "emote" : data?.category, |
| | 419 | | }; |
| | 420 | |
|
| | 421 | | public virtual bool ShowInBackpack() => |
| 0 | 422 | | true; |
| | 423 | |
|
| | 424 | | public override string ToString() => |
| 0 | 425 | | id; |
| | 426 | |
|
| | 427 | | public string GetMarketplaceLink() |
| | 428 | | { |
| 0 | 429 | | if (!IsCollectible()) |
| 0 | 430 | | return ""; |
| | 431 | |
|
| | 432 | | const string MARKETPLACE = "https://market.decentraland.org/contracts/{0}/items/{1}"; |
| 0 | 433 | | var split = id.Split(":"); |
| | 434 | |
|
| 0 | 435 | | if (split.Length < 2) |
| 0 | 436 | | return ""; |
| | 437 | |
|
| | 438 | | // If this is not correct, we could retrieve the marketplace link by checking TheGraph, but that's super slow |
| 0 | 439 | | if (!split[^2].StartsWith("0x") || !int.TryParse(split[^1], out int _)) |
| 0 | 440 | | return ""; |
| | 441 | |
|
| 0 | 442 | | return string.Format(MARKETPLACE, split[^2], split[^1]); |
| | 443 | | } |
| | 444 | | } |
| | 445 | |
|
| | 446 | | [Serializable] |
| | 447 | | public class EmoteItem : WearableItem |
| | 448 | | { |
| | 449 | | public EmoteItem(string bodyShapeId, string emoteId, string emoteHash, string contentUrl, bool loop, bool needUrlApp |
| | 450 | | { |
| | 451 | | data = new Data |
| | 452 | | { |
| | 453 | | representations = new[] |
| | 454 | | { |
| | 455 | | new Representation |
| | 456 | | { |
| | 457 | | bodyShapes = new[] { bodyShapeId }, |
| | 458 | | contents = new[] |
| | 459 | | { |
| | 460 | | new MappingPair |
| | 461 | | { |
| | 462 | | hash = emoteHash, key = emoteHash |
| | 463 | | }, |
| | 464 | | }, |
| | 465 | | mainFile = emoteHash, |
| | 466 | | }, |
| | 467 | | }, |
| | 468 | | loop = loop, |
| | 469 | | }; |
| | 470 | |
|
| | 471 | | emoteDataV0 = new EmoteDataV0 { loop = loop }; |
| | 472 | |
|
| | 473 | | id = emoteId; |
| | 474 | | baseUrl = needUrlAppend ? $"{contentUrl}contents/" : contentUrl; |
| | 475 | | } |
| | 476 | | } |
| | 477 | |
|
| | 478 | | [Serializable] |
| | 479 | | public class WearablesRequestResponse |
| | 480 | | { |
| | 481 | | public WearableItem[] wearables; |
| | 482 | | public string context; |
| | 483 | | } |
| | 484 | |
|
| | 485 | | [Serializable] |
| | 486 | | public class WearablesRequestFailed |
| | 487 | | { |
| | 488 | | public string error; |
| | 489 | | public string context; |
| | 490 | | } |
| | 491 | |
|
| | 492 | | [Serializable] |
| | 493 | | public class WearableContent |
| | 494 | | { |
| | 495 | | public string file; |
| | 496 | | public string hash; |
| | 497 | | } |
| | 498 | |
|
| | 499 | | [Serializable] |
| | 500 | | public class i18n |
| | 501 | | { |
| | 502 | | public string code; |
| | 503 | | public string text; |
| | 504 | | } |