|
| 1 | +using Arch.Core; |
| 2 | +using Arch.System; |
| 3 | +using Arch.SystemGroups; |
| 4 | +using Arch.SystemGroups.DefaultSystemGroups; |
| 5 | +using CommunicationData.URLHelpers; |
| 6 | +using DCL.AvatarRendering.Loading.Components; |
| 7 | +using DCL.Diagnostics; |
| 8 | +using DCL.SDKComponents.AudioSources; |
| 9 | +using ECS.Abstract; |
| 10 | +using ECS.Prioritization.Components; |
| 11 | +using ECS.StreamableLoading.GLTF; |
| 12 | +using System; |
| 13 | + |
| 14 | +using StreamableResult = ECS.StreamableLoading.Common.Components.StreamableLoadingResult<DCL.AvatarRendering.Emotes.EmotesResolution>; |
| 15 | +using GltfPromise = ECS.StreamableLoading.Common.AssetPromise<ECS.StreamableLoading.GLTF.GLTFData, ECS.StreamableLoading.GLTF.GetGLTFIntention>; |
| 16 | + |
| 17 | +namespace DCL.AvatarRendering.Emotes.Systems |
| 18 | +{ |
| 19 | + [UpdateInGroup(typeof(PresentationSystemGroup))] |
| 20 | + [UpdateAfter(typeof(FinalizeEmoteLoadingSystem))] |
| 21 | + [LogCategory(ReportCategory.EMOTE)] |
| 22 | + public partial class ResolveBuilderEmotePromisesSystem : BaseUnityLoopSystem |
| 23 | + { |
| 24 | + private static readonly BodyShape[] ALL_BODYSHAPES = { BodyShape.MALE, BodyShape.FEMALE }; |
| 25 | + |
| 26 | + private readonly IEmoteStorage emoteStorage; |
| 27 | + internal readonly IURLBuilder urlBuilder = new URLBuilder(); |
| 28 | + |
| 29 | + public ResolveBuilderEmotePromisesSystem( |
| 30 | + World world, |
| 31 | + IEmoteStorage emoteStorage |
| 32 | + ) : base(world) |
| 33 | + { |
| 34 | + this.emoteStorage = emoteStorage; |
| 35 | + } |
| 36 | + |
| 37 | + protected override void Update(float t) |
| 38 | + { |
| 39 | + ResolveBuilderEmotePromiseQuery(World); |
| 40 | + } |
| 41 | + |
| 42 | + [Query] |
| 43 | + [None(typeof(StreamableResult))] |
| 44 | + private void ResolveBuilderEmotePromise(Entity entity, ref GetOwnedEmotesFromRealmIntention intention, ref IPartitionComponent partitionComponent) |
| 45 | + { |
| 46 | + if (intention.CancellationTokenSource.IsCancellationRequested) |
| 47 | + { |
| 48 | + World!.Add(entity, new StreamableResult(GetReportCategory(), new OperationCanceledException("Emotes request cancelled"))); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + // Only create promises for builder collections |
| 53 | + if (intention is { NeedsBuilderAPISigning: false } || intention.Result.List is not { Count: > 0 }) |
| 54 | + return; |
| 55 | + |
| 56 | + bool allEmotesProcessed = true; |
| 57 | + |
| 58 | + foreach (IEmote emote in intention.Result.List) |
| 59 | + { |
| 60 | + if (TryCreateBuilderEmoteAssetPromises(emote, partitionComponent)) |
| 61 | + allEmotesProcessed = false; |
| 62 | + } |
| 63 | + |
| 64 | + if (allEmotesProcessed) |
| 65 | + World!.Add(entity, new StreamableResult(new EmotesResolution(intention.Result, intention.TotalAmount))); |
| 66 | + } |
| 67 | + |
| 68 | + private bool TryCreateBuilderEmoteAssetPromises(in IEmote emote, in IPartitionComponent partitionComponent) |
| 69 | + { |
| 70 | + if (string.IsNullOrEmpty(emote.DTO.ContentDownloadUrl)) |
| 71 | + return false; |
| 72 | + |
| 73 | + // Check if emote already has assets loaded or is loading |
| 74 | + if (emote.IsLoading) |
| 75 | + return true; // Still processing, not done yet |
| 76 | + |
| 77 | + // Check if we already have this emote in storage with assets |
| 78 | + if (emoteStorage.TryGetElement(emote.GetUrn(), out IEmote existingEmote)) |
| 79 | + { |
| 80 | + // For unisex emotes with same clip, check if either bodyshape has assets |
| 81 | + if (existingEmote.IsUnisex() && existingEmote.HasSameClipForAllGenders()) |
| 82 | + { |
| 83 | + if (existingEmote.AssetResults[BodyShape.MALE] != null || existingEmote.AssetResults[BodyShape.FEMALE] != null) |
| 84 | + return false; // Already processed |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + // For non-unisex emotes, check both bodyshapes |
| 89 | + if (existingEmote.AssetResults[BodyShape.MALE] != null && existingEmote.AssetResults[BodyShape.FEMALE] != null) |
| 90 | + return false; // Already processed |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + bool foundGlb = false; |
| 95 | + bool stillProcessing = false; |
| 96 | + |
| 97 | + BodyShape? targetBodyShape = null; |
| 98 | + if (!emote.IsUnisex()) |
| 99 | + targetBodyShape = BodyShape.FromStringSafe(emote.DTO.Metadata.AbstractData.representations[0].bodyShapes[0]); |
| 100 | + |
| 101 | + // The resolution of these promises will be finalized by FinalizeEmoteLoadingSystem |
| 102 | + foreach (var content in emote.DTO.content) |
| 103 | + { |
| 104 | + if (content.file.EndsWith(".glb")) |
| 105 | + { |
| 106 | + for (int i = 0; i < ALL_BODYSHAPES.Length; i++) |
| 107 | + { |
| 108 | + BodyShape bodyShape = ALL_BODYSHAPES[i]; |
| 109 | + if (!emote.IsUnisex() && !bodyShape.Equals(targetBodyShape!)) |
| 110 | + continue; |
| 111 | + |
| 112 | + // Skip if this bodyshape already has an asset result |
| 113 | + if (emote.AssetResults[bodyShape] != null) |
| 114 | + continue; |
| 115 | + |
| 116 | + var gltfPromise = GltfPromise.Create(World!, GetGLTFIntention.Create(content.file, content.hash), partitionComponent); |
| 117 | + World!.Create(gltfPromise, emote, bodyShape); |
| 118 | + emote.UpdateLoadingStatus(true); |
| 119 | + foundGlb = true; |
| 120 | + stillProcessing = true; |
| 121 | + } |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + // Supported audio format in emotes: https://docs.decentraland.org/creator/emotes/props-and-sounds/#add-audio-to-the-emotes |
| 126 | + if (content.file.EndsWith(".mp3") || content.file.EndsWith(".ogg")) |
| 127 | + { |
| 128 | + var audioType = content.file.ToAudioType(); |
| 129 | + urlBuilder.Clear(); |
| 130 | + urlBuilder.AppendDomain(URLDomain.FromString(emote.DTO.ContentDownloadUrl)).AppendPath(new URLPath(content.hash)); |
| 131 | + URLAddress url = urlBuilder.Build(); |
| 132 | + |
| 133 | + for (int i = 0; i < ALL_BODYSHAPES.Length; i++) |
| 134 | + { |
| 135 | + BodyShape bodyShape = ALL_BODYSHAPES[i]; |
| 136 | + if (!emote.IsUnisex() && !bodyShape.Equals(targetBodyShape)) |
| 137 | + continue; |
| 138 | + |
| 139 | + var audioPromise = AudioUtils.CreateAudioClipPromise(World!, url.Value, audioType, partitionComponent); |
| 140 | + World!.Create(audioPromise, emote, bodyShape); |
| 141 | + } |
| 142 | + |
| 143 | + if (foundGlb) break; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return stillProcessing; |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments