| | 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 | |
|
| 4452 | 50 | | private readonly Dictionary<string, string> cachedI18n = new Dictionary<string, string>(); |
| | 51 | |
|
| | 52 | | public Representation GetRepresentation(string bodyShapeType) |
| | 53 | | { |
| 3608 | 54 | | if (data?.representations == null) |
| 2 | 55 | | return null; |
| | 56 | |
|
| 7330 | 57 | | for (int i = 0; i < data.representations.Length; i++) |
| | 58 | | { |
| 3663 | 59 | | if (data.representations[i].bodyShapes.Contains(bodyShapeType)) |
| | 60 | | { |
| 3604 | 61 | | return data.representations[i]; |
| | 62 | | } |
| | 63 | | } |
| | 64 | |
|
| 2 | 65 | | return null; |
| | 66 | | } |
| | 67 | |
|
| 4452 | 68 | | private readonly Dictionary<string, ContentProvider> cachedContentProviers = new Dictionary<string, ContentProvider> |
| | 69 | |
|
| | 70 | | public ContentProvider GetContentProvider(string bodyShapeType) |
| | 71 | | { |
| 14 | 72 | | var representation = GetRepresentation(bodyShapeType); |
| | 73 | |
|
| 14 | 74 | | if (representation == null) |
| 0 | 75 | | return null; |
| | 76 | |
|
| 14 | 77 | | if (!cachedContentProviers.ContainsKey(bodyShapeType)) |
| | 78 | | { |
| 13 | 79 | | var contentProvider = CreateContentProvider(baseUrl, representation.contents); |
| 13 | 80 | | contentProvider.BakeHashes(); |
| 13 | 81 | | cachedContentProviers.Add(bodyShapeType, contentProvider); |
| | 82 | | } |
| | 83 | |
|
| 14 | 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 | | { |
| 3594 | 98 | | if (data?.representations == null) |
| 0 | 99 | | return false; |
| | 100 | |
|
| 9708 | 101 | | for (int i = 0; i < data.representations.Length; i++) |
| | 102 | | { |
| 3991 | 103 | | if (data.representations[i].bodyShapes.Contains(bodyShapeType)) |
| | 104 | | { |
| 2731 | 105 | | return true; |
| | 106 | | } |
| | 107 | | } |
| | 108 | |
|
| 863 | 109 | | return false; |
| | 110 | | } |
| | 111 | |
|
| | 112 | | public string[] GetReplacesList(string bodyShapeType) |
| | 113 | | { |
| 3571 | 114 | | var representation = GetRepresentation(bodyShapeType); |
| | 115 | |
|
| 3571 | 116 | | if (representation?.overrideReplaces == null || representation.overrideReplaces.Length == 0) |
| 3569 | 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 string GetName(string langCode = "en") |
| | 135 | | { |
| 33 | 136 | | if (!cachedI18n.ContainsKey(langCode)) |
| | 137 | | { |
| 32 | 138 | | cachedI18n.Add(langCode, i18n.FirstOrDefault(x => x.code == langCode)?.text); |
| | 139 | | } |
| | 140 | |
|
| 33 | 141 | | return cachedI18n[langCode]; |
| | 142 | | } |
| | 143 | |
|
| | 144 | | public int GetIssuedCountFromRarity(string rarity) |
| | 145 | | { |
| | 146 | | switch (rarity) |
| | 147 | | { |
| | 148 | | case WearableLiterals.ItemRarity.RARE: |
| 2 | 149 | | return 5000; |
| | 150 | | case WearableLiterals.ItemRarity.EPIC: |
| 25 | 151 | | return 1000; |
| | 152 | | case WearableLiterals.ItemRarity.LEGENDARY: |
| 2 | 153 | | return 100; |
| | 154 | | case WearableLiterals.ItemRarity.MYTHIC: |
| 2 | 155 | | return 10; |
| | 156 | | case WearableLiterals.ItemRarity.UNIQUE: |
| 2 | 157 | | return 1; |
| | 158 | | } |
| | 159 | |
|
| 0 | 160 | | return int.MaxValue; |
| | 161 | | } |
| | 162 | |
|
| 276 | 163 | | public string ComposeThumbnailUrl() { return baseUrl + thumbnail; } |
| | 164 | |
|
| | 165 | | public static HashSet<string> CompoundHidesList(string bodyShapeId, List<WearableItem> wearables) |
| | 166 | | { |
| 0 | 167 | | HashSet<string> result = new HashSet<string>(); |
| | 168 | | //Last wearable added has priority over the rest |
| 0 | 169 | | for (int i = wearables.Count - 1; i >= 0; i--) |
| | 170 | | { |
| 0 | 171 | | var wearableItem = wearables[i]; |
| | 172 | |
|
| 0 | 173 | | if (result.Contains(wearableItem.data.category)) //Skip hidden elements to avoid two elements hiding each ot |
| | 174 | | continue; |
| | 175 | |
|
| 0 | 176 | | var wearableHidesList = wearableItem.GetHidesList(bodyShapeId); |
| 0 | 177 | | if (wearableHidesList != null) |
| | 178 | | { |
| 0 | 179 | | result.UnionWith(wearableHidesList); |
| | 180 | | } |
| | 181 | | } |
| | 182 | |
|
| 0 | 183 | | return result; |
| | 184 | | } |
| | 185 | |
|
| | 186 | | //Workaround to know the net of a wearable. |
| | 187 | | //Once wearables are allowed to be moved from Ethereum to Polygon this method wont be reliable anymore |
| | 188 | | //To retrieve this properly first we need the catalyst to send the net of each wearable, not just the ID |
| | 189 | | public bool IsInL2() |
| | 190 | | { |
| 32 | 191 | | if (id.StartsWith("urn:decentraland:matic") || id.StartsWith("urn:decentraland:mumbai")) |
| 0 | 192 | | return true; |
| 32 | 193 | | return false; |
| | 194 | | } |
| | 195 | | } |
| | 196 | |
|
| | 197 | | [System.Serializable] |
| | 198 | | public class WearablesRequestResponse |
| | 199 | | { |
| | 200 | | public WearableItem[] wearables; |
| | 201 | | public string context; |
| | 202 | | } |
| | 203 | |
|
| | 204 | | [System.Serializable] |
| | 205 | | public class WearablesRequestFailed |
| | 206 | | { |
| | 207 | | public string error; |
| | 208 | | public string context; |
| | 209 | | } |
| | 210 | |
|
| | 211 | | [System.Serializable] |
| | 212 | | public class WearableContent |
| | 213 | | { |
| | 214 | | public string file; |
| | 215 | | public string hash; |
| | 216 | | } |
| | 217 | |
|
| | 218 | | [System.Serializable] |
| | 219 | | public class i18n |
| | 220 | | { |
| | 221 | | public string code; |
| | 222 | | public string text; |
| | 223 | | } |