| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Runtime.ExceptionServices; |
| | 5 | | using System.Threading; |
| | 6 | | using AvatarSystem; |
| | 7 | | using Cysharp.Threading.Tasks; |
| | 8 | | using DCL.Configuration; |
| | 9 | | using UnityEngine; |
| | 10 | | using Object = UnityEngine.Object; |
| | 11 | |
|
| | 12 | | namespace DCL.Emotes |
| | 13 | | { |
| | 14 | | public class EmoteAnimationsTracker : IDisposable |
| | 15 | | { |
| | 16 | | internal readonly DataStore_Emotes dataStore; |
| | 17 | | internal readonly EmoteAnimationLoaderFactory emoteAnimationLoaderFactory; |
| | 18 | | internal readonly IWearableItemResolver wearableItemResolver; |
| | 19 | | private readonly IEmotesCatalogService emotesCatalogService; |
| | 20 | |
|
| 3 | 21 | | internal Dictionary<(string bodyshapeId, string emoteId), IEmoteAnimationLoader> loaders = new Dictionary<(strin |
| | 22 | |
|
| 3 | 23 | | private CancellationTokenSource cts = new CancellationTokenSource(); |
| | 24 | |
|
| | 25 | | internal GameObject animationsModelsContainer; |
| | 26 | |
|
| | 27 | | // Alex: While we are supporting the old Emotes flow, we need the wearableItemResolver |
| 3 | 28 | | public EmoteAnimationsTracker(DataStore_Emotes dataStore, EmoteAnimationLoaderFactory emoteAnimationLoaderFactor |
| | 29 | | { |
| 3 | 30 | | animationsModelsContainer = new GameObject("_EmoteAnimationsHolder"); |
| 3 | 31 | | animationsModelsContainer.transform.position = EnvironmentSettings.MORDOR; |
| 3 | 32 | | this.dataStore = dataStore; |
| 3 | 33 | | this.emoteAnimationLoaderFactory = emoteAnimationLoaderFactory; |
| 3 | 34 | | this.wearableItemResolver = wearableItemResolver; |
| 3 | 35 | | this.emotesCatalogService = emotesCatalogService; |
| 3 | 36 | | this.dataStore.animations.Clear(); |
| | 37 | |
|
| 3 | 38 | | InitializeEmbeddedEmotes(); |
| 3 | 39 | | InitializeEmotes(this.dataStore.emotesOnUse.GetAllRefCounts()); |
| | 40 | |
|
| 3 | 41 | | this.dataStore.emotesOnUse.OnRefCountUpdated += OnRefCountUpdated; |
| 3 | 42 | | } |
| | 43 | |
|
| | 44 | | private void InitializeEmbeddedEmotes() |
| | 45 | | { |
| | 46 | | //To avoid circular references in assemblies we hardcode this here instead of using WearableLiterals |
| | 47 | | //Embedded Emotes are only temporary until they can be retrieved from the content server |
| | 48 | | const string FEMALE = "urn:decentraland:off-chain:base-avatars:BaseFemale"; |
| | 49 | | const string MALE = "urn:decentraland:off-chain:base-avatars:BaseMale"; |
| | 50 | |
|
| 3 | 51 | | EmbeddedEmotesSO embeddedEmotes = Resources.Load<EmbeddedEmotesSO>("EmbeddedEmotes"); |
| | 52 | |
|
| 120 | 53 | | foreach (EmbeddedEmote embeddedEmote in embeddedEmotes.emotes) |
| | 54 | | { |
| 57 | 55 | | if (embeddedEmote.maleAnimation != null) |
| | 56 | | { |
| | 57 | | //We match the animation id with its name due to performance reasons |
| | 58 | | //Unity's Animation uses the name to play the clips. |
| 57 | 59 | | embeddedEmote.maleAnimation.name = embeddedEmote.id; |
| 57 | 60 | | dataStore.emotesOnUse.SetRefCount((MALE, embeddedEmote.id), 5000); |
| 57 | 61 | | var clipData = new EmoteClipData(embeddedEmote.maleAnimation, embeddedEmote.emoteDataV0); |
| 57 | 62 | | dataStore.animations.Add((MALE, embeddedEmote.id), clipData); |
| 57 | 63 | | loaders.Add((MALE, embeddedEmote.id), emoteAnimationLoaderFactory.Get()); |
| | 64 | | } |
| | 65 | |
|
| 57 | 66 | | if (embeddedEmote.femaleAnimation != null) |
| | 67 | | { |
| | 68 | | //We match the animation id with its name due to performance reasons |
| | 69 | | //Unity's Animation uses the name to play the clips. |
| 57 | 70 | | embeddedEmote.femaleAnimation.name = embeddedEmote.id; |
| 57 | 71 | | dataStore.emotesOnUse.SetRefCount((FEMALE, embeddedEmote.id), 5000); |
| 57 | 72 | | var emoteClipData = new EmoteClipData(embeddedEmote.femaleAnimation, embeddedEmote.emoteDataV0); |
| 57 | 73 | | dataStore.animations.Add((FEMALE, embeddedEmote.id), emoteClipData); |
| 57 | 74 | | loaders.Add((FEMALE, embeddedEmote.id), emoteAnimationLoaderFactory.Get()); |
| | 75 | | } |
| | 76 | | } |
| 3 | 77 | | CatalogController.i.EmbedWearables(embeddedEmotes.emotes); |
| 3 | 78 | | } |
| | 79 | |
|
| | 80 | | private void OnRefCountUpdated((string bodyshapeId, string emoteId) value, int refCount) |
| | 81 | | { |
| 2 | 82 | | if (refCount > 0) |
| 1 | 83 | | LoadEmote(value.bodyshapeId, value.emoteId, cts.Token); |
| | 84 | | else |
| 1 | 85 | | UnloadEmote(value.bodyshapeId, value.emoteId); |
| 1 | 86 | | } |
| | 87 | |
|
| | 88 | | private void InitializeEmotes(IEnumerable<KeyValuePair<(string bodyshapeId, string emoteId), int>> refCounts) |
| | 89 | | { |
| 234 | 90 | | foreach (KeyValuePair<(string bodyshapeId, string emoteId), int> keyValuePair in refCounts) |
| | 91 | | { |
| 114 | 92 | | LoadEmote(keyValuePair.Key.bodyshapeId, keyValuePair.Key.emoteId, cts.Token); |
| | 93 | | } |
| 3 | 94 | | } |
| | 95 | |
|
| | 96 | | private async UniTaskVoid LoadEmote(string bodyShapeId, string emoteId, CancellationToken ct) |
| | 97 | | { |
| 115 | 98 | | ct.ThrowIfCancellationRequested(); |
| | 99 | |
|
| 115 | 100 | | if (loaders.ContainsKey((bodyShapeId, emoteId))) |
| 115 | 101 | | return; |
| | 102 | |
|
| | 103 | | try |
| | 104 | | { |
| 0 | 105 | | var emote = await(emotesCatalogService.RequestEmoteAsync(emoteId, ct)); |
| | 106 | |
|
| 0 | 107 | | IEmoteAnimationLoader animationLoader = emoteAnimationLoaderFactory.Get(); |
| 0 | 108 | | loaders.Add((bodyShapeId, emoteId), animationLoader); |
| 0 | 109 | | await animationLoader.LoadEmote(animationsModelsContainer, emote, bodyShapeId, ct); |
| | 110 | | EmoteClipData emoteClipData; |
| 0 | 111 | | if(emote is EmoteItem newEmoteItem) |
| 0 | 112 | | emoteClipData = new EmoteClipData(animationLoader.animation, newEmoteItem.data.loop); |
| | 113 | | else |
| 0 | 114 | | emoteClipData = new EmoteClipData(animationLoader.animation, emote.emoteDataV0); |
| | 115 | |
|
| 0 | 116 | | dataStore.animations.Add((bodyShapeId, emoteId), emoteClipData); |
| 0 | 117 | | } |
| | 118 | | catch (Exception e) |
| | 119 | | { |
| 0 | 120 | | loaders.Remove((bodyShapeId, emoteId)); |
| 0 | 121 | | ExceptionDispatchInfo.Capture(e).Throw(); |
| 0 | 122 | | throw; |
| | 123 | | } |
| 115 | 124 | | } |
| | 125 | |
|
| | 126 | | private void UnloadEmote(string bodyShapeId, string emoteId) |
| | 127 | | { |
| 1 | 128 | | if (!loaders.TryGetValue((bodyShapeId, emoteId), out IEmoteAnimationLoader loader)) |
| 0 | 129 | | return; |
| | 130 | |
|
| 1 | 131 | | dataStore.animations.Remove((bodyShapeId, emoteId)); |
| 1 | 132 | | loaders.Remove((bodyShapeId, emoteId)); |
| 1 | 133 | | loader?.Dispose(); |
| 1 | 134 | | } |
| | 135 | |
|
| | 136 | | public void Dispose() |
| | 137 | | { |
| 0 | 138 | | dataStore.emotesOnUse.OnRefCountUpdated -= OnRefCountUpdated; |
| 0 | 139 | | (string bodyshapeId, string emoteId)[] keys = loaders.Keys.ToArray(); |
| 0 | 140 | | foreach ((string bodyshapeId, string emoteId) in keys) |
| | 141 | | { |
| 0 | 142 | | UnloadEmote(bodyshapeId, emoteId); |
| | 143 | | } |
| 0 | 144 | | loaders.Clear(); |
| 0 | 145 | | cts.Cancel(); |
| 0 | 146 | | Object.Destroy(animationsModelsContainer); |
| 0 | 147 | | } |
| | 148 | | } |
| | 149 | | } |