| | 1 | | using AvatarAssets; |
| | 2 | | using AvatarSystem; |
| | 3 | | using Cysharp.Threading.Tasks; |
| | 4 | | using DCL.Emotes; |
| | 5 | | using DCL.Tasks; |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Threading; |
| | 9 | | using UnityEngine; |
| | 10 | |
|
| | 11 | | namespace DCL |
| | 12 | | { |
| | 13 | | public class AvatarSceneEmoteHandler |
| | 14 | | { |
| | 15 | | private readonly IAvatarEmotesController emotesController; |
| | 16 | | private readonly IEmotesService emotesService; |
| | 17 | | private readonly HashSet<string> equippedEmotes; |
| | 18 | |
|
| | 19 | | private long lamportTimestamp; |
| | 20 | | private CancellationTokenSource cts; |
| | 21 | |
|
| 310 | 22 | | public AvatarSceneEmoteHandler(IAvatarEmotesController emotesController, IEmotesService emotesService) |
| | 23 | | { |
| 310 | 24 | | this.emotesController = emotesController; |
| 310 | 25 | | this.emotesService = emotesService; |
| 310 | 26 | | equippedEmotes = new HashSet<string>(); |
| 310 | 27 | | } |
| | 28 | |
|
| | 29 | | public bool IsSceneEmote(string emoteId) => |
| 0 | 30 | | SceneEmoteHelper.IsSceneEmote(emoteId); |
| | 31 | |
|
| | 32 | | public void SetExpressionLamportTimestamp(long timestamp) |
| | 33 | | { |
| 0 | 34 | | lamportTimestamp = timestamp; |
| 0 | 35 | | } |
| | 36 | |
|
| | 37 | | public async UniTask LoadAndPlayEmote(string bodyShapeId, string emoteId) |
| | 38 | | { |
| 0 | 39 | | long timestamp = lamportTimestamp; |
| 0 | 40 | | cts ??= new CancellationTokenSource(); |
| | 41 | |
|
| 0 | 42 | | var emoteKey = new EmoteBodyId(bodyShapeId, emoteId); |
| | 43 | |
|
| 0 | 44 | | if (equippedEmotes.Contains(emoteId)) |
| | 45 | | { |
| 0 | 46 | | TriggerEmote(emoteId, timestamp); |
| 0 | 47 | | return; |
| | 48 | | } |
| | 49 | |
|
| | 50 | | try |
| | 51 | | { |
| 0 | 52 | | var loadedEmote = await emotesService.RequestEmote(emoteKey, cts.Token); |
| | 53 | |
|
| 0 | 54 | | emotesController.EquipEmote(emoteId, loadedEmote); |
| 0 | 55 | | equippedEmotes.Add(emoteId); |
| | 56 | |
|
| 0 | 57 | | TriggerEmote(emoteId, timestamp); |
| 0 | 58 | | } |
| 0 | 59 | | catch (OperationCanceledException) { } |
| 0 | 60 | | catch (Exception e) { Debug.LogException(e); } |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | private void TriggerEmote(string emoteId, long timestamp) |
| | 64 | | { |
| | 65 | | //avoid playing emote if timestamp has change, |
| | 66 | | //meaning a new emote was trigger while this one was loading |
| 0 | 67 | | if (timestamp == lamportTimestamp) |
| 0 | 68 | | emotesController.PlayEmote(emoteId, lamportTimestamp); |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | public void CleanUp() |
| | 72 | | { |
| 310 | 73 | | cts?.SafeCancelAndDispose(); |
| 310 | 74 | | cts = null; |
| | 75 | |
|
| 620 | 76 | | foreach (string emoteId in equippedEmotes) |
| 0 | 77 | | emotesController?.UnEquipEmote(emoteId); |
| | 78 | |
|
| 310 | 79 | | equippedEmotes.Clear(); |
| 310 | 80 | | } |
| | 81 | | } |
| | 82 | | } |