< 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:73
Coverable lines:116
Total lines:298
Line coverage:37% (43 of 116)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PlayerAvatarController()0%110100%
Start()0%3.113076.92%
GetStandardAvatar()0%110100%
GetAvatarWithHologram()0%2100%
OnBaseWereablesFail()0%6200%
ShowWearablesWarning()0%2100%
Update()0%24.436020%
SetAvatarVisibility(...)0%6200%
OnEnable()0%110100%
OnAvatarEmote(...)0%3.023087.5%
OnUserProfileOnUpdate(...)0%330100%
LoadingAvatarRoutine()0%109.7312012.12%
ApplyHideAvatarModifier()0%6200%
RemoveHideAvatarModifier()0%6200%
ApplyHidePassportModifier()0%2100%
RemoveHidePassportModifier()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 Environment = DCL.Environment;
 16using Type = DCL.NotificationModel.Type;
 17
 18public class PlayerAvatarController : MonoBehaviour, IHideAvatarAreaHandler, IHidePassportAreaHandler
 19{
 20    private const string LOADING_WEARABLES_ERROR_MESSAGE = "There was a problem loading your wearables";
 21    private const string IN_HIDE_AREA = "IN_HIDE_AREA";
 22    private const string INSIDE_CAMERA = "INSIDE_CAMERA";
 23
 24    private IAvatar avatar;
 25    private CancellationTokenSource avatarLoadingCts = null;
 26    public GameObject avatarContainer;
 27    public GameObject armatureContainer;
 28    public Transform loadingAvatarContainer;
 29    public StickersController stickersControllers;
 43430    private readonly AvatarModel currentAvatar = new AvatarModel { wearables = new List<string>() };
 31
 32    public Collider avatarCollider;
 33    [SerializeField] private GameObject loadingParticlesPrefab;
 43434    public float cameraDistanceToDeactivate = 1.0f;
 35
 158436    private UserProfile userProfile => UserProfile.GetOwnUserProfile();
 037    private bool repositioningWorld => DCLCharacterController.i.characterPosition.RepositionedWorldLastFrame();
 38
 39    private bool enableCameraCheck = false;
 40    private Camera mainCamera;
 41    private IFatalErrorReporter fatalErrorReporter; // TODO?
 42    private string VISIBILITY_CONSTRAIN;
 43    private BaseRefCounter<AvatarModifierAreaID> currentActiveModifiers;
 44
 45    internal ISocialAnalytics socialAnalytics;
 46
 47    private void Start()
 48    {
 39549        DataStore.i.common.isPlayerRendererLoaded.Set(false);
 39550        socialAnalytics = new SocialAnalytics(
 51            DCL.Environment.i.platform.serviceProviders.analytics,
 52            new UserProfileWebInterfaceBridge());
 53
 39554        if (DataStore.i.avatarConfig.useHologramAvatar.Get())
 055            avatar = GetAvatarWithHologram();
 56        else
 39557            avatar = GetStandardAvatar();
 58
 39559        if ( UserProfileController.i != null )
 60        {
 061            UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail;
 062            UserProfileController.i.OnBaseWereablesFail += OnBaseWereablesFail;
 63        }
 64
 39565        CommonScriptableObjects.rendererState.AddLock(this);
 66
 67#if UNITY_WEBGL
 39568        fatalErrorReporter = new WebFatalErrorReporter();
 69#else
 70        fatalErrorReporter = new DefaultFatalErrorReporter(DataStore.i);
 71#endif
 72
 39573        mainCamera = Camera.main;
 39574        currentActiveModifiers = new BaseRefCounter<AvatarModifierAreaID>();
 39575    }
 76
 77    private IAvatar GetStandardAvatar() =>
 39578        Environment.i.serviceLocator.Get<IAvatarFactory>()
 79                   .CreateAvatar(avatarContainer, GetComponentInChildren<AvatarAnimatorLegacy>(), NoLODs.i, new Visibili
 80
 81    private IAvatar GetAvatarWithHologram()
 82    {
 083        AvatarAnimatorLegacy animator = GetComponentInChildren<AvatarAnimatorLegacy>();
 084        AvatarSystem.NoLODs noLod = new NoLODs();
 085        BaseAvatar baseAvatar = new BaseAvatar(loadingAvatarContainer, armatureContainer, noLod);
 086        return new AvatarSystem.AvatarWithHologram(
 87            baseAvatar,
 88            new AvatarCurator(new WearableItemResolver(), Environment.i.serviceLocator.Get<IEmotesCatalogService>()),
 89            new Loader(new WearableLoaderFactory(), avatarContainer, new AvatarMeshCombinerHelper()),
 90            animator,
 91            new Visibility(),
 92            noLod,
 93            new SimpleGPUSkinning(),
 94            new GPUSkinningThrottler(),
 95            new EmoteAnimationEquipper(animator, DataStore.i.emotes));
 96    }
 97
 98    private void OnBaseWereablesFail()
 99    {
 0100        UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail;
 101
 0102        if (enableCameraCheck)
 0103            ShowWearablesWarning();
 0104    }
 105
 106    private void ShowWearablesWarning()
 107    {
 0108        NotificationsController.i.ShowNotification(new Model
 109        {
 110            message = LOADING_WEARABLES_ERROR_MESSAGE,
 111            type = Type.GENERIC,
 112            timer = 10f,
 113            destroyOnFinish = true
 114        });
 0115    }
 116
 117    private void Update()
 118    {
 16060119        if (!enableCameraCheck || repositioningWorld)
 16060120            return;
 121
 0122        if (mainCamera == null)
 123        {
 0124            mainCamera = Camera.main;
 125
 0126            if (mainCamera == null)
 0127                return;
 128        }
 129
 0130        if (Vector3.Distance(mainCamera.transform.position, transform.position) > cameraDistanceToDeactivate)
 0131            avatar.RemoveVisibilityConstrain(INSIDE_CAMERA);
 132        else
 0133            avatar.AddVisibilityConstraint(INSIDE_CAMERA);
 0134    }
 135
 136    public void SetAvatarVisibility(bool isVisible)
 137    {
 0138        VISIBILITY_CONSTRAIN = "own_player_invisible";
 0139        if (isVisible)
 0140            avatar.RemoveVisibilityConstrain(VISIBILITY_CONSTRAIN);
 141        else
 0142            avatar.AddVisibilityConstraint(VISIBILITY_CONSTRAIN);
 0143    }
 144
 145    private void OnEnable()
 146    {
 396147        userProfile.OnUpdate += OnUserProfileOnUpdate;
 396148        userProfile.OnAvatarEmoteSet += OnAvatarEmote;
 396149    }
 150
 151    private void OnAvatarEmote(string id, long timestamp, UserProfile.EmoteSource source)
 152    {
 1153        avatar.PlayEmote(id, timestamp);
 154
 1155        bool found = DataStore.i.common.wearables.TryGetValue(id, out WearableItem emoteItem);
 1156        if (!found)
 157        {
 1158            var emotesCatalog = Environment.i.serviceLocator.Get<IEmotesCatalogService>();
 1159            emotesCatalog.TryGetLoadedEmote(id, out emoteItem);
 160        }
 161
 1162        if (emoteItem != null)
 163        {
 0164            socialAnalytics.SendPlayEmote(
 165                emoteItem.id,
 166                emoteItem.GetName(),
 167                emoteItem.rarity,
 168                emoteItem.data.tags.Contains(WearableLiterals.Tags.BASE_WEARABLE),
 169                source,
 170                $"{CommonScriptableObjects.playerCoords.Get().x},{CommonScriptableObjects.playerCoords.Get().y}");
 171        }
 1172    }
 173
 174    private void OnUserProfileOnUpdate(UserProfile profile)
 175    {
 6176        avatarLoadingCts?.Cancel();
 6177        avatarLoadingCts?.Dispose();
 6178        avatarLoadingCts = new CancellationTokenSource();
 6179        LoadingAvatarRoutine(profile, avatarLoadingCts.Token);
 6180    }
 181
 182    private async UniTaskVoid LoadingAvatarRoutine(UserProfile profile, CancellationToken ct)
 183    {
 6184        if (string.IsNullOrEmpty(profile.avatar.bodyShape) || profile.avatar.wearables == null)
 185        {
 6186            avatar.Dispose();
 6187            return;
 188        }
 189
 190        try
 191        {
 0192            ct.ThrowIfCancellationRequested();
 0193            if (avatar.status != IAvatar.Status.Loaded || !profile.avatar.HaveSameWearablesAndColors(currentAvatar))
 194            {
 0195                currentAvatar.CopyFrom(profile.avatar);
 196
 0197                List<string> wearableItems = profile.avatar.wearables.ToList();
 0198                wearableItems.Add(profile.avatar.bodyShape);
 199
 0200                HashSet<string> emotes = new HashSet<string>(currentAvatar.emotes.Select(x => x.urn));
 0201                var embeddedEmotesSo = Resources.Load<EmbeddedEmotesSO>("EmbeddedEmotes");
 0202                emotes.UnionWith(embeddedEmotesSo.emotes.Select(x => x.id));
 0203                wearableItems.AddRange(embeddedEmotesSo.emotes.Select(x => x.id));
 204
 0205                await avatar.Load(wearableItems, emotes.ToList(), new AvatarSettings
 206                {
 207                    bodyshapeId = profile.avatar.bodyShape,
 208                    eyesColor = profile.avatar.eyeColor,
 209                    skinColor = profile.avatar.skinColor,
 210                    hairColor = profile.avatar.hairColor,
 211                }, ct);
 212
 0213                if (avatar.lodLevel <= 1)
 0214                    AvatarSystemUtils.SpawnAvatarLoadedParticles(avatarContainer.transform, loadingParticlesPrefab);
 215
 0216                avatar.PlayEmote(profile.avatar.expressionTriggerId, profile.avatar.expressionTriggerTimestamp);
 217            }
 0218        }
 219        catch (OperationCanceledException ex)
 220        {
 0221            Debug.LogException(ex);
 0222            return;
 223        }
 224        catch (Exception e)
 225        {
 0226            Debug.LogException(e);
 0227            WebInterface.ReportAvatarFatalError(e.ToString());
 0228            return;
 229        }
 230
 0231        IAvatarAnchorPoints anchorPoints = new AvatarAnchorPoints();
 0232        anchorPoints.Prepare(avatarContainer.transform, avatar.GetBones(), AvatarSystemUtils.AVATAR_Y_OFFSET + avatar.ex
 233
 0234        var player = new Player()
 235        {
 236            id = userProfile.userId,
 237            name = userProfile.name,
 238            avatar = avatar,
 239            anchorPoints = anchorPoints,
 240            collider = avatarCollider
 241        };
 0242        DataStore.i.player.ownPlayer.Set(player);
 243
 0244        enableCameraCheck = true;
 0245        avatarCollider.gameObject.SetActive(true);
 0246        CommonScriptableObjects.rendererState.RemoveLock(this);
 0247        DataStore.i.common.isPlayerRendererLoaded.Set(true);
 6248    }
 249
 250    public void ApplyHideAvatarModifier()
 251    {
 0252        if (!currentActiveModifiers.ContainsKey(AvatarModifierAreaID.HIDE_AVATAR))
 253        {
 0254            avatar.AddVisibilityConstraint(IN_HIDE_AREA);
 0255            stickersControllers.ToggleHideArea(true);
 256        }
 0257        currentActiveModifiers.AddRefCount(AvatarModifierAreaID.HIDE_AVATAR);
 0258        DataStore.i.HUDs.avatarAreaWarnings.AddRefCount(AvatarModifierAreaID.HIDE_AVATAR);
 0259    }
 260
 261    public void RemoveHideAvatarModifier()
 262    {
 0263        DataStore.i.HUDs.avatarAreaWarnings.RemoveRefCount(AvatarModifierAreaID.HIDE_AVATAR);
 0264        currentActiveModifiers.RemoveRefCount(AvatarModifierAreaID.HIDE_AVATAR);
 0265        if (!currentActiveModifiers.ContainsKey(AvatarModifierAreaID.HIDE_AVATAR))
 266        {
 0267            avatar.RemoveVisibilityConstrain(IN_HIDE_AREA);
 0268            stickersControllers.ToggleHideArea(false);
 269        }
 0270    }
 271
 272    public void ApplyHidePassportModifier()
 273    {
 0274        DataStore.i.HUDs.avatarAreaWarnings.AddRefCount(AvatarModifierAreaID.DISABLE_PASSPORT);
 0275        currentActiveModifiers.AddRefCount(AvatarModifierAreaID.DISABLE_PASSPORT);
 0276    }
 277
 278    public void RemoveHidePassportModifier()
 279    {
 0280        DataStore.i.HUDs.avatarAreaWarnings.RemoveRefCount(AvatarModifierAreaID.DISABLE_PASSPORT);
 0281        currentActiveModifiers.RemoveRefCount(AvatarModifierAreaID.DISABLE_PASSPORT);
 0282    }
 283
 284    private void OnDisable()
 285    {
 396286        userProfile.OnUpdate -= OnUserProfileOnUpdate;
 396287        userProfile.OnAvatarEmoteSet -= OnAvatarEmote;
 396288    }
 289
 290    private void OnDestroy()
 291    {
 419292        avatarLoadingCts?.Cancel();
 419293        avatarLoadingCts?.Dispose();
 419294        avatarLoadingCts = null;
 419295        avatar?.Dispose();
 395296    }
 297
 298}