< 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:38
Coverable lines:81
Total lines:207
Line coverage:53% (43 of 81)
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%
OnAvatarExpression(...)0%110100%
OnUserProfileOnUpdate(...)0%330100%
LoadingAvatarRoutine()0%609014.29%
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.FatalErrorReporter;
 9using DCL.Interface;
 10using DCL.NotificationModel;
 11using GPUSkinning;
 12using UnityEngine;
 13using Type = DCL.NotificationModel.Type;
 14
 15public class PlayerAvatarController : MonoBehaviour
 16{
 17    private const string LOADING_WEARABLES_ERROR_MESSAGE = "There was a problem loading your wearables";
 18
 19    private AvatarSystem.Avatar avatar;
 20    private CancellationTokenSource avatarLoadingCts = null;
 21    public GameObject avatarContainer;
 59722    private readonly AvatarModel currentAvatar = new AvatarModel { wearables = new List<string>() };
 23
 24    public Collider avatarCollider;
 25    public AvatarVisibility avatarVisibility;
 26    [SerializeField] private GameObject loadingParticlesPrefab;
 59727    public float cameraDistanceToDeactivate = 1.0f;
 28
 228829    private UserProfile userProfile => UserProfile.GetOwnUserProfile();
 030    private bool repositioningWorld => DCLCharacterController.i.characterPosition.RepositionedWorldLastFrame();
 31
 32    private bool enableCameraCheck = false;
 33    private Camera mainCamera;
 34    private PlayerAvatarAnalytics playerAvatarAnalytics;
 35    private IFatalErrorReporter fatalErrorReporter; // TODO?
 36    private string VISIBILITY_CONSTRAIN;
 37
 38    private void Start()
 39    {
 57140        DataStore.i.common.isPlayerRendererLoaded.Set(false);
 57141        IAnalytics analytics = DCL.Environment.i.platform.serviceProviders.analytics;
 57142        playerAvatarAnalytics = new PlayerAvatarAnalytics(analytics, CommonScriptableObjects.playerCoords);
 43
 57144        avatar = new AvatarSystem.Avatar(
 45            new AvatarCurator(new WearableItemResolver()),
 46            new Loader(new WearableLoaderFactory(), avatarContainer, new AvatarMeshCombinerHelper()),
 47            GetComponentInChildren<AvatarAnimatorLegacy>(),
 48            new Visibility(),
 49            new NoLODs(),
 50            new SimpleGPUSkinning(),
 51            new GPUSkinningThrottler());
 52
 57153        if ( UserProfileController.i != null )
 54        {
 055            UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail;
 056            UserProfileController.i.OnBaseWereablesFail += OnBaseWereablesFail;
 57        }
 58
 57159        DataStore.i.player.playerCollider.Set(avatarCollider);
 57160        CommonScriptableObjects.rendererState.AddLock(this);
 61
 62#if UNITY_WEBGL
 63        fatalErrorReporter = new WebFatalErrorReporter();
 64#else
 57165        fatalErrorReporter = new DefaultFatalErrorReporter(DataStore.i);
 66#endif
 67
 57168        mainCamera = Camera.main;
 57169    }
 70
 71    private void OnBaseWereablesFail()
 72    {
 073        UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail;
 74
 075        if (enableCameraCheck)
 076            ShowWearablesWarning();
 077    }
 78
 79    private void ShowWearablesWarning()
 80    {
 081        NotificationsController.i.ShowNotification(new Model
 82        {
 83            message = LOADING_WEARABLES_ERROR_MESSAGE,
 84            type = Type.GENERIC,
 85            timer = 10f,
 86            destroyOnFinish = true
 87        });
 088    }
 89
 90    private void Update()
 91    {
 657092        if (!enableCameraCheck || repositioningWorld)
 657093            return;
 94
 095        if (mainCamera == null)
 96        {
 097            mainCamera = Camera.main;
 98
 099            if (mainCamera == null)
 0100                return;
 101        }
 102
 0103        bool shouldBeVisible = Vector3.Distance(mainCamera.transform.position, transform.position) > cameraDistanceToDea
 0104        avatarVisibility.SetVisibility("PLAYER_AVATAR_CONTROLLER", shouldBeVisible);
 0105    }
 106
 107    public void SetAvatarVisibility(bool isVisible)
 108    {
 43109        VISIBILITY_CONSTRAIN = "own_player_invisible";
 43110        if (isVisible)
 5111            avatar.RemoveVisibilityConstrain(VISIBILITY_CONSTRAIN);
 112        else
 38113            avatar.AddVisibilityConstrain(VISIBILITY_CONSTRAIN);
 38114    }
 115
 116    private void OnEnable()
 117    {
 572118        userProfile.OnUpdate += OnUserProfileOnUpdate;
 572119        userProfile.OnAvatarExpressionSet += OnAvatarExpression;
 572120    }
 121
 122    private void OnAvatarExpression(string id, long timestamp)
 123    {
 2124        avatar.SetExpression(id, timestamp);
 2125        playerAvatarAnalytics.ReportExpression(id);
 2126    }
 127
 128    private void OnUserProfileOnUpdate(UserProfile profile)
 129    {
 21130        avatarLoadingCts?.Cancel();
 21131        avatarLoadingCts?.Dispose();
 21132        avatarLoadingCts = new CancellationTokenSource();
 21133        LoadingAvatarRoutine(profile, avatarLoadingCts.Token);
 21134    }
 135
 136    private async UniTaskVoid LoadingAvatarRoutine(UserProfile profile, CancellationToken ct)
 137    {
 21138        if (string.IsNullOrEmpty(profile.avatar.bodyShape) || profile.avatar.wearables == null)
 139        {
 21140            avatar.Dispose();
 21141            return;
 142        }
 143
 0144        ct.ThrowIfCancellationRequested();
 0145        if (avatar.status != IAvatar.Status.Loaded || !profile.avatar.HaveSameWearablesAndColors(currentAvatar))
 146        {
 147            try
 148            {
 0149                currentAvatar.CopyFrom(profile.avatar);
 150
 0151                List<string> wearableItems = profile.avatar.wearables.ToList();
 0152                wearableItems.Add(profile.avatar.bodyShape);
 0153                await avatar.Load(wearableItems, new AvatarSettings
 154                {
 155                    bodyshapeId = profile.avatar.bodyShape,
 156                    eyesColor = profile.avatar.eyeColor,
 157                    skinColor = profile.avatar.skinColor,
 158                    hairColor = profile.avatar.hairColor,
 159                }, ct);
 160
 0161                if (avatar.lodLevel <= 1)
 0162                    AvatarSystemUtils.SpawnAvatarLoadedParticles(avatarContainer.transform, loadingParticlesPrefab);
 0163            }
 0164            catch (OperationCanceledException)
 165            {
 0166                return;
 167            }
 168            catch (Exception e)
 169            {
 0170                Debug.LogException(e);
 0171                WebInterface.ReportAvatarFatalError();
 0172                return;
 173            }
 174        }
 175
 0176        IAvatarAnchorPoints anchorPoints = new AvatarAnchorPoints();
 0177        anchorPoints.Prepare(avatarContainer.transform, avatar.GetBones(), avatar.extents.y);
 178
 0179        var player = new Player()
 180        {
 181            id = userProfile.userId,
 182            name = userProfile.name,
 183            avatar = avatar,
 184            anchorPoints = anchorPoints
 185        };
 0186        DataStore.i.player.ownPlayer.Set(player);
 187
 0188        enableCameraCheck = true;
 0189        avatarCollider.gameObject.SetActive(true);
 0190        CommonScriptableObjects.rendererState.RemoveLock(this);
 0191        DataStore.i.common.isPlayerRendererLoaded.Set(true);
 21192    }
 193
 194    private void OnDisable()
 195    {
 572196        userProfile.OnUpdate -= OnUserProfileOnUpdate;
 572197        userProfile.OnAvatarExpressionSet -= OnAvatarExpression;
 572198    }
 199
 200    private void OnDestroy()
 201    {
 596202        avatarLoadingCts?.Cancel();
 596203        avatarLoadingCts?.Dispose();
 596204        avatarLoadingCts = null;
 596205        avatar?.Dispose();
 571206    }
 207}