| | 1 | | using DCL; |
| | 2 | | using DCL.Configuration; |
| | 3 | | using DCL.Helpers.NFT; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | public static class BIWCatalogManager |
| | 9 | | { |
| | 10 | | public static bool VERBOSE = false; |
| | 11 | | private static bool IS_INIT = false; |
| | 12 | |
|
| | 13 | | public static void Init() |
| | 14 | | { |
| 64 | 15 | | if (!IS_INIT) |
| | 16 | | { |
| 15 | 17 | | BIWNFTController.i.OnNftsFetched += ConvertCollectiblesPack; |
| 15 | 18 | | AssetCatalogBridge.OnItemAdded += AddSceneObject; |
| 15 | 19 | | AssetCatalogBridge.OnSceneCatalogItemAdded += AddSceneCatalog; |
| 15 | 20 | | AssetCatalogBridge.OnSceneAssetPackAdded += AddSceneAssetPack; |
| 15 | 21 | | IS_INIT = true; |
| | 22 | | } |
| 64 | 23 | | } |
| | 24 | |
|
| | 25 | | public static void Dispose() |
| | 26 | | { |
| 14 | 27 | | BIWNFTController.i.OnNftsFetched -= ConvertCollectiblesPack; |
| 14 | 28 | | AssetCatalogBridge.OnItemAdded -= AddSceneObject; |
| 14 | 29 | | AssetCatalogBridge.OnSceneCatalogItemAdded -= AddSceneCatalog; |
| 14 | 30 | | AssetCatalogBridge.OnSceneAssetPackAdded -= AddSceneAssetPack; |
| 14 | 31 | | IS_INIT = false; |
| 14 | 32 | | } |
| | 33 | |
|
| | 34 | | public static void ClearCatalog() |
| | 35 | | { |
| 67 | 36 | | DataStore.i.builderInWorld.catalogItemDict.Clear(); |
| 67 | 37 | | DataStore.i.builderInWorld.catalogItemPackDict.Clear(); |
| 67 | 38 | | } |
| | 39 | |
|
| | 40 | | public static List<CatalogItemPack> GetCatalogItemPackList() |
| | 41 | | { |
| 1 | 42 | | var assetPacks = DataStore.i.builderInWorld.catalogItemPackDict.GetValues(); |
| | 43 | |
|
| 4 | 44 | | foreach (CatalogItemPack catalogAssetPack in assetPacks) |
| | 45 | | { |
| 6 | 46 | | foreach (CatalogItem catalogItem in catalogAssetPack.assets) |
| | 47 | | { |
| 2 | 48 | | catalogItem.categoryName = catalogItem.category; |
| | 49 | | } |
| | 50 | | } |
| | 51 | |
|
| 2 | 52 | | return assetPacks.OrderBy(x => x.title).ToList(); |
| | 53 | | } |
| | 54 | |
|
| | 55 | | public static List<CatalogItemPack> GetCatalogItemPacksFilteredByCategories() |
| | 56 | | { |
| 2 | 57 | | var assetPacks = DataStore.i.builderInWorld.catalogItemPackDict.GetValues(); |
| | 58 | |
|
| 2 | 59 | | Dictionary<string, CatalogItemPack> assetPackDic = new Dictionary<string, CatalogItemPack>(); |
| | 60 | |
|
| 2 | 61 | | if (DataStore.i.builderInWorld.catalogItemPackDict.ContainsKey(BIWSettings.ASSETS_COLLECTIBLES)) |
| 1 | 62 | | assetPackDic.Add(BIWSettings.ASSETS_COLLECTIBLES, DataStore.i.builderInWorld.catalogItemPackDict[BIWSettings |
| | 63 | | else |
| 1 | 64 | | CreateNewCollectiblePack(); |
| | 65 | |
|
| 8 | 66 | | foreach (CatalogItemPack catalogAssetPack in assetPacks) |
| | 67 | | { |
| 8 | 68 | | foreach (CatalogItem catalogItem in catalogAssetPack.assets) |
| | 69 | | { |
| 2 | 70 | | if (catalogItem.IsNFT()) |
| | 71 | | continue; |
| 1 | 72 | | if (!assetPackDic.ContainsKey(catalogItem.category)) |
| | 73 | | { |
| 1 | 74 | | CatalogItemPack categoryAssetPack = new CatalogItemPack(); |
| 1 | 75 | | categoryAssetPack.SetThumbnailULR(catalogItem.thumbnailURL); |
| 1 | 76 | | categoryAssetPack.title = catalogItem.category; |
| 1 | 77 | | categoryAssetPack.assets = new List<CatalogItem>(); |
| 1 | 78 | | catalogItem.categoryName = catalogAssetPack.title; |
| 1 | 79 | | categoryAssetPack.assets.Add(catalogItem); |
| | 80 | |
|
| 1 | 81 | | if (!string.IsNullOrEmpty(categoryAssetPack.title)) |
| | 82 | | { |
| 1 | 83 | | if (categoryAssetPack.title.Length == 1) |
| 0 | 84 | | categoryAssetPack.title = categoryAssetPack.title.ToUpper(); |
| | 85 | | else |
| 1 | 86 | | categoryAssetPack.title = char.ToUpper(categoryAssetPack.title[0]) + categoryAssetPack.title |
| | 87 | | } |
| | 88 | |
|
| 1 | 89 | | assetPackDic.Add(catalogItem.category, categoryAssetPack); |
| 1 | 90 | | continue; |
| | 91 | | } |
| | 92 | | else |
| | 93 | | { |
| 0 | 94 | | catalogItem.categoryName = catalogAssetPack.title; |
| 0 | 95 | | assetPackDic[catalogItem.category].assets.Add(catalogItem); |
| | 96 | | } |
| | 97 | | } |
| | 98 | | } |
| | 99 | |
|
| 4 | 100 | | return assetPackDic.Values.OrderBy(x => x.title).ToList(); |
| | 101 | | } |
| | 102 | |
|
| 78 | 103 | | public static void AddSceneObject(SceneObject sceneObject) { AddSceneObject(sceneObject, DataStore.i.builderInWorld. |
| | 104 | |
|
| | 105 | | public static void AddSceneObject(SceneObject sceneObject , BaseDictionary<string, CatalogItem> catalogToAdd ) |
| | 106 | | { |
| 40 | 107 | | if (catalogToAdd.ContainsKey(sceneObject.id)) |
| 0 | 108 | | return; |
| | 109 | |
|
| 40 | 110 | | CatalogItem catalogItem = CreateCatalogItem(sceneObject); |
| 40 | 111 | | catalogToAdd.Add(catalogItem.id, catalogItem); |
| 40 | 112 | | } |
| | 113 | |
|
| 2 | 114 | | public static void AddSceneCatalog(SceneObject sceneObject) { AddSceneObject(sceneObject, DataStore.i.builderInWorld |
| | 115 | |
|
| | 116 | | public static void AddSceneAssetPack(SceneAssetPack sceneAssetPack) |
| | 117 | | { |
| 32 | 118 | | if (DataStore.i.builderInWorld.catalogItemPackDict.ContainsKey(sceneAssetPack.id)) |
| 0 | 119 | | return; |
| | 120 | |
|
| 32 | 121 | | CatalogItemPack catalogItemPack = CreateCatalogItemPack(sceneAssetPack); |
| | 122 | |
|
| 32 | 123 | | if (catalogItemPack.assets.Count != 0) |
| 31 | 124 | | DataStore.i.builderInWorld.catalogItemPackDict.Add(catalogItemPack.id, catalogItemPack); |
| 32 | 125 | | } |
| | 126 | |
|
| | 127 | | public static string GetAssetPackNameById(string assetPackId) |
| | 128 | | { |
| 41 | 129 | | if (string.IsNullOrEmpty(assetPackId)) |
| 3 | 130 | | return ""; |
| | 131 | |
|
| 38 | 132 | | DataStore.i.builderInWorld.catalogItemPackDict.TryGetValue(assetPackId, out CatalogItemPack catalogItemPack); |
| 38 | 133 | | if (catalogItemPack != null) |
| 0 | 134 | | return catalogItemPack.title; |
| | 135 | |
|
| 38 | 136 | | return ""; |
| | 137 | | } |
| | 138 | |
|
| | 139 | | public static void ConvertCollectiblesPack(List<NFTInfo> nftList) |
| | 140 | | { |
| 4 | 141 | | if (nftList == null) |
| 0 | 142 | | return; |
| | 143 | |
|
| | 144 | | CatalogItemPack collectiblesItemPack; |
| | 145 | |
|
| 4 | 146 | | if (!DataStore.i.builderInWorld.catalogItemPackDict.ContainsKey(BIWSettings.ASSETS_COLLECTIBLES)) |
| | 147 | | { |
| 3 | 148 | | collectiblesItemPack = CreateNewCollectiblePack(); |
| 3 | 149 | | } |
| | 150 | | else |
| | 151 | | { |
| 1 | 152 | | collectiblesItemPack = DataStore.i.builderInWorld.catalogItemPackDict[BIWSettings.ASSETS_COLLECTIBLES]; |
| 4 | 153 | | foreach (CatalogItem catalogItem in collectiblesItemPack.assets) |
| | 154 | | { |
| 1 | 155 | | if (DataStore.i.builderInWorld.catalogItemDict.ContainsKey(catalogItem.id)) |
| 1 | 156 | | DataStore.i.builderInWorld.catalogItemDict.Remove(catalogItem.id); |
| | 157 | | } |
| | 158 | |
|
| 1 | 159 | | collectiblesItemPack.assets.Clear(); |
| | 160 | | } |
| | 161 | |
|
| 16 | 162 | | foreach (NFTInfo info in nftList) |
| | 163 | | { |
| 4 | 164 | | CatalogItem catalogItem = CreateCatalogItem(info); |
| 4 | 165 | | if (!DataStore.i.builderInWorld.catalogItemDict.ContainsKey(catalogItem.id)) |
| 4 | 166 | | DataStore.i.builderInWorld.catalogItemDict.Add(catalogItem.id, catalogItem); |
| | 167 | |
|
| 4 | 168 | | collectiblesItemPack.assets.Add(catalogItem); |
| | 169 | | } |
| 4 | 170 | | } |
| | 171 | |
|
| | 172 | | private static CatalogItemPack CreateNewCollectiblePack() |
| | 173 | | { |
| 4 | 174 | | CatalogItemPack collectiblesItemPack = new CatalogItemPack(); |
| 4 | 175 | | collectiblesItemPack.id = BIWSettings.ASSETS_COLLECTIBLES; |
| 4 | 176 | | collectiblesItemPack.title = BIWSettings.ASSETS_COLLECTIBLES; |
| 4 | 177 | | collectiblesItemPack.assets = new List<CatalogItem>(); |
| 4 | 178 | | if (!DataStore.i.builderInWorld.catalogItemPackDict.ContainsKey(collectiblesItemPack.id)) |
| 4 | 179 | | DataStore.i.builderInWorld.catalogItemPackDict.Add(collectiblesItemPack.id, collectiblesItemPack); |
| 4 | 180 | | return collectiblesItemPack; |
| | 181 | | } |
| | 182 | |
|
| | 183 | | public static CatalogItemPack CreateCatalogItemPack(SceneAssetPack sceneAssetPack) |
| | 184 | | { |
| 32 | 185 | | CatalogItemPack catalogItemPack = new CatalogItemPack(); |
| | 186 | |
|
| 32 | 187 | | catalogItemPack.id = sceneAssetPack.id; |
| 32 | 188 | | catalogItemPack.title = sceneAssetPack.title; |
| | 189 | |
|
| 32 | 190 | | catalogItemPack.assets = new List<CatalogItem>(); |
| | 191 | |
|
| 32 | 192 | | catalogItemPack.SetThumbnailULR(sceneAssetPack.ComposeThumbnailUrl()); |
| | 193 | |
|
| 142 | 194 | | foreach (SceneObject sceneObject in sceneAssetPack.assets) |
| | 195 | | { |
| | 196 | | //TODO: SmartItems disabled until kernel has them implemented |
| 39 | 197 | | if (sceneObject.IsSmartItem()) |
| | 198 | | continue; |
| | 199 | |
|
| 38 | 200 | | catalogItemPack.assets.Add(CreateCatalogItem(sceneObject, sceneAssetPack.title)); |
| | 201 | | } |
| | 202 | |
|
| 32 | 203 | | return catalogItemPack; |
| | 204 | | } |
| | 205 | |
|
| | 206 | | public static CatalogItem CreateCatalogItem(SceneObject sceneObject, string assetPackName = null) |
| | 207 | | { |
| 78 | 208 | | CatalogItem catalogItem = new CatalogItem(); |
| 78 | 209 | | catalogItem.id = sceneObject.id; |
| 78 | 210 | | if (sceneObject.asset_pack_id == BIWSettings.VOXEL_ASSETS_PACK_ID) |
| 0 | 211 | | catalogItem.isVoxel = true; |
| 78 | 212 | | catalogItem.name = sceneObject.name; |
| 78 | 213 | | catalogItem.model = sceneObject.model; |
| 78 | 214 | | catalogItem.assetPackName = assetPackName == null ? GetAssetPackNameById(sceneObject.asset_pack_id) : assetPackN |
| 78 | 215 | | catalogItem.thumbnailURL = sceneObject.GetComposedThumbnailUrl(); |
| 78 | 216 | | catalogItem.tags = sceneObject.tags; |
| | 217 | |
|
| 78 | 218 | | catalogItem.category = sceneObject.category; |
| 78 | 219 | | catalogItem.categoryName = catalogItem.category; |
| | 220 | |
|
| 78 | 221 | | catalogItem.contents = sceneObject.contents; |
| | 222 | |
|
| 78 | 223 | | catalogItem.metrics = sceneObject.metrics; |
| | 224 | |
|
| 78 | 225 | | if (!string.IsNullOrEmpty(sceneObject.script)) |
| | 226 | | { |
| 1 | 227 | | catalogItem.itemType = CatalogItem.ItemType.SMART_ITEM; |
| 1 | 228 | | catalogItem.parameters = sceneObject.parameters; |
| 1 | 229 | | catalogItem.actions = sceneObject.actions; |
| 1 | 230 | | } |
| | 231 | | else |
| | 232 | | { |
| 77 | 233 | | catalogItem.itemType = CatalogItem.ItemType.SCENE_OBJECT; |
| | 234 | | } |
| | 235 | |
|
| 78 | 236 | | return catalogItem; |
| | 237 | | } |
| | 238 | |
|
| | 239 | | public static CatalogItem CreateCatalogItem(NFTInfo nFTInfo) |
| | 240 | | { |
| 4 | 241 | | CatalogItem catalogItem = new CatalogItem(); |
| 4 | 242 | | catalogItem.itemType = CatalogItem.ItemType.NFT; |
| | 243 | |
|
| 4 | 244 | | catalogItem.id = nFTInfo.assetContract.address; |
| 4 | 245 | | catalogItem.thumbnailURL = nFTInfo.thumbnailUrl; |
| 4 | 246 | | catalogItem.name = nFTInfo.name; |
| 4 | 247 | | catalogItem.assetPackName = BIWSettings.ASSETS_COLLECTIBLES; |
| 4 | 248 | | catalogItem.category = nFTInfo.assetContract.name; |
| 4 | 249 | | catalogItem.model = $"{BIWSettings.COLLECTIBLE_MODEL_PROTOCOL}{nFTInfo.assetContract.address}/{nFTInfo.tokenId}" |
| 4 | 250 | | catalogItem.tags = new List<string>(); |
| 4 | 251 | | catalogItem.contents = new Dictionary<string, string>(); |
| 4 | 252 | | catalogItem.metrics = new SceneObject.ObjectMetrics(); |
| | 253 | |
|
| 4 | 254 | | return catalogItem; |
| | 255 | | } |
| | 256 | | } |