< Summary

Class:PlayerAvatarController
Assembly:PlayerAvatarController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/PlayerAvatarController/PlayerAvatarController.cs
Covered lines:46
Uncovered lines:49
Coverable lines:95
Total lines:264
Line coverage:48.4% (46 of 95)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PlayerAvatarController()0%110100%
Start()0%3.143075%
GetStandardAvatar()0%110100%
GetAvatarWithHologram()0%2100%
OnBaseWereablesFail()0%6200%
ShowWearablesWarning()0%2100%
Update()0%24.436020%
SetAvatarVisibility(...)0%220100%
OnEnable()0%110100%
OnAvatarEmote(...)0%2.032080%
OnUserProfileOnUpdate(...)0%330100%
LoadingAvatarRoutine()0%75.110013.33%
ApplyHideModifier()0%2100%
RemoveHideModifier()0%2100%
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 SocialFeaturesAnalytics;
 14using UnityEngine;
 15using Type = DCL.NotificationModel.Type;
 16
 17public class PlayerAvatarController : MonoBehaviour, IHideAvatarAreaHandler
 18{
 19    private const string LOADING_WEARABLES_ERROR_MESSAGE = "There was a problem loading your wearables";
 20    private const string IN_HIDE_AREA = "IN_HIDE_AREA";
 21    private const string INSIDE_CAMERA = "INSIDE_CAMERA";
 22
 23    private IAvatar avatar;
 24    private CancellationTokenSource avatarLoadingCts = null;
 25    public GameObject avatarContainer;
 26    public GameObject armatureContainer;
 27    public Transform loadingAvatarContainer;
 59628    private readonly AvatarModel currentAvatar = new AvatarModel { wearables = new List<string>() };
 29
 30    public Collider avatarCollider;
 31    [SerializeField] private GameObject loadingParticlesPrefab;
 59632    public float cameraDistanceToDeactivate = 1.0f;
 33
 224034    private UserProfile userProfile => UserProfile.GetOwnUserProfile();
 035    private bool repositioningWorld => DCLCharacterController.i.characterPosition.RepositionedWorldLastFrame();
 36
 37    private bool enableCameraCheck = false;
 38    private Camera mainCamera;
 39    private IFatalErrorReporter fatalErrorReporter; // TODO?
 40    private string VISIBILITY_CONSTRAIN;
 41
 42    internal ISocialAnalytics socialAnalytics;
 43
 44    private void Start()
 45    {
 55946        DataStore.i.common.isPlayerRendererLoaded.Set(false);
 55947        socialAnalytics = new SocialAnalytics(
 48            DCL.Environment.i.platform.serviceProviders.analytics,
 49            new UserProfileWebInterfaceBridge());
 50
 55951        if (DataStore.i.avatarConfig.useHologramAvatar.Get())
 052            avatar = GetAvatarWithHologram();
 53        else
 55954            avatar = GetStandardAvatar();
 55
 55956        if ( UserProfileController.i != null )
 57        {
 058            UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail;
 059            UserProfileController.i.OnBaseWereablesFail += OnBaseWereablesFail;
 60        }
 61
 55962        CommonScriptableObjects.rendererState.AddLock(this);
 63
 64#if UNITY_WEBGL
 55965        fatalErrorReporter = new WebFatalErrorReporter();
 66#else
 67        fatalErrorReporter = new DefaultFatalErrorReporter(DataStore.i);
 68#endif
 69
 55970        mainCamera = Camera.main;
 55971    }
 72
 73    private AvatarSystem.Avatar GetStandardAvatar()
 74    {
 55975        AvatarAnimatorLegacy animator = GetComponentInChildren<AvatarAnimatorLegacy>();
 55976        AvatarSystem.NoLODs noLod = new NoLODs();
 55977        return new AvatarSystem.Avatar(
 78            new AvatarCurator(new WearableItemResolver()),
 79            new Loader(new WearableLoaderFactory(), avatarContainer, new AvatarMeshCombinerHelper()),
 80            animator,
 81            new Visibility(),
 82            noLod,
 83            new SimpleGPUSkinning(),
 84            new GPUSkinningThrottler(),
 85            new EmoteAnimationEquipper(animator, DataStore.i.emotes));
 86    }
 87
 88    private AvatarWithHologram GetAvatarWithHologram()
 89    {
 090        AvatarAnimatorLegacy animator = GetComponentInChildren<AvatarAnimatorLegacy>();
 091        AvatarSystem.NoLODs noLod = new NoLODs();
 092        BaseAvatar baseAvatar = new BaseAvatar(loadingAvatarContainer, armatureContainer, noLod);
 093        return new AvatarSystem.AvatarWithHologram(
 94            baseAvatar,
 95            new AvatarCurator(new WearableItemResolver()),
 96            new Loader(new WearableLoaderFactory(), avatarContainer, new AvatarMeshCombinerHelper()),
 97            animator,
 98            new Visibility(),
 99            noLod,
 100            new SimpleGPUSkinning(),
 101            new GPUSkinningThrottler(),
 102            new EmoteAnimationEquipper(animator, DataStore.i.emotes));
 103    }
 104
 105    private void OnBaseWereablesFail()
 106    {
 0107        UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail;
 108
 0109        if (enableCameraCheck)
 0110            ShowWearablesWarning();
 0111    }
 112
 113    private void ShowWearablesWarning()
 114    {
 0115        NotificationsController.i.ShowNotification(new Model
 116        {
 117            message = LOADING_WEARABLES_ERROR_MESSAGE,
 118            type = Type.GENERIC,
 119            timer = 10f,
 120            destroyOnFinish = true
 121        });
 0122    }
 123
 124    private void Update()
 125    {
 5253126        if (!enableCameraCheck || repositioningWorld)
 5253127            return;
 128
 0129        if (mainCamera == null)
 130        {
 0131            mainCamera = Camera.main;
 132
 0133            if (mainCamera == null)
 0134                return;
 135        }
 136
 0137        if (Vector3.Distance(mainCamera.transform.position, transform.position) > cameraDistanceToDeactivate)
 0138            avatar.RemoveVisibilityConstrain(INSIDE_CAMERA);
 139        else
 0140            avatar.AddVisibilityConstrain(INSIDE_CAMERA);
 0141    }
 142
 143    public void SetAvatarVisibility(bool isVisible)
 144    {
 6145        VISIBILITY_CONSTRAIN = "own_player_invisible";
 6146        if (isVisible)
 1147            avatar.RemoveVisibilityConstrain(VISIBILITY_CONSTRAIN);
 148        else
 5149            avatar.AddVisibilityConstrain(VISIBILITY_CONSTRAIN);
 5150    }
 151
 152    private void OnEnable()
 153    {
 560154        userProfile.OnUpdate += OnUserProfileOnUpdate;
 560155        userProfile.OnAvatarEmoteSet += OnAvatarEmote;
 560156    }
 157
 158    private void OnAvatarEmote(string id, long timestamp, UserProfile.EmoteSource source)
 159    {
 1160        avatar.PlayEmote(id, timestamp);
 161
 1162        DataStore.i.common.wearables.TryGetValue(id, out WearableItem emoteItem);
 163
 1164        if (emoteItem != null)
 165        {
 0166            socialAnalytics.SendPlayEmote(
 167                emoteItem.id,
 168                emoteItem.GetName(),
 169                emoteItem.rarity,
 170                emoteItem.data.tags.Contains(WearableLiterals.Tags.BASE_WEARABLE),
 171                source,
 172                $"{CommonScriptableObjects.playerCoords.Get().x},{CommonScriptableObjects.playerCoords.Get().y}");
 173        }
 1174    }
 175
 176    private void OnUserProfileOnUpdate(UserProfile profile)
 177    {
 7178        avatarLoadingCts?.Cancel();
 7179        avatarLoadingCts?.Dispose();
 7180        avatarLoadingCts = new CancellationTokenSource();
 7181        LoadingAvatarRoutine(profile, avatarLoadingCts.Token);
 7182    }
 183
 184    private async UniTaskVoid LoadingAvatarRoutine(UserProfile profile, CancellationToken ct)
 185    {
 7186        if (string.IsNullOrEmpty(profile.avatar.bodyShape) || profile.avatar.wearables == null)
 187        {
 7188            avatar.Dispose();
 7189            return;
 190        }
 191
 192        try
 193        {
 0194            ct.ThrowIfCancellationRequested();
 0195            if (avatar.status != IAvatar.Status.Loaded || !profile.avatar.HaveSameWearablesAndColors(currentAvatar))
 196            {
 0197                currentAvatar.CopyFrom(profile.avatar);
 198
 0199                List<string> wearableItems = profile.avatar.wearables.ToList();
 0200                wearableItems.Add(profile.avatar.bodyShape);
 201
 202                //temporarily hardcoding the embedded emotes until the user profile provides the equipped ones
 0203                var embeddedEmotesSo = Resources.Load<EmbeddedEmotesSO>("EmbeddedEmotes");
 0204                wearableItems.AddRange(embeddedEmotesSo.emotes.Select(x => x.id));
 205
 0206                await avatar.Load(wearableItems, new AvatarSettings
 207                {
 208                    bodyshapeId = profile.avatar.bodyShape,
 209                    eyesColor = profile.avatar.eyeColor,
 210                    skinColor = profile.avatar.skinColor,
 211                    hairColor = profile.avatar.hairColor,
 212                }, ct);
 213
 0214                if (avatar.lodLevel <= 1)
 0215                    AvatarSystemUtils.SpawnAvatarLoadedParticles(avatarContainer.transform, loadingParticlesPrefab);
 216            }
 0217        }
 0218        catch (OperationCanceledException)
 219        {
 0220            return;
 221        }
 222        catch (Exception e)
 223        {
 0224            Debug.LogException(e);
 0225            WebInterface.ReportAvatarFatalError();
 0226            return;
 227        }
 228
 0229        IAvatarAnchorPoints anchorPoints = new AvatarAnchorPoints();
 0230        anchorPoints.Prepare(avatarContainer.transform, avatar.GetBones(), AvatarSystemUtils.AVATAR_Y_OFFSET + avatar.ex
 231
 0232        var player = new Player()
 233        {
 234            id = userProfile.userId,
 235            name = userProfile.name,
 236            avatar = avatar,
 237            anchorPoints = anchorPoints,
 238            collider = avatarCollider
 239        };
 0240        DataStore.i.player.ownPlayer.Set(player);
 241
 0242        enableCameraCheck = true;
 0243        avatarCollider.gameObject.SetActive(true);
 0244        CommonScriptableObjects.rendererState.RemoveLock(this);
 0245        DataStore.i.common.isPlayerRendererLoaded.Set(true);
 7246    }
 247
 0248    public void ApplyHideModifier() { avatar.AddVisibilityConstrain(IN_HIDE_AREA); }
 0249    public void RemoveHideModifier() { avatar.RemoveVisibilityConstrain(IN_HIDE_AREA); }
 250
 251    private void OnDisable()
 252    {
 560253        userProfile.OnUpdate -= OnUserProfileOnUpdate;
 560254        userProfile.OnAvatarEmoteSet -= OnAvatarEmote;
 560255    }
 256
 257    private void OnDestroy()
 258    {
 583259        avatarLoadingCts?.Cancel();
 583260        avatarLoadingCts?.Dispose();
 583261        avatarLoadingCts = null;
 583262        avatar?.Dispose();
 559263    }
 264}