| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using DCL; |
| | 5 | | using DCL.Emotes; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | [Serializable] |
| | 9 | | public class WearableItem |
| | 10 | | { |
| | 11 | | private const string THIRD_PARTY_COLLECTIONS_PATH = "collections-thirdparty"; |
| | 12 | |
|
| | 13 | | [Serializable] |
| | 14 | | public class MappingPair |
| | 15 | | { |
| | 16 | | public string key; |
| | 17 | | public string hash; |
| | 18 | | } |
| | 19 | |
|
| | 20 | | [Serializable] |
| | 21 | | public class Representation |
| | 22 | | { |
| | 23 | | public string[] bodyShapes; |
| | 24 | | public string mainFile; |
| | 25 | | public MappingPair[] contents; |
| | 26 | | public string[] overrideHides; |
| | 27 | | public string[] overrideReplaces; |
| | 28 | | } |
| | 29 | |
|
| | 30 | | [Serializable] |
| | 31 | | public class Data |
| | 32 | | { |
| | 33 | | public Representation[] representations; |
| | 34 | | public string category; |
| | 35 | | public string[] tags; |
| | 36 | | public string[] replaces; |
| | 37 | | public string[] hides; |
| | 38 | | public bool loop; |
| | 39 | | } |
| | 40 | |
|
| | 41 | | public Data data; |
| | 42 | | public EmoteDataV0 emoteDataV0; |
| | 43 | | public string id; |
| | 44 | |
|
| | 45 | | public string baseUrl; |
| | 46 | | public string baseUrlBundles; |
| | 47 | |
|
| | 48 | | public i18n[] i18n; |
| | 49 | | public string thumbnail; |
| | 50 | |
|
| | 51 | | private string thirdPartyCollectionId; |
| | 52 | | public string ThirdPartyCollectionId |
| | 53 | | { |
| | 54 | | get |
| | 55 | | { |
| 13839 | 56 | | if (!string.IsNullOrEmpty(thirdPartyCollectionId)) return thirdPartyCollectionId; |
| 27678 | 57 | | if (!id.Contains(THIRD_PARTY_COLLECTIONS_PATH)) return ""; |
| 0 | 58 | | var paths = id.Split(':'); |
| 0 | 59 | | var thirdPartyIndex = Array.IndexOf(paths, THIRD_PARTY_COLLECTIONS_PATH); |
| 0 | 60 | | thirdPartyCollectionId = string.Join(":", paths, 0, thirdPartyIndex + 2); |
| 0 | 61 | | return thirdPartyCollectionId; |
| | 62 | | } |
| | 63 | | } |
| | 64 | |
|
| 13830 | 65 | | public bool IsFromThirdPartyCollection => !string.IsNullOrEmpty(ThirdPartyCollectionId); |
| | 66 | |
|
| | 67 | | public Sprite thumbnailSprite; |
| | 68 | |
|
| | 69 | | //This fields are temporary, once Kernel is finished we must move them to wherever they are placed |
| | 70 | | public string rarity; |
| | 71 | | public string description; |
| | 72 | | public int issuedId; |
| | 73 | |
|
| 3656 | 74 | | private readonly Dictionary<string, string> cachedI18n = new Dictionary<string, string>(); |
| 3656 | 75 | | private readonly Dictionary<string, ContentProvider> cachedContentProviers = |
| | 76 | | new Dictionary<string, ContentProvider>(); |
| | 77 | |
|
| 3656 | 78 | | private readonly string[] skinImplicitCategories = |
| | 79 | | { |
| | 80 | | WearableLiterals.Categories.EYES, |
| | 81 | | WearableLiterals.Categories.MOUTH, |
| | 82 | | WearableLiterals.Categories.EYEBROWS, |
| | 83 | | WearableLiterals.Categories.HAIR, |
| | 84 | | WearableLiterals.Categories.UPPER_BODY, |
| | 85 | | WearableLiterals.Categories.LOWER_BODY, |
| | 86 | | WearableLiterals.Categories.FEET, |
| | 87 | | WearableLiterals.Misc.HEAD, |
| | 88 | | WearableLiterals.Categories.FACIAL_HAIR |
| | 89 | | }; |
| | 90 | |
|
| | 91 | | public bool TryGetRepresentation(string bodyshapeId, out Representation representation) |
| | 92 | | { |
| 14 | 93 | | representation = GetRepresentation(bodyshapeId); |
| 14 | 94 | | return representation != null; |
| | 95 | | } |
| | 96 | |
|
| | 97 | | public Representation GetRepresentation(string bodyShapeType) |
| | 98 | | { |
| 4303 | 99 | | if (data?.representations == null) |
| 3 | 100 | | return null; |
| | 101 | |
|
| 8724 | 102 | | for (int i = 0; i < data.representations.Length; i++) |
| | 103 | | { |
| 4360 | 104 | | if (data.representations[i].bodyShapes.Contains(bodyShapeType)) |
| | 105 | | { |
| 4298 | 106 | | return data.representations[i]; |
| | 107 | | } |
| | 108 | | } |
| | 109 | |
|
| 2 | 110 | | return null; |
| | 111 | | } |
| | 112 | |
|
| | 113 | | public ContentProvider GetContentProvider(string bodyShapeType) |
| | 114 | | { |
| 26 | 115 | | var representation = GetRepresentation(bodyShapeType); |
| | 116 | |
|
| 26 | 117 | | if (representation == null) |
| 0 | 118 | | return null; |
| | 119 | |
|
| 26 | 120 | | if (!cachedContentProviers.ContainsKey(bodyShapeType)) |
| | 121 | | { |
| 23 | 122 | | var contentProvider = CreateContentProvider(baseUrl, representation.contents); |
| 23 | 123 | | contentProvider.BakeHashes(); |
| 23 | 124 | | cachedContentProviers.Add(bodyShapeType, contentProvider); |
| | 125 | | } |
| | 126 | |
|
| 26 | 127 | | return cachedContentProviers[bodyShapeType]; |
| | 128 | | } |
| | 129 | |
|
| | 130 | | protected virtual ContentProvider CreateContentProvider(string baseUrl, MappingPair[] contents) |
| | 131 | | { |
| 1 | 132 | | return new ContentProvider |
| | 133 | | { |
| | 134 | | baseUrl = baseUrl, |
| 1 | 135 | | contents = contents.Select(mapping => new ContentServerUtils.MappingPair() |
| | 136 | | { file = mapping.key, hash = mapping.hash }) |
| | 137 | | .ToList() |
| | 138 | | }; |
| | 139 | | } |
| | 140 | |
|
| | 141 | | public bool SupportsBodyShape(string bodyShapeType) |
| | 142 | | { |
| 1112 | 143 | | if (data?.representations == null) |
| 0 | 144 | | return false; |
| | 145 | |
|
| 2280 | 146 | | for (int i = 0; i < data.representations.Length; i++) |
| | 147 | | { |
| 1133 | 148 | | if (data.representations[i].bodyShapes.Contains(bodyShapeType)) |
| | 149 | | { |
| 1105 | 150 | | return true; |
| | 151 | | } |
| | 152 | | } |
| | 153 | |
|
| 7 | 154 | | return false; |
| | 155 | | } |
| | 156 | |
|
| | 157 | | public string[] GetReplacesList(string bodyShapeType) |
| | 158 | | { |
| 4190 | 159 | | var representation = GetRepresentation(bodyShapeType); |
| | 160 | |
|
| 4190 | 161 | | if (representation?.overrideReplaces == null || representation.overrideReplaces.Length == 0) |
| 4188 | 162 | | return data.replaces; |
| | 163 | |
|
| 2 | 164 | | return representation.overrideReplaces; |
| | 165 | | } |
| | 166 | |
|
| | 167 | | public string[] GetHidesList(string bodyShapeType) |
| | 168 | | { |
| 44 | 169 | | var representation = GetRepresentation(bodyShapeType); |
| | 170 | |
|
| | 171 | | string[] hides; |
| | 172 | |
|
| 44 | 173 | | if (representation?.overrideHides == null || representation.overrideHides.Length == 0) |
| 42 | 174 | | hides = data.hides; |
| | 175 | | else |
| 2 | 176 | | hides = representation.overrideHides; |
| | 177 | |
|
| 44 | 178 | | if (IsSkin()) |
| | 179 | | { |
| 11 | 180 | | hides = hides == null |
| | 181 | | ? skinImplicitCategories |
| | 182 | | : hides.Concat(skinImplicitCategories).Distinct().ToArray(); |
| | 183 | | } |
| | 184 | |
|
| 44 | 185 | | return hides; |
| | 186 | | } |
| | 187 | |
|
| | 188 | | public void SanitizeHidesLists() |
| | 189 | | { |
| | 190 | | //remove bodyshape from hides list |
| 3097 | 191 | | if (data.hides != null) |
| 3097 | 192 | | data.hides = data.hides.Except(new [] { WearableLiterals.Categories.BODY_SHAPE }).ToArray(); |
| 14280 | 193 | | for (int i = 0; i < data.representations.Length; i++) |
| | 194 | | { |
| 4043 | 195 | | Representation representation = data.representations[i]; |
| 4043 | 196 | | if (representation.overrideHides != null) |
| 4043 | 197 | | representation.overrideHides = representation.overrideHides.Except(new [] { WearableLiterals.Categories. |
| | 198 | |
|
| | 199 | | } |
| 3097 | 200 | | } |
| | 201 | |
|
| 54 | 202 | | public bool DoesHide(string category, string bodyShape) => GetHidesList(bodyShape).Any(s => s == category); |
| | 203 | |
|
| | 204 | | public bool IsCollectible() |
| | 205 | | { |
| 16983 | 206 | | if (id == null) |
| 0 | 207 | | return false; |
| | 208 | |
|
| 16983 | 209 | | return !id.StartsWith("urn:decentraland:off-chain:base-avatars:"); |
| | 210 | | } |
| | 211 | |
|
| 5524 | 212 | | public bool IsSkin() => data.category == WearableLiterals.Categories.SKIN; |
| | 213 | |
|
| | 214 | | public bool IsSmart() |
| | 215 | | { |
| 6456 | 216 | | if (data?.representations == null) return false; |
| | 217 | |
|
| 27562 | 218 | | for (var i = 0; i < data.representations.Length; i++) |
| | 219 | | { |
| 7328 | 220 | | var representation = data.representations[i]; |
| 23645 | 221 | | var containsGameJs = representation.contents?.Any(pair => pair.key.EndsWith("game.js")) ?? false; |
| 7331 | 222 | | if (containsGameJs) return true; |
| | 223 | | } |
| | 224 | |
|
| 6453 | 225 | | return false; |
| | 226 | | } |
| | 227 | |
|
| | 228 | | public string GetName(string langCode = "en") |
| | 229 | | { |
| 7374 | 230 | | if (!cachedI18n.ContainsKey(langCode)) |
| | 231 | | { |
| 2760 | 232 | | cachedI18n.Add(langCode, i18n.FirstOrDefault(x => x.code == langCode)?.text); |
| | 233 | | } |
| | 234 | |
|
| 7374 | 235 | | return cachedI18n[langCode]; |
| | 236 | | } |
| | 237 | |
|
| | 238 | | public int GetIssuedCountFromRarity(string rarity) |
| | 239 | | { |
| | 240 | | switch (rarity) |
| | 241 | | { |
| | 242 | | case WearableLiterals.ItemRarity.RARE: |
| 6 | 243 | | return 5000; |
| | 244 | | case WearableLiterals.ItemRarity.EPIC: |
| 76 | 245 | | return 1000; |
| | 246 | | case WearableLiterals.ItemRarity.LEGENDARY: |
| 12 | 247 | | return 100; |
| | 248 | | case WearableLiterals.ItemRarity.MYTHIC: |
| 3 | 249 | | return 10; |
| | 250 | | case WearableLiterals.ItemRarity.UNIQUE: |
| 3 | 251 | | return 1; |
| | 252 | | } |
| | 253 | |
|
| 6348 | 254 | | return int.MaxValue; |
| | 255 | | } |
| | 256 | |
|
| 1075 | 257 | | public string ComposeThumbnailUrl() { return baseUrl + thumbnail; } |
| | 258 | |
|
| | 259 | | public static HashSet<string> ComposeHiddenCategories(string bodyShapeId, List<WearableItem> wearables) |
| | 260 | | { |
| 2 | 261 | | HashSet<string> result = new HashSet<string>(); |
| | 262 | | //Last wearable added has priority over the rest |
| 22 | 263 | | for (int index = 0; index < wearables.Count; index++) |
| | 264 | | { |
| 9 | 265 | | WearableItem wearableItem = wearables[index]; |
| 9 | 266 | | if (result.Contains(wearableItem.data.category)) //Skip hidden elements to avoid two elements hiding each ot |
| | 267 | | continue; |
| | 268 | |
|
| 9 | 269 | | string[] wearableHidesList = wearableItem.GetHidesList(bodyShapeId); |
| 9 | 270 | | if (wearableHidesList != null) |
| | 271 | | { |
| 0 | 272 | | result.UnionWith(wearableHidesList); |
| | 273 | | } |
| | 274 | | } |
| | 275 | |
|
| 2 | 276 | | return result; |
| | 277 | | } |
| | 278 | |
|
| | 279 | | //Workaround to know the net of a wearable. |
| | 280 | | //Once wearables are allowed to be moved from Ethereum to Polygon this method wont be reliable anymore |
| | 281 | | //To retrieve this properly first we need the catalyst to send the net of each wearable, not just the ID |
| | 282 | | public bool IsInL2() |
| | 283 | | { |
| 7371 | 284 | | if (id.StartsWith("urn:decentraland:matic") || id.StartsWith("urn:decentraland:mumbai")) |
| 0 | 285 | | return true; |
| 7371 | 286 | | return false; |
| | 287 | | } |
| | 288 | |
|
| 0 | 289 | | public bool IsEmote() { return emoteDataV0 != null; } |
| | 290 | |
|
| 0 | 291 | | public override string ToString() { return id; } |
| | 292 | | } |
| | 293 | |
|
| | 294 | | [Serializable] |
| | 295 | | public class EmoteItem : WearableItem{ |
| | 296 | | } |
| | 297 | |
|
| | 298 | | [Serializable] |
| | 299 | | public class WearablesRequestResponse |
| | 300 | | { |
| | 301 | | public WearableItem[] wearables; |
| | 302 | | public string context; |
| | 303 | | } |
| | 304 | |
|
| | 305 | | [Serializable] |
| | 306 | | public class WearablesRequestFailed |
| | 307 | | { |
| | 308 | | public string error; |
| | 309 | | public string context; |
| | 310 | | } |
| | 311 | |
|
| | 312 | | [Serializable] |
| | 313 | | public class WearableContent |
| | 314 | | { |
| | 315 | | public string file; |
| | 316 | | public string hash; |
| | 317 | | } |
| | 318 | |
|
| | 319 | | [Serializable] |
| | 320 | | public class i18n |
| | 321 | | { |
| | 322 | | public string code; |
| | 323 | | public string text; |
| | 324 | | } |