< 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:67
Coverable lines:114
Total lines:301
Line coverage:41.2% (47 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.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%108.4712012.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;
 60630    private readonly AvatarModel currentAvatar = new AvatarModel { wearables = new List<string>() };
 31
 32    public Collider avatarCollider;
 33    [SerializeField] private GameObject loadingParticlesPrefab;
 60634    public float cameraDistanceToDeactivate = 1.0f;
 35
 227636    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    {
 56849        DataStore.i.common.isPlayerRendererLoaded.Set(false);
 56850        socialAnalytics = new SocialAnalytics(
 51            DCL.Environment.i.platform.serviceProviders.analytics,
 52            new UserProfileWebInterfaceBridge());
 53
 56854        if (DataStore.i.avatarConfig.useHologramAvatar.Get())
 055            avatar = GetAvatarWithHologram();
 56        else
 56857            avatar = GetStandardAvatar();
 58
 56859        if ( UserProfileController.i != null )
 60        {
 061            UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail;
 062            UserProfileController.i.OnBaseWereablesFail += OnBaseWereablesFail;
 63        }
 64
 56865        CommonScriptableObjects.rendererState.AddLock(this);
 66
 67#if UNITY_WEBGL
 68        fatalErrorReporter = new WebFatalErrorReporter();
 69#else
 56870        fatalErrorReporter = new DefaultFatalErrorReporter(DataStore.i);
 71#endif
 72
 56873        mainCamera = Camera.main;
 56874        currentActiveModifiers = new BaseRefCounter<AvatarModifierAreaID>();
 56875    }
 76
 77    private AvatarSystem.Avatar GetStandardAvatar()
 78    {
 56879        AvatarAnimatorLegacy animator = GetComponentInChildren<AvatarAnimatorLegacy>();
 56880        AvatarSystem.NoLODs noLod = new NoLODs();
 56881        return new AvatarSystem.Avatar(
 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 AvatarWithHologram GetAvatarWithHologram()
 93    {
 094        AvatarAnimatorLegacy animator = GetComponentInChildren<AvatarAnimatorLegacy>();
 095        AvatarSystem.NoLODs noLod = new NoLODs();
 096        BaseAvatar baseAvatar = new BaseAvatar(loadingAvatarContainer, armatureContainer, noLod);
 097        return new AvatarSystem.AvatarWithHologram(
 98            baseAvatar,
 99            new AvatarCurator(new WearableItemResolver(), Environment.i.serviceLocator.Get<IEmotesCatalogService>()),
 100            new Loader(new WearableLoaderFactory(), avatarContainer, new AvatarMeshCombinerHelper()),
 101            animator,
 102            new Visibility(),
 103            noLod,
 104            new SimpleGPUSkinning(),
 105            new GPUSkinningThrottler(),
 106            new EmoteAnimationEquipper(animator, DataStore.i.emotes));
 107    }
 108
 109    private void OnBaseWereablesFail()
 110    {
 0111        UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail;
 112
 0113        if (enableCameraCheck)
 0114            ShowWearablesWarning();
 0115    }
 116
 117    private void ShowWearablesWarning()
 118    {
 0119        NotificationsController.i.ShowNotification(new Model
 120        {
 121            message = LOADING_WEARABLES_ERROR_MESSAGE,
 122            type = Type.GENERIC,
 123            timer = 10f,
 124            destroyOnFinish = true
 125        });
 0126    }
 127
 128    private void Update()
 129    {
 5289130        if (!enableCameraCheck || repositioningWorld)
 5289131            return;
 132
 0133        if (mainCamera == null)
 134        {
 0135            mainCamera = Camera.main;
 136
 0137            if (mainCamera == null)
 0138                return;
 139        }
 140
 0141        if (Vector3.Distance(mainCamera.transform.position, transform.position) > cameraDistanceToDeactivate)
 0142            avatar.RemoveVisibilityConstrain(INSIDE_CAMERA);
 143        else
 0144            avatar.AddVisibilityConstrain(INSIDE_CAMERA);
 0145    }
 146
 147    public void SetAvatarVisibility(bool isVisible)
 148    {
 6149        VISIBILITY_CONSTRAIN = "own_player_invisible";
 6150        if (isVisible)
 1151            avatar.RemoveVisibilityConstrain(VISIBILITY_CONSTRAIN);
 152        else
 5153            avatar.AddVisibilityConstrain(VISIBILITY_CONSTRAIN);
 5154    }
 155
 156    private void OnEnable()
 157    {
 569158        userProfile.OnUpdate += OnUserProfileOnUpdate;
 569159        userProfile.OnAvatarEmoteSet += OnAvatarEmote;
 569160    }
 161
 162    private void OnAvatarEmote(string id, long timestamp, UserProfile.EmoteSource source)
 163    {
 1164        avatar.PlayEmote(id, timestamp);
 165
 1166        DataStore.i.common.wearables.TryGetValue(id, out WearableItem emoteItem);
 167
 1168        if (emoteItem != null)
 169        {
 0170            socialAnalytics.SendPlayEmote(
 171                emoteItem.id,
 172                emoteItem.GetName(),
 173                emoteItem.rarity,
 174                emoteItem.data.tags.Contains(WearableLiterals.Tags.BASE_WEARABLE),
 175                source,
 176                $"{CommonScriptableObjects.playerCoords.Get().x},{CommonScriptableObjects.playerCoords.Get().y}");
 177        }
 1178    }
 179
 180    private void OnUserProfileOnUpdate(UserProfile profile)
 181    {
 7182        avatarLoadingCts?.Cancel();
 7183        avatarLoadingCts?.Dispose();
 7184        avatarLoadingCts = new CancellationTokenSource();
 7185        LoadingAvatarRoutine(profile, avatarLoadingCts.Token);
 7186    }
 187
 188    private async UniTaskVoid LoadingAvatarRoutine(UserProfile profile, CancellationToken ct)
 189    {
 7190        if (string.IsNullOrEmpty(profile.avatar.bodyShape) || profile.avatar.wearables == null)
 191        {
 7192            avatar.Dispose();
 7193            return;
 194        }
 195
 196        try
 197        {
 0198            ct.ThrowIfCancellationRequested();
 0199            if (avatar.status != IAvatar.Status.Loaded || !profile.avatar.HaveSameWearablesAndColors(currentAvatar))
 200            {
 0201                currentAvatar.CopyFrom(profile.avatar);
 202
 0203                List<string> wearableItems = profile.avatar.wearables.ToList();
 0204                wearableItems.Add(profile.avatar.bodyShape);
 205
 0206                HashSet<string> emotes = new HashSet<string>(currentAvatar.emotes.Select(x => x.urn));
 0207                var embeddedEmotesSo = Resources.Load<EmbeddedEmotesSO>("EmbeddedEmotes");
 0208                emotes.UnionWith(embeddedEmotesSo.emotes.Select(x => x.id));
 0209                wearableItems.AddRange(embeddedEmotesSo.emotes.Select(x => x.id));
 210
 0211                await avatar.Load(wearableItems, emotes.ToList(), new AvatarSettings
 212                {
 213                    bodyshapeId = profile.avatar.bodyShape,
 214                    eyesColor = profile.avatar.eyeColor,
 215                    skinColor = profile.avatar.skinColor,
 216                    hairColor = profile.avatar.hairColor,
 217                }, ct);
 218
 0219                if (avatar.lodLevel <= 1)
 0220                    AvatarSystemUtils.SpawnAvatarLoadedParticles(avatarContainer.transform, loadingParticlesPrefab);
 221            }
 0222        }
 0223        catch (OperationCanceledException)
 224        {
 0225            return;
 226        }
 227        catch (Exception e)
 228        {
 0229            Debug.LogException(e);
 0230            WebInterface.ReportAvatarFatalError();
 0231            return;
 232        }
 233
 0234        IAvatarAnchorPoints anchorPoints = new AvatarAnchorPoints();
 0235        anchorPoints.Prepare(avatarContainer.transform, avatar.GetBones(), AvatarSystemUtils.AVATAR_Y_OFFSET + avatar.ex
 236
 0237        var player = new Player()
 238        {
 239            id = userProfile.userId,
 240            name = userProfile.name,
 241            avatar = avatar,
 242            anchorPoints = anchorPoints,
 243            collider = avatarCollider
 244        };
 0245        DataStore.i.player.ownPlayer.Set(player);
 246
 0247        enableCameraCheck = true;
 0248        avatarCollider.gameObject.SetActive(true);
 0249        CommonScriptableObjects.rendererState.RemoveLock(this);
 0250        DataStore.i.common.isPlayerRendererLoaded.Set(true);
 7251    }
 252
 253    public void ApplyHideAvatarModifier()
 254    {
 0255        if (!currentActiveModifiers.ContainsKey(AvatarModifierAreaID.HIDE_AVATAR))
 256        {
 0257            avatar.AddVisibilityConstrain(IN_HIDE_AREA);
 0258            stickersControllers.ToggleHideArea(true);
 259        }
 0260        currentActiveModifiers.AddRefCount(AvatarModifierAreaID.HIDE_AVATAR);
 0261        DataStore.i.HUDs.avatarAreaWarnings.AddRefCount(AvatarModifierAreaID.HIDE_AVATAR);
 0262    }
 263
 264    public void RemoveHideAvatarModifier()
 265    {
 0266        DataStore.i.HUDs.avatarAreaWarnings.RemoveRefCount(AvatarModifierAreaID.HIDE_AVATAR);
 0267        currentActiveModifiers.RemoveRefCount(AvatarModifierAreaID.HIDE_AVATAR);
 0268        if (!currentActiveModifiers.ContainsKey(AvatarModifierAreaID.HIDE_AVATAR))
 269        {
 0270            avatar.RemoveVisibilityConstrain(IN_HIDE_AREA);
 0271            stickersControllers.ToggleHideArea(false);
 272        }
 0273    }
 274
 275    public void ApplyHidePassportModifier()
 276    {
 0277        DataStore.i.HUDs.avatarAreaWarnings.AddRefCount(AvatarModifierAreaID.DISABLE_PASSPORT);
 0278        currentActiveModifiers.AddRefCount(AvatarModifierAreaID.DISABLE_PASSPORT);
 0279    }
 280
 281    public void RemoveHidePassportModifier()
 282    {
 0283        DataStore.i.HUDs.avatarAreaWarnings.RemoveRefCount(AvatarModifierAreaID.DISABLE_PASSPORT);
 0284        currentActiveModifiers.RemoveRefCount(AvatarModifierAreaID.DISABLE_PASSPORT);
 0285    }
 286
 287    private void OnDisable()
 288    {
 569289        userProfile.OnUpdate -= OnUserProfileOnUpdate;
 569290        userProfile.OnAvatarEmoteSet -= OnAvatarEmote;
 569291    }
 292
 293    private void OnDestroy()
 294    {
 592295        avatarLoadingCts?.Cancel();
 592296        avatarLoadingCts?.Dispose();
 592297        avatarLoadingCts = null;
 592298        avatar?.Dispose();
 568299    }
 300
 301}