< Summary

Class:DCL.Components.DCLAvatarTexture
Assembly:DCL.Components.Texture
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Textures/DCLAvatarTexture.cs
Covered lines:0
Uncovered lines:36
Coverable lines:36
Total lines:123
Line coverage:0% (0 of 36)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:5
Method coverage:0% (0 of 5)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GetDataFromJSON(...)0%2100%
GetDataFromPb(...)0%30500%
DCLAvatarTexture()0%2100%
ApplyChanges()0%1101000%
GetAvatarUrls()0%20400%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Textures/DCLAvatarTexture.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using DCL.Helpers;
 4using UnityEngine;
 5using UnityEngine.Networking;
 6using Decentraland.Sdk.Ecs6;
 7
 8namespace 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) =>
 042                Utils.SafeFromJson<AvatarModel>(json);
 43
 44            public override BaseModel GetDataFromPb(ComponentBodyPayload pbModel) {
 045                if (pbModel.PayloadCase != ComponentBodyPayload.PayloadOneofCase.AvatarTexture)
 046                    return Utils.SafeUnimplemented<DCLTexture, Model>(expected: ComponentBodyPayload.PayloadOneofCase.Av
 47
 048                var pb = new AvatarModel();
 049                if (pbModel.AvatarTexture.HasWrap) pb.wrap = (BabylonWrapMode)pbModel.AvatarTexture.Wrap;
 050                if (pbModel.AvatarTexture.HasSamplingMode) pb.samplingMode = (FilterMode)pbModel.AvatarTexture.SamplingM
 051                if (pbModel.AvatarTexture.HasUserId)
 52                {
 053                    pb.src = pbModel.AvatarTexture.UserId;
 054                    pb.userId = pbModel.AvatarTexture.UserId;
 55                }
 56
 057                return pb;
 58
 59            }
 60        }
 61
 062        public DCLAvatarTexture()
 63        {
 064            model = new AvatarModel();
 065        }
 66
 67        public override IEnumerator ApplyChanges(BaseModel newModel)
 68        {
 069            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
 073            if (isDisposed)
 074                yield break;
 75
 076            AvatarModel model = (AvatarModel) newModel;
 77
 078            if (texture == null && !string.IsNullOrEmpty(model.userId))
 79            {
 080                string textureUrl = string.Empty;
 081                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.
 091                yield return GetAvatarUrls(sourceUrl, (faceUrl) =>
 92                {
 093                    textureUrl = faceUrl;
 094                });
 95
 096                if (!string.IsNullOrEmpty(textureUrl))
 97                {
 098                    model.src = textureUrl;
 099                    yield return base.ApplyChanges(model);
 100                }
 0101            }
 0102        }
 103
 104        private static IEnumerator GetAvatarUrls(string url, Action<string> onURLSuccess)
 105        {
 0106            yield return Environment.i.platform.webRequest.Get(
 107                url: url,
 108                downloadHandler: new DownloadHandlerBuffer(),
 109                timeout: 10,
 110                disposeOnCompleted: false,
 111                OnFail: (webRequest) =>
 112                {
 0113                    Debug.LogWarning($"Request error! profile data couldn't be fetched! -- {webRequest.webRequest.error}
 0114                },
 115                OnSuccess: (webRequest) =>
 116                {
 0117                    ProfileRequestData[] data = DCL.Helpers.Utils.ParseJsonArray<ProfileRequestData[]>(webRequest.webReq
 0118                    string face256Url = data[0]?.avatars[0]?.avatar.snapshots.face256;
 0119                    onURLSuccess?.Invoke(face256Url);
 0120                });
 0121        }
 122    }
 123}