| | 1 | | using AvatarSystem; |
| | 2 | | using Cysharp.Threading.Tasks; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Providers; |
| | 5 | | using DCLServices.EmotesService; |
| | 6 | | using System; |
| | 7 | | using System.Threading; |
| | 8 | | using UnityEngine; |
| | 9 | | using Object = UnityEngine.Object; |
| | 10 | |
|
| | 11 | | namespace DCL.Emotes |
| | 12 | | { |
| | 13 | | public class EmoteAnimationLoader : IEmoteAnimationLoader |
| | 14 | | { |
| | 15 | | private const string EMOTE_AUDIO_SOURCE = "EmoteAudioSource"; |
| | 16 | | private readonly IWearableRetriever retriever; |
| | 17 | | private readonly AddressableResourceProvider resourceProvider; |
| 0 | 18 | | public AnimationClip mainClip { get; private set; } |
| 0 | 19 | | public GameObject container { get; private set; } |
| 0 | 20 | | public AudioSource audioSource { get; private set; } |
| | 21 | |
|
| | 22 | | private AudioClip audioClip; |
| | 23 | | private AssetPromise_AudioClip audioClipPromise; |
| | 24 | | private readonly EmoteVolumeHandler emoteVolumeHandler; |
| | 25 | |
|
| 0 | 26 | | public EmoteAnimationLoader(IWearableRetriever retriever, AddressableResourceProvider resourceProvider, EmoteVol |
| | 27 | | { |
| 0 | 28 | | this.emoteVolumeHandler = emoteVolumeHandler; |
| 0 | 29 | | this.retriever = retriever; |
| 0 | 30 | | this.resourceProvider = resourceProvider; |
| 0 | 31 | | } |
| | 32 | |
|
| | 33 | | public async UniTask LoadRemoteEmote(GameObject targetContainer, WearableItem emote, string bodyShapeId, Cancell |
| | 34 | | { |
| 0 | 35 | | if (targetContainer == null) |
| 0 | 36 | | throw new NullReferenceException("Container cannot be null"); |
| | 37 | |
|
| 0 | 38 | | if (emote == null) |
| 0 | 39 | | throw new NullReferenceException("Emote cannot be null"); |
| | 40 | |
|
| 0 | 41 | | if (string.IsNullOrEmpty(bodyShapeId)) |
| 0 | 42 | | throw new NullReferenceException("bodyShapeId cannot be null or empty"); |
| | 43 | |
|
| 0 | 44 | | ct.ThrowIfCancellationRequested(); |
| | 45 | |
|
| 0 | 46 | | Rendereable rendereable = await retriever.Retrieve(targetContainer, emote, bodyShapeId, ct); |
| | 47 | |
|
| 0 | 48 | | foreach (Renderer renderer in rendereable.renderers) |
| 0 | 49 | | renderer.enabled = false; |
| | 50 | |
|
| 0 | 51 | | GameObject emoteInstance = rendereable.container; |
| | 52 | |
|
| 0 | 53 | | SetupEmote(emoteInstance, emote.id); |
| | 54 | |
|
| 0 | 55 | | var contentProvider = emote.GetContentProvider(bodyShapeId); |
| | 56 | |
|
| 0 | 57 | | foreach (var contentMap in contentProvider.contents) |
| | 58 | | { |
| 0 | 59 | | if (!IsValidAudioClip(contentMap.file)) continue; |
| | 60 | |
|
| 0 | 61 | | AudioClip audioClip = null; |
| | 62 | | try |
| | 63 | | { |
| 0 | 64 | | audioClip = await AsyncLoadAudioClip(contentMap.file, contentProvider); |
| 0 | 65 | | } |
| | 66 | | catch (Exception e) |
| | 67 | | { |
| 0 | 68 | | Debug.LogError(e); |
| 0 | 69 | | } |
| | 70 | |
|
| 0 | 71 | | if (audioClip != null) |
| 0 | 72 | | await SetupAudioClip(audioClip, emoteInstance, ct); |
| | 73 | |
|
| | 74 | | // we only support one audio clip |
| 0 | 75 | | break; |
| | 76 | | } |
| | 77 | |
|
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | public async UniTask LoadLocalEmote(GameObject targetContainer, ExtendedEmote embeddedEmote, CancellationToken c |
| | 81 | | { |
| 0 | 82 | | GameObject emoteInstance = Object.Instantiate(embeddedEmote.propPrefab, targetContainer.transform, false); |
| 0 | 83 | | var renderers = emoteInstance.GetComponentsInChildren<Renderer>(true); |
| 0 | 84 | | foreach (Renderer renderer in renderers) |
| 0 | 85 | | renderer.enabled = false; |
| | 86 | |
|
| 0 | 87 | | SetupEmote(emoteInstance, embeddedEmote.id); |
| 0 | 88 | | await SetupAudioClip(embeddedEmote.clip, emoteInstance, ct); |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | private void SetupEmote(GameObject emoteInstance, string emoteId) |
| | 92 | | { |
| 0 | 93 | | var animation = emoteInstance.GetComponentInChildren<Animation>(); |
| | 94 | |
|
| 0 | 95 | | if (animation == null) |
| | 96 | | { |
| 0 | 97 | | Debug.LogError("Animation component not found in the container for emote " + emoteId); |
| 0 | 98 | | return; |
| | 99 | | } |
| | 100 | |
|
| 0 | 101 | | mainClip = animation.clip; |
| | 102 | |
|
| 0 | 103 | | if (animation.GetClipCount() > 1) |
| | 104 | | { |
| 0 | 105 | | this.container = emoteInstance; |
| | 106 | |
|
| | 107 | | // we cant use the animation order so we use the naming convention at /creator/emotes/props-and-sounds/ |
| 0 | 108 | | foreach (AnimationState state in animation) |
| | 109 | | { |
| 0 | 110 | | AnimationClip clip = state.clip; |
| | 111 | |
|
| | 112 | | // Replace the main clip with the one that's correctly named |
| 0 | 113 | | if (clip.name.Contains("_avatar", StringComparison.OrdinalIgnoreCase) || clip.name == emoteId) |
| 0 | 114 | | mainClip = clip; |
| | 115 | |
|
| | 116 | | // There's a bug with the legacy animation where animations start ahead of time the first time |
| | 117 | | // our workaround is to play every animation while we load the audio clip and then disable the anima |
| 0 | 118 | | animation.Play(clip.name); |
| | 119 | | } |
| | 120 | | } |
| | 121 | |
|
| 0 | 122 | | if (mainClip == null) |
| | 123 | | { |
| 0 | 124 | | Debug.LogError("AnimationClip not found in the container for emote " + emoteId); |
| 0 | 125 | | return; |
| | 126 | | } |
| | 127 | |
|
| | 128 | | // Clip names should be unique because of the Legacy Animation string based usage. |
| | 129 | | // In rare cases some animations might use the same GLB, thus causing this clip to be used by 2 different em |
| | 130 | | // so we avoid renaming the clip again witch can cause problems as we use the clip names internally |
| 0 | 131 | | if (!mainClip.name.Contains("urn")) |
| 0 | 132 | | mainClip.name = emoteId; |
| | 133 | |
|
| 0 | 134 | | animation.Stop(); |
| 0 | 135 | | animation.enabled = false; |
| 0 | 136 | | } |
| | 137 | |
|
| | 138 | | private async UniTask SetupAudioClip(AudioClip clip, GameObject audioSourceParent, CancellationToken ct) |
| | 139 | | { |
| 0 | 140 | | audioClip = clip; |
| 0 | 141 | | audioSource = await resourceProvider.Instantiate<AudioSource>(EMOTE_AUDIO_SOURCE, "EmoteAudioSource", |
| | 142 | | cancellationToken: ct); |
| 0 | 143 | | audioSource.clip = audioClip; |
| 0 | 144 | | audioSource.transform.SetParent(audioSourceParent.transform, false); |
| 0 | 145 | | audioSource.transform.ResetLocalTRS(); |
| | 146 | |
|
| 0 | 147 | | emoteVolumeHandler.AddAudioSource(audioSource); |
| 0 | 148 | | } |
| | 149 | |
|
| | 150 | | private bool IsValidAudioClip(string fileName) => |
| 0 | 151 | | fileName.EndsWith(".ogg") || fileName.EndsWith(".mp3"); |
| | 152 | |
|
| | 153 | | private async UniTask<AudioClip> AsyncLoadAudioClip(string file, ContentProvider contentProvider) |
| | 154 | | { |
| 0 | 155 | | audioClipPromise = new AssetPromise_AudioClip(file, contentProvider); |
| 0 | 156 | | AssetPromiseKeeper_AudioClip.i.Keep(audioClipPromise); |
| 0 | 157 | | await audioClipPromise; |
| 0 | 158 | | return audioClipPromise.asset.audioClip; |
| 0 | 159 | | } |
| | 160 | |
|
| | 161 | | public void Dispose() |
| | 162 | | { |
| 0 | 163 | | emoteVolumeHandler.RemoveAudioSource(audioSource); |
| 0 | 164 | | AssetPromiseKeeper_AudioClip.i.Forget(audioClipPromise); |
| 0 | 165 | | retriever?.Dispose(); |
| 0 | 166 | | } |
| | 167 | | } |
| | 168 | | } |