| | 1 | | using AvatarAssets; |
| | 2 | | using AvatarSystem; |
| | 3 | | using Cysharp.Threading.Tasks; |
| | 4 | | using DCL; |
| | 5 | | using DCL.Controllers; |
| | 6 | | using DCL.Emotes; |
| | 7 | | using Decentraland.Renderer.RendererServices; |
| | 8 | | using rpc_csharp; |
| | 9 | | using System; |
| | 10 | | using System.Collections.Generic; |
| | 11 | | using System.Threading; |
| | 12 | | using UnityEngine; |
| | 13 | | using Environment = DCL.Environment; |
| | 14 | |
|
| | 15 | | namespace RPC.Services |
| | 16 | | { |
| | 17 | | public class EmotesRendererServiceImpl : IEmotesRendererService<RPCContext> |
| | 18 | | { |
| 0 | 19 | | private static readonly SuccessResponse FAILURE_RESPONSE = new () { Success = false }; |
| 0 | 20 | | private static readonly SuccessResponse SUCCESS_RESPONSE = new () { Success = true }; |
| | 21 | |
|
| | 22 | | private readonly IWorldState worldState; |
| | 23 | | private readonly ISceneController sceneController; |
| | 24 | | private readonly UserProfile userProfile; |
| | 25 | | private readonly BaseVariable<Player> ownPlayer; |
| | 26 | | private readonly IEmotesService emotesService; |
| | 27 | |
|
| | 28 | | private readonly IDictionary<IParcelScene, HashSet<EmoteBodyId>> emotesByScene; |
| | 29 | | private readonly IDictionary<IParcelScene, CancellationTokenSource> cancellationTokenSources; |
| | 30 | |
|
| | 31 | | private IAvatarEmotesController emotesController; |
| | 32 | |
|
| | 33 | | public static void RegisterService(RpcServerPort<RPCContext> port) |
| | 34 | | { |
| 0 | 35 | | EmotesRendererServiceCodeGen.RegisterService( |
| | 36 | | port, |
| | 37 | | new EmotesRendererServiceImpl( |
| | 38 | | port: port, |
| | 39 | | worldState: Environment.i.world.state, |
| | 40 | | sceneController: Environment.i.world.sceneController, |
| | 41 | | userProfile: UserProfile.GetOwnUserProfile(), |
| | 42 | | ownPlayer: DataStore.i.player.ownPlayer, |
| | 43 | | emotesService: Environment.i.serviceLocator.Get<IEmotesService>(), |
| | 44 | | emotesByScene: new Dictionary<IParcelScene, HashSet<EmoteBodyId>>(), |
| | 45 | | cancellationTokenSources: new Dictionary<IParcelScene, CancellationTokenSource>() |
| | 46 | | )); |
| 0 | 47 | | } |
| | 48 | |
|
| 0 | 49 | | public EmotesRendererServiceImpl( |
| | 50 | | RpcServerPort<RPCContext> port, |
| | 51 | | IWorldState worldState, |
| | 52 | | ISceneController sceneController, |
| | 53 | | UserProfile userProfile, |
| | 54 | | BaseVariable<Player> ownPlayer, |
| | 55 | | IEmotesService emotesService, |
| | 56 | | IDictionary<IParcelScene, HashSet<EmoteBodyId>> emotesByScene, |
| | 57 | | IDictionary<IParcelScene, CancellationTokenSource> cancellationTokenSources |
| | 58 | | ) |
| | 59 | | { |
| 0 | 60 | | this.worldState = worldState; |
| 0 | 61 | | this.sceneController = sceneController; |
| 0 | 62 | | this.userProfile = userProfile; |
| 0 | 63 | | this.ownPlayer = ownPlayer; |
| 0 | 64 | | this.emotesService = emotesService; |
| 0 | 65 | | this.emotesByScene = emotesByScene; |
| 0 | 66 | | this.cancellationTokenSources = cancellationTokenSources; |
| | 67 | |
|
| 0 | 68 | | port.OnClose += OnPortClosed; |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | private void OnPortClosed() |
| | 72 | | { |
| 0 | 73 | | foreach (var scenes in worldState.GetLoadedScenes()) |
| | 74 | | { |
| 0 | 75 | | OnSceneRemoved(scenes.Value); |
| | 76 | | } |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | public async UniTask<TriggerSelfUserExpressionResponse> TriggerSelfUserExpression(TriggerSelfUserExpressionReque |
| | 80 | | { |
| 0 | 81 | | await UniTask.SwitchToMainThread(ct); |
| 0 | 82 | | userProfile.SetAvatarExpression(request.Id, UserProfile.EmoteSource.Command); |
| 0 | 83 | | return default; |
| 0 | 84 | | } |
| | 85 | |
|
| | 86 | | public async UniTask<SuccessResponse> TriggerSceneExpression(TriggerSceneExpressionRequest request, RPCContext c |
| | 87 | | { |
| | 88 | | // try get loaded scene |
| 0 | 89 | | if (!worldState.TryGetScene(request.SceneNumber, out IParcelScene scene)) |
| 0 | 90 | | return FAILURE_RESPONSE; |
| | 91 | |
|
| | 92 | | // generates an emote id |
| 0 | 93 | | if (!SceneEmoteHelper.TryGenerateEmoteId(scene, request.Path, request.Loop, out string emoteId)) |
| 0 | 94 | | return FAILURE_RESPONSE; |
| | 95 | |
|
| 0 | 96 | | emotesController ??= ownPlayer.Get()?.avatar.GetEmotesController(); |
| | 97 | |
|
| 0 | 98 | | if (emotesController == null) |
| 0 | 99 | | return FAILURE_RESPONSE; |
| | 100 | |
|
| 0 | 101 | | string userBodyShape = userProfile.avatar.bodyShape; |
| | 102 | |
|
| | 103 | | // get hashset for scene emotes that are already equipped |
| 0 | 104 | | if (!emotesByScene.TryGetValue(scene, out HashSet<EmoteBodyId> sceneEquippedEmotes)) |
| | 105 | | { |
| 0 | 106 | | sceneEquippedEmotes = new HashSet<EmoteBodyId>(); |
| 0 | 107 | | emotesByScene.Add(scene, sceneEquippedEmotes); |
| | 108 | | } |
| | 109 | |
|
| | 110 | | // get / create cancellation source for scene |
| 0 | 111 | | if (!cancellationTokenSources.TryGetValue(scene, out var cancellationTokenSource)) |
| | 112 | | { |
| 0 | 113 | | cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(ct); |
| 0 | 114 | | cancellationTokenSources.Add(scene, cancellationTokenSource); |
| | 115 | | } |
| | 116 | |
|
| 0 | 117 | | sceneController.OnSceneRemoved -= OnSceneRemoved; |
| 0 | 118 | | sceneController.OnSceneRemoved += OnSceneRemoved; |
| | 119 | |
|
| | 120 | | try |
| | 121 | | { |
| 0 | 122 | | await UniTask.SwitchToMainThread(ct); |
| | 123 | |
|
| 0 | 124 | | var emoteKey = new EmoteBodyId(userBodyShape, emoteId); |
| | 125 | |
|
| 0 | 126 | | if (!emotesByScene[scene].Contains(emoteKey)) |
| | 127 | | { |
| 0 | 128 | | var result = await emotesService.RequestEmote(emoteKey, cancellationTokenSource.Token, scene.content |
| 0 | 129 | | emotesController.EquipEmote(emoteId, result); |
| 0 | 130 | | emotesByScene[scene].Add(emoteKey); |
| | 131 | | } |
| | 132 | |
|
| 0 | 133 | | userProfile.SetAvatarExpression(emoteId, UserProfile.EmoteSource.Command); |
| | 134 | |
|
| 0 | 135 | | return SUCCESS_RESPONSE; |
| | 136 | | } |
| 0 | 137 | | catch (OperationCanceledException _) |
| | 138 | | { |
| 0 | 139 | | return FAILURE_RESPONSE; |
| | 140 | | } |
| | 141 | | catch (Exception e) |
| | 142 | | { |
| 0 | 143 | | Debug.LogException(e); |
| 0 | 144 | | return FAILURE_RESPONSE; |
| | 145 | | } |
| 0 | 146 | | } |
| | 147 | |
|
| | 148 | | private void OnSceneRemoved(IParcelScene scene) |
| | 149 | | { |
| 0 | 150 | | if (cancellationTokenSources.TryGetValue(scene, out var cancellationTokenSource)) |
| | 151 | | { |
| 0 | 152 | | cancellationTokenSource.Cancel(); |
| 0 | 153 | | cancellationTokenSource.Dispose(); |
| 0 | 154 | | cancellationTokenSources.Remove(scene); |
| | 155 | | } |
| | 156 | |
|
| 0 | 157 | | if (!emotesByScene.TryGetValue(scene, out var equippedEmotes)) return; |
| | 158 | |
|
| 0 | 159 | | foreach (var emoteData in equippedEmotes) |
| 0 | 160 | | emotesController?.UnEquipEmote(emoteData.EmoteId); |
| | 161 | |
|
| 0 | 162 | | emotesByScene.Remove(scene); |
| 0 | 163 | | } |
| | 164 | | } |
| | 165 | | } |