| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using DCL.CRDT; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using DCL.Models; |
| | 7 | | using Decentraland.Renderer.RendererServices; |
| | 8 | | using Google.Protobuf; |
| | 9 | | using MainScripts.DCL.Components; |
| | 10 | | using rpc_csharp; |
| | 11 | | using RPC.Context; |
| | 12 | | using System; |
| | 13 | | using System.Collections.Generic; |
| | 14 | | using System.IO; |
| | 15 | | using System.Threading; |
| | 16 | | using UnityEngine; |
| | 17 | | using BinaryWriter = KernelCommunication.BinaryWriter; |
| | 18 | |
|
| | 19 | | namespace RPC.Services |
| | 20 | | { |
| | 21 | | public class SceneControllerServiceImpl : IRpcSceneControllerService<RPCContext> |
| | 22 | | { |
| | 23 | | // HACK: Until we fix the code generator, we must replace all 'Decentraland.Common.Entity' for 'DCL.ECSComponent |
| | 24 | | // to be able to access request.Entity properties. |
| 1 | 25 | | private static readonly UnloadSceneResult defaultUnloadSceneResult = new UnloadSceneResult(); |
| | 26 | |
|
| 1 | 27 | | private static readonly SendBatchResponse defaultSendBatchResult = new SendBatchResponse(); |
| | 28 | |
|
| | 29 | | private const string REQUIRED_PORT_ID_START = "scene-"; |
| | 30 | |
|
| 7 | 31 | | private int sceneNumber = -1; |
| | 32 | | private RPCContext context; |
| | 33 | | private RpcServerPort<RPCContext> port; |
| 7 | 34 | | private bool isFirstMessage = true; |
| | 35 | | private int receivedSendCrdtCalls = 0; |
| | 36 | |
|
| | 37 | | private readonly MemoryStream sendCrdtMemoryStream; |
| | 38 | | private readonly BinaryWriter sendCrdtBinaryWriter; |
| | 39 | | private readonly MemoryStream getStateMemoryStream; |
| | 40 | | private readonly BinaryWriter getStateBinaryWriter; |
| | 41 | |
|
| | 42 | | private readonly MemoryStream serializeComponentBuffer; |
| | 43 | | private readonly CodedOutputStream serializeComponentStream; |
| | 44 | |
|
| 7 | 45 | | private readonly CRDTSceneMessage reusableCrdtMessageResult = new CRDTSceneMessage(); |
| | 46 | |
|
| | 47 | | public static void RegisterService(RpcServerPort<RPCContext> port) |
| | 48 | | { |
| 3 | 49 | | if (!port.portName.StartsWith(REQUIRED_PORT_ID_START)) return; |
| | 50 | |
|
| 1 | 51 | | RpcSceneControllerServiceCodeGen.RegisterService(port, new SceneControllerServiceImpl(port)); |
| 1 | 52 | | } |
| | 53 | |
|
| 7 | 54 | | public SceneControllerServiceImpl(RpcServerPort<RPCContext> port) |
| | 55 | | { |
| 7 | 56 | | port.OnClose += OnPortClose; |
| 7 | 57 | | this.port = port; |
| | 58 | |
|
| 7 | 59 | | sendCrdtMemoryStream = new MemoryStream(); |
| 7 | 60 | | sendCrdtBinaryWriter = new BinaryWriter(sendCrdtMemoryStream); |
| | 61 | |
|
| 7 | 62 | | getStateMemoryStream = new MemoryStream(); |
| 7 | 63 | | getStateBinaryWriter = new BinaryWriter(getStateMemoryStream); |
| | 64 | |
|
| 7 | 65 | | serializeComponentBuffer = new MemoryStream(); |
| 7 | 66 | | serializeComponentStream = new CodedOutputStream(serializeComponentBuffer); |
| 7 | 67 | | } |
| | 68 | |
|
| | 69 | | private void OnPortClose() |
| | 70 | | { |
| 7 | 71 | | port.OnClose -= OnPortClose; |
| | 72 | |
|
| 7 | 73 | | if (context != null && context.crdt.WorldState.ContainsScene(sceneNumber)) |
| 5 | 74 | | UnloadScene(null, context, new CancellationToken()).Forget(); |
| 7 | 75 | | } |
| | 76 | |
|
| | 77 | | public async UniTask<LoadSceneResult> LoadScene(LoadSceneMessage request, RPCContext context, CancellationToken |
| | 78 | | { |
| 6 | 79 | | sceneNumber = request.SceneNumber; |
| 6 | 80 | | this.context = context; |
| | 81 | |
|
| 6 | 82 | | List<ContentServerUtils.MappingPair> parsedContent = new List<ContentServerUtils.MappingPair>(); |
| | 83 | |
|
| 12 | 84 | | for (var i = 0; i < request.Entity.Content.Count; i++) |
| | 85 | | { |
| 0 | 86 | | parsedContent.Add(new ContentServerUtils.MappingPair() |
| | 87 | | { |
| | 88 | | file = request.Entity.Content[i].File, |
| | 89 | | hash = request.Entity.Content[i].Hash |
| | 90 | | }); |
| | 91 | | } |
| | 92 | |
|
| 6 | 93 | | CatalystSceneEntityMetadata parsedMetadata = Utils.SafeFromJson<CatalystSceneEntityMetadata>(request.Entity. |
| 6 | 94 | | Vector2Int[] parsedParcels = new Vector2Int[parsedMetadata.scene.parcels.Length]; |
| | 95 | |
|
| 24 | 96 | | for (int i = 0; i < parsedMetadata.scene.parcels.Length; i++) |
| | 97 | | { |
| 6 | 98 | | parsedParcels[i] = Utils.StringToVector2Int(parsedMetadata.scene.parcels[i]); |
| | 99 | | } |
| | 100 | |
|
| 6 | 101 | | await UniTask.SwitchToMainThread(ct); |
| | 102 | |
|
| 6 | 103 | | if (request.IsGlobalScene) |
| | 104 | | { |
| 0 | 105 | | CreateGlobalSceneMessage globalScene = new CreateGlobalSceneMessage() |
| | 106 | | { |
| | 107 | | contents = parsedContent, |
| | 108 | | id = request.Entity.Id, |
| | 109 | | sdk7 = request.Sdk7, |
| | 110 | | name = parsedMetadata.display?.title ?? request.SceneName, |
| | 111 | | baseUrl = request.BaseUrl, |
| | 112 | | sceneNumber = sceneNumber, |
| | 113 | | isPortableExperience = request.IsPortableExperience, |
| | 114 | | requiredPermissions = parsedMetadata.requiredPermissions, |
| | 115 | | allowedMediaHostnames = parsedMetadata.allowedMediaHostnames, |
| | 116 | | icon = parsedMetadata.display?.navmapThumbnail, |
| | 117 | | description = parsedMetadata.display?.description, |
| | 118 | | }; |
| | 119 | |
|
| | 120 | | try |
| | 121 | | { |
| 0 | 122 | | context.crdt.SceneController.CreateGlobalScene(globalScene); |
| 0 | 123 | | } |
| | 124 | | catch (Exception e) |
| | 125 | | { |
| 0 | 126 | | Debug.LogException(e); |
| 0 | 127 | | } |
| | 128 | | } |
| | 129 | | else |
| | 130 | | { |
| 6 | 131 | | LoadParcelScenesMessage.UnityParcelScene unityParcelScene = new LoadParcelScenesMessage.UnityParcelScene |
| | 132 | | { |
| | 133 | | sceneNumber = sceneNumber, |
| | 134 | | id = request.Entity.Id, |
| | 135 | | sdk7 = request.Sdk7, |
| | 136 | | baseUrl = request.BaseUrl, |
| | 137 | | baseUrlBundles = request.BaseUrlAssetBundles, |
| | 138 | | basePosition = Utils.StringToVector2Int(parsedMetadata.scene.@base), |
| | 139 | | parcels = parsedParcels, |
| | 140 | | contents = parsedContent, |
| | 141 | | requiredPermissions = parsedMetadata.requiredPermissions, |
| | 142 | | allowedMediaHostnames = parsedMetadata.allowedMediaHostnames, |
| | 143 | | scenePortableExperienceFeatureToggles = ToScenePortableExperienceFeatureToggle(parsedMetadata.featur |
| | 144 | | }; |
| | 145 | |
|
| | 146 | | try |
| | 147 | | { |
| 6 | 148 | | context.crdt.SceneController.LoadUnityParcelScene(unityParcelScene); |
| 6 | 149 | | } |
| | 150 | | catch (Exception e) |
| | 151 | | { |
| 0 | 152 | | Debug.LogException(e); |
| 0 | 153 | | } |
| | 154 | | } |
| | 155 | |
|
| 6 | 156 | | LoadSceneResult result = new LoadSceneResult() { Success = true }; |
| 6 | 157 | | return result; |
| 6 | 158 | | } |
| | 159 | |
|
| | 160 | | public async UniTask<UnloadSceneResult> UnloadScene(UnloadSceneMessage request, RPCContext context, Cancellation |
| | 161 | | { |
| 6 | 162 | | await UniTask.SwitchToMainThread(ct); |
| 6 | 163 | | context.crdt.SceneController.UnloadParcelSceneExecute(sceneNumber); |
| | 164 | |
|
| 6 | 165 | | return defaultUnloadSceneResult; |
| 6 | 166 | | } |
| | 167 | |
|
| | 168 | | public async UniTask<CRDTSceneMessage> SendCrdt(CRDTSceneMessage request, RPCContext context, CancellationToken |
| | 169 | | { |
| 2 | 170 | | IParcelScene scene = null; |
| 2 | 171 | | CRDTServiceContext crdtContext = context.crdt; |
| | 172 | |
|
| 2 | 173 | | await UniTask.SwitchToMainThread(ct); |
| | 174 | |
|
| | 175 | | // This line is to avoid a race condition because a CRDT message could be sent before the scene was loaded |
| | 176 | | // more info: https://github.com/decentraland/sdk/issues/480#issuecomment-1331309908 |
| 2 | 177 | | while (!crdtContext.WorldState.TryGetScene(sceneNumber, out scene)) |
| | 178 | | { |
| 0 | 179 | | await UniTask.Yield(ct); |
| | 180 | | } |
| | 181 | |
|
| 2 | 182 | | bool checkPendingMessages = !scene.sceneData.sdk7 |
| | 183 | | || (scene.sceneData.sdk7 && !scene.IsInitMessageDone()); |
| | 184 | |
|
| 2 | 185 | | while (checkPendingMessages && crdtContext.MessagingControllersManager.HasScenePendingMessages(sceneNumber)) |
| | 186 | | { |
| 0 | 187 | | await UniTask.Yield(ct); |
| | 188 | | } |
| | 189 | |
|
| 2 | 190 | | reusableCrdtMessageResult.Payload = ByteString.Empty; |
| 2 | 191 | | if (!scene.sceneData.sdk7) return reusableCrdtMessageResult; |
| | 192 | |
|
| 2 | 193 | | if (isFirstMessage && (!request.Payload.IsEmpty || receivedSendCrdtCalls > 0)) |
| 2 | 194 | | isFirstMessage = false; |
| | 195 | |
|
| 2 | 196 | | receivedSendCrdtCalls++; |
| | 197 | |
|
| | 198 | | try |
| | 199 | | { |
| 2 | 200 | | if (!isFirstMessage) |
| | 201 | | { |
| 2 | 202 | | using (var iterator = CRDTDeserializer.DeserializeBatch(request.Payload.Memory)) |
| | 203 | | { |
| 4 | 204 | | while (iterator.MoveNext()) |
| | 205 | | { |
| 2 | 206 | | if (!(iterator.Current is CrdtMessage crdtMessage)) |
| | 207 | | continue; |
| | 208 | |
|
| 2 | 209 | | crdtContext.CrdtMessageReceived?.Invoke(sceneNumber, crdtMessage); |
| | 210 | | } |
| 2 | 211 | | } |
| | 212 | |
|
| 2 | 213 | | if (crdtContext.GetSceneTick(sceneNumber) == 0) |
| | 214 | | { |
| | 215 | | // pause scene update until GLTFs are loaded |
| 3 | 216 | | while (!crdtContext.IsSceneGltfLoadingFinished(scene.sceneData.sceneNumber)) |
| | 217 | | { |
| 6 | 218 | | await UniTask.Yield(ct); |
| | 219 | | } |
| | 220 | |
|
| | 221 | | // When sdk7 scene receive it first crdt we set `InitMessagesDone` since |
| | 222 | | // kernel won't be sending that message for those scenes |
| 1 | 223 | | if (scene.sceneData.sdk7 && !scene.IsInitMessageDone()) |
| | 224 | | { |
| 1 | 225 | | crdtContext.SceneController.EnqueueSceneMessage(new QueuedSceneMessage_Scene() |
| | 226 | | { |
| | 227 | | sceneNumber = sceneNumber, |
| | 228 | | tag = "scene", |
| | 229 | | payload = new Protocol.SceneReady(), |
| | 230 | | method = MessagingTypes.INIT_DONE, |
| | 231 | | type = QueuedSceneMessage.Type.SCENE_MESSAGE |
| | 232 | | }); |
| | 233 | | } |
| | 234 | | } |
| | 235 | | } |
| | 236 | |
|
| 2 | 237 | | sendCrdtMemoryStream.SetLength(0); |
| | 238 | |
|
| 2 | 239 | | SendSceneMessages(crdtContext, sceneNumber, sendCrdtBinaryWriter, serializeComponentBuffer, serializeCom |
| | 240 | |
|
| 2 | 241 | | reusableCrdtMessageResult.Payload = ByteString.CopyFrom(sendCrdtMemoryStream.ToArray()); |
| | 242 | |
|
| 2 | 243 | | if (!isFirstMessage) |
| 2 | 244 | | crdtContext.IncreaseSceneTick(sceneNumber); |
| 2 | 245 | | } |
| 0 | 246 | | catch (OperationCanceledException _) |
| | 247 | | { |
| | 248 | | // Ignored |
| 0 | 249 | | } |
| | 250 | | catch (Exception e) |
| | 251 | | { |
| 0 | 252 | | Debug.LogError(e); |
| 0 | 253 | | } |
| | 254 | |
|
| 2 | 255 | | return reusableCrdtMessageResult; |
| 2 | 256 | | } |
| | 257 | |
|
| | 258 | | public async UniTask<CRDTSceneCurrentState> GetCurrentState(GetCurrentStateMessage request, RPCContext context, |
| | 259 | | { |
| 2 | 260 | | CRDTProtocol sceneState = null; |
| 2 | 261 | | CRDTServiceContext crdtContext = context.crdt; |
| | 262 | |
|
| 2 | 263 | | await UniTask.SwitchToMainThread(ct); |
| | 264 | |
|
| 2 | 265 | | if (crdtContext.CrdtExecutors != null && crdtContext.CrdtExecutors.TryGetValue(sceneNumber, out ICRDTExecuto |
| | 266 | | { |
| 2 | 267 | | sceneState = executor.crdtProtocol; |
| | 268 | | } |
| | 269 | |
|
| 2 | 270 | | CRDTSceneCurrentState result = new CRDTSceneCurrentState |
| | 271 | | { |
| | 272 | | HasOwnEntities = false |
| | 273 | | }; |
| | 274 | |
|
| | 275 | | try |
| | 276 | | { |
| 2 | 277 | | getStateMemoryStream.SetLength(0); |
| | 278 | |
|
| 2 | 279 | | SendSceneMessages(crdtContext, sceneNumber, sendCrdtBinaryWriter, serializeComponentBuffer, serializeCom |
| | 280 | |
|
| | 281 | | // serialize scene state |
| 2 | 282 | | if (sceneState != null) |
| | 283 | | { |
| 2 | 284 | | var crdtMessages = sceneState.GetStateAsMessages(); |
| | 285 | |
|
| 8 | 286 | | for (int i = 0; i < crdtMessages.Count; i++) |
| | 287 | | { |
| 2 | 288 | | CRDTSerializer.Serialize(getStateBinaryWriter, crdtMessages[i]); |
| | 289 | | } |
| | 290 | | } |
| | 291 | |
|
| 2 | 292 | | result.Payload = ByteString.CopyFrom(getStateMemoryStream.ToArray()); |
| 2 | 293 | | } |
| | 294 | | catch (Exception e) |
| | 295 | | { |
| 0 | 296 | | Debug.LogError(e); |
| 0 | 297 | | } |
| | 298 | |
|
| 2 | 299 | | return result; |
| 2 | 300 | | } |
| | 301 | |
|
| | 302 | | public async UniTask<SendBatchResponse> SendBatch(SendBatchRequest request, RPCContext context, CancellationToke |
| | 303 | | { |
| 0 | 304 | | await UniTask.SwitchToMainThread(ct); |
| | 305 | |
|
| | 306 | | try |
| | 307 | | { |
| 0 | 308 | | RendererManyEntityActions sceneRequest = RendererManyEntityActions.Parser.ParseFrom(request.Payload); |
| | 309 | |
|
| 0 | 310 | | for (var i = 0; i < sceneRequest.Actions.Count; i++) |
| | 311 | | { |
| 0 | 312 | | context.crdt.SceneController.EnqueueSceneMessage( |
| | 313 | | SDK6DataMapExtensions.SceneMessageFromSdk6Message(sceneRequest.Actions[i], sceneNumber) |
| | 314 | | ); |
| | 315 | | } |
| 0 | 316 | | } |
| | 317 | | catch (Exception e) |
| | 318 | | { |
| 0 | 319 | | Debug.LogError(e); |
| 0 | 320 | | } |
| | 321 | |
|
| 0 | 322 | | return defaultSendBatchResult; |
| 0 | 323 | | } |
| | 324 | |
|
| | 325 | | private static void SendSceneMessages(CRDTServiceContext crdtContext, |
| | 326 | | int sceneNumber, |
| | 327 | | BinaryWriter sendCrdtBinaryWriter, |
| | 328 | | MemoryStream serializeComponentBuffer, |
| | 329 | | CodedOutputStream serializeComponentStream, |
| | 330 | | bool clearMessages) |
| | 331 | | { |
| 4 | 332 | | if (!crdtContext.CrdtExecutors.TryGetValue(sceneNumber, out ICRDTExecutor executor)) |
| 0 | 333 | | return; |
| | 334 | |
|
| 4 | 335 | | if (crdtContext.ScenesOutgoingMsgs.TryGetValue(sceneNumber, out var msgs)) |
| | 336 | | { |
| 0 | 337 | | var pairs = msgs.Pairs; |
| | 338 | |
|
| 0 | 339 | | for (int i = 0; i < pairs.Count; i++) |
| | 340 | | { |
| 0 | 341 | | var msg = pairs[i].value; |
| 0 | 342 | | int entityId = (int)pairs[i].key1; |
| 0 | 343 | | int componentId = pairs[i].key2; |
| | 344 | |
|
| 0 | 345 | | if (msg.MessageType != CrdtMessageType.APPEND_COMPONENT |
| | 346 | | && msg.MessageType != CrdtMessageType.PUT_COMPONENT |
| | 347 | | && msg.MessageType != CrdtMessageType.DELETE_COMPONENT) |
| | 348 | | { |
| 0 | 349 | | if (clearMessages) |
| 0 | 350 | | msg.Dispose(); |
| | 351 | |
|
| 0 | 352 | | continue; |
| | 353 | | } |
| | 354 | |
|
| 0 | 355 | | CRDTProtocol crdtProtocol = executor.crdtProtocol; |
| | 356 | | CrdtMessage crdtMessage; |
| | 357 | |
|
| 0 | 358 | | if (msg.MessageType == CrdtMessageType.APPEND_COMPONENT || msg.MessageType == CrdtMessageType.PUT_CO |
| 0 | 359 | | msg.PooledWrappedComponent.WrappedComponentBase.SerializeTo(serializeComponentBuffer, serializeC |
| | 360 | |
|
| 0 | 361 | | if (msg.MessageType == CrdtMessageType.APPEND_COMPONENT) |
| | 362 | | { |
| 0 | 363 | | crdtMessage = crdtProtocol.CreateSetMessage(entityId, componentId, serializeComponentBuffer.ToAr |
| | 364 | | } |
| 0 | 365 | | else if (msg.MessageType == CrdtMessageType.PUT_COMPONENT) |
| | 366 | | { |
| 0 | 367 | | crdtMessage = crdtProtocol.CreateLwwMessage(entityId, componentId, serializeComponentBuffer.ToAr |
| | 368 | | } |
| | 369 | | else |
| | 370 | | { |
| 0 | 371 | | crdtMessage = crdtProtocol.CreateLwwMessage(entityId, componentId, null); |
| | 372 | | } |
| | 373 | |
|
| 0 | 374 | | CRDTProtocol.ProcessMessageResultType resultType = crdtProtocol.ProcessMessage(crdtMessage); |
| | 375 | |
|
| 0 | 376 | | if (resultType == CRDTProtocol.ProcessMessageResultType.StateUpdatedData || |
| | 377 | | resultType == CRDTProtocol.ProcessMessageResultType.StateUpdatedTimestamp || |
| | 378 | | resultType == CRDTProtocol.ProcessMessageResultType.EntityWasDeleted || |
| | 379 | | resultType == CRDTProtocol.ProcessMessageResultType.StateElementAddedToSet) |
| | 380 | | { |
| 0 | 381 | | CRDTSerializer.Serialize(sendCrdtBinaryWriter, crdtMessage); |
| | 382 | | } |
| | 383 | |
|
| 0 | 384 | | if (clearMessages) |
| 0 | 385 | | msg.Dispose(); |
| | 386 | | } |
| | 387 | |
|
| 0 | 388 | | if (clearMessages) |
| 0 | 389 | | msgs.Clear(); |
| | 390 | | } |
| 4 | 391 | | } |
| | 392 | |
|
| | 393 | | private ScenePortableExperienceFeatureToggles ToScenePortableExperienceFeatureToggle(string str) |
| | 394 | | { |
| 6 | 395 | | if (string.Compare(str, "enabled", StringComparison.OrdinalIgnoreCase) == 0) |
| 0 | 396 | | return ScenePortableExperienceFeatureToggles.Enable; |
| | 397 | |
|
| 6 | 398 | | if (string.Compare(str, "disabled", StringComparison.OrdinalIgnoreCase) == 0) |
| 0 | 399 | | return ScenePortableExperienceFeatureToggles.Disable; |
| | 400 | |
|
| 6 | 401 | | if (string.Compare(str, "hideui", StringComparison.OrdinalIgnoreCase) == 0) |
| 0 | 402 | | return ScenePortableExperienceFeatureToggles.HideUi; |
| | 403 | |
|
| 6 | 404 | | return ScenePortableExperienceFeatureToggles.Enable; |
| | 405 | | } |
| | 406 | | } |
| | 407 | | } |