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