| | 1 | | using AvatarAssets; |
| | 2 | | using Cysharp.Threading.Tasks; |
| | 3 | | using DCL.Configuration; |
| | 4 | | using DCLServices.EmotesService; |
| | 5 | | using DCLServices.WearablesCatalogService; |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Threading; |
| | 9 | | using UnityEngine; |
| | 10 | | using Object = UnityEngine.Object; |
| | 11 | |
|
| | 12 | | namespace DCL.Emotes |
| | 13 | | { |
| | 14 | | public class EmotesService : IEmotesService |
| | 15 | | { |
| | 16 | | //To avoid circular references in assemblies we hardcode this here instead of using WearableLiterals |
| | 17 | | //Embedded Emotes are only temporary until they can be retrieved from the content server |
| | 18 | | private const string FEMALE = "urn:decentraland:off-chain:base-avatars:BaseFemale"; |
| | 19 | | private const string MALE = "urn:decentraland:off-chain:base-avatars:BaseMale"; |
| | 20 | |
|
| | 21 | | private readonly EmoteAnimationLoaderFactory emoteAnimationLoaderFactory; |
| | 22 | | private readonly IEmotesCatalogService emotesCatalogService; |
| | 23 | | private readonly IWearablesCatalogService wearablesCatalogService; |
| | 24 | | private readonly ICatalyst catalyst; |
| | 25 | | private GameObject animationsModelsContainer; |
| | 26 | | private readonly EmoteVolumeHandler audioVolumeHandler; |
| | 27 | |
|
| 425 | 28 | | private readonly Dictionary<EmoteBodyId, IEmoteReference> embeddedEmotes = new (); |
| 425 | 29 | | private readonly Dictionary<EmoteBodyId, ExtendedEmote> extendedEmotes = new (); |
| | 30 | |
|
| 425 | 31 | | public EmotesService( |
| | 32 | | EmoteAnimationLoaderFactory emoteAnimationLoaderFactory, |
| | 33 | | IEmotesCatalogService emotesCatalogService, |
| | 34 | | IWearablesCatalogService wearablesCatalogService, |
| | 35 | | ICatalyst catalyst) |
| | 36 | | { |
| 425 | 37 | | this.emoteAnimationLoaderFactory = emoteAnimationLoaderFactory; |
| 425 | 38 | | this.emotesCatalogService = emotesCatalogService; |
| 425 | 39 | | this.wearablesCatalogService = wearablesCatalogService; |
| 425 | 40 | | this.catalyst = catalyst; |
| 425 | 41 | | } |
| | 42 | |
|
| | 43 | | public void Initialize() |
| | 44 | | { |
| 425 | 45 | | animationsModelsContainer = new GameObject("_EmotesAnimationContainer") |
| | 46 | | { |
| | 47 | | transform = |
| | 48 | | { |
| | 49 | | position = EnvironmentSettings.MORDOR, |
| | 50 | | }, |
| | 51 | | }; |
| 425 | 52 | | } |
| | 53 | |
|
| | 54 | | public async UniTask InitializeAsync(CancellationToken cancellationToken) |
| | 55 | | { |
| 476 | 56 | | EmbeddedEmotesSO embedEmotes = await emotesCatalogService.GetEmbeddedEmotes(); |
| | 57 | |
|
| | 58 | | // early return for not configured emotesCatalogService substitutes in legacy test base |
| 471 | 59 | | if (embedEmotes == null) return; |
| | 60 | |
|
| 1368 | 61 | | foreach (EmbeddedEmote embeddedEmote in embedEmotes.GetEmbeddedEmotes()) |
| | 62 | | { |
| 363 | 63 | | if (embeddedEmote.maleAnimation != null) |
| 363 | 64 | | SetupEmbeddedClip(embeddedEmote, embeddedEmote.maleAnimation, MALE); |
| | 65 | |
|
| 363 | 66 | | if (embeddedEmote.femaleAnimation != null) |
| 363 | 67 | | SetupEmbeddedClip(embeddedEmote, embeddedEmote.femaleAnimation, FEMALE); |
| | 68 | | } |
| | 69 | |
|
| 664 | 70 | | foreach (ExtendedEmote embeddedEmote in embedEmotes.GetExtendedEmbeddedEmotes()) |
| | 71 | | { |
| 11 | 72 | | SetupEmbeddedExtendedEmote(embeddedEmote); |
| | 73 | | } |
| | 74 | |
|
| 321 | 75 | | wearablesCatalogService.AddEmbeddedWearablesToCatalog(embedEmotes.GetAllEmotes()); |
| 396 | 76 | | } |
| | 77 | |
|
| | 78 | | private void SetupEmbeddedExtendedEmote(ExtendedEmote embeddedEmote) |
| | 79 | | { |
| 11 | 80 | | extendedEmotes.Add(new EmoteBodyId(MALE, embeddedEmote.id), embeddedEmote); |
| 11 | 81 | | extendedEmotes.Add(new EmoteBodyId(FEMALE, embeddedEmote.id), embeddedEmote); |
| 11 | 82 | | } |
| | 83 | |
|
| | 84 | | private void SetupEmbeddedClip(EmbeddedEmote embeddedEmote, AnimationClip clip, string urnPrefix) |
| | 85 | | { |
| 726 | 86 | | clip.name = embeddedEmote.id; |
| 726 | 87 | | var clipData = new EmoteClipData(clip, embeddedEmote.emoteDataV0?.loop ?? false); |
| 726 | 88 | | embeddedEmotes.Add(new EmoteBodyId(urnPrefix, embeddedEmote.id), new EmbedEmoteReference(embeddedEmote, clip |
| 726 | 89 | | } |
| | 90 | |
|
| | 91 | | public async UniTask<IEmoteReference> RequestEmote(EmoteBodyId emoteBodyId, CancellationToken cancellationToken |
| | 92 | | { |
| 0 | 93 | | if (embeddedEmotes.TryGetValue(emoteBodyId, out var value)) |
| 0 | 94 | | return value; |
| | 95 | |
|
| 0 | 96 | | if (extendedEmotes.TryGetValue(emoteBodyId, out var extendedEmote)) |
| | 97 | | { |
| 0 | 98 | | IEmoteAnimationLoader loader = emoteAnimationLoaderFactory.Get(); |
| 0 | 99 | | await loader.LoadLocalEmote(animationsModelsContainer, extendedEmote, cancellationToken); |
| 0 | 100 | | return new NftEmoteReference(extendedEmote, loader, extendedEmote.emoteDataV0?.loop ?? false); |
| | 101 | | } |
| | 102 | |
|
| | 103 | | try |
| | 104 | | { |
| 0 | 105 | | var emote = await FetchEmote(emoteBodyId, cancellationToken, contentUrl); |
| | 106 | |
|
| 0 | 107 | | if (emote == null) |
| | 108 | | { |
| 0 | 109 | | Debug.LogError($"Unexpected null emote when requesting {emoteBodyId}"); |
| 0 | 110 | | return null; |
| | 111 | | } |
| | 112 | |
|
| | 113 | | // Loader disposal is being handled by the emote reference |
| 0 | 114 | | IEmoteAnimationLoader animationLoader = emoteAnimationLoaderFactory.Get(); |
| 0 | 115 | | await animationLoader.LoadRemoteEmote(animationsModelsContainer, emote, emoteBodyId.BodyShapeId, cancell |
| | 116 | |
|
| 0 | 117 | | if (animationLoader.mainClip == null) |
| 0 | 118 | | Debug.LogError("Emote animation failed to load for emote " + emote.id); |
| | 119 | |
|
| 0 | 120 | | bool loop = emote is EmoteItem newEmoteItem ? newEmoteItem.data.loop : emote.emoteDataV0?.loop ?? false; |
| 0 | 121 | | IEmoteReference emoteReference = new NftEmoteReference(emote, animationLoader, loop); |
| 0 | 122 | | return emoteReference; |
| | 123 | | } |
| | 124 | | catch (Exception e) |
| | 125 | | { |
| 0 | 126 | | Debug.LogException(e); |
| 0 | 127 | | return null; |
| | 128 | | } |
| 0 | 129 | | } |
| | 130 | |
|
| | 131 | | private UniTask<WearableItem> FetchEmote(EmoteBodyId emoteBodyId, CancellationToken ct, string contentUrl = null |
| | 132 | | { |
| 0 | 133 | | string emoteId = emoteBodyId.EmoteId; |
| | 134 | |
|
| 0 | 135 | | if (!SceneEmoteHelper.IsSceneEmote(emoteId)) |
| | 136 | | { |
| 0 | 137 | | return emotesCatalogService.RequestEmoteAsync(emoteId, ct); |
| | 138 | | } |
| | 139 | |
|
| 0 | 140 | | if (!emoteId.StartsWith("urn")) |
| | 141 | | { |
| 0 | 142 | | return emotesCatalogService.RequestEmoteFromBuilderAsync(emoteId, ct); |
| | 143 | | } |
| | 144 | |
|
| 0 | 145 | | WearableItem emoteItem = null; |
| 0 | 146 | | if (SceneEmoteHelper.TryGetDataFromEmoteId(emoteId, out string emoteHash, out bool loop)) |
| | 147 | | { |
| 0 | 148 | | if (contentUrl != null) |
| 0 | 149 | | emoteItem = new EmoteItem(emoteBodyId.BodyShapeId, emoteId, emoteHash, contentUrl, loop, false); |
| | 150 | | else |
| 0 | 151 | | emoteItem = new EmoteItem(emoteBodyId.BodyShapeId, emoteId, emoteHash, catalyst.contentUrl, loop); |
| | 152 | | } |
| | 153 | |
|
| 0 | 154 | | return new UniTask<WearableItem>(emoteItem); |
| | 155 | | } |
| | 156 | |
|
| | 157 | | public void Dispose() |
| | 158 | | { |
| 425 | 159 | | Object.Destroy(animationsModelsContainer); |
| 425 | 160 | | } |
| | 161 | | } |
| | 162 | | } |