| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.ECS7.InternalComponents; |
| | 3 | | using DCL.ECSRuntime; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using DCL.Helpers.NFT; |
| | 6 | | using DCL.Models; |
| | 7 | | using Decentraland.Common; |
| | 8 | | using MainScripts.DCL.ServiceProviders.OpenSea.Interfaces; |
| | 9 | | using NFTShape_Internal; |
| | 10 | | using System; |
| | 11 | | using System.Threading.Tasks; |
| | 12 | | using UnityEngine; |
| | 13 | | using Object = UnityEngine.Object; |
| | 14 | |
|
| | 15 | | namespace DCL.ECSComponents |
| | 16 | | { |
| | 17 | | public class ECSNFTShapeComponentHandler : IECSComponentHandler<PBNftShape> |
| | 18 | | { |
| | 19 | | internal INFTShapeFrameFactory factory; |
| | 20 | |
|
| | 21 | | internal INFTInfoRetriever infoRetriever; |
| | 22 | | internal INFTAssetRetriever assetRetriever; |
| | 23 | | internal INFTShapeFrame shapeFrame; |
| | 24 | |
|
| | 25 | | private PBNftShape prevModel; |
| | 26 | |
|
| | 27 | | private string nftLoadedScr = null; |
| | 28 | | private bool infoRetrieverDisposed = false; |
| | 29 | | private bool assetRetrieverDisposed = false; |
| | 30 | | private readonly IInternalECSComponent<InternalRenderers> renderersComponent; |
| | 31 | |
|
| 6 | 32 | | public ECSNFTShapeComponentHandler(INFTShapeFrameFactory factory, INFTInfoRetriever infoRetriever, |
| | 33 | | INFTAssetRetriever assetRetriever, |
| | 34 | | IInternalECSComponent<InternalRenderers> renderersComponent) |
| | 35 | | { |
| 6 | 36 | | this.factory = factory; |
| 6 | 37 | | this.infoRetriever = infoRetriever; |
| 6 | 38 | | this.assetRetriever = assetRetriever; |
| 6 | 39 | | this.renderersComponent = renderersComponent; |
| 6 | 40 | | } |
| | 41 | |
|
| 0 | 42 | | public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) { } |
| | 43 | |
|
| | 44 | | public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity) |
| | 45 | | { |
| 6 | 46 | | if (!infoRetrieverDisposed) |
| | 47 | | { |
| 6 | 48 | | infoRetriever.Dispose(); |
| | 49 | | } |
| 6 | 50 | | if (!assetRetrieverDisposed) |
| | 51 | | { |
| 6 | 52 | | assetRetriever.Dispose(); |
| | 53 | | } |
| 6 | 54 | | DisposeShapeFrame(scene, entity, shapeFrame, renderersComponent); |
| 6 | 55 | | } |
| | 56 | |
|
| | 57 | | public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBNftShape model) |
| | 58 | | { |
| 9 | 59 | | bool shouldReloadFrame = shapeFrame != null && prevModel.GetStyle() != model.GetStyle(); |
| | 60 | |
|
| | 61 | | // destroy previous shape if style does not match |
| 9 | 62 | | if (shouldReloadFrame) |
| | 63 | | { |
| 1 | 64 | | DisposeShapeFrame(scene, entity, shapeFrame, renderersComponent); |
| 1 | 65 | | shapeFrame = null; |
| 1 | 66 | | nftLoadedScr = null; |
| | 67 | | } |
| | 68 | |
|
| | 69 | | // create shape if it does not exist |
| 9 | 70 | | if (shapeFrame == null) |
| | 71 | | { |
| 7 | 72 | | int style = (int)model.GetStyle(); |
| 7 | 73 | | shapeFrame = factory.InstantiateLoaderController(style); |
| 7 | 74 | | shapeFrame.gameObject.name = "NFT Shape mesh"; |
| 7 | 75 | | shapeFrame.gameObject.transform.SetParent(entity.gameObject.transform); |
| 7 | 76 | | shapeFrame.gameObject.transform.ResetLocalTRS(); |
| 7 | 77 | | renderersComponent.AddRenderer(scene, entity, shapeFrame.frameRenderer); |
| | 78 | | } |
| | 79 | |
|
| 9 | 80 | | Color3 modelColor = model.GetColor(); |
| 9 | 81 | | if (prevModel == null || !modelColor.Equals(prevModel.GetColor())) |
| | 82 | | { |
| 7 | 83 | | shapeFrame.UpdateBackgroundColor(new Color(modelColor.R, modelColor.G, modelColor.B, 1)); |
| | 84 | | } |
| | 85 | |
|
| 9 | 86 | | if (!string.IsNullOrEmpty(model.Urn) && nftLoadedScr != model.Urn) |
| | 87 | | { |
| 2 | 88 | | if (!infoRetrieverDisposed) |
| | 89 | | { |
| 2 | 90 | | infoRetriever.Dispose(); |
| 2 | 91 | | infoRetrieverDisposed = true; |
| | 92 | | } |
| 2 | 93 | | if (!assetRetrieverDisposed) |
| | 94 | | { |
| 2 | 95 | | assetRetriever.Dispose(); |
| 2 | 96 | | assetRetrieverDisposed = true; |
| | 97 | | } |
| 2 | 98 | | LoadNFT(model.Urn); |
| | 99 | | } |
| | 100 | |
|
| 9 | 101 | | prevModel = model; |
| 9 | 102 | | } |
| | 103 | |
|
| | 104 | | private static void DisposeShapeFrame(IParcelScene scene, IDCLEntity entity, |
| | 105 | | INFTShapeFrame nftFrame, IInternalECSComponent<InternalRenderers> renderersComponent) |
| | 106 | | { |
| 7 | 107 | | if (nftFrame == null) |
| 0 | 108 | | return; |
| | 109 | |
|
| 7 | 110 | | renderersComponent.RemoveRenderer(scene, entity, nftFrame.frameRenderer); |
| 7 | 111 | | nftFrame.Dispose(); |
| 7 | 112 | | Object.Destroy(nftFrame.gameObject); |
| 7 | 113 | | } |
| | 114 | |
|
| | 115 | | internal async void LoadNFT(string urn) |
| | 116 | | { |
| | 117 | | try |
| | 118 | | { |
| 2 | 119 | | infoRetrieverDisposed = false; |
| 2 | 120 | | NFTUtils.TryParseUrn(urn, out string chain, out string contractAddress, out string tokenId); |
| 2 | 121 | | NFTInfo? info = await infoRetriever.FetchNFTInfoAsync(chain, contractAddress, tokenId); |
| | 122 | |
|
| 2 | 123 | | if (!info.HasValue) |
| | 124 | | { |
| 0 | 125 | | LoadFailed(); |
| 0 | 126 | | return; |
| | 127 | | } |
| | 128 | |
|
| 2 | 129 | | assetRetrieverDisposed = false; |
| 2 | 130 | | NFTInfo nftInfo = info.Value; |
| 2 | 131 | | INFTAsset nftAsset = await assetRetriever.LoadNFTAsset(nftInfo.previewImageUrl); |
| | 132 | |
|
| 2 | 133 | | if (nftAsset == null) |
| | 134 | | { |
| 0 | 135 | | LoadFailed(); |
| 0 | 136 | | return; |
| | 137 | | } |
| 2 | 138 | | shapeFrame.SetImage(nftInfo.name, nftInfo.imageUrl, nftAsset); |
| 2 | 139 | | nftLoadedScr = urn; |
| 2 | 140 | | } |
| 0 | 141 | | catch (TaskCanceledException) { } |
| 0 | 142 | | catch (OperationCanceledException) { } |
| | 143 | | catch (Exception exception) |
| | 144 | | { |
| 0 | 145 | | Debug.LogError(exception); |
| 0 | 146 | | } |
| 2 | 147 | | } |
| | 148 | |
|
| | 149 | | private void LoadFailed() |
| | 150 | | { |
| 0 | 151 | | factory.InstantiateErrorFeedback(shapeFrame.gameObject); |
| 0 | 152 | | shapeFrame.FailLoading(); |
| 0 | 153 | | } |
| | 154 | | } |
| | 155 | | } |