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