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