< Summary

Class:AvatarSystem.Avatar
Assembly:AvatarSystem
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/AvatarSystem/Avatar.cs
Covered lines:60
Uncovered lines:9
Coverable lines:69
Total lines:139
Line coverage:86.9% (60 of 69)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Avatar(...)0%110100%
Load()0%10.0110095.12%
AddVisibilityConstrain(...)0%110100%
RemoveVisibilityConstrain(...)0%110100%
PlayEmote(...)0%220100%
SetLODLevel(...)0%2100%
SetAnimationThrottling(...)0%2100%
SetImpostorTexture(...)0%2100%
SetImpostorTint(...)0%2100%
GetBones()0%2100%
Dispose()0%880100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/AvatarSystem/Avatar.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Runtime.ExceptionServices;
 5using System.Threading;
 6using Cysharp.Threading.Tasks;
 7using DCL.Emotes;
 8using GPUSkinning;
 9using UnityEngine;
 10
 11namespace AvatarSystem
 12{
 13    public class Avatar : IAvatar
 14    {
 15        private const float RESCALING_BOUNDS_FACTOR = 100f;
 16        internal const string LOADING_VISIBILITY_CONSTRAIN = "Loading";
 17        private readonly IAvatarCurator avatarCurator;
 18        private readonly ILoader loader;
 19        private readonly IAnimator animator;
 20        private readonly IVisibility visibility;
 21        private readonly ILOD lod;
 22        private readonly IGPUSkinning gpuSkinning;
 23        private readonly IGPUSkinningThrottler gpuSkinningThrottler;
 24        private readonly IEmoteAnimationEquipper emoteAnimationEquipper;
 130825        private CancellationTokenSource disposeCts = new CancellationTokenSource();
 26
 327        public IAvatar.Status status { get; private set; } = IAvatar.Status.Idle;
 028        public Vector3 extents { get; private set; }
 029        public int lodLevel => lod?.lodIndex ?? 0;
 30
 130831        public Avatar(IAvatarCurator avatarCurator, ILoader loader, IAnimator animator, IVisibility visibility, ILOD lod
 32        {
 130833            this.avatarCurator = avatarCurator;
 130834            this.loader = loader;
 130835            this.animator = animator;
 130836            this.visibility = visibility;
 130837            this.lod = lod;
 130838            this.gpuSkinning = gpuSkinning;
 130839            this.gpuSkinningThrottler = gpuSkinningThrottler;
 130840            this.emoteAnimationEquipper = emoteAnimationEquipper;
 130841        }
 42
 43        /// <summary>
 44        /// Starts the loading process for the Avatar.
 45        /// </summary>
 46        /// <param name="wearablesIds"></param>
 47        /// <param name="settings"></param>
 48        /// <param name="ct"></param>
 49        public async UniTask Load(List<string> wearablesIds, AvatarSettings settings, CancellationToken ct = default)
 50        {
 8151            disposeCts ??= new CancellationTokenSource();
 52
 8153            status = IAvatar.Status.Idle;
 8154            CancellationToken linkedCt = CancellationTokenSource.CreateLinkedTokenSource(ct, disposeCts.Token).Token;
 55
 8156            linkedCt.ThrowIfCancellationRequested();
 57
 58            try
 59            {
 8060                visibility.AddGlobalConstrain(LOADING_VISIBILITY_CONSTRAIN);
 8061                WearableItem bodyshape = null;
 8062                WearableItem eyes = null;
 8063                WearableItem eyebrows = null;
 8064                WearableItem mouth = null;
 8065                List<WearableItem> wearables = null;
 8066                List<WearableItem> emotes = null;
 67
 8268                (bodyshape, eyes, eyebrows, mouth, wearables, emotes) = await avatarCurator.Curate(settings, wearablesId
 69
 23070                await loader.Load(bodyshape, eyes, eyebrows, mouth, wearables, settings, linkedCt);
 71
 72                //Scale the bounds due to the giant avatar not being skinned yet
 173                extents = loader.combinedRenderer.localBounds.extents * 2f / RESCALING_BOUNDS_FACTOR;
 74
 175                animator.Prepare(settings.bodyshapeId, loader.bodyshapeContainer);
 76
 177                emoteAnimationEquipper.SetEquippedEmotes(settings.bodyshapeId, emotes);
 78
 179                gpuSkinning.Prepare(loader.combinedRenderer);
 180                gpuSkinningThrottler.Bind(gpuSkinning);
 81
 182                visibility.Bind(gpuSkinning.renderer, loader.facialFeaturesRenderers);
 183                visibility.RemoveGlobalConstrain(LOADING_VISIBILITY_CONSTRAIN);
 84
 185                lod.Bind(gpuSkinning.renderer);
 186                gpuSkinningThrottler.Start();
 87
 188                status = IAvatar.Status.Loaded;
 189            }
 7790            catch (OperationCanceledException)
 91            {
 7792                Dispose();
 7793            }
 294            catch (Exception e)
 95            {
 296                Dispose();
 297                Debug.Log($"Avatar.Load failed with wearables:[{string.Join(",", wearablesIds)}] for bodyshape:{settings
 298                if (e.InnerException != null)
 099                    ExceptionDispatchInfo.Capture(e.InnerException).Throw();
 100                else
 2101                    throw;
 0102            }
 103            finally
 104            {
 80105                disposeCts?.Dispose();
 80106                disposeCts = null;
 107            }
 78108        }
 109
 76110        public void AddVisibilityConstrain(string key) { visibility.AddGlobalConstrain(key); }
 111
 10112        public void RemoveVisibilityConstrain(string key) { visibility.RemoveGlobalConstrain(key); }
 113
 4114        public void PlayEmote(string emoteId, long timestamps) { animator?.PlayEmote(emoteId, timestamps); }
 115
 0116        public void SetLODLevel(int lodIndex) { lod.SetLodIndex(lodIndex); }
 117
 0118        public void SetAnimationThrottling(int framesBetweenUpdate) { gpuSkinningThrottler.SetThrottling(framesBetweenUp
 119
 0120        public void SetImpostorTexture(Texture2D impostorTexture) { lod.SetImpostorTexture(impostorTexture); }
 121
 0122        public void SetImpostorTint(Color color) { lod.SetImpostorTint(color); }
 123
 0124        public Transform[] GetBones() => loader.GetBones();
 125
 126        public void Dispose()
 127        {
 1407128            status = IAvatar.Status.Idle;
 1407129            disposeCts?.Cancel();
 1407130            disposeCts?.Dispose();
 1407131            disposeCts = null;
 1407132            avatarCurator?.Dispose();
 1407133            loader?.Dispose();
 1407134            visibility?.Dispose();
 1407135            lod?.Dispose();
 1407136            gpuSkinningThrottler?.Dispose();
 1407137        }
 138    }
 139}