| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using DCL; |
| | 5 | | using DCL.Emotes; |
| | 6 | |
|
| | 7 | | [Serializable] |
| | 8 | | public class WearableItem |
| | 9 | | { |
| | 10 | | [Serializable] |
| | 11 | | public class MappingPair |
| | 12 | | { |
| | 13 | | public string key; |
| | 14 | | public string hash; |
| | 15 | | } |
| | 16 | |
|
| | 17 | | [Serializable] |
| | 18 | | public class Representation |
| | 19 | | { |
| | 20 | | public string[] bodyShapes; |
| | 21 | | public string mainFile; |
| | 22 | | public MappingPair[] contents; |
| | 23 | | public string[] overrideHides; |
| | 24 | | public string[] overrideReplaces; |
| | 25 | | } |
| | 26 | |
|
| | 27 | | [Serializable] |
| | 28 | | public class Data |
| | 29 | | { |
| | 30 | | public Representation[] representations; |
| | 31 | | public string category; |
| | 32 | | public string[] tags; |
| | 33 | | public string[] replaces; |
| | 34 | | public string[] hides; |
| | 35 | | } |
| | 36 | |
|
| | 37 | | public Data data; |
| | 38 | | public EmoteDataV0 emoteDataV0 = null; |
| | 39 | | public string id; |
| | 40 | |
|
| | 41 | | public string baseUrl; |
| | 42 | | public string baseUrlBundles; |
| | 43 | |
|
| | 44 | | public i18n[] i18n; |
| | 45 | | public string thumbnail; |
| | 46 | |
|
| | 47 | | //This fields are temporary, once Kernel is finished we must move them to wherever they are placed |
| | 48 | | public string rarity; |
| | 49 | | public string description; |
| | 50 | | public int issuedId; |
| | 51 | |
|
| 3255 | 52 | | private readonly Dictionary<string, string> cachedI18n = new Dictionary<string, string>(); |
| 3255 | 53 | | private readonly Dictionary<string, ContentProvider> cachedContentProviers = |
| | 54 | | new Dictionary<string, ContentProvider>(); |
| | 55 | |
|
| 3255 | 56 | | private readonly string[] skinImplicitCategories = |
| | 57 | | { |
| | 58 | | WearableLiterals.Categories.EYES, |
| | 59 | | WearableLiterals.Categories.MOUTH, |
| | 60 | | WearableLiterals.Categories.EYEBROWS, |
| | 61 | | WearableLiterals.Categories.HAIR, |
| | 62 | | WearableLiterals.Categories.UPPER_BODY, |
| | 63 | | WearableLiterals.Categories.LOWER_BODY, |
| | 64 | | WearableLiterals.Categories.FEET, |
| | 65 | | WearableLiterals.Misc.HEAD, |
| | 66 | | WearableLiterals.Categories.FACIAL_HAIR |
| | 67 | | }; |
| | 68 | |
|
| | 69 | | public bool TryGetRepresentation(string bodyshapeId, out Representation representation) |
| | 70 | | { |
| 576 | 71 | | representation = GetRepresentation(bodyshapeId); |
| 576 | 72 | | return representation != null; |
| | 73 | | } |
| | 74 | |
|
| | 75 | | public Representation GetRepresentation(string bodyShapeType) |
| | 76 | | { |
| 5086 | 77 | | if (data?.representations == null) |
| 2 | 78 | | return null; |
| | 79 | |
|
| 10362 | 80 | | for (int i = 0; i < data.representations.Length; i++) |
| | 81 | | { |
| 5179 | 82 | | if (data.representations[i].bodyShapes.Contains(bodyShapeType)) |
| | 83 | | { |
| 5082 | 84 | | return data.representations[i]; |
| | 85 | | } |
| | 86 | | } |
| | 87 | |
|
| 2 | 88 | | return null; |
| | 89 | | } |
| | 90 | |
|
| | 91 | | public ContentProvider GetContentProvider(string bodyShapeType) |
| | 92 | | { |
| 101 | 93 | | var representation = GetRepresentation(bodyShapeType); |
| | 94 | |
|
| 101 | 95 | | if (representation == null) |
| 0 | 96 | | return null; |
| | 97 | |
|
| 101 | 98 | | if (!cachedContentProviers.ContainsKey(bodyShapeType)) |
| | 99 | | { |
| 72 | 100 | | var contentProvider = CreateContentProvider(baseUrl, representation.contents); |
| 72 | 101 | | contentProvider.BakeHashes(); |
| 72 | 102 | | cachedContentProviers.Add(bodyShapeType, contentProvider); |
| | 103 | | } |
| | 104 | |
|
| 101 | 105 | | return cachedContentProviers[bodyShapeType]; |
| | 106 | | } |
| | 107 | |
|
| | 108 | | protected virtual ContentProvider CreateContentProvider(string baseUrl, MappingPair[] contents) |
| | 109 | | { |
| 0 | 110 | | return new ContentProvider |
| | 111 | | { |
| | 112 | | baseUrl = baseUrl, |
| 0 | 113 | | contents = contents.Select(mapping => new ContentServerUtils.MappingPair() |
| | 114 | | { file = mapping.key, hash = mapping.hash }) |
| | 115 | | .ToList() |
| | 116 | | }; |
| | 117 | | } |
| | 118 | |
|
| | 119 | | public bool SupportsBodyShape(string bodyShapeType) |
| | 120 | | { |
| 3740 | 121 | | if (data?.representations == null) |
| 0 | 122 | | return false; |
| | 123 | |
|
| 10096 | 124 | | for (int i = 0; i < data.representations.Length; i++) |
| | 125 | | { |
| 4153 | 126 | | if (data.representations[i].bodyShapes.Contains(bodyShapeType)) |
| | 127 | | { |
| 2845 | 128 | | return true; |
| | 129 | | } |
| | 130 | | } |
| | 131 | |
|
| 895 | 132 | | return false; |
| | 133 | | } |
| | 134 | |
|
| | 135 | | public string[] GetReplacesList(string bodyShapeType) |
| | 136 | | { |
| 3711 | 137 | | var representation = GetRepresentation(bodyShapeType); |
| | 138 | |
|
| 3711 | 139 | | if (representation?.overrideReplaces == null || representation.overrideReplaces.Length == 0) |
| 3709 | 140 | | return data.replaces; |
| | 141 | |
|
| 2 | 142 | | return representation.overrideReplaces; |
| | 143 | | } |
| | 144 | |
|
| | 145 | | public string[] GetHidesList(string bodyShapeType) |
| | 146 | | { |
| 595 | 147 | | var representation = GetRepresentation(bodyShapeType); |
| | 148 | |
|
| | 149 | | string[] hides; |
| | 150 | |
|
| 595 | 151 | | if (representation?.overrideHides == null || representation.overrideHides.Length == 0) |
| 594 | 152 | | hides = data.hides; |
| | 153 | | else |
| 1 | 154 | | hides = representation.overrideHides; |
| | 155 | |
|
| 595 | 156 | | if (IsSkin()) |
| | 157 | | { |
| 11 | 158 | | hides = hides == null |
| | 159 | | ? skinImplicitCategories |
| | 160 | | : hides.Concat(skinImplicitCategories).Distinct().ToArray(); |
| | 161 | | } |
| | 162 | |
|
| 595 | 163 | | return hides; |
| | 164 | | } |
| | 165 | |
|
| 54 | 166 | | public bool DoesHide(string category, string bodyShape) => GetHidesList(bodyShape).Any(s => s == category); |
| | 167 | |
|
| 0 | 168 | | public bool IsCollectible() { return !string.IsNullOrEmpty(rarity); } |
| | 169 | |
|
| 675 | 170 | | public bool IsSkin() => data.category == WearableLiterals.Categories.SKIN; |
| | 171 | |
|
| | 172 | | public bool IsSmart() |
| | 173 | | { |
| 36 | 174 | | if (data?.representations == null) return false; |
| | 175 | |
|
| 140 | 176 | | for (var i = 0; i < data.representations.Length; i++) |
| | 177 | | { |
| 36 | 178 | | var representation = data.representations[i]; |
| 98 | 179 | | var containsGameJs = representation.contents?.Any(pair => pair.key.EndsWith("game.js")) ?? false; |
| 38 | 180 | | if (containsGameJs) return true; |
| | 181 | | } |
| | 182 | |
|
| 34 | 183 | | return false; |
| | 184 | | } |
| | 185 | |
|
| | 186 | | public string GetName(string langCode = "en") |
| | 187 | | { |
| 37 | 188 | | if (!cachedI18n.ContainsKey(langCode)) |
| | 189 | | { |
| 36 | 190 | | cachedI18n.Add(langCode, i18n.FirstOrDefault(x => x.code == langCode)?.text); |
| | 191 | | } |
| | 192 | |
|
| 37 | 193 | | return cachedI18n[langCode]; |
| | 194 | | } |
| | 195 | |
|
| | 196 | | public int GetIssuedCountFromRarity(string rarity) |
| | 197 | | { |
| | 198 | | switch (rarity) |
| | 199 | | { |
| | 200 | | case WearableLiterals.ItemRarity.RARE: |
| 4 | 201 | | return 5000; |
| | 202 | | case WearableLiterals.ItemRarity.EPIC: |
| 27 | 203 | | return 1000; |
| | 204 | | case WearableLiterals.ItemRarity.LEGENDARY: |
| 2 | 205 | | return 100; |
| | 206 | | case WearableLiterals.ItemRarity.MYTHIC: |
| 2 | 207 | | return 10; |
| | 208 | | case WearableLiterals.ItemRarity.UNIQUE: |
| 2 | 209 | | return 1; |
| | 210 | | } |
| | 211 | |
|
| 0 | 212 | | return int.MaxValue; |
| | 213 | | } |
| | 214 | |
|
| 198 | 215 | | public string ComposeThumbnailUrl() { return baseUrl + thumbnail; } |
| | 216 | |
|
| | 217 | | public static HashSet<string> ComposeHiddenCategories(string bodyShapeId, List<WearableItem> wearables) |
| | 218 | | { |
| 78 | 219 | | HashSet<string> result = new HashSet<string>(); |
| | 220 | | //Last wearable added has priority over the rest |
| 1298 | 221 | | for (int index = 0; index < wearables.Count; index++) |
| | 222 | | { |
| 571 | 223 | | WearableItem wearableItem = wearables[index]; |
| 571 | 224 | | if (result.Contains(wearableItem.data.category)) //Skip hidden elements to avoid two elements hiding each ot |
| | 225 | | continue; |
| | 226 | |
|
| 571 | 227 | | string[] wearableHidesList = wearableItem.GetHidesList(bodyShapeId); |
| 571 | 228 | | if (wearableHidesList != null) |
| | 229 | | { |
| 562 | 230 | | result.UnionWith(wearableHidesList); |
| | 231 | | } |
| | 232 | | } |
| | 233 | |
|
| 78 | 234 | | return result; |
| | 235 | | } |
| | 236 | |
|
| | 237 | | //Workaround to know the net of a wearable. |
| | 238 | | //Once wearables are allowed to be moved from Ethereum to Polygon this method wont be reliable anymore |
| | 239 | | //To retrieve this properly first we need the catalyst to send the net of each wearable, not just the ID |
| | 240 | | public bool IsInL2() |
| | 241 | | { |
| 36 | 242 | | if (id.StartsWith("urn:decentraland:matic") || id.StartsWith("urn:decentraland:mumbai")) |
| 0 | 243 | | return true; |
| 36 | 244 | | return false; |
| | 245 | | } |
| | 246 | |
|
| 0 | 247 | | public bool IsEmote() { return emoteDataV0 != null; } |
| | 248 | | } |
| | 249 | |
|
| | 250 | | [Serializable] |
| | 251 | | public class WearablesRequestResponse |
| | 252 | | { |
| | 253 | | public WearableItem[] wearables; |
| | 254 | | public string context; |
| | 255 | | } |
| | 256 | |
|
| | 257 | | [Serializable] |
| | 258 | | public class WearablesRequestFailed |
| | 259 | | { |
| | 260 | | public string error; |
| | 261 | | public string context; |
| | 262 | | } |
| | 263 | |
|
| | 264 | | [Serializable] |
| | 265 | | public class WearableContent |
| | 266 | | { |
| | 267 | | public string file; |
| | 268 | | public string hash; |
| | 269 | | } |
| | 270 | |
|
| | 271 | | [Serializable] |
| | 272 | | public class i18n |
| | 273 | | { |
| | 274 | | public string code; |
| | 275 | | public string text; |
| | 276 | | } |