-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathCharacterEmoteSystem.cs
More file actions
631 lines (533 loc) · 29.9 KB
/
Copy pathCharacterEmoteSystem.cs
File metadata and controls
631 lines (533 loc) · 29.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
using Arch.Core;
using Arch.System;
using Arch.SystemGroups;
using Arch.SystemGroups.DefaultSystemGroups;
using CommunicationData.URLHelpers;
using DCL.AvatarRendering.AvatarShape;
using DCL.AvatarRendering.AvatarShape.Components;
using DCL.AvatarRendering.AvatarShape.UnityInterface;
using DCL.AvatarRendering.Emotes.Load;
using DCL.AvatarRendering.Loading.Assets;
using DCL.AvatarRendering.Loading.Components;
using DCL.Character.CharacterMotion.Components;
using DCL.Character.Components;
using DCL.CharacterMotion.Components;
using DCL.CharacterMotion.Systems;
using DCL.Multiplayer.Movement;
using DCL.DebugUtilities;
using DCL.Diagnostics;
using DCL.ECSComponents;
using DCL.Multiplayer.Emotes;
using DCL.Profiles;
using ECS.Abstract;
using ECS.LifeCycle.Components;
using ECS.Prioritization.Components;
using ECS.SceneLifeCycle;
using ECS.StreamableLoading.AudioClips;
using ECS.StreamableLoading.Common.Components;
using SceneRunner.Scene;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
using Utility.Animations;
using EmotePromise = ECS.StreamableLoading.Common.AssetPromise<DCL.AvatarRendering.Emotes.EmotesResolution, DCL.AvatarRendering.Emotes.GetEmotesByPointersIntention>;
using SceneEmoteFromRealmPromise = ECS.StreamableLoading.Common.AssetPromise<DCL.AvatarRendering.Emotes.EmotesResolution, DCL.AvatarRendering.Emotes.GetSceneEmoteFromRealmIntention>;
using SceneEmoteFromLocalPromise = ECS.StreamableLoading.Common.AssetPromise<DCL.AvatarRendering.Emotes.EmotesResolution, DCL.AvatarRendering.Emotes.GetSceneEmoteFromLocalSceneIntention>;
namespace DCL.AvatarRendering.Emotes.Play
{
[LogCategory(ReportCategory.EMOTE)]
[UpdateInGroup(typeof(PresentationSystemGroup))]
[UpdateAfter(typeof(AvatarGroup))]
[UpdateAfter(typeof(RemoteEmotesSystem))]
[UpdateAfter(typeof(LoadEmotesByPointersSystem))]
[UpdateBefore(typeof(ChangeCharacterPositionGroup))]
public partial class CharacterEmoteSystem : BaseUnityLoopSystem
{
private static readonly string SCENE_EMOTE_PREFIX_WITH_COLON = GetSceneEmoteFromRealmIntention.SCENE_EMOTE_PREFIX + ":";
// todo: use this to add nice Debug UI to trigger any emote?
private readonly IDebugContainerBuilder debugContainerBuilder;
private readonly IScenesCache scenesCache;
private readonly IEmoteStorage emoteStorage;
private readonly EmotePlayer emotePlayer;
private readonly IEmotesMessageBus messageBus;
private readonly URN[] loadEmoteBuffer = new URN[1];
private readonly bool localSceneDevelopment;
public CharacterEmoteSystem(
World world,
IEmoteStorage emoteStorage,
IEmotesMessageBus messageBus,
EmotePlayer emotePlayer,
IDebugContainerBuilder debugContainerBuilder,
bool localSceneDevelopment,
IScenesCache scenesCache) : base(world)
{
this.messageBus = messageBus;
this.emoteStorage = emoteStorage;
this.emotePlayer = emotePlayer;
this.debugContainerBuilder = debugContainerBuilder;
this.scenesCache = scenesCache;
this.localSceneDevelopment = localSceneDevelopment;
}
protected override void Update(float t)
{
CancelSceneEmotesBySceneChangeQuery(World);
CancelEmotesQuery(World);
CancelRemoteMaskedEmotesQuery(World);
CancelEmotesByTeleportIntentionQuery(World);
CancelEmotesByMoveToWithDurationQuery(World);
CancelEmotesByMovementInputQuery(World);
ReplicateLoopingEmotesQuery(World);
ConsumeEmoteIntentQuery(World, t);
BroadcastEmoteOnLocalPlayerQuery(World);
DiscardEmoteBroadcastOnRemotePlayersQuery(World);
CancelEmotesByDeletionQuery(World);
CancelMaskedEmotesByDeletionQuery(World);
UpdateEmoteTagsQuery(World);
UpdateRemoteMaskedEmoteTagsQuery(World);
DisableCharacterControllerQuery(World);
CleanUpQuery(World);
}
/// <summary>
/// Stops scene emotes when the player is no longer in the scene that triggered them.
/// PXs and smart wearables are exempt while their scene remains loaded
/// </summary>
[Query]
[All(typeof(PlayerComponent))]
[None(typeof(DeleteEntityIntention))]
private void CancelSceneEmotesBySceneChange(Entity entity, ref CharacterEmoteComponent emoteComponent, in IAvatarView avatarView)
{
if (emoteComponent.CurrentEmoteReference == null) return;
if (!TryParseSceneEmoteURN(emoteComponent.EmoteUrn, out _, out _, out _, out ISceneFacade? emoteScene, out bool isPortableExperience)) return;
//Skips this for Pxs and smart wearables as they are their own scenes
if (emoteScene != null && (isPortableExperience || emoteScene == scenesCache.CurrentScene.Value)) return;
StopEmote(entity, ref emoteComponent, avatarView);
messageBus.SendStop();
}
[Query]
[All(typeof(DeleteEntityIntention))]
private void CancelEmotesByDeletion(Entity entity, ref CharacterEmoteComponent emoteComponent, in IAvatarView avatarView) =>
StopEmote(entity, ref emoteComponent, avatarView);
[Query]
[All(typeof(DeleteEntityIntention))]
private void CancelMaskedEmotesByDeletion(ref CharacterMaskedEmoteComponent masked, in IAvatarView avatarView)
{
if (masked.CurrentEmoteReference == null) return;
// Force-stop regardless of animator state — the entity is being destroyed.
masked.StopEmote = true;
emotePlayer.TryCancelMaskedEmote(ref masked, avatarView);
}
/// <summary>
/// Stops emote playback whenever the teleport intent is present on the entity.
/// Doesn't handle movePlayerTo calls.
/// </summary>
[Query]
[All(typeof(PlayerTeleportIntent))]
[None(typeof(CharacterEmoteIntent))]
private void CancelEmotesByTeleportIntention(Entity entity, ref CharacterEmoteComponent emoteComponent, in IAvatarView avatarView) =>
StopEmote(entity, ref emoteComponent, avatarView);
/// <summary>
/// Stops emote playback when smooth movement with duration is initiated.
/// </summary>
[Query]
[All(typeof(PlayerMoveToWithDurationIntent))]
[None(typeof(CharacterEmoteIntent))]
private void CancelEmotesByMoveToWithDuration(Entity entity, ref CharacterEmoteComponent emoteComponent, in IAvatarView avatarView) =>
StopEmote(entity, ref emoteComponent, avatarView);
// looping emotes and cancelling emotes by tag depend on tag change, this query alone is the one that updates that value at the ond of the update
[Query]
private void UpdateEmoteTags(ref CharacterEmoteComponent emoteComponent, in IAvatarView avatarView)
{
int currentStateTag = avatarView.GetAnimatorCurrentStateTag(AnimatorEmoteLayers.BASE_LAYER);
emoteComponent.SetAnimationTag(currentStateTag);
}
// emotes that do not loop need to trigger some kind of cancellation, so we can take care of the emote props and sounds
[Query]
[None(typeof(CharacterEmoteIntent), typeof(DeleteEntityIntention))]
private void CancelEmotes(Entity entity, ref CharacterEmoteComponent emoteComponent, in IAvatarView avatarView)
{
bool wantsToCancelEmote = emoteComponent.StopEmote;
emoteComponent.StopEmote = false;
EmoteReferences? emoteReference = emoteComponent.CurrentEmoteReference;
if (!emoteReference) return;
bool shouldCancelEmote = wantsToCancelEmote || World.Has<HiddenPlayerComponent>(entity);
if (shouldCancelEmote)
{
StopEmote(entity, ref emoteComponent, avatarView);
return;
}
// Tear down only when the legacy Animation component has stopped on its own (non-looping).
if (emoteReference.legacy)
{
if (!avatarView.IsLegacyAnimationPlaying)
StopEmote(entity, ref emoteComponent, avatarView);
return;
}
if (!emoteComponent.IsPlayingEmote)
{
avatarView.ResetAnimatorTrigger(AnimationHashes.EMOTE_STOP);
return;
}
int animatorCurrentStateTag = avatarView.GetAnimatorCurrentStateTag(AnimatorEmoteLayers.BASE_LAYER);
bool isOnAnotherTag = animatorCurrentStateTag != AnimationHashes.EMOTE && animatorCurrentStateTag != AnimationHashes.EMOTE_LOOP;
if (isOnAnotherTag)
StopEmote(entity, ref emoteComponent, avatarView);
}
// Related issues:
// https://github.com/decentraland/unity-explorer/issues/6246
// https://github.com/decentraland/unity-explorer/issues/4306
// Following how it works on alternative clients, we should only cancel emotes given on the user's input
// and not by the physics velocity. This prevents undesired interruptions like movePlayerTo + triggerEmote simultaneously
// or random jumps due to physics imprecision
// This is a base on which we can keep growing how scenes may interact with the avatar's emotes
[Query]
[None(typeof(CharacterEmoteIntent), typeof(PlayerTeleportIntent.JustTeleported))]
private void CancelEmotesByMovementInput(
Entity entity,
ref CharacterEmoteComponent emoteComponent,
in IAvatarView avatarView,
ref JumpInputComponent jumpInputComponent,
ref MovementInputComponent movementInputComponent,
in Profile profile)
{
if (!emoteComponent.IsPlayingEmote) return;
const float HORIZONTAL_THRESHOLD_SQ = 0.1f * 0.1f;
float horizontalSpeedSq = movementInputComponent.Axes.sqrMagnitude;
bool shouldCancelEmote = horizontalSpeedSq > HORIZONTAL_THRESHOLD_SQ || jumpInputComponent.IsPressed;
if (!shouldCancelEmote) return;
ReportHub.Log(ReportCategory.EMOTE, $"CancelEmotesByMovementInput() {profile.UserId} Stopping emote");
StopEmote(entity, ref emoteComponent, avatarView);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void StopEmote(Entity entity, ref CharacterEmoteComponent emoteComponent, IAvatarView avatarView)
{
if (emoteComponent.CurrentEmoteReference == null) return;
emotePlayer.Stop(emoteComponent.CurrentEmoteReference);
// Legacy emotes keep the Mecanim animator disabled while playing
avatarView.StopLegacyAnimation();
// Create a clean slate for the animator before setting the stop trigger
avatarView.ResetAnimatorTrigger(AnimationHashes.EMOTE);
avatarView.ResetAnimatorTrigger(AnimationHashes.EMOTE_RESET);
avatarView.SetAnimatorTrigger(AnimationHashes.EMOTE_STOP);
// See https://github.com/decentraland/unity-explorer/issues/4198
// Some emotes changes the armature rotation, we need to restore it
avatarView.ResetArmatureInclination();
// Propagate emote stop only for local player
if (World.Has<PlayerComponent>(entity))
messageBus.SendStop();
emoteComponent.Reset();
}
/// <summary>
/// Handles cancellation of masked emotes on remote player entities in the global world.
/// Local player masked emotes are handled by SceneMaskedEmoteSystem in each scene world.
/// </summary>
[Query]
[None(typeof(CharacterEmoteIntent), typeof(DeleteEntityIntention), typeof(PlayerComponent))]
private void CancelRemoteMaskedEmotes(ref CharacterMaskedEmoteComponent masked, in IAvatarView avatarView) =>
emotePlayer.TryCancelMaskedEmote(ref masked, avatarView);
[Query]
[None(typeof(PlayerComponent))]
private void UpdateRemoteMaskedEmoteTags(ref CharacterMaskedEmoteComponent masked, in IAvatarView avatarView) =>
EmotePlayer.UpdateMaskedEmoteTag(ref masked, avatarView);
// This query takes care of consuming the CharacterEmoteIntent to trigger an emote
[Query]
[None(typeof(DeleteEntityIntention), typeof(PlayerTeleportIntent.JustTeleported))]
private void ConsumeEmoteIntent([Data] float dt, Entity entity,
ref CharacterEmoteComponent emoteComponent,
ref CharacterEmoteIntent emoteIntent,
in IAvatarView avatarView,
ref AvatarShapeComponent avatarShapeComponent)
{
URN emoteId = emoteIntent.EmoteId;
// it's very important to catch any exception here to avoid not consuming the emote intent, so we don't infinitely create props
try
{
// we wait until the avatar finishes moving to trigger the emote,
// avoid the case where: you stop moving, trigger the emote, the emote gets triggered and next frame it gets cancelled because inertia keeps moving the avatar
// We also avoid triggering the emote while the character is jumping or landing, as the landing animation breaks the emote flow if they have props
// Skipped while an emote plays: the disabled CharacterController makes grounded/blend readings stale, parking the intent forever and soft-locking movement.
if (emoteIntent.Mask == AvatarEmoteMask.AemFullBody &&
!emoteComponent.IsPlayingEmote &&
(avatarView.IsAnimatorInTag(AnimationHashes.JUMPING_TAG) ||
!avatarView.GetAnimatorBool(AnimationHashes.GROUNDED) ||
avatarView.GetAnimatorFloat(AnimationHashes.MOVEMENT_BLEND) > 0.1f))
return;
if (emoteStorage.TryGetElement(emoteId.Shorten(), out IEmote emote))
{
if (emote.IsLoading)
return;
// emote failed to load? remove intent
if (emote.Model is { IsInitialized: true, Succeeded: false })
{
ReportHub.LogError(GetReportData(), $"Cant play emote {emoteId} since it failed loading \n the DTO");
World.Remove<CharacterEmoteIntent>(entity);
return;
}
// emote failed to load? remove intent
if (emote.DTO.assetBundleManifestVersion is { assetBundleManifestRequestFailed: true } and { IsLSDAsset: false })
{
ReportHub.LogError(GetReportData(), $"Cant play emote {emoteId} since it failed loading the manifest");
World.Remove<CharacterEmoteIntent>(entity);
return;
}
// Fixes https://github.com/decentraland/unity-explorer/issues/6531
// Rarely happens for an unknown reason that emote.AssetResults[bodyShape] is null, provoking the emote intent to never finish,
// thus props of the previous emote cannot be disposed either.
// By setting a timeout we force unstuck the process
if (emoteIntent.UpdatePlayTimeout(dt))
{
ReportHub.LogError(GetReportData(), $"Cant play emote {emoteId} timeout reached.");
World.Remove<CharacterEmoteIntent>(entity);
return;
}
BodyShape bodyShape = avatarShapeComponent.BodyShape;
// Loading not complete
if (emote.AssetResults[bodyShape] == null)
return;
StreamableLoadingResult<AttachmentRegularAsset> streamableAssetValue = emote.AssetResults[bodyShape].Value;
GameObject? mainAsset;
if (streamableAssetValue is { Succeeded: false } || (mainAsset = streamableAssetValue.Asset?.MainAsset) == null)
{
// We can't play emote, remove intent, otherwise there is no place to remove it
World.Remove<CharacterEmoteIntent>(entity);
return;
}
StreamableLoadingResult<AudioClipData>? audioAssetResult = emote.AudioAssetResults[bodyShape];
AudioClip? audioClip = audioAssetResult?.Asset;
// Capture intent and view values before any structural change that invalidate query-provided refs.
AvatarEmoteMask mask = emoteIntent.Mask;
bool spatial = emoteIntent.Spatial;
IAvatarView view = avatarView;
bool isLooping = emote.IsLooping();
if (mask == AvatarEmoteMask.AemFullBody)
{
// A masked emote may still be playing on a separate animator layer (remote players).
// EmoteStart replaces the current emote, so the full-body emote supersedes it —
// mirrors the masked branch below, which stops a playing full-body emote.
ref CharacterMaskedEmoteComponent masked = ref World.TryGetRef<CharacterMaskedEmoteComponent>(entity, out bool hasMasked);
if (hasMasked && masked.CurrentEmoteReference != null)
{
masked.StopEmote = true;
emotePlayer.TryCancelMaskedEmote(ref masked, view);
}
emoteComponent.EmoteUrn = emoteId;
emoteComponent.Mask = mask;
if (!emotePlayer.Play(mainAsset, audioClip, isLooping, spatial, in view, ref emoteComponent))
ReportHub.LogError(ReportCategory.EMOTE, $"Emote name:{emoteId} cant be played.");
else
{
uint durationMs = !isLooping ? (uint)(emoteComponent.PlayingEmoteDuration * 1000) : 0;
World.Add(entity, new EmotePendingToBroadcast { EmoteId = emoteId, DurationMs = durationMs, Mask = mask});
}
}
else
{
// Masked emotes for remote players are handled here in the global world.
// Local player masked emotes go through SceneMaskedEmoteSystem instead.
if (emoteComponent.CurrentEmoteReference != null)
StopEmote(entity, ref emoteComponent, view);
// After AddOrGet the query-provided refs (emoteIntent, avatarView,
// emoteComponent) are potentially dangling, use only local copies.
ref CharacterMaskedEmoteComponent masked = ref World.AddOrGet<CharacterMaskedEmoteComponent>(entity);
masked.EmoteUrn = emoteId;
masked.Mask = mask;
if (!emotePlayer.PlayMasked(mainAsset, audioClip, isLooping, spatial, in view, ref masked))
ReportHub.LogError(ReportCategory.EMOTE, $"Emote name:{emoteId} cant be played.");
else
{
// The duration comes from the masked emote's own clip; a looping emote has no end to report.
uint durationMs = !isLooping ? (uint)(masked.PlayingEmoteDuration * 1000) : 0;
World.Add(entity, new EmotePendingToBroadcast { EmoteId = emoteId, DurationMs = durationMs, Mask = mask});
}
}
World.Remove<CharacterEmoteIntent>(entity);
}
else
// Request the emote when not it cache. It will eventually endup in the emoteStorage so it can be played by this query
CreateEmotePromise(emoteId, avatarShapeComponent.BodyShape, emoteIntent.Mask);
}
catch (Exception e) { ReportHub.LogException(e, GetReportData()); }
}
[Query]
[All(typeof(PlayerComponent))]
private void BroadcastEmoteOnLocalPlayer(
in Entity entity,
ref EmotePendingToBroadcast broadcast,
in PlayerMovementNetworkComponent playerMovement,
in CharacterAnimationComponent animation,
in StunComponent stun,
in MovementInputComponent input,
in HeadIKComponent headIK,
in HandPointAtComponent pointAt)
{
var playerState = new NetworkMovementMessage
{
timestamp = UnityEngine.Time.unscaledTime,
position = playerMovement.Character.transform.position,
velocity = playerMovement.Character.velocity,
velocitySqrMagnitude = playerMovement.Character.velocity.sqrMagnitude,
rotationY = playerMovement.Character.transform.eulerAngles.y,
isEmoting = true,
isInstant = true,
isStunned = stun.IsStunned,
isSliding = animation.States.IsSliding,
isPointingAt = pointAt.IsPointing,
pointAtWorldHitPoint = pointAt.WorldHitPoint,
headIKYawEnabled = headIK.YawEnabled,
headIKPitchEnabled = headIK.PitchEnabled,
headYawAndPitch = headIK.GetHeadYawAndPitch(),
movementKind = input.Kind,
animState = new AnimationStates
{
IsSliding = animation.States.IsSliding,
IsGrounded = animation.States.IsGrounded,
JumpCount = animation.States.JumpCount,
IsLongJump = animation.States.IsLongJump,
IsFalling = animation.States.IsFalling,
IsLongFall = animation.States.IsLongFall,
IsStunned = stun.IsStunned,
GlideState = animation.States.GlideState,
SlideBlendValue = animation.States.SlideBlendValue,
MovementBlendValue = animation.States.MovementBlendValue,
},
};
URN emoteId = broadcast.EmoteId;
uint durationMs = broadcast.DurationMs;
AvatarEmoteMask mask = broadcast.Mask;
World.Remove<EmotePendingToBroadcast>(entity);
messageBus.Send(emoteId, false, mask, durationMs, playerState);
}
[Query]
[None(typeof(PlayerComponent))]
[All(typeof(EmotePendingToBroadcast))]
private void DiscardEmoteBroadcastOnRemotePlayers(Entity entity)
{
World.Remove<EmotePendingToBroadcast>(entity);
}
// Every time the emote is looped we send a new message that should refresh the looping emotes on clients that didn't receive the initial message yet
// TODO: we can safely remove this propagation for pulse multiplayer as it is no longer needed (based on emote start/emote stop events)
[Query]
[All(typeof(PlayerComponent))]
[None(typeof(CharacterEmoteIntent))]
private void ReplicateLoopingEmotes(ref CharacterEmoteComponent animationComponent, in IAvatarView avatarView)
{
int prevTag = animationComponent.CurrentAnimationTag;
if (prevTag == 0) return;
int currentTag = avatarView.GetAnimatorCurrentStateTag(AnimatorEmoteLayers.BASE_LAYER);
if ((prevTag != AnimationHashes.EMOTE || currentTag != AnimationHashes.EMOTE_LOOP)
&& (prevTag != AnimationHashes.EMOTE_LOOP || currentTag != AnimationHashes.EMOTE)) return;
messageBus.Send(animationComponent.EmoteUrn, true, animationComponent.Mask);
}
[Query]
private void DisableCharacterController(ref CharacterController characterController, in CharacterEmoteComponent emoteComponent) =>
characterController.enabled = !emoteComponent.IsPlayingEmote;
[Query]
private void CleanUp(Profile profile, in DeleteEntityIntention deleteEntityIntention)
{
if (!deleteEntityIntention.DeferDeletion)
messageBus.OnPlayerRemoved(profile.UserId);
}
private void CreateEmotePromise(URN urn, BodyShape bodyShape, AvatarEmoteMask mask)
{
loadEmoteBuffer[0] = urn;
if (TryParseSceneEmoteURN(urn, out string sceneId, out string emoteHash, out bool loop, out ISceneFacade? scene, out _))
{
if (scene == null)
return;
// Local scene preview path, this is needed if a remote client plays a scene emote this client has not yet played
if (localSceneDevelopment && TryResolveLocalSceneEmotePath(scene, emoteHash, out string emotePath))
{
SceneEmoteFromLocalPromise.Create(World,
new GetSceneEmoteFromLocalSceneIntention(scene.SceneData, emotePath, emoteHash, bodyShape, loop, mask),
PartitionComponent.TOP_PRIORITY);
return;
}
// Deployed scenes path (asset bundles)
if (scene.SceneData.SceneEntityDefinition.assetBundleManifestVersion == null)
return;
SceneEmoteFromRealmPromise.Create(World,
new GetSceneEmoteFromRealmIntention(sceneId, scene.SceneData.SceneEntityDefinition.assetBundleManifestVersion!, emoteHash, loop, bodyShape),
PartitionComponent.TOP_PRIORITY);
}
else
EmotePromise.Create(World,
EmoteComponentsUtils.CreateGetEmotesByPointersIntention(bodyShape, loadEmoteBuffer),
PartitionComponent.TOP_PRIORITY);
}
private bool TryParseSceneEmoteURN(URN urnToParse, out string sceneId, out string parsedEmoteHash, out bool parsedLoop, out ISceneFacade? resolvedScene, out bool isPortableExperience)
{
sceneId = string.Empty;
parsedEmoteHash = string.Empty;
parsedLoop = false;
resolvedScene = null;
isPortableExperience = false;
ReadOnlySpan<char> urnStr = urnToParse.ToString().AsSpan();
ReadOnlySpan<char> emotePrefixWithColon = (ReadOnlySpan<char>)SCENE_EMOTE_PREFIX_WITH_COLON;
if (urnStr.IsEmpty || !urnStr.StartsWith(emotePrefixWithColon, StringComparison.OrdinalIgnoreCase))
return false;
ReadOnlySpan<char> payload = urnStr.Slice(emotePrefixWithColon.Length);
// Parse loop from the right-most "-{bool}" segment
int lastDash = payload.LastIndexOf('-');
if (lastDash <= 0 || lastDash == payload.Length - 1)
return false;
ReadOnlySpan<char> loopSpan = payload.Slice(lastDash + 1);
if (!bool.TryParse(loopSpan, out parsedLoop))
return false;
ReadOnlySpan<char> payloadWithoutLoop = payload.Slice(0, lastDash);
// sceneId and emoteHash can contain '-' in local preview,
// so we can't just split by "last two dashes". Instead, match against loaded scenes by prefix.
if (TryResolveSceneByNamePrefix(scenesCache.Scenes, payloadWithoutLoop, ref sceneId, ref parsedEmoteHash, ref resolvedScene))
return true;
if (TryResolveSceneByNamePrefix(scenesCache.PortableExperiencesScenes, payloadWithoutLoop, ref sceneId, ref parsedEmoteHash, ref resolvedScene))
{
isPortableExperience = true;
return true;
}
// Fallback: assume "{sceneKey}-{emoteHash}" split by first dash.
// This is primarily for deployed realm format where scene id has no '-'.
int firstDash = payloadWithoutLoop.IndexOf('-');
if (firstDash > 0 && firstDash < payloadWithoutLoop.Length - 1)
{
sceneId = payloadWithoutLoop.Slice(0, firstDash).ToString();
parsedEmoteHash = payloadWithoutLoop.Slice(firstDash + 1).ToString();
if (!scenesCache.TryGetBySceneId(sceneId, out resolvedScene) &&
scenesCache.TryGetPortableExperienceBySceneUrn(sceneId, out resolvedScene))
isPortableExperience = true;
return true;
}
return false;
}
private static bool TryResolveSceneByNamePrefix(IReadOnlyCollection<ISceneFacade> scenes, ReadOnlySpan<char> payloadWithoutLoop, ref string sceneId, ref string parsedEmoteHash, ref ISceneFacade? resolvedScene)
{
foreach (ISceneFacade facade in scenes)
{
string candidateName = facade.SceneData.SceneShortInfo.Name;
if (candidateName.Length == 0)
continue;
ReadOnlySpan<char> candidatePrefix = (candidateName + "-").AsSpan();
if (payloadWithoutLoop.StartsWith(candidatePrefix, StringComparison.Ordinal))
{
sceneId = candidateName;
parsedEmoteHash = payloadWithoutLoop.Slice(candidatePrefix.Length).ToString();
resolvedScene = facade;
return true;
}
}
return false;
}
private bool TryResolveLocalSceneEmotePath(ISceneFacade sceneFacade, string hash, out string emotePath)
{
var content = sceneFacade.SceneData.SceneEntityDefinition.content;
for (var i = 0; i < content.Length; i++)
{
if (content[i].hash.Equals(hash, StringComparison.Ordinal))
{
emotePath = content[i].file;
return true;
}
}
emotePath = string.Empty;
return false;
}
}
}