| | 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 | |
|
| 4 | 21 | | internal Dictionary<(string bodyshapeId, string emoteId), IEmoteAnimationLoader> loaders = new Dictionary<(strin |
| | 22 | |
|
| 4 | 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 |
| 4 | 28 | | public EmoteAnimationsTracker(DataStore_Emotes dataStore, EmoteAnimationLoaderFactory emoteAnimationLoaderFactor |
| | 29 | | { |
| 4 | 30 | | animationsModelsContainer = new GameObject("_EmoteAnimationsHolder"); |
| 4 | 31 | | animationsModelsContainer.transform.position = EnvironmentSettings.MORDOR; |
| 4 | 32 | | this.dataStore = dataStore; |
| 4 | 33 | | this.emoteAnimationLoaderFactory = emoteAnimationLoaderFactory; |
| 4 | 34 | | this.wearableItemResolver = wearableItemResolver; |
| 4 | 35 | | this.emotesCatalogService = emotesCatalogService; |
| 4 | 36 | | this.dataStore.animations.Clear(); |
| | 37 | |
|
| 4 | 38 | | InitializeEmbeddedEmotes(); |
| 4 | 39 | | InitializeEmotes(this.dataStore.emotesOnUse.GetAllRefCounts()); |
| | 40 | |
|
| 4 | 41 | | this.dataStore.emotesOnUse.OnRefCountUpdated += OnRefCountUpdated; |
| 4 | 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 | |
|
| 4 | 51 | | EmbeddedEmotesSO embeddedEmotes = Resources.Load<EmbeddedEmotesSO>("EmbeddedEmotes"); |
| | 52 | |
|
| 160 | 53 | | foreach (EmbeddedEmote embeddedEmote in embeddedEmotes.emotes) |
| | 54 | | { |
| 76 | 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. |
| 76 | 59 | | embeddedEmote.maleAnimation.name = embeddedEmote.id; |
| 76 | 60 | | dataStore.emotesOnUse.SetRefCount((MALE, embeddedEmote.id), 5000); |
| 76 | 61 | | var clipData = new EmoteClipData(embeddedEmote.maleAnimation); |
| 76 | 62 | | dataStore.animations.Add((MALE, embeddedEmote.id), clipData); |
| 76 | 63 | | loaders.Add((MALE, embeddedEmote.id), emoteAnimationLoaderFactory.Get()); |
| | 64 | | } |
| | 65 | |
|
| 76 | 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. |
| 76 | 70 | | embeddedEmote.femaleAnimation.name = embeddedEmote.id; |
| 76 | 71 | | dataStore.emotesOnUse.SetRefCount((FEMALE, embeddedEmote.id), 5000); |
| 76 | 72 | | var emoteClipData = new EmoteClipData(embeddedEmote.femaleAnimation); |
| 76 | 73 | | dataStore.animations.Add((FEMALE, embeddedEmote.id), emoteClipData); |
| 76 | 74 | | loaders.Add((FEMALE, embeddedEmote.id), emoteAnimationLoaderFactory.Get()); |
| | 75 | | } |
| | 76 | | } |
| 4 | 77 | | CatalogController.i.EmbedWearables(embeddedEmotes.emotes); |
| 4 | 78 | | } |
| | 79 | |
|
| | 80 | | private void OnRefCountUpdated((string bodyshapeId, string emoteId) value, int refCount) |
| | 81 | | { |
| 3 | 82 | | if (refCount > 0) |
| 2 | 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 | | { |
| 312 | 90 | | foreach (KeyValuePair<(string bodyshapeId, string emoteId), int> keyValuePair in refCounts) |
| | 91 | | { |
| 152 | 92 | | LoadEmote(keyValuePair.Key.bodyshapeId, keyValuePair.Key.emoteId, cts.Token); |
| | 93 | | } |
| 4 | 94 | | } |
| | 95 | |
|
| | 96 | | private async UniTaskVoid LoadEmote(string bodyShapeId, string emoteId, CancellationToken ct) |
| | 97 | | { |
| 154 | 98 | | ct.ThrowIfCancellationRequested(); |
| | 99 | |
|
| 154 | 100 | | if (loaders.ContainsKey((bodyShapeId, emoteId))) |
| 153 | 101 | | return; |
| | 102 | |
|
| | 103 | | try |
| | 104 | | { |
| 1 | 105 | | var emote = await emotesCatalogService.RequestEmoteAsync(emoteId, ct) ?? |
| | 106 | | await wearableItemResolver.Resolve(emoteId, ct); |
| | 107 | |
|
| 1 | 108 | | IEmoteAnimationLoader animationLoader = emoteAnimationLoaderFactory.Get(); |
| 1 | 109 | | loaders.Add((bodyShapeId, emoteId), animationLoader); |
| 1 | 110 | | await animationLoader.LoadEmote(animationsModelsContainer, emote, bodyShapeId, ct); |
| | 111 | |
|
| 1 | 112 | | var emoteClipData = new EmoteClipData(animationLoader.animation, emote.emoteDataV0); |
| | 113 | |
|
| 1 | 114 | | dataStore.animations.Add((bodyShapeId, emoteId), emoteClipData); |
| 1 | 115 | | } |
| | 116 | | catch (Exception e) |
| | 117 | | { |
| 0 | 118 | | loaders.Remove((bodyShapeId, emoteId)); |
| 0 | 119 | | ExceptionDispatchInfo.Capture(e).Throw(); |
| 0 | 120 | | throw; |
| | 121 | | } |
| 154 | 122 | | } |
| | 123 | |
|
| | 124 | | private void UnloadEmote(string bodyShapeId, string emoteId) |
| | 125 | | { |
| 1 | 126 | | if (!loaders.TryGetValue((bodyShapeId, emoteId), out IEmoteAnimationLoader loader)) |
| 0 | 127 | | return; |
| | 128 | |
|
| 1 | 129 | | dataStore.animations.Remove((bodyShapeId, emoteId)); |
| 1 | 130 | | loaders.Remove((bodyShapeId, emoteId)); |
| 1 | 131 | | loader?.Dispose(); |
| 1 | 132 | | } |
| | 133 | |
|
| | 134 | | public void Dispose() |
| | 135 | | { |
| 0 | 136 | | dataStore.emotesOnUse.OnRefCountUpdated -= OnRefCountUpdated; |
| 0 | 137 | | (string bodyshapeId, string emoteId)[] keys = loaders.Keys.ToArray(); |
| 0 | 138 | | foreach ((string bodyshapeId, string emoteId) in keys) |
| | 139 | | { |
| 0 | 140 | | UnloadEmote(bodyshapeId, emoteId); |
| | 141 | | } |
| 0 | 142 | | loaders.Clear(); |
| 0 | 143 | | cts.Cancel(); |
| 0 | 144 | | Object.Destroy(animationsModelsContainer); |
| 0 | 145 | | } |
| | 146 | | } |
| | 147 | | } |