< Summary

Class:PlayerAvatarController
Assembly:PlayerAvatarController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/PlayerAvatarController/PlayerAvatarController.cs
Covered lines:42
Uncovered lines:72
Coverable lines:114
Total lines:295
Line coverage:36.8% (42 of 114)
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%6200%
OnEnable()0%110100%
OnAvatarEmote(...)0%3.023087.5%
OnUserProfileOnUpdate(...)0%330100%
LoadingAvatarRoutine()0%126.2213012.5%
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;
 42330    private readonly AvatarModel currentAvatar = new AvatarModel { wearables = new List<string>() };
 31
 32    public Collider avatarCollider;
 33    [SerializeField] private GameObject loadingParticlesPrefab;
 42334    public float cameraDistanceToDeactivate = 1.0f;
 35
 152836    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    {
 38149        DataStore.i.common.isPlayerRendererLoaded.Set(false);
 38150        socialAnalytics = new SocialAnalytics(
 51            DCL.Environment.i.platform.serviceProviders.analytics,
 52            new UserProfileWebInterfaceBridge());
 53
 38154        if (DataStore.i.avatarConfig.useHologramAvatar.Get())
 055            avatar = GetAvatarWithHologram();
 56        else
 38157            avatar = GetStandardAvatar();
 58
 38159        if ( UserProfileController.i != null )
 60        {
 061            UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail;
 062            UserProfileController.i.OnBaseWereablesFail += OnBaseWereablesFail;
 63        }
 64
 38165        CommonScriptableObjects.rendererState.AddLock(this);
 66
 38167        mainCamera = Camera.main;
 38168        currentActiveModifiers = new BaseRefCounter<AvatarModifierAreaID>();
 38169    }
 70
 71    private IAvatar GetStandardAvatar() =>
 38172        Environment.i.serviceLocator.Get<IAvatarFactory>()
 73                   .CreateAvatar(avatarContainer, GetComponentInChildren<AvatarAnimatorLegacy>(), NoLODs.i, new Visibili
 74
 75    private IAvatar GetAvatarWithHologram()
 76    {
 077        AvatarAnimatorLegacy animator = GetComponentInChildren<AvatarAnimatorLegacy>();
 078        AvatarSystem.NoLODs noLod = new NoLODs();
 079        BaseAvatar baseAvatar = new BaseAvatar(loadingAvatarContainer, armatureContainer, noLod);
 080        return new AvatarSystem.AvatarWithHologram(
 81            baseAvatar,
 82            new AvatarCurator(new WearableItemResolver(), Environment.i.serviceLocator.Get<IEmotesCatalogService>()),
 83            new Loader(new WearableLoaderFactory(), avatarContainer, new AvatarMeshCombinerHelper()),
 84            animator,
 85            new Visibility(),
 86            noLod,
 87            new SimpleGPUSkinning(),
 88            new GPUSkinningThrottler(),
 89            new EmoteAnimationEquipper(animator, DataStore.i.emotes));
 90    }
 91
 92    private void OnBaseWereablesFail()
 93    {
 094        UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail;
 95
 096        if (enableCameraCheck)
 097            ShowWearablesWarning();
 098    }
 99
 100    private void ShowWearablesWarning()
 101    {
 0102        NotificationsController.i.ShowNotification(new Model
 103        {
 104            message = LOADING_WEARABLES_ERROR_MESSAGE,
 105            type = Type.GENERIC,
 106            timer = 10f,
 107            destroyOnFinish = true
 108        });
 0109    }
 110
 111    private void Update()
 112    {
 14965113        if (!enableCameraCheck || repositioningWorld)
 14965114            return;
 115
 0116        if (mainCamera == null)
 117        {
 0118            mainCamera = Camera.main;
 119
 0120            if (mainCamera == null)
 0121                return;
 122        }
 123
 0124        if (Vector3.Distance(mainCamera.transform.position, transform.position) > cameraDistanceToDeactivate)
 0125            avatar.RemoveVisibilityConstrain(INSIDE_CAMERA);
 126        else
 0127            avatar.AddVisibilityConstraint(INSIDE_CAMERA);
 0128    }
 129
 130    public void SetAvatarVisibility(bool isVisible)
 131    {
 0132        VISIBILITY_CONSTRAIN = "own_player_invisible";
 0133        if (isVisible)
 0134            avatar.RemoveVisibilityConstrain(VISIBILITY_CONSTRAIN);
 135        else
 0136            avatar.AddVisibilityConstraint(VISIBILITY_CONSTRAIN);
 0137    }
 138
 139    private void OnEnable()
 140    {
 382141        userProfile.OnUpdate += OnUserProfileOnUpdate;
 382142        userProfile.OnAvatarEmoteSet += OnAvatarEmote;
 382143    }
 144
 145    private void OnAvatarEmote(string id, long timestamp, UserProfile.EmoteSource source)
 146    {
 1147        avatar.PlayEmote(id, timestamp);
 148
 1149        bool found = DataStore.i.common.wearables.TryGetValue(id, out WearableItem emoteItem);
 1150        if (!found)
 151        {
 1152            var emotesCatalog = Environment.i.serviceLocator.Get<IEmotesCatalogService>();
 1153            emotesCatalog.TryGetLoadedEmote(id, out emoteItem);
 154        }
 155
 1156        if (emoteItem != null)
 157        {
 0158            socialAnalytics.SendPlayEmote(
 159                emoteItem.id,
 160                emoteItem.GetName(),
 161                emoteItem.rarity,
 162                emoteItem.data.tags.Contains(WearableLiterals.Tags.BASE_WEARABLE),
 163                source,
 164                $"{CommonScriptableObjects.playerCoords.Get().x},{CommonScriptableObjects.playerCoords.Get().y}");
 165        }
 1166    }
 167
 168    private void OnUserProfileOnUpdate(UserProfile profile)
 169    {
 1170        avatarLoadingCts?.Cancel();
 1171        avatarLoadingCts?.Dispose();
 1172        avatarLoadingCts = new CancellationTokenSource();
 1173        LoadingAvatarRoutine(profile, avatarLoadingCts.Token);
 1174    }
 175
 176    private async UniTaskVoid LoadingAvatarRoutine(UserProfile profile, CancellationToken ct)
 177    {
 1178        if (string.IsNullOrEmpty(profile.avatar.bodyShape) || profile.avatar.wearables == null)
 179        {
 1180            avatar.Dispose();
 1181            return;
 182        }
 183
 184        try
 185        {
 0186            ct.ThrowIfCancellationRequested();
 187
 0188            if (avatar.status != IAvatar.Status.Loaded || !profile.avatar.HaveSameWearablesAndColors(currentAvatar))
 189            {
 0190                currentAvatar.CopyFrom(profile.avatar);
 191
 0192                List<string> wearableItems = profile.avatar.wearables.ToList();
 0193                wearableItems.Add(profile.avatar.bodyShape);
 194
 0195                HashSet<string> emotes = new HashSet<string>(currentAvatar.emotes.Select(x => x.urn));
 0196                var embeddedEmotesSo = Resources.Load<EmbeddedEmotesSO>("EmbeddedEmotes");
 0197                emotes.UnionWith(embeddedEmotesSo.emotes.Select(x => x.id));
 0198                wearableItems.AddRange(embeddedEmotesSo.emotes.Select(x => x.id));
 199
 0200                await avatar.Load(wearableItems, emotes.ToList(), new AvatarSettings
 201                {
 202                    bodyshapeId = profile.avatar.bodyShape,
 203                    eyesColor = profile.avatar.eyeColor,
 204                    skinColor = profile.avatar.skinColor,
 205                    hairColor = profile.avatar.hairColor,
 206                }, ct);
 207
 0208                if (avatar.lodLevel <= 1)
 0209                    AvatarSystemUtils.SpawnAvatarLoadedParticles(avatarContainer.transform, loadingParticlesPrefab);
 210
 0211                avatar.PlayEmote(profile.avatar.expressionTriggerId, profile.avatar.expressionTriggerTimestamp);
 212            }
 0213        }
 214        catch (OperationCanceledException ex)
 215        {
 0216            Debug.LogException(ex);
 0217            return;
 218        }
 219        catch (Exception e)
 220        {
 0221            Debug.LogException(e);
 222            //WebInterface.ReportAvatarFatalError(e.ToString());
 0223            return;
 224        }
 225        finally
 226        {
 0227            IAvatarAnchorPoints anchorPoints = new AvatarAnchorPoints();
 0228            anchorPoints.Prepare(avatarContainer.transform, avatar.GetBones(), AvatarSystemUtils.AVATAR_Y_OFFSET + avata
 229
 0230            var player = new Player()
 231            {
 232                id = userProfile.userId,
 233                name = userProfile.name,
 234                avatar = avatar,
 235                anchorPoints = anchorPoints,
 236                collider = avatarCollider
 237            };
 0238            DataStore.i.player.ownPlayer.Set(player);
 239
 0240            enableCameraCheck = true;
 0241            avatarCollider.gameObject.SetActive(true);
 0242            CommonScriptableObjects.rendererState.RemoveLock(this);
 0243            DataStore.i.common.isPlayerRendererLoaded.Set(true);
 244        }
 1245    }
 246
 247    public void ApplyHideAvatarModifier()
 248    {
 0249        if (!currentActiveModifiers.ContainsKey(AvatarModifierAreaID.HIDE_AVATAR))
 250        {
 0251            avatar.AddVisibilityConstraint(IN_HIDE_AREA);
 0252            stickersControllers.ToggleHideArea(true);
 253        }
 0254        currentActiveModifiers.AddRefCount(AvatarModifierAreaID.HIDE_AVATAR);
 0255        DataStore.i.HUDs.avatarAreaWarnings.AddRefCount(AvatarModifierAreaID.HIDE_AVATAR);
 0256    }
 257
 258    public void RemoveHideAvatarModifier()
 259    {
 0260        DataStore.i.HUDs.avatarAreaWarnings.RemoveRefCount(AvatarModifierAreaID.HIDE_AVATAR);
 0261        currentActiveModifiers.RemoveRefCount(AvatarModifierAreaID.HIDE_AVATAR);
 0262        if (!currentActiveModifiers.ContainsKey(AvatarModifierAreaID.HIDE_AVATAR))
 263        {
 0264            avatar.RemoveVisibilityConstrain(IN_HIDE_AREA);
 0265            stickersControllers.ToggleHideArea(false);
 266        }
 0267    }
 268
 269    public void ApplyHidePassportModifier()
 270    {
 0271        DataStore.i.HUDs.avatarAreaWarnings.AddRefCount(AvatarModifierAreaID.DISABLE_PASSPORT);
 0272        currentActiveModifiers.AddRefCount(AvatarModifierAreaID.DISABLE_PASSPORT);
 0273    }
 274
 275    public void RemoveHidePassportModifier()
 276    {
 0277        DataStore.i.HUDs.avatarAreaWarnings.RemoveRefCount(AvatarModifierAreaID.DISABLE_PASSPORT);
 0278        currentActiveModifiers.RemoveRefCount(AvatarModifierAreaID.DISABLE_PASSPORT);
 0279    }
 280
 281    private void OnDisable()
 282    {
 382283        userProfile.OnUpdate -= OnUserProfileOnUpdate;
 382284        userProfile.OnAvatarEmoteSet -= OnAvatarEmote;
 382285    }
 286
 287    private void OnDestroy()
 288    {
 405289        avatarLoadingCts?.Cancel();
 405290        avatarLoadingCts?.Dispose();
 405291        avatarLoadingCts = null;
 405292        avatar?.Dispose();
 381293    }
 294
 295}