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