| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.Networking; |
| | 6 | | using Decentraland.Sdk.Ecs6; |
| | 7 | |
|
| | 8 | | namespace DCL.Components |
| | 9 | | { |
| | 10 | | public class DCLAvatarTexture : DCLTexture |
| | 11 | | { |
| | 12 | | [System.Serializable] |
| | 13 | | public class ProfileRequestData |
| | 14 | | { |
| | 15 | | [System.Serializable] |
| | 16 | | public class Avatars |
| | 17 | | { |
| | 18 | | public Avatar avatar; |
| | 19 | | } |
| | 20 | |
|
| | 21 | | [System.Serializable] |
| | 22 | | public class Avatar |
| | 23 | | { |
| | 24 | | public Snapshots snapshots; |
| | 25 | | } |
| | 26 | |
|
| | 27 | | [System.Serializable] |
| | 28 | | public class Snapshots |
| | 29 | | { |
| | 30 | | public string face256; |
| | 31 | | public string body; |
| | 32 | | } |
| | 33 | |
|
| | 34 | | public Avatars[] avatars; |
| | 35 | | } |
| | 36 | |
|
| | 37 | | [System.Serializable] |
| | 38 | | public class AvatarModel : Model |
| | 39 | | { |
| | 40 | | public string userId; |
| | 41 | | public override BaseModel GetDataFromJSON(string json) => |
| 0 | 42 | | Utils.SafeFromJson<AvatarModel>(json); |
| | 43 | |
|
| | 44 | | public override BaseModel GetDataFromPb(ComponentBodyPayload pbModel) { |
| 0 | 45 | | if (pbModel.PayloadCase != ComponentBodyPayload.PayloadOneofCase.AvatarTexture) |
| 0 | 46 | | return Utils.SafeUnimplemented<DCLTexture, Model>(expected: ComponentBodyPayload.PayloadOneofCase.Av |
| | 47 | |
|
| 0 | 48 | | var pb = new AvatarModel(); |
| 0 | 49 | | if (pbModel.AvatarTexture.HasWrap) pb.wrap = (BabylonWrapMode)pbModel.AvatarTexture.Wrap; |
| 0 | 50 | | if (pbModel.AvatarTexture.HasSamplingMode) pb.samplingMode = (FilterMode)pbModel.AvatarTexture.SamplingM |
| 0 | 51 | | if (pbModel.AvatarTexture.HasUserId) |
| | 52 | | { |
| 0 | 53 | | pb.src = pbModel.AvatarTexture.UserId; |
| 0 | 54 | | pb.userId = pbModel.AvatarTexture.UserId; |
| | 55 | | } |
| | 56 | |
|
| 0 | 57 | | return pb; |
| | 58 | |
|
| | 59 | | } |
| | 60 | | } |
| | 61 | |
|
| 0 | 62 | | public DCLAvatarTexture() |
| | 63 | | { |
| 0 | 64 | | model = new AvatarModel(); |
| 0 | 65 | | } |
| | 66 | |
|
| | 67 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 68 | | { |
| 0 | 69 | | yield return new WaitUntil(() => CommonScriptableObjects.rendererState.Get()); |
| | 70 | |
|
| | 71 | | //If the scene creates and destroy the component before our renderer has been turned on bad things happen! |
| | 72 | | //TODO: Analyze if we can catch this upstream and stop the IEnumerator |
| 0 | 73 | | if (isDisposed) |
| 0 | 74 | | yield break; |
| | 75 | |
|
| 0 | 76 | | AvatarModel model = (AvatarModel) newModel; |
| | 77 | |
|
| 0 | 78 | | if (texture == null && !string.IsNullOrEmpty(model.userId)) |
| | 79 | | { |
| 0 | 80 | | string textureUrl = string.Empty; |
| 0 | 81 | | string sourceUrl = Environment.i.platform.serviceProviders.catalyst.lambdasUrl + "/profiles?id=" + model |
| | 82 | |
|
| | 83 | | // The sourceUrl request should return an array, with an object |
| | 84 | | // the object has `timerstamp` and `avatars`, `avatars` is an array |
| | 85 | | // we only request a single avatar so with length=1 |
| | 86 | | // avatars[0] has the avatar and we have to access to |
| | 87 | | // avatars[0].avatar.snapshots, and the links are |
| | 88 | | // face,face128,face256 and body |
| | 89 | |
|
| | 90 | | // TODO: check if this user data already exists to avoid this fetch. |
| 0 | 91 | | yield return GetAvatarUrls(sourceUrl, (faceUrl) => |
| | 92 | | { |
| 0 | 93 | | textureUrl = faceUrl; |
| 0 | 94 | | }); |
| | 95 | |
|
| 0 | 96 | | if (!string.IsNullOrEmpty(textureUrl)) |
| | 97 | | { |
| 0 | 98 | | model.src = textureUrl; |
| 0 | 99 | | yield return base.ApplyChanges(model); |
| | 100 | | } |
| 0 | 101 | | } |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | private static IEnumerator GetAvatarUrls(string url, Action<string> onURLSuccess) |
| | 105 | | { |
| 0 | 106 | | yield return Environment.i.platform.webRequest.Get( |
| | 107 | | url: url, |
| | 108 | | downloadHandler: new DownloadHandlerBuffer(), |
| | 109 | | timeout: 10, |
| | 110 | | disposeOnCompleted: false, |
| | 111 | | OnFail: (webRequest) => |
| | 112 | | { |
| 0 | 113 | | Debug.LogWarning($"Request error! profile data couldn't be fetched! -- {webRequest.webRequest.error} |
| 0 | 114 | | }, |
| | 115 | | OnSuccess: (webRequest) => |
| | 116 | | { |
| 0 | 117 | | ProfileRequestData[] data = DCL.Helpers.Utils.ParseJsonArray<ProfileRequestData[]>(webRequest.webReq |
| 0 | 118 | | string face256Url = data[0]?.avatars[0]?.avatar.snapshots.face256; |
| 0 | 119 | | onURLSuccess?.Invoke(face256Url); |
| 0 | 120 | | }); |
| 0 | 121 | | } |
| | 122 | | } |
| | 123 | | } |