< Summary

Class:PlayerAvatarController
Assembly:PlayerAvatarController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/PlayerAvatarController/PlayerAvatarController.cs
Covered lines:47
Uncovered lines:65
Coverable lines:112
Total lines:299
Line coverage:41.9% (47 of 112)
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%220100%
OnEnable()0%110100%
OnAvatarEmote(...)0%2.032080%
OnUserProfileOnUpdate(...)0%330100%
LoadingAvatarRoutine()0%75.110013.33%
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 Type = DCL.NotificationModel.Type;
 16
 17public class PlayerAvatarController : MonoBehaviour, IHideAvatarAreaHandler, IHidePassportAreaHandler
 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;
 28    public StickersController stickersControllers;
 60529    private readonly AvatarModel currentAvatar = new AvatarModel { wearables = new List<string>() };
 30
 31    public Collider avatarCollider;
 32    [SerializeField] private GameObject loadingParticlesPrefab;
 60533    public float cameraDistanceToDeactivate = 1.0f;
 34
 227235    private UserProfile userProfile => UserProfile.GetOwnUserProfile();
 036    private bool repositioningWorld => DCLCharacterController.i.characterPosition.RepositionedWorldLastFrame();
 37
 38    private bool enableCameraCheck = false;
 39    private Camera mainCamera;
 40    private IFatalErrorReporter fatalErrorReporter; // TODO?
 41    private string VISIBILITY_CONSTRAIN;
 42    private BaseRefCounter<AvatarModifierAreaID> currentActiveModifiers;
 43
 44    internal ISocialAnalytics socialAnalytics;
 45
 46    private void Start()
 47    {
 56748        DataStore.i.common.isPlayerRendererLoaded.Set(false);
 56749        socialAnalytics = new SocialAnalytics(
 50            DCL.Environment.i.platform.serviceProviders.analytics,
 51            new UserProfileWebInterfaceBridge());
 52
 56753        if (DataStore.i.avatarConfig.useHologramAvatar.Get())
 054            avatar = GetAvatarWithHologram();
 55        else
 56756            avatar = GetStandardAvatar();
 57
 56758        if ( UserProfileController.i != null )
 59        {
 060            UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail;
 061            UserProfileController.i.OnBaseWereablesFail += OnBaseWereablesFail;
 62        }
 63
 56764        CommonScriptableObjects.rendererState.AddLock(this);
 65
 66#if UNITY_WEBGL
 56767        fatalErrorReporter = new WebFatalErrorReporter();
 68#else
 69        fatalErrorReporter = new DefaultFatalErrorReporter(DataStore.i);
 70#endif
 71
 56772        mainCamera = Camera.main;
 56773        currentActiveModifiers = new BaseRefCounter<AvatarModifierAreaID>();
 56774    }
 75
 76    private AvatarSystem.Avatar GetStandardAvatar()
 77    {
 56778        AvatarAnimatorLegacy animator = GetComponentInChildren<AvatarAnimatorLegacy>();
 56779        AvatarSystem.NoLODs noLod = new NoLODs();
 56780        return new AvatarSystem.Avatar(
 81            new AvatarCurator(new WearableItemResolver()),
 82            new Loader(new WearableLoaderFactory(), avatarContainer, new AvatarMeshCombinerHelper()),
 83            animator,
 84            new Visibility(),
 85            noLod,
 86            new SimpleGPUSkinning(),
 87            new GPUSkinningThrottler(),
 88            new EmoteAnimationEquipper(animator, DataStore.i.emotes));
 89    }
 90
 91    private AvatarWithHologram GetAvatarWithHologram()
 92    {
 093        AvatarAnimatorLegacy animator = GetComponentInChildren<AvatarAnimatorLegacy>();
 094        AvatarSystem.NoLODs noLod = new NoLODs();
 095        BaseAvatar baseAvatar = new BaseAvatar(loadingAvatarContainer, armatureContainer, noLod);
 096        return new AvatarSystem.AvatarWithHologram(
 97            baseAvatar,
 98            new AvatarCurator(new WearableItemResolver()),
 99            new Loader(new WearableLoaderFactory(), avatarContainer, new AvatarMeshCombinerHelper()),
 100            animator,
 101            new Visibility(),
 102            noLod,
 103            new SimpleGPUSkinning(),
 104            new GPUSkinningThrottler(),
 105            new EmoteAnimationEquipper(animator, DataStore.i.emotes));
 106    }
 107
 108    private void OnBaseWereablesFail()
 109    {
 0110        UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail;
 111
 0112        if (enableCameraCheck)
 0113            ShowWearablesWarning();
 0114    }
 115
 116    private void ShowWearablesWarning()
 117    {
 0118        NotificationsController.i.ShowNotification(new Model
 119        {
 120            message = LOADING_WEARABLES_ERROR_MESSAGE,
 121            type = Type.GENERIC,
 122            timer = 10f,
 123            destroyOnFinish = true
 124        });
 0125    }
 126
 127    private void Update()
 128    {
 5284129        if (!enableCameraCheck || repositioningWorld)
 5284130            return;
 131
 0132        if (mainCamera == null)
 133        {
 0134            mainCamera = Camera.main;
 135
 0136            if (mainCamera == null)
 0137                return;
 138        }
 139
 0140        if (Vector3.Distance(mainCamera.transform.position, transform.position) > cameraDistanceToDeactivate)
 0141            avatar.RemoveVisibilityConstrain(INSIDE_CAMERA);
 142        else
 0143            avatar.AddVisibilityConstrain(INSIDE_CAMERA);
 0144    }
 145
 146    public void SetAvatarVisibility(bool isVisible)
 147    {
 6148        VISIBILITY_CONSTRAIN = "own_player_invisible";
 6149        if (isVisible)
 1150            avatar.RemoveVisibilityConstrain(VISIBILITY_CONSTRAIN);
 151        else
 5152            avatar.AddVisibilityConstrain(VISIBILITY_CONSTRAIN);
 5153    }
 154
 155    private void OnEnable()
 156    {
 568157        userProfile.OnUpdate += OnUserProfileOnUpdate;
 568158        userProfile.OnAvatarEmoteSet += OnAvatarEmote;
 568159    }
 160
 161    private void OnAvatarEmote(string id, long timestamp, UserProfile.EmoteSource source)
 162    {
 1163        avatar.PlayEmote(id, timestamp);
 164
 1165        DataStore.i.common.wearables.TryGetValue(id, out WearableItem emoteItem);
 166
 1167        if (emoteItem != null)
 168        {
 0169            socialAnalytics.SendPlayEmote(
 170                emoteItem.id,
 171                emoteItem.GetName(),
 172                emoteItem.rarity,
 173                emoteItem.data.tags.Contains(WearableLiterals.Tags.BASE_WEARABLE),
 174                source,
 175                $"{CommonScriptableObjects.playerCoords.Get().x},{CommonScriptableObjects.playerCoords.Get().y}");
 176        }
 1177    }
 178
 179    private void OnUserProfileOnUpdate(UserProfile profile)
 180    {
 7181        avatarLoadingCts?.Cancel();
 7182        avatarLoadingCts?.Dispose();
 7183        avatarLoadingCts = new CancellationTokenSource();
 7184        LoadingAvatarRoutine(profile, avatarLoadingCts.Token);
 7185    }
 186
 187    private async UniTaskVoid LoadingAvatarRoutine(UserProfile profile, CancellationToken ct)
 188    {
 7189        if (string.IsNullOrEmpty(profile.avatar.bodyShape) || profile.avatar.wearables == null)
 190        {
 7191            avatar.Dispose();
 7192            return;
 193        }
 194
 195        try
 196        {
 0197            ct.ThrowIfCancellationRequested();
 0198            if (avatar.status != IAvatar.Status.Loaded || !profile.avatar.HaveSameWearablesAndColors(currentAvatar))
 199            {
 0200                currentAvatar.CopyFrom(profile.avatar);
 201
 0202                List<string> wearableItems = profile.avatar.wearables.ToList();
 0203                wearableItems.Add(profile.avatar.bodyShape);
 204
 205                //temporarily hardcoding the embedded emotes until the user profile provides the equipped ones
 0206                var embeddedEmotesSo = Resources.Load<EmbeddedEmotesSO>("EmbeddedEmotes");
 0207                wearableItems.AddRange(embeddedEmotesSo.emotes.Select(x => x.id));
 208
 0209                await avatar.Load(wearableItems, new AvatarSettings
 210                {
 211                    bodyshapeId = profile.avatar.bodyShape,
 212                    eyesColor = profile.avatar.eyeColor,
 213                    skinColor = profile.avatar.skinColor,
 214                    hairColor = profile.avatar.hairColor,
 215                }, ct);
 216
 0217                if (avatar.lodLevel <= 1)
 0218                    AvatarSystemUtils.SpawnAvatarLoadedParticles(avatarContainer.transform, loadingParticlesPrefab);
 219            }
 0220        }
 0221        catch (OperationCanceledException)
 222        {
 0223            return;
 224        }
 225        catch (Exception e)
 226        {
 0227            Debug.LogException(e);
 0228            WebInterface.ReportAvatarFatalError();
 0229            return;
 230        }
 231
 0232        IAvatarAnchorPoints anchorPoints = new AvatarAnchorPoints();
 0233        anchorPoints.Prepare(avatarContainer.transform, avatar.GetBones(), AvatarSystemUtils.AVATAR_Y_OFFSET + avatar.ex
 234
 0235        var player = new Player()
 236        {
 237            id = userProfile.userId,
 238            name = userProfile.name,
 239            avatar = avatar,
 240            anchorPoints = anchorPoints,
 241            collider = avatarCollider
 242        };
 0243        DataStore.i.player.ownPlayer.Set(player);
 244
 0245        enableCameraCheck = true;
 0246        avatarCollider.gameObject.SetActive(true);
 0247        CommonScriptableObjects.rendererState.RemoveLock(this);
 0248        DataStore.i.common.isPlayerRendererLoaded.Set(true);
 7249    }
 250
 251    public void ApplyHideAvatarModifier()
 252    {
 0253        if (!currentActiveModifiers.ContainsKey(AvatarModifierAreaID.HIDE_AVATAR))
 254        {
 0255            avatar.AddVisibilityConstrain(IN_HIDE_AREA);
 0256            stickersControllers.ToggleHideArea(true);
 257        }
 0258        currentActiveModifiers.AddRefCount(AvatarModifierAreaID.HIDE_AVATAR);
 0259        DataStore.i.HUDs.avatarAreaWarnings.AddRefCount(AvatarModifierAreaID.HIDE_AVATAR);
 0260    }
 261
 262    public void RemoveHideAvatarModifier()
 263    {
 0264        DataStore.i.HUDs.avatarAreaWarnings.RemoveRefCount(AvatarModifierAreaID.HIDE_AVATAR);
 0265        currentActiveModifiers.RemoveRefCount(AvatarModifierAreaID.HIDE_AVATAR);
 0266        if (!currentActiveModifiers.ContainsKey(AvatarModifierAreaID.HIDE_AVATAR))
 267        {
 0268            avatar.RemoveVisibilityConstrain(IN_HIDE_AREA);
 0269            stickersControllers.ToggleHideArea(false);
 270        }
 0271    }
 272
 273    public void ApplyHidePassportModifier()
 274    {
 0275        DataStore.i.HUDs.avatarAreaWarnings.AddRefCount(AvatarModifierAreaID.DISABLE_PASSPORT);
 0276        currentActiveModifiers.AddRefCount(AvatarModifierAreaID.DISABLE_PASSPORT);
 0277    }
 278
 279    public void RemoveHidePassportModifier()
 280    {
 0281        DataStore.i.HUDs.avatarAreaWarnings.RemoveRefCount(AvatarModifierAreaID.DISABLE_PASSPORT);
 0282        currentActiveModifiers.RemoveRefCount(AvatarModifierAreaID.DISABLE_PASSPORT);
 0283    }
 284
 285    private void OnDisable()
 286    {
 568287        userProfile.OnUpdate -= OnUserProfileOnUpdate;
 568288        userProfile.OnAvatarEmoteSet -= OnAvatarEmote;
 568289    }
 290
 291    private void OnDestroy()
 292    {
 591293        avatarLoadingCts?.Cancel();
 591294        avatarLoadingCts?.Dispose();
 591295        avatarLoadingCts = null;
 591296        avatar?.Dispose();
 567297    }
 298
 299}