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