< Summary

Class:PlayerAvatarController
Assembly:PlayerAvatarController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/PlayerAvatarController/PlayerAvatarController.cs
Covered lines:43
Uncovered lines:40
Coverable lines:83
Total lines:215
Line coverage:51.8% (43 of 83)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PlayerAvatarController()0%110100%
Start()0%2.022083.33%
OnBaseWereablesFail()0%6200%
ShowWearablesWarning()0%2100%
Update()0%16.765022.22%
SetAvatarVisibility(...)0%220100%
OnEnable()0%110100%
OnAvatarEmote(...)0%110100%
OnUserProfileOnUpdate(...)0%330100%
LoadingAvatarRoutine()0%75.110013.33%
OnDisable()0%110100%
OnDestroy()0%440100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/PlayerAvatarController/PlayerAvatarController.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Threading;
 5using AvatarSystem;
 6using Cysharp.Threading.Tasks;
 7using DCL;
 8using DCL.Emotes;
 9using DCL.FatalErrorReporter;
 10using DCL.Interface;
 11using DCL.NotificationModel;
 12using GPUSkinning;
 13using UnityEngine;
 14using Type = DCL.NotificationModel.Type;
 15
 16public class PlayerAvatarController : MonoBehaviour
 17{
 18    private const string LOADING_WEARABLES_ERROR_MESSAGE = "There was a problem loading your wearables";
 19
 20    private AvatarSystem.Avatar avatar;
 21    private CancellationTokenSource avatarLoadingCts = null;
 22    public GameObject avatarContainer;
 59823    private readonly AvatarModel currentAvatar = new AvatarModel { wearables = new List<string>() };
 24
 25    public Collider avatarCollider;
 26    public AvatarVisibility avatarVisibility;
 27    [SerializeField] private GameObject loadingParticlesPrefab;
 59828    public float cameraDistanceToDeactivate = 1.0f;
 29
 229230    private UserProfile userProfile => UserProfile.GetOwnUserProfile();
 031    private bool repositioningWorld => DCLCharacterController.i.characterPosition.RepositionedWorldLastFrame();
 32
 33    private bool enableCameraCheck = false;
 34    private Camera mainCamera;
 35    private PlayerAvatarAnalytics playerAvatarAnalytics;
 36    private IFatalErrorReporter fatalErrorReporter; // TODO?
 37    private string VISIBILITY_CONSTRAIN;
 38
 39    private void Start()
 40    {
 57241        DataStore.i.common.isPlayerRendererLoaded.Set(false);
 57242        IAnalytics analytics = DCL.Environment.i.platform.serviceProviders.analytics;
 57243        playerAvatarAnalytics = new PlayerAvatarAnalytics(analytics, CommonScriptableObjects.playerCoords);
 44
 57245        AvatarAnimatorLegacy animator = GetComponentInChildren<AvatarAnimatorLegacy>();
 57246        avatar = new AvatarSystem.Avatar(
 47            new AvatarCurator(new WearableItemResolver()),
 48            new Loader(new WearableLoaderFactory(), avatarContainer, new AvatarMeshCombinerHelper()),
 49            animator,
 50            new Visibility(),
 51            new NoLODs(),
 52            new SimpleGPUSkinning(),
 53            new GPUSkinningThrottler(),
 54            new EmoteAnimationEquipper(animator, DataStore.i.emotes));
 55
 57256        if ( UserProfileController.i != null )
 57        {
 058            UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail;
 059            UserProfileController.i.OnBaseWereablesFail += OnBaseWereablesFail;
 60        }
 61
 57262        CommonScriptableObjects.rendererState.AddLock(this);
 63
 64#if UNITY_WEBGL
 65        fatalErrorReporter = new WebFatalErrorReporter();
 66#else
 57267        fatalErrorReporter = new DefaultFatalErrorReporter(DataStore.i);
 68#endif
 69
 57270        mainCamera = Camera.main;
 57271    }
 72
 73    private void OnBaseWereablesFail()
 74    {
 075        UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail;
 76
 077        if (enableCameraCheck)
 078            ShowWearablesWarning();
 079    }
 80
 81    private void ShowWearablesWarning()
 82    {
 083        NotificationsController.i.ShowNotification(new Model
 84        {
 85            message = LOADING_WEARABLES_ERROR_MESSAGE,
 86            type = Type.GENERIC,
 87            timer = 10f,
 88            destroyOnFinish = true
 89        });
 090    }
 91
 92    private void Update()
 93    {
 554894        if (!enableCameraCheck || repositioningWorld)
 554895            return;
 96
 097        if (mainCamera == null)
 98        {
 099            mainCamera = Camera.main;
 100
 0101            if (mainCamera == null)
 0102                return;
 103        }
 104
 0105        bool shouldBeVisible = Vector3.Distance(mainCamera.transform.position, transform.position) > cameraDistanceToDea
 0106        avatarVisibility.SetVisibility("PLAYER_AVATAR_CONTROLLER", shouldBeVisible);
 0107    }
 108
 109    public void SetAvatarVisibility(bool isVisible)
 110    {
 43111        VISIBILITY_CONSTRAIN = "own_player_invisible";
 43112        if (isVisible)
 5113            avatar.RemoveVisibilityConstrain(VISIBILITY_CONSTRAIN);
 114        else
 38115            avatar.AddVisibilityConstrain(VISIBILITY_CONSTRAIN);
 38116    }
 117
 118    private void OnEnable()
 119    {
 573120        userProfile.OnUpdate += OnUserProfileOnUpdate;
 573121        userProfile.OnAvatarEmoteSet += OnAvatarEmote;
 573122    }
 123
 124    private void OnAvatarEmote(string id, long timestamp)
 125    {
 2126        avatar.PlayEmote(id, timestamp);
 2127        playerAvatarAnalytics.ReportExpression(id);
 2128    }
 129
 130    private void OnUserProfileOnUpdate(UserProfile profile)
 131    {
 21132        avatarLoadingCts?.Cancel();
 21133        avatarLoadingCts?.Dispose();
 21134        avatarLoadingCts = new CancellationTokenSource();
 21135        LoadingAvatarRoutine(profile, avatarLoadingCts.Token);
 21136    }
 137
 138    private async UniTaskVoid LoadingAvatarRoutine(UserProfile profile, CancellationToken ct)
 139    {
 21140        if (string.IsNullOrEmpty(profile.avatar.bodyShape) || profile.avatar.wearables == null)
 141        {
 21142            avatar.Dispose();
 21143            return;
 144        }
 145
 0146        ct.ThrowIfCancellationRequested();
 0147        if (avatar.status != IAvatar.Status.Loaded || !profile.avatar.HaveSameWearablesAndColors(currentAvatar))
 148        {
 149            try
 150            {
 0151                currentAvatar.CopyFrom(profile.avatar);
 152
 0153                List<string> wearableItems = profile.avatar.wearables.ToList();
 0154                wearableItems.Add(profile.avatar.bodyShape);
 155
 156                //temporarily hardcoding the embedded emotes until the user profile provides the equipped ones
 0157                var embeddedEmotesSo = Resources.Load<EmbeddedEmotesSO>("EmbeddedEmotes");
 0158                wearableItems.AddRange(embeddedEmotesSo.emotes.Select(x => x.id));
 159
 0160                await avatar.Load(wearableItems, new AvatarSettings
 161                {
 162                    bodyshapeId = profile.avatar.bodyShape,
 163                    eyesColor = profile.avatar.eyeColor,
 164                    skinColor = profile.avatar.skinColor,
 165                    hairColor = profile.avatar.hairColor,
 166                }, ct);
 167
 0168                if (avatar.lodLevel <= 1)
 0169                    AvatarSystemUtils.SpawnAvatarLoadedParticles(avatarContainer.transform, loadingParticlesPrefab);
 0170            }
 0171            catch (OperationCanceledException)
 172            {
 0173                return;
 174            }
 175            catch (Exception e)
 176            {
 0177                Debug.LogException(e);
 0178                WebInterface.ReportAvatarFatalError();
 0179                return;
 180            }
 181        }
 182
 0183        IAvatarAnchorPoints anchorPoints = new AvatarAnchorPoints();
 0184        anchorPoints.Prepare(avatarContainer.transform, avatar.GetBones(), avatar.extents.y);
 185
 0186        var player = new Player()
 187        {
 188            id = userProfile.userId,
 189            name = userProfile.name,
 190            avatar = avatar,
 191            anchorPoints = anchorPoints,
 192            collider = avatarCollider
 193        };
 0194        DataStore.i.player.ownPlayer.Set(player);
 195
 0196        enableCameraCheck = true;
 0197        avatarCollider.gameObject.SetActive(true);
 0198        CommonScriptableObjects.rendererState.RemoveLock(this);
 0199        DataStore.i.common.isPlayerRendererLoaded.Set(true);
 21200    }
 201
 202    private void OnDisable()
 203    {
 573204        userProfile.OnUpdate -= OnUserProfileOnUpdate;
 573205        userProfile.OnAvatarEmoteSet -= OnAvatarEmote;
 573206    }
 207
 208    private void OnDestroy()
 209    {
 597210        avatarLoadingCts?.Cancel();
 597211        avatarLoadingCts?.Dispose();
 597212        avatarLoadingCts = null;
 597213        avatar?.Dispose();
 572214    }
 215}