| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Emotes; |
| | 4 | | using DCL.EmotesCustomization; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using DCL.Interface; |
| | 7 | | using DCL.NotificationModel; |
| | 8 | | using DCL.Tasks; |
| | 9 | | using DCLServices.WearablesCatalogService; |
| | 10 | | using MainScripts.DCL.Controllers.HUD.CharacterPreview; |
| | 11 | | using System; |
| | 12 | | using System.Collections.Generic; |
| | 13 | | using System.Linq; |
| | 14 | | using System.Threading; |
| | 15 | | using UnityEngine; |
| | 16 | | using UnityEngine.Rendering; |
| | 17 | | using UnityEngine.Rendering.Universal; |
| | 18 | | using Environment = DCL.Environment; |
| | 19 | | using Random = UnityEngine.Random; |
| | 20 | | using Type = DCL.NotificationModel.Type; |
| | 21 | | using Categories = WearableLiterals.Categories; |
| | 22 | |
|
| | 23 | | public class AvatarEditorHUDController : IHUD |
| | 24 | | { |
| | 25 | | private const string LOADING_OWNED_WEARABLES_ERROR_MESSAGE = "There was a problem loading your wearables"; |
| | 26 | | private const string URL_MARKET_PLACE = "https://market.decentraland.org/browse?section=wearables"; |
| | 27 | | private const string URL_GET_A_WALLET = "https://docs.decentraland.org/get-a-wallet"; |
| | 28 | | private const string URL_SELL_COLLECTIBLE_GENERIC = "https://market.decentraland.org/account"; |
| | 29 | | private const string URL_SELL_SPECIFIC_COLLECTIBLE = "https://market.decentraland.org/contracts/{collectionId}/token |
| | 30 | | private const string THIRD_PARTY_COLLECTIONS_FEATURE_FLAG = "third_party_collections"; |
| | 31 | | private const int REQUESTS_COOLDOWN_TIME = 60; |
| | 32 | | internal const string EQUIP_WEARABLE_METRIC = "equip_wearable"; |
| 0 | 33 | | protected static readonly string[] categoriesThatMustHaveSelection = { Categories.BODY_SHAPE, Categories.UPPER_BODY, |
| 0 | 34 | | protected static readonly string[] categoriesToRandomize = { Categories.HAIR, Categories.EYES, Categories.EYEBROWS, |
| | 35 | |
|
| | 36 | | [NonSerialized] |
| | 37 | | public bool bypassUpdateAvatarPreview = false; |
| | 38 | |
|
| 0 | 39 | | private UserProfile userProfile => userProfileBridge.GetOwn(); |
| | 40 | | private readonly IAnalytics analytics; |
| | 41 | | private readonly INewUserExperienceAnalytics newUserExperienceAnalytics; |
| | 42 | | private BaseDictionary<string, WearableItem> catalog; |
| | 43 | | private readonly IWearablesCatalogService wearablesCatalogService; |
| | 44 | | private readonly IUserProfileBridge userProfileBridge; |
| 0 | 45 | | private bool renderingEnabled => CommonScriptableObjects.rendererState.Get(); |
| 0 | 46 | | private bool isPlayerRendererLoaded => DataStore.i.common.isPlayerRendererLoaded.Get(); |
| 0 | 47 | | private BaseVariable<bool> avatarEditorVisible => DataStore.i.HUDs.avatarEditorVisible; |
| 0 | 48 | | private BaseVariable<Transform> configureBackpackInFullscreenMenu => DataStore.i.exploreV2.configureBackpackInFullsc |
| 0 | 49 | | private BaseVariable<bool> exploreV2IsOpen => DataStore.i.exploreV2.isOpen; |
| 0 | 50 | | private DataStore_EmotesCustomization emotesCustomizationDataStore => DataStore.i.emotesCustomization; |
| | 51 | |
|
| | 52 | | private readonly DataStore_FeatureFlag featureFlags; |
| | 53 | |
|
| 0 | 54 | | private readonly Dictionary<string, List<WearableItem>> wearablesByCategory = new Dictionary<string, List<WearableIt |
| 0 | 55 | | protected readonly AvatarEditorHUDModel model = new AvatarEditorHUDModel(); |
| | 56 | |
|
| | 57 | | private ColorList skinColorList; |
| | 58 | | private ColorList eyeColorList; |
| | 59 | | private ColorList hairColorList; |
| | 60 | | private bool prevMouseLockState = false; |
| | 61 | | private bool ownedWearablesAlreadyLoaded = false; |
| 0 | 62 | | private List<Nft> ownedNftCollectionsL1 = new List<Nft>(); |
| 0 | 63 | | private List<Nft> ownedNftCollectionsL2 = new List<Nft>(); |
| | 64 | | internal bool avatarIsDirty = false; |
| | 65 | | private float lastTimeOwnedWearablesChecked = 0; |
| 0 | 66 | | private float lastTimeOwnedEmotesChecked = float.MinValue; |
| | 67 | | internal bool collectionsAlreadyLoaded = false; |
| 0 | 68 | | private float prevRenderScale = 1.0f; |
| | 69 | | private bool isAvatarPreviewReady; |
| 0 | 70 | | private List<string> thirdPartyWearablesLoaded = new List<string>(); |
| 0 | 71 | | private List<string> thirdPartyCollectionsActive = new List<string>(); |
| 0 | 72 | | private CancellationTokenSource loadEmotesCTS = new CancellationTokenSource(); |
| 0 | 73 | | private CancellationTokenSource loadOwnedWearablesCTS = new CancellationTokenSource(); |
| 0 | 74 | | private CancellationTokenSource loadThirdPartyWearablesCTS = new CancellationTokenSource(); |
| | 75 | | private bool loadingWearables; |
| | 76 | | private WearableItem[] emotesLoadedAsWearables; |
| | 77 | | private AvatarEditorHUDAnimationController avatarEditorHUDAnimationController; |
| | 78 | | private IEmotesCustomizationComponentController emotesCustomizationComponentController; |
| 0 | 79 | | private bool isThirdPartyCollectionsEnabled => featureFlags.flags.Get().IsFeatureEnabled(THIRD_PARTY_COLLECTIONS_FEA |
| | 80 | | private Service<IEmotesCatalogService> emotesCatalogService; |
| | 81 | |
|
| | 82 | | internal AvatarEditorHUDView view; |
| | 83 | |
|
| | 84 | | public event Action OnOpen; |
| | 85 | | public event Action OnClose; |
| | 86 | |
|
| 0 | 87 | | public AvatarEditorHUDController( |
| | 88 | | DataStore_FeatureFlag featureFlags, |
| | 89 | | IAnalytics analytics, |
| | 90 | | IWearablesCatalogService wearablesCatalogService, |
| | 91 | | IUserProfileBridge userProfileBridge) |
| | 92 | | { |
| 0 | 93 | | this.featureFlags = featureFlags; |
| 0 | 94 | | this.analytics = analytics; |
| 0 | 95 | | this.newUserExperienceAnalytics = new NewUserExperienceAnalytics(analytics); |
| 0 | 96 | | this.wearablesCatalogService = wearablesCatalogService; |
| 0 | 97 | | this.userProfileBridge = userProfileBridge; |
| 0 | 98 | | } |
| | 99 | |
|
| | 100 | | public void Initialize( |
| | 101 | | bool bypassUpdateAvatarPreview, |
| | 102 | | IPreviewCameraRotationController previewCameraRotationController) |
| | 103 | | { |
| 0 | 104 | | this.bypassUpdateAvatarPreview = bypassUpdateAvatarPreview; |
| | 105 | |
|
| 0 | 106 | | view = AvatarEditorHUDView.Create(this, previewCameraRotationController); |
| | 107 | |
|
| 0 | 108 | | view.skinsFeatureContainer.SetActive(true); |
| 0 | 109 | | avatarEditorVisible.OnChange += OnAvatarEditorVisibleChanged; |
| 0 | 110 | | OnAvatarEditorVisibleChanged(avatarEditorVisible.Get(), false); |
| | 111 | |
|
| 0 | 112 | | configureBackpackInFullscreenMenu.OnChange += ConfigureBackpackInFullscreenMenuChanged; |
| 0 | 113 | | ConfigureBackpackInFullscreenMenuChanged(configureBackpackInFullscreenMenu.Get(), null); |
| | 114 | |
|
| 0 | 115 | | exploreV2IsOpen.OnChange += ExploreV2IsOpenChanged; |
| | 116 | |
|
| 0 | 117 | | skinColorList = Resources.Load<ColorList>("SkinTone"); |
| 0 | 118 | | hairColorList = Resources.Load<ColorList>("HairColor"); |
| 0 | 119 | | eyeColorList = Resources.Load<ColorList>("EyeColor"); |
| 0 | 120 | | view.SetColors(skinColorList.colors, hairColorList.colors, eyeColorList.colors); |
| | 121 | |
|
| 0 | 122 | | SetCatalog(wearablesCatalogService.WearablesCatalog); |
| | 123 | |
|
| 0 | 124 | | this.userProfile.OnUpdate += LoadUserProfile; |
| | 125 | |
|
| 0 | 126 | | view.SetSectionActive(AvatarEditorHUDView.EMOTES_SECTION_INDEX, false); |
| | 127 | |
|
| 0 | 128 | | InitializeAsyncEmoteController(); |
| 0 | 129 | | } |
| | 130 | |
|
| | 131 | | private async void InitializeAsyncEmoteController() |
| | 132 | | { |
| 0 | 133 | | EmbeddedEmotesSO embeddedEmotesSo = await emotesCatalogService.Ref.GetEmbeddedEmotes(); |
| | 134 | |
|
| 0 | 135 | | emotesCustomizationComponentController = new EmotesCustomizationComponentController( |
| | 136 | | DataStore.i.emotesCustomization, |
| | 137 | | view.CharacterPreview.GetEmotesController(), |
| | 138 | | DataStore.i.exploreV2, |
| | 139 | | DataStore.i.HUDs, |
| | 140 | | view.emotesSection.transform); |
| | 141 | | //Initialize with embedded emotes |
| 0 | 142 | | emotesCustomizationComponentController.SetEmotes(embeddedEmotesSo.GetAllEmotes().ToArray()); |
| 0 | 143 | | view.SetSectionActive(AvatarEditorHUDView.EMOTES_SECTION_INDEX, true); |
| | 144 | |
|
| 0 | 145 | | emotesCustomizationDataStore.isEmotesCustomizationSelected.OnChange += HandleEmotesCostumizationSelection; |
| 0 | 146 | | emotesCustomizationDataStore.currentLoadedEmotes.OnAdded += OnNewEmoteAdded; |
| | 147 | |
|
| 0 | 148 | | emotesCustomizationComponentController.onEmotePreviewed += OnPreviewEmote; |
| 0 | 149 | | emotesCustomizationComponentController.onEmoteEquipped += OnEmoteEquipped; |
| 0 | 150 | | emotesCustomizationComponentController.onEmoteUnequipped += OnEmoteUnequipped; |
| 0 | 151 | | emotesCustomizationComponentController.onEmoteSell += OnRedirectToEmoteSelling; |
| | 152 | |
|
| 0 | 153 | | LoadUserProfile(userProfile, true); |
| | 154 | |
|
| 0 | 155 | | DataStore.i.HUDs.isAvatarEditorInitialized.Set(true); |
| | 156 | |
|
| 0 | 157 | | view.SetThirdPartyCollectionsVisibility(isThirdPartyCollectionsEnabled); |
| | 158 | |
|
| 0 | 159 | | this.avatarEditorHUDAnimationController = new AvatarEditorHUDAnimationController(view, wearablesCatalogService); |
| | 160 | |
|
| 0 | 161 | | Environment.i.serviceLocator.Get<IApplicationFocusService>().OnApplicationFocus += OnApplicationFocus; |
| 0 | 162 | | } |
| | 163 | |
|
| | 164 | | private void SetCatalog(BaseDictionary<string, WearableItem> catalog) |
| | 165 | | { |
| 0 | 166 | | if (this.catalog != null) |
| | 167 | | { |
| 0 | 168 | | this.catalog.OnAdded -= OnAdditionalWearableAdded; |
| 0 | 169 | | this.catalog.OnRemoved -= OnAdditionalWearableRemoved; |
| | 170 | | } |
| | 171 | |
|
| 0 | 172 | | this.catalog = catalog; |
| | 173 | |
|
| 0 | 174 | | ProcessCatalog(this.catalog); |
| | 175 | |
|
| 0 | 176 | | this.catalog.OnAdded += OnAdditionalWearableAdded; |
| 0 | 177 | | this.catalog.OnRemoved += OnAdditionalWearableRemoved; |
| 0 | 178 | | } |
| | 179 | |
|
| | 180 | | private void OnAdditionalWearableRemoved(string s, WearableItem item) |
| | 181 | | { |
| 0 | 182 | | RemoveWearable(s, item); |
| 0 | 183 | | view.RefreshSelectorsSize(); |
| 0 | 184 | | } |
| | 185 | |
|
| | 186 | | private void OnAdditionalWearableAdded(string id, WearableItem item) |
| | 187 | | { |
| 0 | 188 | | AddWearable(id, item); |
| 0 | 189 | | view.RefreshSelectorsSize(); |
| 0 | 190 | | } |
| | 191 | |
|
| | 192 | | private void LoadUserProfile(UserProfile userProfile) |
| | 193 | | { |
| 0 | 194 | | LoadUserProfile(userProfile, false); |
| 0 | 195 | | QueryNftCollections(userProfile.userId); |
| 0 | 196 | | } |
| | 197 | |
|
| | 198 | | private void LoadOwnedWereables(UserProfile userProfile) |
| | 199 | | { |
| | 200 | | async UniTaskVoid RequestOwnedWearablesAsync(CancellationToken ct) |
| | 201 | | { |
| 0 | 202 | | lastTimeOwnedWearablesChecked = Time.realtimeSinceStartup; |
| 0 | 203 | | loadingWearables = true; |
| | 204 | |
|
| | 205 | | try |
| | 206 | | { |
| 0 | 207 | | var ownedWearables = await wearablesCatalogService.RequestOwnedWearablesAsync( |
| | 208 | | userProfile.userId, |
| | 209 | | 1, |
| | 210 | | int.MaxValue, |
| | 211 | | true, |
| | 212 | | ct); |
| | 213 | |
|
| 0 | 214 | | ownedWearablesAlreadyLoaded = true; |
| | 215 | | //Prior profile V1 emotes must be retrieved along the wearables, onwards they will be requested separate |
| 0 | 216 | | this.userProfile.SetInventory(ownedWearables.wearables.Select(x => x.id).Concat(thirdPartyWearablesLoade |
| 0 | 217 | | LoadUserProfile(userProfile, true); |
| 0 | 218 | | if (userProfile != null && userProfile.avatar != null) |
| 0 | 219 | | emotesLoadedAsWearables = ownedWearables.wearables.Where(x => x.IsEmote()).ToArray(); |
| 0 | 220 | | } |
| | 221 | | catch (Exception e) |
| | 222 | | { |
| 0 | 223 | | LoadUserProfile(userProfile, true); |
| | 224 | |
|
| 0 | 225 | | NotificationsController.i.ShowNotification(new Model |
| | 226 | | { |
| | 227 | | message = LOADING_OWNED_WEARABLES_ERROR_MESSAGE, |
| | 228 | | type = Type.GENERIC, |
| | 229 | | timer = 10f, |
| | 230 | | destroyOnFinish = true |
| | 231 | | }); |
| | 232 | |
|
| 0 | 233 | | Debug.LogException(e); |
| 0 | 234 | | } |
| | 235 | | finally |
| | 236 | | { |
| 0 | 237 | | loadingWearables = false; |
| | 238 | | } |
| 0 | 239 | | } |
| | 240 | |
|
| | 241 | | //If there is no userID we dont fetch owned wearabales |
| 0 | 242 | | if(string.IsNullOrEmpty(userProfile.userId)) |
| 0 | 243 | | return; |
| | 244 | |
|
| | 245 | | // If wearables are loaded, we are in wearable cooldown, we dont fetch again owned wearables |
| 0 | 246 | | if (ownedWearablesAlreadyLoaded && IsWearableUpdateInCooldown()) |
| 0 | 247 | | return; |
| | 248 | |
|
| 0 | 249 | | loadOwnedWearablesCTS = loadOwnedWearablesCTS.SafeRestart(); |
| 0 | 250 | | RequestOwnedWearablesAsync(loadOwnedWearablesCTS.Token).Forget(); |
| 0 | 251 | | } |
| | 252 | |
|
| | 253 | | private void LoadOwnedEmotes() |
| | 254 | | { |
| | 255 | | //Only check emotes once every 60 seconds |
| 0 | 256 | | if (IsEmotesUpdateInCooldown()) |
| 0 | 257 | | return; |
| | 258 | |
|
| 0 | 259 | | lastTimeOwnedEmotesChecked = Time.realtimeSinceStartup; |
| | 260 | | //TODO only request OwnedEmotes once every minute |
| | 261 | |
|
| 0 | 262 | | loadEmotesCTS.SafeCancelAndDispose(); |
| | 263 | | // we only follow this flow with new profiles |
| 0 | 264 | | if (userProfile?.avatar != null) |
| | 265 | | { |
| 0 | 266 | | loadEmotesCTS = loadEmotesCTS.SafeRestart(); |
| 0 | 267 | | LoadOwnedEmotesTask(loadEmotesCTS.Token); |
| | 268 | | } |
| 0 | 269 | | } |
| | 270 | |
|
| | 271 | | private async UniTaskVoid LoadOwnedEmotesTask(CancellationToken ct = default, int retries = 3) |
| | 272 | | { |
| 0 | 273 | | var emotesCatalog = Environment.i.serviceLocator.Get<IEmotesCatalogService>(); |
| | 274 | | try |
| | 275 | | { |
| 0 | 276 | | EmbeddedEmotesSO embeddedEmoteTask = await emotesCatalogService.Ref.GetEmbeddedEmotes(); |
| 0 | 277 | | var embeddedEmotes = embeddedEmoteTask.GetAllEmotes(); |
| 0 | 278 | | var emotes = await emotesCatalog.RequestOwnedEmotesAsync(userProfile.userId, ct); |
| 0 | 279 | | var emotesList = emotes == null ? embeddedEmotes.Cast<WearableItem>().ToList() : emotes.Concat(embeddedEmote |
| 0 | 280 | | var emotesFilter = new HashSet<string>(); |
| 0 | 281 | | foreach (var e in emotesList) |
| 0 | 282 | | emotesFilter.Add(e.id); |
| | 283 | |
|
| 0 | 284 | | if (loadingWearables) |
| 0 | 285 | | await UniTaskUtils.WaitForBoolean(ref loadingWearables, false, cancellationToken: ct); |
| | 286 | |
|
| 0 | 287 | | if (emotesLoadedAsWearables != null) |
| | 288 | | { |
| 0 | 289 | | foreach (var emoteAsWearable in emotesLoadedAsWearables) |
| | 290 | | { |
| 0 | 291 | | if (emotesFilter.Contains(emoteAsWearable.id)) |
| | 292 | | continue; |
| | 293 | |
|
| 0 | 294 | | emotesList.Add(emoteAsWearable); |
| | 295 | | } |
| | 296 | |
|
| 0 | 297 | | emotesLoadedAsWearables = null; |
| | 298 | | } |
| | 299 | |
|
| 0 | 300 | | emotesCustomizationDataStore.UnequipMissingEmotes(emotesList); |
| 0 | 301 | | emotesCustomizationComponentController.SetEmotes(emotesList.ToArray()); |
| | 302 | |
|
| 0 | 303 | | } |
| 0 | 304 | | catch (Exception e) |
| | 305 | | { |
| 0 | 306 | | OperationCanceledException opCanceled = e as OperationCanceledException; |
| | 307 | | // If the cancellation was requested upwards, dont retry |
| 0 | 308 | | if (opCanceled != null && ct.IsCancellationRequested) |
| 0 | 309 | | return; |
| | 310 | |
|
| 0 | 311 | | if (retries > 0) |
| | 312 | | { |
| 0 | 313 | | LoadOwnedEmotesTask(ct, retries - 1); |
| | 314 | | } |
| | 315 | | else |
| | 316 | | { |
| 0 | 317 | | if (opCanceled == null) // Ignore operation canceled exceptions when logging |
| 0 | 318 | | Debug.LogWarning(e.ToString()); |
| | 319 | | const string ERROR = "There was a problem loading your emotes"; |
| 0 | 320 | | NotificationsController.i.ShowNotification(new Model |
| | 321 | | { |
| | 322 | | message = ERROR, |
| | 323 | | type = Type.GENERIC, |
| | 324 | | timer = 10f, |
| | 325 | | destroyOnFinish = true |
| | 326 | | }); |
| | 327 | | } |
| 0 | 328 | | } |
| 0 | 329 | | } |
| | 330 | |
|
| | 331 | | private void QueryNftCollections(string userId) |
| | 332 | | { |
| 0 | 333 | | if (string.IsNullOrEmpty(userId)) |
| 0 | 334 | | return; |
| | 335 | |
|
| 0 | 336 | | Environment.i.platform.serviceProviders.theGraph.QueryNftCollections(userProfile.userId, NftCollectionsLayer.ETH |
| 0 | 337 | | .Then((nfts) => ownedNftCollectionsL1 = nfts) |
| 0 | 338 | | .Catch((error) => Debug.LogError(error)); |
| | 339 | |
|
| 0 | 340 | | Environment.i.platform.serviceProviders.theGraph.QueryNftCollections(userProfile.userId, NftCollectionsLayer.MAT |
| 0 | 341 | | .Then((nfts) => ownedNftCollectionsL2 = nfts) |
| 0 | 342 | | .Catch((error) => Debug.LogError(error)); |
| 0 | 343 | | } |
| | 344 | |
|
| | 345 | | private void PlayerRendererLoaded(bool current, bool previous) |
| | 346 | | { |
| 0 | 347 | | if (!current) |
| 0 | 348 | | return; |
| | 349 | |
|
| 0 | 350 | | if (!ownedWearablesAlreadyLoaded) |
| | 351 | | { |
| 0 | 352 | | List<string> equippedOwnedWearables = new List<string>(); |
| 0 | 353 | | for (int i = 0; i < userProfile.avatar.wearables.Count; i++) |
| | 354 | | { |
| 0 | 355 | | if (catalog.TryGetValue(userProfile.avatar.wearables[i], out WearableItem wearable) && |
| | 356 | | !wearable.data.tags.Contains(WearableLiterals.Tags.BASE_WEARABLE)) |
| | 357 | | { |
| 0 | 358 | | equippedOwnedWearables.Add(userProfile.avatar.wearables[i]); |
| | 359 | | } |
| | 360 | | } |
| | 361 | |
|
| 0 | 362 | | userProfile.SetInventory(equippedOwnedWearables); |
| | 363 | | } |
| | 364 | |
|
| 0 | 365 | | LoadUserProfile(userProfile, true); |
| 0 | 366 | | DataStore.i.common.isPlayerRendererLoaded.OnChange -= PlayerRendererLoaded; |
| 0 | 367 | | } |
| | 368 | |
|
| | 369 | | private void LoadUserProfile(UserProfile userProfile, bool forceLoading) |
| | 370 | | { |
| 0 | 371 | | bool avatarEditorNotVisible = renderingEnabled && !view.isOpen; |
| 0 | 372 | | bool isPlaying = !Application.isBatchMode; |
| | 373 | |
|
| 0 | 374 | | if (!forceLoading) |
| | 375 | | { |
| 0 | 376 | | if (isPlaying && avatarEditorNotVisible) |
| 0 | 377 | | return; |
| | 378 | | } |
| | 379 | |
|
| 0 | 380 | | if (userProfile == null) |
| 0 | 381 | | return; |
| | 382 | |
|
| 0 | 383 | | if (userProfile.avatar == null || string.IsNullOrEmpty(userProfile.avatar.bodyShape)) |
| 0 | 384 | | return; |
| | 385 | |
|
| 0 | 386 | | view.InitializeNavigationEvents(!userProfile.hasConnectedWeb3); |
| | 387 | |
|
| 0 | 388 | | wearablesCatalogService.WearablesCatalog.TryGetValue(userProfile.avatar.bodyShape, out var bodyShape); |
| | 389 | |
|
| 0 | 390 | | if (bodyShape == null) |
| | 391 | | { |
| 0 | 392 | | return; |
| | 393 | | } |
| | 394 | |
|
| 0 | 395 | | view.SetIsWeb3(userProfile.hasConnectedWeb3); |
| | 396 | |
|
| 0 | 397 | | ProcessCatalog(catalog); |
| | 398 | |
|
| 0 | 399 | | if (avatarIsDirty) |
| 0 | 400 | | return; |
| | 401 | |
|
| 0 | 402 | | EquipBodyShape(bodyShape); |
| 0 | 403 | | EquipSkinColor(userProfile.avatar.skinColor); |
| 0 | 404 | | EquipHairColor(userProfile.avatar.hairColor); |
| 0 | 405 | | EquipEyesColor(userProfile.avatar.eyeColor); |
| | 406 | |
|
| 0 | 407 | | model.wearables.Clear(); |
| 0 | 408 | | view.UnselectAllWearables(); |
| | 409 | |
|
| 0 | 410 | | int wearablesCount = userProfile.avatar.wearables.Count; |
| | 411 | |
|
| 0 | 412 | | if (isPlayerRendererLoaded) |
| | 413 | | { |
| 0 | 414 | | for (var i = 0; i < wearablesCount; i++) |
| | 415 | | { |
| 0 | 416 | | wearablesCatalogService.WearablesCatalog.TryGetValue(userProfile.avatar.wearables[i], out var wearable); |
| 0 | 417 | | if (wearable == null) |
| | 418 | | { |
| 0 | 419 | | Debug.LogError($"Couldn't find wearable with ID {userProfile.avatar.wearables[i]}"); |
| 0 | 420 | | continue; |
| | 421 | | } |
| | 422 | |
|
| 0 | 423 | | if (wearable.IsEmote()) |
| 0 | 424 | | EquipEmote(wearable); |
| | 425 | | else |
| 0 | 426 | | EquipWearable(wearable); |
| | 427 | | } |
| | 428 | | } |
| | 429 | |
|
| 0 | 430 | | EnsureWearablesCategoriesNotEmpty(); |
| | 431 | |
|
| 0 | 432 | | UpdateAvatarPreview(); |
| 0 | 433 | | isAvatarPreviewReady = true; |
| 0 | 434 | | } |
| | 435 | |
|
| | 436 | | private void EnsureWearablesCategoriesNotEmpty() |
| | 437 | | { |
| 0 | 438 | | var categoriesInUse = model.wearables |
| 0 | 439 | | .Where(x => !x.IsEmote()) |
| 0 | 440 | | .Select(x => x.data.category).ToArray(); |
| | 441 | |
|
| 0 | 442 | | for (var i = 0; i < categoriesThatMustHaveSelection.Length; i++) |
| | 443 | | { |
| 0 | 444 | | var category = categoriesThatMustHaveSelection[i]; |
| 0 | 445 | | if (category != Categories.BODY_SHAPE && !(categoriesInUse.Contains(category))) |
| | 446 | | { |
| | 447 | | WearableItem wearable; |
| 0 | 448 | | var defaultItemId = WearableLiterals.DefaultWearables.GetDefaultWearable(model.bodyShape.id, category); |
| 0 | 449 | | if (defaultItemId != null) |
| | 450 | | { |
| 0 | 451 | | wearablesCatalogService.WearablesCatalog.TryGetValue(defaultItemId, out wearable); |
| | 452 | | } |
| | 453 | | else |
| | 454 | | { |
| 0 | 455 | | wearable = wearablesByCategory[category].FirstOrDefault(x => x.SupportsBodyShape(model.bodyShape.id) |
| | 456 | | } |
| | 457 | |
|
| 0 | 458 | | if (wearable != null) |
| | 459 | | { |
| 0 | 460 | | EquipWearable(wearable); |
| | 461 | | } |
| | 462 | | } |
| | 463 | | } |
| 0 | 464 | | } |
| | 465 | |
|
| | 466 | | public void WearableClicked(string wearableId) |
| | 467 | | { |
| 0 | 468 | | wearablesCatalogService.WearablesCatalog.TryGetValue(wearableId, out var wearable); |
| 0 | 469 | | if (wearable == null) return; |
| | 470 | |
|
| 0 | 471 | | if (wearable.data.category == Categories.BODY_SHAPE) |
| | 472 | | { |
| 0 | 473 | | if (wearable.id == model.bodyShape.id) |
| 0 | 474 | | return; |
| 0 | 475 | | EquipBodyShape(wearable); |
| | 476 | | } |
| | 477 | | else |
| | 478 | | { |
| 0 | 479 | | if (model.wearables.Contains(wearable)) |
| | 480 | | { |
| 0 | 481 | | if (!categoriesThatMustHaveSelection.Contains(wearable.data.category)) |
| | 482 | | { |
| 0 | 483 | | UnequipWearable(wearable); |
| | 484 | | } |
| | 485 | | else |
| | 486 | | { |
| 0 | 487 | | return; |
| | 488 | | } |
| | 489 | | } |
| | 490 | | else |
| | 491 | | { |
| 0 | 492 | | if (IsTryingToReplaceSkin(wearable)) |
| 0 | 493 | | UnequipWearable(model.GetWearable(Categories.SKIN)); |
| | 494 | |
|
| 0 | 495 | | var sameCategoryEquipped = model.GetWearable(wearable.data.category); |
| 0 | 496 | | if (sameCategoryEquipped != null) |
| 0 | 497 | | UnequipWearable(sameCategoryEquipped); |
| | 498 | |
|
| 0 | 499 | | EquipWearable(wearable); |
| | 500 | | } |
| | 501 | | } |
| | 502 | |
|
| 0 | 503 | | UpdateAvatarPreview(); |
| 0 | 504 | | view.AddFeedbackOnAppear(); |
| 0 | 505 | | avatarIsDirty = true; |
| 0 | 506 | | } |
| | 507 | |
|
| | 508 | | public void HairColorClicked(Color color) |
| | 509 | | { |
| 0 | 510 | | EquipHairColor(color); |
| 0 | 511 | | view.SelectHairColor(model.hairColor); |
| 0 | 512 | | UpdateAvatarPreview(); |
| 0 | 513 | | view.AddFeedbackOnAppear(); |
| 0 | 514 | | } |
| | 515 | |
|
| | 516 | | public void SkinColorClicked(Color color) |
| | 517 | | { |
| 0 | 518 | | EquipSkinColor(color); |
| 0 | 519 | | view.SelectSkinColor(model.skinColor); |
| 0 | 520 | | UpdateAvatarPreview(); |
| 0 | 521 | | } |
| | 522 | |
|
| | 523 | | public void EyesColorClicked(Color color) |
| | 524 | | { |
| 0 | 525 | | EquipEyesColor(color); |
| 0 | 526 | | view.SelectEyeColor(model.eyesColor); |
| 0 | 527 | | UpdateAvatarPreview(); |
| 0 | 528 | | } |
| | 529 | |
|
| | 530 | | protected virtual void UpdateAvatarPreview() |
| | 531 | | { |
| 0 | 532 | | if (bypassUpdateAvatarPreview) |
| 0 | 533 | | return; |
| | 534 | |
|
| 0 | 535 | | AvatarModel modelToUpdate = model.ToAvatarModel(); |
| | 536 | |
|
| | 537 | | // We always keep the loaded emotes into the Avatar Preview |
| 0 | 538 | | foreach (string emoteId in emotesCustomizationDataStore.currentLoadedEmotes.Get()) |
| | 539 | | { |
| 0 | 540 | | modelToUpdate.emotes.Add(new AvatarModel.AvatarEmoteEntry() { urn = emoteId }); |
| | 541 | | } |
| | 542 | |
|
| 0 | 543 | | view.UpdateAvatarPreview(modelToUpdate); |
| 0 | 544 | | } |
| | 545 | |
|
| | 546 | | private void EquipHairColor(Color color) |
| | 547 | | { |
| 0 | 548 | | model.hairColor = color; |
| 0 | 549 | | view.SelectHairColor(model.hairColor); |
| 0 | 550 | | } |
| | 551 | |
|
| | 552 | | private void EquipEyesColor(Color color) |
| | 553 | | { |
| 0 | 554 | | model.eyesColor = color; |
| 0 | 555 | | view.SelectEyeColor(model.eyesColor); |
| 0 | 556 | | } |
| | 557 | |
|
| | 558 | | private void EquipSkinColor(Color color) |
| | 559 | | { |
| 0 | 560 | | model.skinColor = color; |
| 0 | 561 | | view.SelectSkinColor(model.skinColor); |
| 0 | 562 | | } |
| | 563 | |
|
| | 564 | | private void EquipBodyShape(WearableItem bodyShape) |
| | 565 | | { |
| 0 | 566 | | if (bodyShape.data.category != Categories.BODY_SHAPE) |
| | 567 | | { |
| 0 | 568 | | Debug.LogError($"Item ({bodyShape.id} is not a body shape"); |
| 0 | 569 | | return; |
| | 570 | | } |
| | 571 | |
|
| 0 | 572 | | if (model.bodyShape == bodyShape) |
| 0 | 573 | | return; |
| | 574 | |
|
| 0 | 575 | | model.bodyShape = bodyShape; |
| 0 | 576 | | emotesCustomizationComponentController.SetEquippedBodyShape(bodyShape.id); |
| 0 | 577 | | view.UpdateSelectedBody(bodyShape); |
| | 578 | |
|
| 0 | 579 | | int wearablesCount = model.wearables.Count; |
| 0 | 580 | | for (var i = wearablesCount - 1; i >= 0; i--) |
| | 581 | | { |
| 0 | 582 | | UnequipWearable(model.wearables[i]); |
| | 583 | | } |
| | 584 | |
|
| 0 | 585 | | var defaultWearables = WearableLiterals.DefaultWearables.GetDefaultWearables(bodyShape.id); |
| 0 | 586 | | for (var i = 0; i < defaultWearables.Length; i++) |
| | 587 | | { |
| 0 | 588 | | if (catalog.TryGetValue(defaultWearables[i], out var wearable)) |
| 0 | 589 | | EquipWearable(wearable); |
| | 590 | | } |
| 0 | 591 | | } |
| | 592 | |
|
| | 593 | | private void EquipWearable(WearableItem wearable) |
| | 594 | | { |
| 0 | 595 | | if (wearable.IsEmote()) |
| 0 | 596 | | return; |
| | 597 | |
|
| 0 | 598 | | if (!wearablesByCategory.ContainsKey(wearable.data.category)) |
| 0 | 599 | | return; |
| | 600 | |
|
| 0 | 601 | | if (wearablesByCategory[wearable.data.category].Contains(wearable) && wearable.SupportsBodyShape(model.bodyShape |
| | 602 | | { |
| 0 | 603 | | var toReplace = GetWearablesReplacedBy(wearable); |
| 0 | 604 | | toReplace.ForEach(UnequipWearable); |
| 0 | 605 | | model.wearables.Add(wearable); |
| 0 | 606 | | view.EquipWearable(wearable); |
| | 607 | | } |
| 0 | 608 | | } |
| | 609 | |
|
| | 610 | | private void UnequipWearable(WearableItem wearable) |
| | 611 | | { |
| 0 | 612 | | if (wearable.IsEmote()) |
| 0 | 613 | | return; |
| | 614 | |
|
| 0 | 615 | | if (model.wearables.Contains(wearable)) |
| | 616 | | { |
| 0 | 617 | | model.wearables.Remove(wearable); |
| 0 | 618 | | view.UnequipWearable(wearable); |
| 0 | 619 | | avatarIsDirty = true; |
| | 620 | | } |
| 0 | 621 | | } |
| | 622 | |
|
| | 623 | | private void EquipEmote(WearableItem emote) |
| | 624 | | { |
| 0 | 625 | | if (!emote.IsEmote()) |
| 0 | 626 | | return; |
| 0 | 627 | | avatarIsDirty = true; |
| 0 | 628 | | } |
| | 629 | |
|
| | 630 | | private void UnequipEmote(WearableItem emote) |
| | 631 | | { |
| 0 | 632 | | if (!emote.IsEmote()) |
| 0 | 633 | | return; |
| | 634 | |
|
| 0 | 635 | | avatarIsDirty = true; |
| 0 | 636 | | } |
| | 637 | |
|
| | 638 | | public void UnequipAllWearables() |
| | 639 | | { |
| 0 | 640 | | foreach (var wearable in model.wearables) |
| | 641 | | { |
| 0 | 642 | | if (!wearable.IsEmote()) |
| 0 | 643 | | view.UnequipWearable(wearable); |
| | 644 | | } |
| | 645 | |
|
| 0 | 646 | | model.wearables.Clear(); |
| 0 | 647 | | } |
| | 648 | |
|
| | 649 | | private void ProcessCatalog(BaseDictionary<string, WearableItem> catalog) |
| | 650 | | { |
| 0 | 651 | | wearablesByCategory.Clear(); |
| 0 | 652 | | view.RemoveAllWearables(); |
| 0 | 653 | | bool hasSkin = false; |
| 0 | 654 | | bool hasCollectible = false; |
| 0 | 655 | | using (var iterator = catalog.Get().GetEnumerator()) |
| | 656 | | { |
| 0 | 657 | | while (iterator.MoveNext()) |
| | 658 | | { |
| 0 | 659 | | if (iterator.Current.Value.IsEmote()) |
| | 660 | | continue; |
| | 661 | |
|
| 0 | 662 | | if (iterator.Current.Value.IsFromThirdPartyCollection |
| | 663 | | && !thirdPartyCollectionsActive.Contains(iterator.Current.Value.ThirdPartyCollectionId)) |
| | 664 | | continue; |
| | 665 | |
|
| 0 | 666 | | AddWearable(iterator.Current.Key, iterator.Current.Value); |
| 0 | 667 | | hasSkin = iterator.Current.Value.IsSkin() || hasSkin; |
| 0 | 668 | | hasCollectible = iterator.Current.Value.IsCollectible() || hasCollectible; |
| | 669 | | } |
| 0 | 670 | | } |
| 0 | 671 | | view.ShowSkinPopulatedList(hasSkin); |
| 0 | 672 | | view.ShowCollectiblesPopulatedList(hasCollectible); |
| 0 | 673 | | view.RefreshSelectorsSize(); |
| 0 | 674 | | } |
| | 675 | |
|
| | 676 | | private void AddWearable(string id, WearableItem wearable) |
| | 677 | | { |
| 0 | 678 | | if (!wearable.data.tags.Contains(WearableLiterals.Tags.BASE_WEARABLE) && userProfile.GetItemAmount(id) == 0) |
| 0 | 679 | | return; |
| | 680 | |
|
| 0 | 681 | | if (!wearablesByCategory.ContainsKey(wearable.data.category)) |
| 0 | 682 | | wearablesByCategory.Add(wearable.data.category, new List<WearableItem>()); |
| | 683 | |
|
| 0 | 684 | | wearablesByCategory[wearable.data.category].Add(wearable); |
| 0 | 685 | | view.AddWearable(wearable, userProfile.GetItemAmount(id), |
| | 686 | | ShouldShowHideOtherWearablesToast, |
| | 687 | | ShouldShowReplaceOtherWearablesToast, |
| | 688 | | ShouldShowIncompatibleWearableToast); |
| 0 | 689 | | } |
| | 690 | |
|
| | 691 | | private void RemoveWearable(string id, WearableItem wearable) |
| | 692 | | { |
| 0 | 693 | | if (wearablesByCategory.ContainsKey(wearable.data.category)) |
| | 694 | | { |
| 0 | 695 | | if (wearablesByCategory[wearable.data.category].Remove(wearable)) |
| | 696 | | { |
| 0 | 697 | | if (wearablesByCategory[wearable.data.category].Count == 0) |
| | 698 | | { |
| 0 | 699 | | wearablesByCategory.Remove(wearable.data.category); |
| | 700 | | } |
| | 701 | | } |
| | 702 | | } |
| | 703 | |
|
| 0 | 704 | | view.RemoveWearable(wearable); |
| 0 | 705 | | } |
| | 706 | |
|
| | 707 | | public void RandomizeWearables() |
| | 708 | | { |
| 0 | 709 | | EquipHairColor(view.GetRandomColor()); |
| 0 | 710 | | EquipEyesColor(view.GetRandomColor()); |
| | 711 | |
|
| 0 | 712 | | List<WearableItem> wearablesToRemove = model.wearables.Where(x => !x.IsEmote()).ToList(); |
| 0 | 713 | | foreach (var wearable in wearablesToRemove) |
| | 714 | | { |
| 0 | 715 | | model.wearables.Remove(wearable); |
| | 716 | | } |
| | 717 | |
|
| 0 | 718 | | view.UnselectAllWearables(); |
| 0 | 719 | | using (var iterator = wearablesByCategory.GetEnumerator()) |
| | 720 | | { |
| 0 | 721 | | while (iterator.MoveNext()) |
| | 722 | | { |
| 0 | 723 | | string category = iterator.Current.Key; |
| 0 | 724 | | if (!categoriesToRandomize.Contains(category)) |
| | 725 | | { |
| | 726 | | continue; |
| | 727 | | } |
| | 728 | |
|
| 0 | 729 | | var supportedWearables = iterator.Current.Value.Where(x => x.SupportsBodyShape(model.bodyShape.id)).ToAr |
| 0 | 730 | | if (supportedWearables.Length == 0) |
| | 731 | | { |
| 0 | 732 | | Debug.LogError($"Couldn't get any wearable for category {category} and bodyshape {model.bodyShape.id |
| 0 | 733 | | continue; |
| | 734 | | } |
| | 735 | |
|
| 0 | 736 | | var wearable = supportedWearables[Random.Range(0, supportedWearables.Length - 1)]; |
| 0 | 737 | | EquipWearable(wearable); |
| | 738 | | } |
| 0 | 739 | | } |
| | 740 | |
|
| 0 | 741 | | UpdateAvatarPreview(); |
| 0 | 742 | | view.AddFeedbackOnAppear(); |
| 0 | 743 | | } |
| | 744 | |
|
| | 745 | | private List<WearableItem> GetWearablesReplacedBy(WearableItem wearableItem) |
| | 746 | | { |
| 0 | 747 | | var wearablesToReplace = new List<WearableItem>(); |
| 0 | 748 | | var categoriesToReplace = new HashSet<string>(wearableItem.GetReplacesList(model.bodyShape.id) ?? new string[0]) |
| | 749 | |
|
| 0 | 750 | | int wearableCount = model.wearables.Count; |
| 0 | 751 | | for (int i = 0; i < wearableCount; i++) |
| | 752 | | { |
| 0 | 753 | | var wearable = model.wearables[i]; |
| 0 | 754 | | if (wearable == null) continue; |
| | 755 | |
|
| 0 | 756 | | if (categoriesToReplace.Contains(wearable.data.category)) |
| | 757 | | { |
| 0 | 758 | | wearablesToReplace.Add(wearable); |
| | 759 | | } |
| | 760 | | else |
| | 761 | | { |
| | 762 | | //For retrocompatibility's sake we check current wearables against new one (compatibility matrix is symm |
| 0 | 763 | | HashSet<string> replacesList = new HashSet<string>(wearable.GetReplacesList(model.bodyShape.id) ?? new s |
| 0 | 764 | | if (replacesList.Contains(wearableItem.data.category)) |
| | 765 | | { |
| 0 | 766 | | wearablesToReplace.Add(wearable); |
| | 767 | | } |
| | 768 | | } |
| | 769 | | } |
| | 770 | |
|
| 0 | 771 | | return wearablesToReplace; |
| | 772 | | } |
| | 773 | |
|
| 0 | 774 | | public void SetVisibility(bool visible) { avatarEditorVisible.Set(visible); } |
| | 775 | |
|
| 0 | 776 | | private void OnAvatarEditorVisibleChanged(bool current, bool previous) { SetVisibility_Internal(current); } |
| | 777 | |
|
| | 778 | | private void SetVisibility_Internal(bool visible) |
| | 779 | | { |
| 0 | 780 | | bool isSignUpFlow = DataStore.i.common.isSignUpFlow.Get(); |
| 0 | 781 | | if (!visible && view.isOpen) |
| | 782 | | { |
| 0 | 783 | | view.ResetPreviewEmote(); |
| | 784 | |
|
| 0 | 785 | | if (isSignUpFlow) |
| 0 | 786 | | DataStore.i.virtualAudioMixer.sceneSFXVolume.Set(1f); |
| | 787 | |
|
| 0 | 788 | | Environment.i.messaging.manager.paused = false; |
| 0 | 789 | | DataStore.i.skyboxConfig.avatarMatProfile.Set(AvatarMaterialProfile.InWorld); |
| 0 | 790 | | if (prevMouseLockState && isSignUpFlow) |
| | 791 | | { |
| 0 | 792 | | Utils.LockCursor(); |
| | 793 | | } |
| | 794 | |
|
| | 795 | | // NOTE(Brian): SSAO doesn't work correctly with the offseted avatar preview if the renderScale != 1.0 |
| 0 | 796 | | var asset = GraphicsSettings.renderPipelineAsset as UniversalRenderPipelineAsset; |
| 0 | 797 | | asset.renderScale = prevRenderScale; |
| | 798 | |
|
| 0 | 799 | | if (isSignUpFlow) |
| 0 | 800 | | CommonScriptableObjects.isFullscreenHUDOpen.Set(false); |
| | 801 | |
|
| 0 | 802 | | DataStore.i.common.isPlayerRendererLoaded.OnChange -= PlayerRendererLoaded; |
| | 803 | |
|
| 0 | 804 | | OnClose?.Invoke(); |
| | 805 | | } |
| 0 | 806 | | else if (visible && !view.isOpen) |
| | 807 | | { |
| 0 | 808 | | if (isSignUpFlow) |
| | 809 | | { |
| 0 | 810 | | DataStore.i.virtualAudioMixer.sceneSFXVolume.Set(0f); |
| 0 | 811 | | view.sectionSelector.Hide(true); |
| | 812 | | } |
| | 813 | | else |
| | 814 | | { |
| 0 | 815 | | view.sectionSelector.Show(true); |
| | 816 | | } |
| | 817 | |
|
| 0 | 818 | | if (!isSignUpFlow) |
| | 819 | | { |
| 0 | 820 | | LoadOwnedWereables(userProfile); |
| 0 | 821 | | LoadOwnedEmotes(); |
| | 822 | | } |
| | 823 | |
|
| 0 | 824 | | LoadCollections(); |
| 0 | 825 | | Environment.i.messaging.manager.paused = isSignUpFlow; |
| 0 | 826 | | DataStore.i.skyboxConfig.avatarMatProfile.Set(AvatarMaterialProfile.InEditor); |
| | 827 | |
|
| 0 | 828 | | prevMouseLockState = Utils.IsCursorLocked; |
| | 829 | |
|
| 0 | 830 | | if (isSignUpFlow || !DataStore.i.exploreV2.isInitialized.Get()) |
| 0 | 831 | | Utils.UnlockCursor(); |
| | 832 | |
|
| | 833 | | // NOTE(Brian): SSAO doesn't work correctly with the offseted avatar preview if the renderScale != 1.0 |
| 0 | 834 | | var asset = GraphicsSettings.renderPipelineAsset as UniversalRenderPipelineAsset; |
| 0 | 835 | | prevRenderScale = asset.renderScale; |
| 0 | 836 | | asset.renderScale = 1.0f; |
| | 837 | |
|
| 0 | 838 | | if (isSignUpFlow) |
| 0 | 839 | | CommonScriptableObjects.isFullscreenHUDOpen.Set(true); |
| | 840 | |
|
| 0 | 841 | | DataStore.i.common.isPlayerRendererLoaded.OnChange += PlayerRendererLoaded; |
| | 842 | |
|
| 0 | 843 | | OnOpen?.Invoke(); |
| | 844 | | } |
| | 845 | |
|
| 0 | 846 | | view.SetVisibility(visible); |
| 0 | 847 | | } |
| | 848 | |
|
| | 849 | | public void Dispose() |
| | 850 | | { |
| 0 | 851 | | loadEmotesCTS.SafeCancelAndDispose(); |
| 0 | 852 | | loadEmotesCTS = null; |
| | 853 | |
|
| 0 | 854 | | loadOwnedWearablesCTS.SafeCancelAndDispose(); |
| 0 | 855 | | loadOwnedWearablesCTS = null; |
| | 856 | |
|
| 0 | 857 | | loadThirdPartyWearablesCTS.SafeCancelAndDispose(); |
| 0 | 858 | | loadThirdPartyWearablesCTS = null; |
| | 859 | |
|
| 0 | 860 | | avatarEditorVisible.OnChange -= OnAvatarEditorVisibleChanged; |
| 0 | 861 | | configureBackpackInFullscreenMenu.OnChange -= ConfigureBackpackInFullscreenMenuChanged; |
| 0 | 862 | | DataStore.i.common.isPlayerRendererLoaded.OnChange -= PlayerRendererLoaded; |
| 0 | 863 | | exploreV2IsOpen.OnChange -= ExploreV2IsOpenChanged; |
| 0 | 864 | | emotesCustomizationDataStore.isEmotesCustomizationSelected.OnChange -= HandleEmotesCostumizationSelection; |
| 0 | 865 | | emotesCustomizationDataStore.currentLoadedEmotes.OnAdded -= OnNewEmoteAdded; |
| | 866 | |
|
| 0 | 867 | | emotesCustomizationComponentController.onEmotePreviewed -= OnPreviewEmote; |
| 0 | 868 | | emotesCustomizationComponentController.onEmoteEquipped -= OnEmoteEquipped; |
| 0 | 869 | | emotesCustomizationComponentController.onEmoteUnequipped -= OnEmoteUnequipped; |
| 0 | 870 | | emotesCustomizationComponentController.onEmoteSell -= OnRedirectToEmoteSelling; |
| | 871 | |
|
| 0 | 872 | | avatarEditorHUDAnimationController.Dispose(); |
| 0 | 873 | | Environment.i.serviceLocator.Get<IApplicationFocusService>().OnApplicationFocus -= OnApplicationFocus; |
| | 874 | |
|
| 0 | 875 | | CleanUp(); |
| 0 | 876 | | } |
| | 877 | |
|
| | 878 | | public void CleanUp() |
| | 879 | | { |
| 0 | 880 | | UnequipAllWearables(); |
| | 881 | |
|
| 0 | 882 | | if (view != null) |
| 0 | 883 | | view.Dispose(); |
| | 884 | |
|
| 0 | 885 | | this.userProfile.OnUpdate -= LoadUserProfile; |
| 0 | 886 | | this.catalog.OnAdded -= AddWearable; |
| 0 | 887 | | this.catalog.OnRemoved -= RemoveWearable; |
| 0 | 888 | | DataStore.i.common.isPlayerRendererLoaded.OnChange -= PlayerRendererLoaded; |
| 0 | 889 | | } |
| | 890 | |
|
| 0 | 891 | | public void SetConfiguration(HUDConfiguration configuration) { SetVisibility(configuration.active); } |
| | 892 | |
|
| | 893 | | public void SaveAvatar(Texture2D face256Snapshot, Texture2D bodySnapshot) |
| | 894 | | { |
| 0 | 895 | | var avatarModel = model.ToAvatarModel(); |
| | 896 | |
|
| | 897 | | // Add the equipped emotes to the avatar model |
| 0 | 898 | | List<AvatarModel.AvatarEmoteEntry> emoteEntries = new List<AvatarModel.AvatarEmoteEntry>(); |
| 0 | 899 | | int equippedEmotesCount = emotesCustomizationDataStore.unsavedEquippedEmotes.Count(); |
| 0 | 900 | | for (int i = 0; i < equippedEmotesCount; i++) |
| | 901 | | { |
| 0 | 902 | | var equippedEmote = emotesCustomizationDataStore.unsavedEquippedEmotes[i]; |
| 0 | 903 | | if (equippedEmote == null) |
| | 904 | | continue; |
| | 905 | |
|
| 0 | 906 | | emoteEntries.Add(new AvatarModel.AvatarEmoteEntry { slot = i, urn = equippedEmote.id }); |
| | 907 | | } |
| | 908 | |
|
| 0 | 909 | | avatarModel.emotes = emoteEntries; |
| | 910 | |
|
| 0 | 911 | | SendNewEquippedWearablesAnalytics(userProfile.avatar.wearables, avatarModel.wearables); |
| 0 | 912 | | emotesCustomizationDataStore.equippedEmotes.Set(emotesCustomizationDataStore.unsavedEquippedEmotes.Get()); |
| | 913 | |
|
| 0 | 914 | | WebInterface.SendSaveAvatar(avatarModel, face256Snapshot, bodySnapshot, DataStore.i.common.isSignUpFlow.Get()); |
| 0 | 915 | | userProfile.OverrideAvatar(avatarModel, face256Snapshot); |
| | 916 | |
|
| 0 | 917 | | if (DataStore.i.common.isSignUpFlow.Get()) |
| | 918 | | { |
| 0 | 919 | | DataStore.i.HUDs.signupVisible.Set(true); |
| 0 | 920 | | newUserExperienceAnalytics.AvatarEditSuccessNux(); |
| | 921 | | } |
| | 922 | |
|
| 0 | 923 | | avatarIsDirty = false; |
| 0 | 924 | | SetVisibility(false); |
| 0 | 925 | | } |
| | 926 | |
|
| | 927 | | public void GoToMarketplaceOrConnectWallet() |
| | 928 | | { |
| 0 | 929 | | if (userProfile.hasConnectedWeb3) |
| 0 | 930 | | WebInterface.OpenURL(URL_MARKET_PLACE); |
| | 931 | | else |
| 0 | 932 | | WebInterface.OpenURL(URL_GET_A_WALLET); |
| 0 | 933 | | } |
| | 934 | |
|
| | 935 | | public void SellCollectible(string collectibleId) |
| | 936 | | { |
| 0 | 937 | | var ownedCollectible = ownedNftCollectionsL1.FirstOrDefault(nft => nft.urn == collectibleId); |
| 0 | 938 | | if (ownedCollectible == null) |
| 0 | 939 | | ownedCollectible = ownedNftCollectionsL2.FirstOrDefault(nft => nft.urn == collectibleId); |
| | 940 | |
|
| 0 | 941 | | if (ownedCollectible != null) |
| 0 | 942 | | WebInterface.OpenURL(URL_SELL_SPECIFIC_COLLECTIBLE.Replace("{collectionId}", ownedCollectible.collectionId). |
| | 943 | | else |
| 0 | 944 | | WebInterface.OpenURL(URL_SELL_COLLECTIBLE_GENERIC); |
| 0 | 945 | | } |
| | 946 | |
|
| 0 | 947 | | public void ToggleVisibility() { SetVisibility(!view.isOpen); } |
| | 948 | |
|
| 0 | 949 | | private void ConfigureBackpackInFullscreenMenuChanged(Transform currentParentTransform, Transform previousParentTran |
| | 950 | |
|
| | 951 | | private void ExploreV2IsOpenChanged(bool current, bool previous) |
| | 952 | | { |
| 0 | 953 | | if (!current && avatarIsDirty) |
| | 954 | | { |
| 0 | 955 | | avatarIsDirty = false; |
| | 956 | |
|
| 0 | 957 | | LoadUserProfile(userProfile, true); |
| | 958 | |
|
| 0 | 959 | | emotesCustomizationComponentController.RestoreEmoteSlots(); |
| | 960 | | } |
| 0 | 961 | | } |
| | 962 | |
|
| | 963 | | private void LoadCollections() |
| | 964 | | { |
| 0 | 965 | | if (!isThirdPartyCollectionsEnabled || collectionsAlreadyLoaded) |
| 0 | 966 | | return; |
| | 967 | |
|
| 0 | 968 | | WearablesFetchingHelper.GetThirdPartyCollections() |
| | 969 | | .Then((collections) => |
| | 970 | | { |
| 0 | 971 | | view.LoadCollectionsDropdown(collections); |
| 0 | 972 | | collectionsAlreadyLoaded = true; |
| 0 | 973 | | LoadUserThirdPartyWearables(); |
| 0 | 974 | | }) |
| 0 | 975 | | .Catch((error) => Debug.LogError(error)); |
| 0 | 976 | | } |
| | 977 | |
|
| | 978 | | private void LoadUserThirdPartyWearables() |
| | 979 | | { |
| 0 | 980 | | List<string> collectionIdsToLoad = new List<string>(); |
| 0 | 981 | | foreach (string wearableId in userProfile.avatar.wearables) |
| | 982 | | { |
| 0 | 983 | | wearablesCatalogService.WearablesCatalog.TryGetValue(wearableId, out var wearable); |
| | 984 | |
|
| 0 | 985 | | if (wearable != null && wearable.IsFromThirdPartyCollection) |
| | 986 | | { |
| 0 | 987 | | if (!collectionIdsToLoad.Contains(wearable.ThirdPartyCollectionId)) |
| 0 | 988 | | collectionIdsToLoad.Add(wearable.ThirdPartyCollectionId); |
| | 989 | | } |
| | 990 | | } |
| | 991 | |
|
| 0 | 992 | | foreach (string collectionId in collectionIdsToLoad) |
| | 993 | | { |
| 0 | 994 | | view.ToggleThirdPartyCollection(collectionId, true); |
| | 995 | | } |
| 0 | 996 | | } |
| | 997 | |
|
| | 998 | | public void ToggleThirdPartyCollection(bool isOn, string collectionId, string _) |
| | 999 | | { |
| 0 | 1000 | | if (isOn) |
| 0 | 1001 | | FetchAndShowThirdPartyCollection(collectionId); |
| | 1002 | | else |
| 0 | 1003 | | RemoveThirdPartyCollection(collectionId); |
| 0 | 1004 | | } |
| | 1005 | |
|
| | 1006 | | private void FetchAndShowThirdPartyCollection(string collectionId) |
| | 1007 | | { |
| | 1008 | | async UniTaskVoid RequestThirdPartyWearablesAsync(CancellationToken ct) |
| | 1009 | | { |
| | 1010 | | try |
| | 1011 | | { |
| 0 | 1012 | | var wearables = await wearablesCatalogService.RequestThirdPartyWearablesByCollectionAsync( |
| | 1013 | | userProfile.userId, |
| | 1014 | | collectionId, |
| | 1015 | | 1, |
| | 1016 | | int.MaxValue, |
| | 1017 | | true, |
| | 1018 | | ct); |
| | 1019 | |
|
| 0 | 1020 | | if (wearables.wearables.Count.Equals(0)) view.ShowNoItemOfWearableCollectionWarning(); |
| 0 | 1021 | | thirdPartyCollectionsActive.Add(collectionId); |
| 0 | 1022 | | foreach (var wearable in wearables.wearables) |
| | 1023 | | { |
| 0 | 1024 | | if (!userProfile.ContainsInInventory(wearable.id)) |
| | 1025 | | { |
| 0 | 1026 | | userProfile.AddToInventory(wearable.id); |
| | 1027 | |
|
| 0 | 1028 | | if (!thirdPartyWearablesLoaded.Contains(wearable.id)) |
| 0 | 1029 | | thirdPartyWearablesLoaded.Add(wearable.id); |
| | 1030 | | } |
| | 1031 | | } |
| | 1032 | |
|
| 0 | 1033 | | view.BlockCollectionsDropdown(false); |
| 0 | 1034 | | LoadUserProfile(userProfile, true); |
| 0 | 1035 | | view.RefreshSelectorsSize(); |
| 0 | 1036 | | } |
| | 1037 | | catch (Exception e) |
| | 1038 | | { |
| 0 | 1039 | | view.BlockCollectionsDropdown(false); |
| 0 | 1040 | | Debug.LogError(e.Message); |
| 0 | 1041 | | } |
| 0 | 1042 | | } |
| | 1043 | |
|
| 0 | 1044 | | view.BlockCollectionsDropdown(true); |
| 0 | 1045 | | RequestThirdPartyWearablesAsync(loadThirdPartyWearablesCTS.Token).Forget(); |
| 0 | 1046 | | } |
| | 1047 | |
|
| | 1048 | | private void RemoveThirdPartyCollection(string collectionId) |
| | 1049 | | { |
| 0 | 1050 | | var wearablesToRemove = wearablesCatalogService.WearablesCatalog |
| 0 | 1051 | | .Where(wearable => !userProfile.HasEquipped(wearable.Key) |
| | 1052 | | && wearable.Value.ThirdPartyCollectionId == co |
| 0 | 1053 | | .Select(item => item.Key) |
| | 1054 | | .ToList(); |
| | 1055 | |
|
| 0 | 1056 | | thirdPartyCollectionsActive.Remove(collectionId); |
| | 1057 | |
|
| 0 | 1058 | | foreach (string wearableId in wearablesToRemove) |
| | 1059 | | { |
| 0 | 1060 | | wearablesCatalogService.RemoveWearableFromCatalog(wearableId); |
| 0 | 1061 | | userProfile.RemoveFromInventory(wearableId); |
| 0 | 1062 | | thirdPartyWearablesLoaded.Remove(wearableId); |
| | 1063 | | } |
| | 1064 | |
|
| 0 | 1065 | | LoadUserProfile(userProfile, true); |
| 0 | 1066 | | } |
| | 1067 | |
|
| | 1068 | | private bool ShouldShowHideOtherWearablesToast(WearableItem wearable) |
| | 1069 | | { |
| 0 | 1070 | | var isWearingSkinAlready = model.wearables.Any(item => item.IsSkin()); |
| 0 | 1071 | | return wearable.IsSkin() && !isWearingSkinAlready; |
| | 1072 | | } |
| | 1073 | |
|
| | 1074 | | private bool ShouldShowIncompatibleWearableToast(WearableItem wearable) |
| | 1075 | | { |
| 0 | 1076 | | if(wearable.data.category == Categories.BODY_SHAPE || wearable.data.category == Categories.SKIN) |
| 0 | 1077 | | return false; |
| | 1078 | | else |
| 0 | 1079 | | return !wearable.SupportsBodyShape(model.bodyShape.id); |
| | 1080 | | } |
| | 1081 | |
|
| | 1082 | | private bool IsTryingToReplaceSkin(WearableItem wearable) |
| | 1083 | | { |
| 0 | 1084 | | return model.wearables.Any(skin => |
| | 1085 | | { |
| 0 | 1086 | | return skin.IsSkin() |
| | 1087 | | && skin.DoesHide(wearable.data.category, model.bodyShape.id); |
| | 1088 | | }); |
| | 1089 | | } |
| | 1090 | |
|
| | 1091 | | private bool ShouldShowReplaceOtherWearablesToast(WearableItem wearable) |
| | 1092 | | { |
| 0 | 1093 | | if (IsTryingToReplaceSkin(wearable)) return true; |
| 0 | 1094 | | var toReplace = GetWearablesReplacedBy(wearable); |
| 0 | 1095 | | if (wearable == null || toReplace.Count == 0) return false; |
| 0 | 1096 | | if (model.wearables.Contains(wearable)) return false; |
| | 1097 | |
|
| | 1098 | | // NOTE: why just 1? |
| 0 | 1099 | | if (toReplace.Count == 1) |
| | 1100 | | { |
| 0 | 1101 | | var w = toReplace[0]; |
| 0 | 1102 | | if (w.data.category == wearable.data.category) |
| 0 | 1103 | | return false; |
| | 1104 | | } |
| 0 | 1105 | | return true; |
| | 1106 | | } |
| | 1107 | |
|
| | 1108 | |
|
| | 1109 | | private void HandleEmotesCostumizationSelection(bool current, bool previous) |
| | 1110 | | { |
| 0 | 1111 | | if (!current) |
| 0 | 1112 | | return; |
| | 1113 | |
|
| 0 | 1114 | | view.sectionSelector.GetSection(AvatarEditorHUDView.EMOTES_SECTION_INDEX).SelectToggle(); |
| 0 | 1115 | | } |
| | 1116 | |
|
| | 1117 | | private void OnNewEmoteAdded(string emoteId) |
| | 1118 | | { |
| 0 | 1119 | | if (!isAvatarPreviewReady) |
| 0 | 1120 | | return; |
| | 1121 | |
|
| 0 | 1122 | | UpdateAvatarPreview(); |
| 0 | 1123 | | } |
| | 1124 | |
|
| 0 | 1125 | | private void OnPreviewEmote(string emoteId) { view.PlayPreviewEmote(emoteId); } |
| | 1126 | |
|
| | 1127 | | private void OnEmoteEquipped(string emoteId) |
| | 1128 | | { |
| 0 | 1129 | | catalog.TryGetValue(emoteId, out WearableItem equippedEmote); |
| | 1130 | |
|
| 0 | 1131 | | if (equippedEmote != null) |
| 0 | 1132 | | EquipEmote(equippedEmote); |
| 0 | 1133 | | } |
| | 1134 | |
|
| | 1135 | | private void OnEmoteUnequipped(string emoteId) |
| | 1136 | | { |
| 0 | 1137 | | catalog.TryGetValue(emoteId, out WearableItem unequippedEmote); |
| | 1138 | |
|
| 0 | 1139 | | if (unequippedEmote != null) |
| 0 | 1140 | | UnequipEmote(unequippedEmote); |
| 0 | 1141 | | } |
| | 1142 | |
|
| 0 | 1143 | | private void OnRedirectToEmoteSelling(string emoteId) { SellCollectible(emoteId); } |
| | 1144 | |
|
| | 1145 | | internal void SendNewEquippedWearablesAnalytics(List<string> oldWearables, List<string> newWearables) |
| | 1146 | | { |
| 0 | 1147 | | for (int i = 0; i < newWearables.Count; i++) |
| | 1148 | | { |
| 0 | 1149 | | if (oldWearables.Contains(newWearables[i])) |
| | 1150 | | continue; |
| | 1151 | |
|
| 0 | 1152 | | catalog.TryGetValue(newWearables[i], out WearableItem newEquippedEmote); |
| 0 | 1153 | | if (newEquippedEmote != null && !newEquippedEmote.IsEmote()) |
| 0 | 1154 | | SendEquipWearableAnalytic(newEquippedEmote); |
| | 1155 | | } |
| 0 | 1156 | | } |
| | 1157 | |
|
| | 1158 | | private void SendEquipWearableAnalytic(WearableItem equippedWearable) |
| | 1159 | | { |
| 0 | 1160 | | Dictionary<string, string> data = new Dictionary<string, string>(); |
| 0 | 1161 | | data.Add("name", equippedWearable.GetName()); |
| 0 | 1162 | | data.Add("rarity", equippedWearable.rarity); |
| 0 | 1163 | | data.Add("category", equippedWearable.data.category); |
| 0 | 1164 | | data.Add("linked_wearable", equippedWearable.IsFromThirdPartyCollection.ToString()); |
| 0 | 1165 | | data.Add("third_party_collection_id", equippedWearable.ThirdPartyCollectionId); |
| 0 | 1166 | | data.Add("is_in_l2", equippedWearable.IsInL2().ToString()); |
| 0 | 1167 | | data.Add("smart_item", equippedWearable.IsSmart().ToString()); |
| | 1168 | |
|
| 0 | 1169 | | analytics.SendAnalytic(EQUIP_WEARABLE_METRIC, data); |
| 0 | 1170 | | } |
| | 1171 | |
|
| | 1172 | | private bool IsWearableUpdateInCooldown() |
| | 1173 | | { |
| 0 | 1174 | | return Time.realtimeSinceStartup < lastTimeOwnedWearablesChecked + REQUESTS_COOLDOWN_TIME; |
| | 1175 | | } |
| | 1176 | |
|
| | 1177 | | private bool IsEmotesUpdateInCooldown() |
| | 1178 | | { |
| 0 | 1179 | | return Time.realtimeSinceStartup < lastTimeOwnedEmotesChecked + REQUESTS_COOLDOWN_TIME; |
| | 1180 | | } |
| | 1181 | |
|
| | 1182 | | private void OnApplicationFocus() |
| | 1183 | | { |
| 0 | 1184 | | lastTimeOwnedWearablesChecked = -REQUESTS_COOLDOWN_TIME; |
| 0 | 1185 | | lastTimeOwnedEmotesChecked = -REQUESTS_COOLDOWN_TIME; |
| 0 | 1186 | | } |
| | 1187 | | } |