| | 1 | | using AvatarSystem; |
| | 2 | | using Cysharp.Threading.Tasks; |
| | 3 | | using DCL; |
| | 4 | | using DCL.Components; |
| | 5 | | using DCL.FatalErrorReporter; |
| | 6 | | using DCL.Interface; |
| | 7 | | using DCL.NotificationModel; |
| | 8 | | using SocialFeaturesAnalytics; |
| | 9 | | using System; |
| | 10 | | using System.Collections.Generic; |
| | 11 | | using System.Linq; |
| | 12 | | using System.Threading; |
| | 13 | | using UnityEngine; |
| | 14 | | using Environment = DCL.Environment; |
| | 15 | | using Type = DCL.NotificationModel.Type; |
| | 16 | |
|
| | 17 | | public 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 | | private const string OPEN_PASSPORT_SOURCE = "World"; |
| | 23 | |
|
| | 24 | | private CancellationTokenSource avatarLoadingCts; |
| | 25 | | public GameObject avatarContainer; |
| | 26 | | public StickersController stickersControllers; |
| 341 | 27 | | private readonly AvatarModel currentAvatar = new AvatarModel { wearables = new List<string>() }; |
| | 28 | |
|
| | 29 | | public Collider avatarCollider; |
| | 30 | | [SerializeField] private GameObject loadingParticlesPrefab; |
| 341 | 31 | | public float cameraDistanceToDeactivate = 1.0f; |
| | 32 | | [SerializeField] internal AvatarOnPointerDown onPointerDown; |
| | 33 | | [SerializeField] internal AvatarOutlineOnHoverEvent outlineOnHover; |
| | 34 | | [SerializeField] internal Transform baseAvatarContainer; |
| | 35 | | [SerializeField] internal BaseAvatarReferences baseAvatarReferencesPrefab; |
| | 36 | |
|
| 1208 | 37 | | private UserProfile userProfile => UserProfile.GetOwnUserProfile(); |
| 0 | 38 | | private bool repositioningWorld => DCLCharacterController.i.characterPosition.RepositionedWorldLastFrame(); |
| | 39 | | private bool enableCameraCheck; |
| | 40 | | private Camera mainCamera; |
| | 41 | | private IFatalErrorReporter fatalErrorReporter; // TODO? |
| | 42 | | private string visibilityConstrain; |
| | 43 | | private BaseRefCounter<AvatarModifierAreaID> currentActiveModifiers; |
| | 44 | | private Service<IEmotesCatalogService> emotesCatalog; |
| | 45 | | private ISocialAnalytics socialAnalytics; |
| | 46 | | private BaseVariable<(string playerId, string source)> currentPlayerInfoCardId; |
| | 47 | | private IAvatar avatar; |
| | 48 | | private IBaseAvatarReferences baseAvatarReferences; |
| 341 | 49 | | private readonly OnPointerEvent.Model pointerEventModel = new () |
| | 50 | | { |
| | 51 | | type = OnPointerDown.NAME, |
| | 52 | | button = WebInterface.ACTION_BUTTON.POINTER.ToString(), |
| | 53 | | hoverText = "View My Profile", |
| | 54 | | }; |
| | 55 | |
|
| 0 | 56 | | public IAvatar Avatar => avatar; |
| | 57 | |
|
| | 58 | | private void Start() |
| | 59 | | { |
| 301 | 60 | | DataStore.i.common.isPlayerRendererLoaded.Set(false); |
| | 61 | |
|
| 301 | 62 | | socialAnalytics = new SocialAnalytics( |
| | 63 | | Environment.i.platform.serviceProviders.analytics, |
| | 64 | | new UserProfileWebInterfaceBridge()); |
| | 65 | |
|
| 301 | 66 | | if (DataStore.i.avatarConfig.useHologramAvatar.Get()) |
| 0 | 67 | | avatar = GetAvatarWithHologram(); |
| | 68 | | else |
| 301 | 69 | | avatar = GetStandardAvatar(); |
| | 70 | |
|
| 301 | 71 | | if (UserProfileController.i != null) |
| | 72 | | { |
| 0 | 73 | | UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail; |
| 0 | 74 | | UserProfileController.i.OnBaseWereablesFail += OnBaseWereablesFail; |
| | 75 | | } |
| | 76 | |
|
| 301 | 77 | | CommonScriptableObjects.rendererState.AddLock(this); |
| | 78 | |
|
| 301 | 79 | | mainCamera = Camera.main; |
| 301 | 80 | | currentActiveModifiers = new BaseRefCounter<AvatarModifierAreaID>(); |
| 301 | 81 | | currentPlayerInfoCardId = DataStore.i.HUDs.currentPlayerId; |
| | 82 | |
|
| 301 | 83 | | onPointerDown.OnPointerDownReport -= PlayerClicked; |
| 301 | 84 | | onPointerDown.OnPointerDownReport += PlayerClicked; |
| 301 | 85 | | } |
| | 86 | |
|
| | 87 | | private void PlayerClicked() |
| | 88 | | { |
| 0 | 89 | | if (currentAvatar == null) return; |
| 0 | 90 | | currentPlayerInfoCardId.Set((currentAvatar.id, OPEN_PASSPORT_SOURCE)); |
| 0 | 91 | | } |
| | 92 | |
|
| | 93 | | private IAvatar GetStandardAvatar() |
| | 94 | | { |
| 301 | 95 | | return Environment.i.serviceLocator.Get<IAvatarFactory>().CreateAvatar( |
| | 96 | | avatarContainer, |
| | 97 | | GetComponentInChildren<AvatarAnimatorLegacy>(), |
| | 98 | | NoLODs.i, |
| | 99 | | new Visibility()); |
| | 100 | | } |
| | 101 | |
|
| | 102 | | private IAvatar GetAvatarWithHologram() |
| | 103 | | { |
| 0 | 104 | | baseAvatarReferences = baseAvatarContainer.GetComponentInChildren<IBaseAvatarReferences>() ?? Instantiate(baseAv |
| | 105 | |
|
| 0 | 106 | | return Environment.i.serviceLocator.Get<IAvatarFactory>().CreateAvatarWithHologram( |
| | 107 | | avatarContainer, |
| | 108 | | new BaseAvatar(baseAvatarReferences), |
| | 109 | | GetComponentInChildren<AvatarAnimatorLegacy>(), |
| | 110 | | NoLODs.i, |
| | 111 | | new Visibility()); |
| | 112 | | } |
| | 113 | |
|
| | 114 | | private void OnBaseWereablesFail() |
| | 115 | | { |
| 0 | 116 | | UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail; |
| | 117 | |
|
| 0 | 118 | | if (enableCameraCheck) |
| 0 | 119 | | ShowWearablesWarning(); |
| 0 | 120 | | } |
| | 121 | |
|
| | 122 | | private void ShowWearablesWarning() |
| | 123 | | { |
| 0 | 124 | | NotificationsController.i.ShowNotification(new Model |
| | 125 | | { |
| | 126 | | message = LOADING_WEARABLES_ERROR_MESSAGE, |
| | 127 | | type = Type.GENERIC, |
| | 128 | | timer = 10f, |
| | 129 | | destroyOnFinish = true |
| | 130 | | }); |
| 0 | 131 | | } |
| | 132 | |
|
| | 133 | | private void Update() |
| | 134 | | { |
| 6051 | 135 | | if (!enableCameraCheck || repositioningWorld) |
| 6051 | 136 | | return; |
| | 137 | |
|
| 0 | 138 | | if (mainCamera == null) |
| | 139 | | { |
| 0 | 140 | | mainCamera = Camera.main; |
| | 141 | |
|
| 0 | 142 | | if (mainCamera == null) |
| 0 | 143 | | return; |
| | 144 | | } |
| | 145 | |
|
| 0 | 146 | | if (Vector3.Distance(mainCamera.transform.position, transform.position) > cameraDistanceToDeactivate) |
| 0 | 147 | | avatar.RemoveVisibilityConstrain(INSIDE_CAMERA); |
| | 148 | | else |
| 0 | 149 | | avatar.AddVisibilityConstraint(INSIDE_CAMERA); |
| 0 | 150 | | } |
| | 151 | |
|
| | 152 | | public void SetAvatarVisibility(bool isVisible) |
| | 153 | | { |
| 0 | 154 | | visibilityConstrain = "own_player_invisible"; |
| | 155 | |
|
| 0 | 156 | | if (isVisible) |
| 0 | 157 | | avatar.RemoveVisibilityConstrain(visibilityConstrain); |
| | 158 | | else |
| 0 | 159 | | avatar.AddVisibilityConstraint(visibilityConstrain); |
| 0 | 160 | | } |
| | 161 | |
|
| | 162 | | private void OnEnable() |
| | 163 | | { |
| 302 | 164 | | userProfile.OnUpdate += OnUserProfileOnUpdate; |
| 302 | 165 | | userProfile.OnAvatarEmoteSet += OnAvatarEmote; |
| 302 | 166 | | } |
| | 167 | |
|
| | 168 | | private void OnAvatarEmote(string id, long timestamp, UserProfile.EmoteSource source) |
| | 169 | | { |
| 1 | 170 | | avatar.GetEmotesController().PlayEmote(id, timestamp); |
| | 171 | |
|
| 1 | 172 | | bool found = DataStore.i.common.wearables.TryGetValue(id, out WearableItem emoteItem); |
| | 173 | |
|
| 1 | 174 | | if (!found) |
| | 175 | | { |
| 1 | 176 | | var emotesCatalog = Environment.i.serviceLocator.Get<IEmotesCatalogService>(); |
| 1 | 177 | | emotesCatalog.TryGetLoadedEmote(id, out emoteItem); |
| | 178 | | } |
| | 179 | |
|
| 1 | 180 | | if (emoteItem != null) |
| | 181 | | { |
| 0 | 182 | | socialAnalytics.SendPlayEmote( |
| | 183 | | emoteItem.id, |
| | 184 | | emoteItem.GetName(), |
| | 185 | | emoteItem.rarity, |
| | 186 | | emoteItem.data.tags.Contains(WearableLiterals.Tags.BASE_WEARABLE), |
| | 187 | | source, |
| | 188 | | $"{CommonScriptableObjects.playerCoords.Get().x},{CommonScriptableObjects.playerCoords.Get().y}"); |
| | 189 | | } |
| 1 | 190 | | } |
| | 191 | |
|
| | 192 | | private void OnUserProfileOnUpdate(UserProfile profile) |
| | 193 | | { |
| 1 | 194 | | avatarLoadingCts?.Cancel(); |
| 1 | 195 | | avatarLoadingCts?.Dispose(); |
| 1 | 196 | | avatarLoadingCts = new CancellationTokenSource(); |
| 1 | 197 | | LoadingAvatarRoutine(profile, avatarLoadingCts.Token); |
| 1 | 198 | | } |
| | 199 | |
|
| | 200 | | private async UniTaskVoid LoadingAvatarRoutine(UserProfile profile, CancellationToken ct) |
| | 201 | | { |
| 1 | 202 | | if (string.IsNullOrEmpty(profile.avatar.bodyShape) || profile.avatar.wearables == null) |
| | 203 | | { |
| 1 | 204 | | avatar.Dispose(); |
| 1 | 205 | | return; |
| | 206 | | } |
| | 207 | |
|
| | 208 | | try |
| | 209 | | { |
| 0 | 210 | | ct.ThrowIfCancellationRequested(); |
| | 211 | |
|
| 0 | 212 | | if (avatar.status != IAvatar.Status.Loaded || !profile.avatar.HaveSameWearablesAndColors(currentAvatar)) |
| | 213 | | { |
| 0 | 214 | | currentAvatar.CopyFrom(profile.avatar); |
| | 215 | | // profile.avatar.id is a null string, so override it with a valid profile id for further usage |
| 0 | 216 | | currentAvatar.id = profile.userId; |
| | 217 | |
|
| 0 | 218 | | List<string> wearableItems = profile.avatar.wearables.ToList(); |
| 0 | 219 | | wearableItems.Add(profile.avatar.bodyShape); |
| | 220 | |
|
| 0 | 221 | | HashSet<string> emotes = new HashSet<string>(currentAvatar.emotes.Select(x => x.urn)); |
| 0 | 222 | | var embeddedEmotesSo = await emotesCatalog.Ref.GetEmbeddedEmotes(); |
| 0 | 223 | | string[] emoteIds = embeddedEmotesSo.GetAllIds(); |
| 0 | 224 | | emotes.UnionWith(emoteIds); |
| 0 | 225 | | wearableItems.AddRange(emoteIds); |
| | 226 | |
|
| 0 | 227 | | await avatar.Load(wearableItems, emotes.ToList(), new AvatarSettings |
| | 228 | | { |
| | 229 | | bodyshapeId = profile.avatar.bodyShape, |
| | 230 | | eyesColor = profile.avatar.eyeColor, |
| | 231 | | skinColor = profile.avatar.skinColor, |
| | 232 | | hairColor = profile.avatar.hairColor, |
| | 233 | | forceRender = new HashSet<string>(profile.avatar.forceRender) |
| | 234 | | }, ct); |
| | 235 | |
|
| 0 | 236 | | if (avatar.lodLevel <= 1) |
| 0 | 237 | | AvatarSystemUtils.SpawnAvatarLoadedParticles(avatarContainer.transform, loadingParticlesPrefab); |
| | 238 | |
|
| 0 | 239 | | avatar.GetEmotesController().PlayEmote(profile.avatar.expressionTriggerId, profile.avatar.expressionTrig |
| 0 | 240 | | } |
| 0 | 241 | | } |
| 0 | 242 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 243 | | { |
| 0 | 244 | | Debug.LogException(e); |
| | 245 | |
|
| | 246 | | //WebInterface.ReportAvatarFatalError(e.ToString()); |
| 0 | 247 | | return; |
| | 248 | | } |
| | 249 | | finally |
| | 250 | | { |
| 0 | 251 | | IAvatarAnchorPoints anchorPoints = new AvatarAnchorPoints(); |
| 0 | 252 | | anchorPoints.Prepare(avatarContainer.transform, baseAvatarReferences.Anchors, AvatarSystemUtils.AVATAR_Y_OFF |
| | 253 | |
|
| 0 | 254 | | var player = new Player |
| | 255 | | { |
| | 256 | | id = userProfile.userId, |
| | 257 | | name = userProfile.name, |
| | 258 | | avatar = avatar, |
| | 259 | | anchorPoints = anchorPoints, |
| | 260 | | collider = avatarCollider |
| | 261 | | }; |
| | 262 | |
|
| 0 | 263 | | DataStore.i.player.ownPlayer.Set(player); |
| | 264 | |
|
| 0 | 265 | | enableCameraCheck = true; |
| 0 | 266 | | avatarCollider.gameObject.SetActive(true); |
| 0 | 267 | | CommonScriptableObjects.rendererState.RemoveLock(this); |
| 0 | 268 | | DataStore.i.common.isPlayerRendererLoaded.Set(true); |
| | 269 | |
|
| 0 | 270 | | onPointerDown.Initialize( |
| | 271 | | pointerEventModel, |
| | 272 | | null, |
| | 273 | | player |
| | 274 | | ); |
| | 275 | |
|
| 0 | 276 | | onPointerDown.ShouldBeInteractableWhenMouseIsLocked = false; |
| | 277 | |
|
| 0 | 278 | | outlineOnHover.Initialize(null, avatar); |
| 0 | 279 | | outlineOnHover.ShouldBeHoveredWhenMouseIsLocked = false; |
| | 280 | |
|
| 0 | 281 | | bool isClickingOwnAvatarEnabled = DataStore.i.featureFlags.flags.Get().IsFeatureEnabled("click_own_avatar_pa |
| 0 | 282 | | onPointerDown.enabled = isClickingOwnAvatarEnabled; |
| 0 | 283 | | outlineOnHover.enabled = isClickingOwnAvatarEnabled; |
| | 284 | | } |
| 1 | 285 | | } |
| | 286 | |
|
| | 287 | | public void ApplyHideAvatarModifier() |
| | 288 | | { |
| 0 | 289 | | if (!currentActiveModifiers.ContainsKey(AvatarModifierAreaID.HIDE_AVATAR)) |
| | 290 | | { |
| 0 | 291 | | avatar.AddVisibilityConstraint(IN_HIDE_AREA); |
| 0 | 292 | | stickersControllers.ToggleHideArea(true); |
| | 293 | | } |
| | 294 | |
|
| 0 | 295 | | currentActiveModifiers.AddRefCount(AvatarModifierAreaID.HIDE_AVATAR); |
| 0 | 296 | | DataStore.i.HUDs.avatarAreaWarnings.AddRefCount(AvatarModifierAreaID.HIDE_AVATAR); |
| 0 | 297 | | } |
| | 298 | |
|
| | 299 | | public void RemoveHideAvatarModifier() |
| | 300 | | { |
| 0 | 301 | | DataStore.i.HUDs.avatarAreaWarnings.RemoveRefCount(AvatarModifierAreaID.HIDE_AVATAR); |
| 0 | 302 | | currentActiveModifiers.RemoveRefCount(AvatarModifierAreaID.HIDE_AVATAR); |
| | 303 | |
|
| 0 | 304 | | if (!currentActiveModifiers.ContainsKey(AvatarModifierAreaID.HIDE_AVATAR)) |
| | 305 | | { |
| 0 | 306 | | avatar.RemoveVisibilityConstrain(IN_HIDE_AREA); |
| 0 | 307 | | stickersControllers.ToggleHideArea(false); |
| | 308 | | } |
| 0 | 309 | | } |
| | 310 | |
|
| | 311 | | public void ApplyHidePassportModifier() |
| | 312 | | { |
| 0 | 313 | | DataStore.i.HUDs.avatarAreaWarnings.AddRefCount(AvatarModifierAreaID.DISABLE_PASSPORT); |
| 0 | 314 | | currentActiveModifiers.AddRefCount(AvatarModifierAreaID.DISABLE_PASSPORT); |
| 0 | 315 | | } |
| | 316 | |
|
| | 317 | | public void RemoveHidePassportModifier() |
| | 318 | | { |
| 0 | 319 | | DataStore.i.HUDs.avatarAreaWarnings.RemoveRefCount(AvatarModifierAreaID.DISABLE_PASSPORT); |
| 0 | 320 | | currentActiveModifiers.RemoveRefCount(AvatarModifierAreaID.DISABLE_PASSPORT); |
| 0 | 321 | | } |
| | 322 | |
|
| | 323 | | private void OnDisable() |
| | 324 | | { |
| 302 | 325 | | userProfile.OnUpdate -= OnUserProfileOnUpdate; |
| 302 | 326 | | userProfile.OnAvatarEmoteSet -= OnAvatarEmote; |
| 302 | 327 | | } |
| | 328 | |
|
| | 329 | | private void OnDestroy() |
| | 330 | | { |
| 324 | 331 | | avatarLoadingCts?.Cancel(); |
| 324 | 332 | | avatarLoadingCts?.Dispose(); |
| 324 | 333 | | avatarLoadingCts = null; |
| 324 | 334 | | avatar?.Dispose(); |
| 301 | 335 | | } |
| | 336 | | } |