From 38dbfa1ec2c72ed4fc11c78dfb954a9b5a242e8e Mon Sep 17 00:00:00 2001 From: nickkhalow Date: Tue, 27 Aug 2024 19:59:08 +0700 Subject: [PATCH 01/27] rename WalletId to FromWalletId --- .../CommunicationsControllerAPIImplementation.cs | 4 ++-- .../Communications/ICommunicationControllerHub.cs | 4 ++-- .../SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementation.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementation.cs index d73651dda6..ab047f5698 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementation.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementation.cs @@ -37,9 +37,9 @@ protected override void OnMessageReceived(ICommunicationControllerHub.SceneMessa return; // Wallet Id - int walletBytesCount = Encoding.UTF8.GetByteCount(receivedMessage.WalletId); + int walletBytesCount = Encoding.UTF8.GetByteCount(receivedMessage.FromWalletId); Span senderBytes = stackalloc byte[walletBytesCount]; - Encoding.UTF8.GetBytes(receivedMessage.WalletId, senderBytes); + Encoding.UTF8.GetBytes(receivedMessage.FromWalletId, senderBytes); int messageLength = senderBytes.Length + decodedMessage.Length + 1; diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/ICommunicationControllerHub.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/ICommunicationControllerHub.cs index 08939391c3..62dd608cd1 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/ICommunicationControllerHub.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/ICommunicationControllerHub.cs @@ -17,13 +17,13 @@ readonly struct SceneMessage { public readonly ReadOnlyMemory Data; public readonly string SceneId; - public readonly string WalletId; + public readonly string FromWalletId; private SceneMessage(in ReceivedMessage message) { Data = message.Payload.Data.Memory; SceneId = message.Payload.SceneId; - WalletId = message.FromWalletId; + FromWalletId = message.FromWalletId; } public static SceneMessage CopyFrom(in ReceivedMessage message) => diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs index f0538f54d3..2a165f6d3e 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs @@ -36,7 +36,7 @@ protected override void OnMessageReceived(ICommunicationControllerHub.SceneMessa SceneCommsMessages.Add(new CommsPayload { - sender = receivedMessage.WalletId, + sender = receivedMessage.FromWalletId, message = Encoding.UTF8.GetString(decodedMessage) }); } From b76b87a497c8f5bf7ab01e08cffcfeabcfe97a68 Mon Sep 17 00:00:00 2001 From: nickkhalow Date: Tue, 27 Aug 2024 20:04:25 +0700 Subject: [PATCH 02/27] ISDKMessageBusCommsControllerAPI clear contract --- .../CommunicationsControllerAPIImplementation.cs | 3 --- .../SDKMessageBusCommsAPIImplementation.cs | 13 +++++++++---- .../ISDKMessageBusCommsControllerAPI.cs | 6 ++++-- .../SDKObservableEventsEngineApiWrapper.cs | 4 ++-- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementation.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementation.cs index ab047f5698..adac8a0f14 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementation.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementation.cs @@ -1,12 +1,9 @@ using CRDT.Memory; -using CrdtEcsBridge.PoolsProviders; using SceneRunner.Scene; using SceneRuntime; using System; using System.Buffers; -using System.Collections.Generic; using System.Text; -using Utility; namespace CrdtEcsBridge.JsModulesImplementation.Communications { diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs index 2a165f6d3e..719e8992c1 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs @@ -10,15 +10,20 @@ namespace CrdtEcsBridge.JsModulesImplementation.Communications.SDKMessageBus { public class SDKMessageBusCommsAPIImplementation : CommunicationsControllerAPIImplementationBase, ISDKMessageBusCommsControllerAPI { - public List SceneCommsMessages { get; } = new (); + private readonly List messages = new (); - public SDKMessageBusCommsAPIImplementation(ISceneData sceneData, ICommunicationControllerHub messagePipesHub, IJsOperations jsOperations, ISceneStateProvider sceneStateProvider) : base(sceneData, messagePipesHub, jsOperations, sceneStateProvider) + public IReadOnlyList SceneCommsMessages => messages; + + public SDKMessageBusCommsAPIImplementation(ISceneData sceneData, ICommunicationControllerHub messagePipesHub, IJsOperations jsOperations, ISceneStateProvider sceneStateProvider) : base(sceneData, messagePipesHub, jsOperations, sceneStateProvider) { } + + public void ClearMessages() { + messages.Clear(); } public void Send(string data) { - var dataBytes = Encoding.UTF8.GetBytes(data); + byte[] dataBytes = Encoding.UTF8.GetBytes(data); Span encodedMessage = stackalloc byte[dataBytes.Length + 1]; encodedMessage[0] = (byte)MsgType.String; dataBytes.CopyTo(encodedMessage[1..]); @@ -34,7 +39,7 @@ protected override void OnMessageReceived(ICommunicationControllerHub.SceneMessa if (msgType != MsgType.String || decodedMessage.Length == 0) return; - SceneCommsMessages.Add(new CommsPayload + messages.Add(new CommsPayload { sender = receivedMessage.FromWalletId, message = Encoding.UTF8.GetString(decodedMessage) diff --git a/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/CommunicationsControllerApi/SDKMessageBus/ISDKMessageBusCommsControllerAPI.cs b/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/CommunicationsControllerApi/SDKMessageBus/ISDKMessageBusCommsControllerAPI.cs index 607c6f4663..22e603cc0e 100644 --- a/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/CommunicationsControllerApi/SDKMessageBus/ISDKMessageBusCommsControllerAPI.cs +++ b/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/CommunicationsControllerApi/SDKMessageBus/ISDKMessageBusCommsControllerAPI.cs @@ -5,8 +5,10 @@ namespace SceneRuntime.Apis.Modules.CommunicationsControllerApi.SDKMessageBus { public interface ISDKMessageBusCommsControllerAPI : ICommunicationsControllerAPI { - List SceneCommsMessages { get; } + IReadOnlyList SceneCommsMessages { get; } + + void ClearMessages(); + void Send(string data); } } - diff --git a/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/SDKObservableEventsEngineApiWrapper.cs b/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/SDKObservableEventsEngineApiWrapper.cs index fe111d6f72..868b8c2b52 100644 --- a/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/SDKObservableEventsEngineApiWrapper.cs +++ b/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/SDKObservableEventsEngineApiWrapper.cs @@ -27,7 +27,7 @@ public SDKObservableEventsEngineApiWrapper(ISDKObservableEventsEngineApi api, IS if (engineApi.SdkObservableEventSubscriptions.Count == 0) { engineApi.SdkObservableEvents.Clear(); - commsApi.SceneCommsMessages.Clear(); + commsApi.ClearMessages(); return null; } @@ -58,7 +58,7 @@ private void DetectSceneMessageBusCommsObservableEvent() foreach (CommsPayload currentPayload in commsApi.SceneCommsMessages) engineApi.SdkObservableEvents.Add(SDKObservableUtils.GenerateSDKObservableEvent(SDKObservableEventIds.Comms, currentPayload)); - commsApi.SceneCommsMessages.Clear(); + commsApi.ClearMessages(); } [UsedImplicitly] From c40a273beac3e1824a76e2f3457a884edebadccf Mon Sep 17 00:00:00 2001 From: nickkhalow Date: Tue, 27 Aug 2024 20:30:14 +0700 Subject: [PATCH 03/27] nullables --- ...SDKObservableEventsEngineAPIImplementation.cs | 16 ++++++++-------- .../Events/SDKObservableEvents.cs | 5 +++-- .../EngineApi/SDKObservableEvents/Events/csc.rsp | 1 + .../SDKObservableEvents/Events/csc.rsp.meta | 3 +++ .../SDKObservableEventsEngineApiWrapper.cs | 2 +- 5 files changed, 16 insertions(+), 11 deletions(-) create mode 100644 Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/Events/csc.rsp create mode 100644 Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/Events/csc.rsp.meta diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/SDKObservableEventsEngineAPIImplementation.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/SDKObservableEventsEngineAPIImplementation.cs index bd4254ecf6..1057481bca 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/SDKObservableEventsEngineAPIImplementation.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/SDKObservableEventsEngineAPIImplementation.cs @@ -90,13 +90,13 @@ private void DetectPlayerIdentityDataComponent(OutgoingCRDTMessagesProvider.Pend bool onPlayerConnectedSubscribed = SdkObservableEventSubscriptions.Contains(SDKObservableEventIds.PlayerConnected); if (onEnterSceneSubscribed) - SdkObservableEvents.Add(SDKObservableUtils.GenerateSDKObservableEvent(SDKObservableEventIds.EnterScene, new UserIdPayload + SdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.EnterScene, new UserIdPayload { userId = playerIdentityData.Address, })); if (onPlayerConnectedSubscribed) - SdkObservableEvents.Add(SDKObservableUtils.GenerateSDKObservableEvent(SDKObservableEventIds.PlayerConnected, new UserIdPayload + SdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.PlayerConnected, new UserIdPayload { userId = playerIdentityData.Address, })); @@ -111,13 +111,13 @@ private void DetectPlayerIdentityDataComponent(OutgoingCRDTMessagesProvider.Pend if (EnableSDKObservableMessagesDetection) { if (SdkObservableEventSubscriptions.Contains(SDKObservableEventIds.LeaveScene)) - SdkObservableEvents.Add(SDKObservableUtils.GenerateSDKObservableEvent(SDKObservableEventIds.LeaveScene, new UserIdPayload + SdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.LeaveScene, new UserIdPayload { userId = userIdEntitiesMap[pendingMessage.Entity], })); if (SdkObservableEventSubscriptions.Contains(SDKObservableEventIds.PlayerDisconnected)) - SdkObservableEvents.Add(SDKObservableUtils.GenerateSDKObservableEvent(SDKObservableEventIds.PlayerDisconnected, new UserIdPayload + SdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.PlayerDisconnected, new UserIdPayload { userId = userIdEntitiesMap[pendingMessage.Entity], })); @@ -142,7 +142,7 @@ private void DetectOtherObservableComponents(OutgoingCRDTMessagesProvider.Pendin if (SdkObservableEventSubscriptions.Contains(SDKObservableEventIds.SceneReady)) { - SdkObservableEvents.Add(SDKObservableUtils.GenerateSDKObservableEvent(SDKObservableEventIds.SceneReady, new SceneReadyPayload())); + SdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.SceneReady, new SceneReadyPayload())); reportedSceneReady = true; } @@ -152,7 +152,7 @@ private void DetectOtherObservableComponents(OutgoingCRDTMessagesProvider.Pendin { var realmInfo = (PBRealmInfo)message.Message; - SdkObservableEvents.Add(SDKObservableUtils.GenerateSDKObservableEvent(SDKObservableEventIds.RealmChanged, new RealmChangedPayload + SdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.RealmChanged, new RealmChangedPayload { domain = realmInfo.BaseUrl, room = realmInfo.Room, @@ -168,7 +168,7 @@ private void DetectOtherObservableComponents(OutgoingCRDTMessagesProvider.Pendin { if (!userIdEntitiesMap.ContainsKey(message.Entity)) break; - SdkObservableEvents.Add(SDKObservableUtils.GenerateSDKObservableEvent(SDKObservableEventIds.ProfileChanged, new ProfileChangedPayload + SdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.ProfileChanged, new ProfileChangedPayload { ethAddress = userIdEntitiesMap[message.Entity], version = 0, @@ -187,7 +187,7 @@ private void DetectOtherObservableComponents(OutgoingCRDTMessagesProvider.Pendin { var avatarEmoteCommand = (PBAvatarEmoteCommand) message.Message; - SdkObservableEvents.Add(SDKObservableUtils.GenerateSDKObservableEvent(SDKObservableEventIds.PlayerExpression, new PlayerExpressionPayload + SdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.PlayerExpression, new PlayerExpressionPayload { expressionId = avatarEmoteCommand.EmoteUrn, })); diff --git a/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/Events/SDKObservableEvents.cs b/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/Events/SDKObservableEvents.cs index eec1c7b038..60cebdf524 100644 --- a/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/Events/SDKObservableEvents.cs +++ b/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/Events/SDKObservableEvents.cs @@ -23,7 +23,7 @@ public static class SDKObservableEventIds public static class SDKObservableComponentIDs { - public static readonly HashSet Ids = new HashSet() + public static readonly HashSet Ids = new () { ComponentID.ENGINE_INFO, ComponentID.REALM_INFO, @@ -36,7 +36,8 @@ public static class SDKObservableComponentIDs public static class SDKObservableUtils { - public static SDKObservableEvent GenerateSDKObservableEvent(string eventId, T eventData) where T: struct => + [Pure] + public static SDKObservableEvent NewSDKObservableEventFromData(string eventId, T eventData) where T: struct => new() { generic = new SDKObservableEvent.Generic diff --git a/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/Events/csc.rsp b/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/Events/csc.rsp new file mode 100644 index 0000000000..dcc377f897 --- /dev/null +++ b/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/Events/csc.rsp @@ -0,0 +1 @@ +-nullable:enable \ No newline at end of file diff --git a/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/Events/csc.rsp.meta b/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/Events/csc.rsp.meta new file mode 100644 index 0000000000..5c40482ec3 --- /dev/null +++ b/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/Events/csc.rsp.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: fef9356f984142499901a80d57c6c545 +timeCreated: 1724764750 \ No newline at end of file diff --git a/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/SDKObservableEventsEngineApiWrapper.cs b/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/SDKObservableEventsEngineApiWrapper.cs index 868b8c2b52..2dd0eabb95 100644 --- a/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/SDKObservableEventsEngineApiWrapper.cs +++ b/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/SDKObservableEventsEngineApiWrapper.cs @@ -56,7 +56,7 @@ private void DetectSceneMessageBusCommsObservableEvent() if (commsApi.SceneCommsMessages.Count == 0) return; foreach (CommsPayload currentPayload in commsApi.SceneCommsMessages) - engineApi.SdkObservableEvents.Add(SDKObservableUtils.GenerateSDKObservableEvent(SDKObservableEventIds.Comms, currentPayload)); + engineApi.SdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.Comms, currentPayload)); commsApi.ClearMessages(); } From 494ac318df5dcb85b054711d063f7a65a75dbf5f Mon Sep 17 00:00:00 2001 From: nickkhalow Date: Tue, 27 Aug 2024 20:35:41 +0700 Subject: [PATCH 04/27] ISDKObservableEventsEngineApi simplify contracts --- ...ObservableEventsEngineAPIImplementation.cs | 58 +++++++++++-------- .../ISDKObservableEventsEngineApi.cs | 9 ++- .../SDKObservableEventsEngineApiWrapper.cs | 4 +- 3 files changed, 42 insertions(+), 29 deletions(-) diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/SDKObservableEventsEngineAPIImplementation.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/SDKObservableEventsEngineAPIImplementation.cs index 1057481bca..1680c0167a 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/SDKObservableEventsEngineAPIImplementation.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/SDKObservableEventsEngineAPIImplementation.cs @@ -19,6 +19,7 @@ namespace CrdtEcsBridge.JsModulesImplementation public class SDKObservableEventsEngineAPIImplementation : EngineAPIImplementation, ISDKObservableEventsEngineApi { private readonly Dictionary userIdEntitiesMap = new (); + private readonly List sdkObservableEvents = new (); private bool reportedSceneReady; public SDKObservableEventsEngineAPIImplementation(ISharedPoolsProvider poolsProvider, IInstancePoolsProvider instancePoolsProvider, ICRDTProtocol crdtProtocol, ICRDTDeserializer crdtDeserializer, ICRDTSerializer crdtSerializer, @@ -26,37 +27,44 @@ public SDKObservableEventsEngineAPIImplementation(ISharedPoolsProvider poolsProv ISystemGroupsUpdateGate systemGroupsUpdateGate, ISceneExceptionsHandler exceptionsHandler, MultithreadSync multithreadSync) : base(poolsProvider, instancePoolsProvider, crdtProtocol, crdtDeserializer, crdtSerializer, crdtWorldSynchronizer, outgoingCrtdMessagesProvider, - systemGroupsUpdateGate, exceptionsHandler, multithreadSync) - { - } + systemGroupsUpdateGate, exceptionsHandler, multithreadSync) { } public bool EnableSDKObservableMessagesDetection { get; set; } = false; - public List SdkObservableEvents { get; } = new (); + public HashSet SdkObservableEventSubscriptions { get; } = new (); - public override void SetIsDisposing() + public void AddSDKObservableEvent(SDKObservableEvent sdkObservableEvent) { - userIdEntitiesMap.Clear(); - SdkObservableEvents.Clear(); - SdkObservableEventSubscriptions.Clear(); + sdkObservableEvents.Add(sdkObservableEvent); + } - base.SetIsDisposing(); + public void ClearSDKObservableEvents() + { + sdkObservableEvents.Clear(); } public PoolableSDKObservableEventArray? ConsumeSDKObservableEvents() { - if (SdkObservableEvents.Count == 0) return null; + if (sdkObservableEvents.Count == 0) return null; - PoolableSDKObservableEventArray serializationBufferPoolable = sharedPoolsProvider.GetSerializationSDKObservableEventsPool(SdkObservableEvents.Count); - for (var i = 0; i < SdkObservableEvents.Count; i++) - { - serializationBufferPoolable.Array[i] = SdkObservableEvents[i]; - } - SdkObservableEvents.Clear(); + PoolableSDKObservableEventArray serializationBufferPoolable = sharedPoolsProvider.GetSerializationSDKObservableEventsPool(sdkObservableEvents.Count); + + for (var i = 0; i < sdkObservableEvents.Count; i++) { serializationBufferPoolable.Array[i] = sdkObservableEvents[i]; } + + sdkObservableEvents.Clear(); return serializationBufferPoolable; } + public override void SetIsDisposing() + { + userIdEntitiesMap.Clear(); + sdkObservableEvents.Clear(); + SdkObservableEventSubscriptions.Clear(); + + base.SetIsDisposing(); + } + protected override void ProcessPendingMessage(OutgoingCRDTMessagesProvider.PendingMessage pendingMessage) { if (SDKObservableComponentIDs.Ids.Contains(pendingMessage.Bridge.Id)) @@ -90,13 +98,13 @@ private void DetectPlayerIdentityDataComponent(OutgoingCRDTMessagesProvider.Pend bool onPlayerConnectedSubscribed = SdkObservableEventSubscriptions.Contains(SDKObservableEventIds.PlayerConnected); if (onEnterSceneSubscribed) - SdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.EnterScene, new UserIdPayload + sdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.EnterScene, new UserIdPayload { userId = playerIdentityData.Address, })); if (onPlayerConnectedSubscribed) - SdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.PlayerConnected, new UserIdPayload + sdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.PlayerConnected, new UserIdPayload { userId = playerIdentityData.Address, })); @@ -111,13 +119,13 @@ private void DetectPlayerIdentityDataComponent(OutgoingCRDTMessagesProvider.Pend if (EnableSDKObservableMessagesDetection) { if (SdkObservableEventSubscriptions.Contains(SDKObservableEventIds.LeaveScene)) - SdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.LeaveScene, new UserIdPayload + sdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.LeaveScene, new UserIdPayload { userId = userIdEntitiesMap[pendingMessage.Entity], })); if (SdkObservableEventSubscriptions.Contains(SDKObservableEventIds.PlayerDisconnected)) - SdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.PlayerDisconnected, new UserIdPayload + sdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.PlayerDisconnected, new UserIdPayload { userId = userIdEntitiesMap[pendingMessage.Entity], })); @@ -142,7 +150,7 @@ private void DetectOtherObservableComponents(OutgoingCRDTMessagesProvider.Pendin if (SdkObservableEventSubscriptions.Contains(SDKObservableEventIds.SceneReady)) { - SdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.SceneReady, new SceneReadyPayload())); + sdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.SceneReady, new SceneReadyPayload())); reportedSceneReady = true; } @@ -152,7 +160,7 @@ private void DetectOtherObservableComponents(OutgoingCRDTMessagesProvider.Pendin { var realmInfo = (PBRealmInfo)message.Message; - SdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.RealmChanged, new RealmChangedPayload + sdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.RealmChanged, new RealmChangedPayload { domain = realmInfo.BaseUrl, room = realmInfo.Room, @@ -168,7 +176,7 @@ private void DetectOtherObservableComponents(OutgoingCRDTMessagesProvider.Pendin { if (!userIdEntitiesMap.ContainsKey(message.Entity)) break; - SdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.ProfileChanged, new ProfileChangedPayload + sdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.ProfileChanged, new ProfileChangedPayload { ethAddress = userIdEntitiesMap[message.Entity], version = 0, @@ -185,9 +193,9 @@ private void DetectOtherObservableComponents(OutgoingCRDTMessagesProvider.Pendin if (message.Bridge.Id == ComponentID.AVATAR_EMOTE_COMMAND) if (SdkObservableEventSubscriptions.Contains(SDKObservableEventIds.PlayerExpression)) { - var avatarEmoteCommand = (PBAvatarEmoteCommand) message.Message; + var avatarEmoteCommand = (PBAvatarEmoteCommand)message.Message; - SdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.PlayerExpression, new PlayerExpressionPayload + sdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.PlayerExpression, new PlayerExpressionPayload { expressionId = avatarEmoteCommand.EmoteUrn, })); diff --git a/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/ISDKObservableEventsEngineApi.cs b/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/ISDKObservableEventsEngineApi.cs index 17b4ebf383..9c60f916e6 100644 --- a/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/ISDKObservableEventsEngineApi.cs +++ b/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/ISDKObservableEventsEngineApi.cs @@ -6,9 +6,14 @@ namespace SceneRuntime.Apis.Modules.EngineApi.SDKObservableEvents { public interface ISDKObservableEventsEngineApi : IEngineApi { - List SdkObservableEvents { get; } - HashSet SdkObservableEventSubscriptions { get; } + void AddSDKObservableEvent(SDKObservableEvent sdkObservableEvent); + + void ClearSDKObservableEvents(); + PoolableSDKObservableEventArray? ConsumeSDKObservableEvents(); + + HashSet SdkObservableEventSubscriptions { get; } + bool EnableSDKObservableMessagesDetection { set; } } } diff --git a/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/SDKObservableEventsEngineApiWrapper.cs b/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/SDKObservableEventsEngineApiWrapper.cs index 2dd0eabb95..6edd5aea7d 100644 --- a/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/SDKObservableEventsEngineApiWrapper.cs +++ b/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/SDKObservableEventsEngineApiWrapper.cs @@ -26,7 +26,7 @@ public SDKObservableEventsEngineApiWrapper(ISDKObservableEventsEngineApi api, IS // If there are no subscriptions at all there is nothing to handle if (engineApi.SdkObservableEventSubscriptions.Count == 0) { - engineApi.SdkObservableEvents.Clear(); + engineApi.ClearSDKObservableEvents(); commsApi.ClearMessages(); return null; } @@ -56,7 +56,7 @@ private void DetectSceneMessageBusCommsObservableEvent() if (commsApi.SceneCommsMessages.Count == 0) return; foreach (CommsPayload currentPayload in commsApi.SceneCommsMessages) - engineApi.SdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.Comms, currentPayload)); + engineApi.AddSDKObservableEvent(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.Comms, currentPayload)); commsApi.ClearMessages(); } From 54c5504c5ade4c4259ec07fa68c5f74d49ec65f8 Mon Sep 17 00:00:00 2001 From: nickkhalow Date: Tue, 27 Aug 2024 20:36:59 +0700 Subject: [PATCH 05/27] EngineApiWrapper update contract --- .../SceneRuntime/Apis/Modules/EngineApi/EngineApiWrapper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/EngineApiWrapper.cs b/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/EngineApiWrapper.cs index 4640b051c6..d12180cb9e 100644 --- a/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/EngineApiWrapper.cs +++ b/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/EngineApiWrapper.cs @@ -70,7 +70,7 @@ public ScriptableByteArray CrdtGetState() } [UsedImplicitly] - public virtual ScriptableSDKObservableEventArray SendBatch() => null; + public virtual ScriptableSDKObservableEventArray? SendBatch() => null; public void SetIsDisposing() { From eabcb228810ddfd513ea626f68e1dcb196cc5dbf Mon Sep 17 00:00:00 2001 From: nickkhalow Date: Tue, 27 Aug 2024 20:38:55 +0700 Subject: [PATCH 06/27] SceneRuntimeImpl remove unused params --- .../Scripts/SceneRuntime/Factory/SceneRuntimeFactory.cs | 2 +- Explorer/Assets/Scripts/SceneRuntime/SceneRuntimeImpl.cs | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/Explorer/Assets/Scripts/SceneRuntime/Factory/SceneRuntimeFactory.cs b/Explorer/Assets/Scripts/SceneRuntime/Factory/SceneRuntimeFactory.cs index 35c0318a78..046936faf0 100644 --- a/Explorer/Assets/Scripts/SceneRuntime/Factory/SceneRuntimeFactory.cs +++ b/Explorer/Assets/Scripts/SceneRuntime/Factory/SceneRuntimeFactory.cs @@ -94,7 +94,7 @@ internal async UniTask CreateBySourceCodeAsync( // Provide basic Thread Pool synchronization context SynchronizationContext.SetSynchronizationContext(new SynchronizationContext()); string wrappedSource = WrapInModuleCommonJs(jsSceneLocalSourceCode.CodeForScene(sceneShortInfo.BaseParcel) ?? sourceCode); - return new SceneRuntimeImpl(wrappedSource, pair, moduleDictionary, instancePoolsProvider, sceneShortInfo, engineFactory, activeEngines); + return new SceneRuntimeImpl(wrappedSource, pair, moduleDictionary, engineFactory, activeEngines); } /// diff --git a/Explorer/Assets/Scripts/SceneRuntime/SceneRuntimeImpl.cs b/Explorer/Assets/Scripts/SceneRuntime/SceneRuntimeImpl.cs index 36f5531711..06c758e716 100644 --- a/Explorer/Assets/Scripts/SceneRuntime/SceneRuntimeImpl.cs +++ b/Explorer/Assets/Scripts/SceneRuntime/SceneRuntimeImpl.cs @@ -20,9 +20,7 @@ namespace SceneRuntime public class SceneRuntimeImpl : ISceneRuntime, IJsOperations { internal readonly V8ScriptEngine engine; - private readonly IInstancePoolsProvider instancePoolsProvider; private readonly SceneShortInfo sceneShortInfo; - private readonly V8EngineFactory engineFactory; private readonly V8ActiveEngines activeEngines; private readonly JsApiBunch jsApiBunch; @@ -38,15 +36,12 @@ public SceneRuntimeImpl( string sourceCode, (string validateCode, string jsInitCode) initCode, IReadOnlyDictionary jsModules, - IInstancePoolsProvider instancePoolsProvider, SceneShortInfo sceneShortInfo, V8EngineFactory engineFactory, V8ActiveEngines activeEngines ) { - this.instancePoolsProvider = instancePoolsProvider; this.sceneShortInfo = sceneShortInfo; - this.engineFactory = engineFactory; this.activeEngines = activeEngines; resetableSource = new JSTaskResolverResetable(); From 3ba260c1ba98d547bd6bdc418cb518037f6673fb Mon Sep 17 00:00:00 2001 From: nickkhalow Date: Tue, 27 Aug 2024 20:54:15 +0700 Subject: [PATCH 07/27] remove unused import --- .../CommunicationsControllerAPIImplementationBase.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs index 8cb1e6f16f..f8d2ac6b1b 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs @@ -1,6 +1,4 @@ using CrdtEcsBridge.PoolsProviders; -using DCL.Multiplayer.Connections.Messaging; -using Decentraland.Kernel.Comms.Rfc4; using SceneRunner.Scene; using SceneRuntime; using SceneRuntime.Apis.Modules.CommunicationsControllerApi; From a4d88746028fa8807e897d26446937ba2749ab5d Mon Sep 17 00:00:00 2001 From: nickkhalow Date: Tue, 27 Aug 2024 20:57:48 +0700 Subject: [PATCH 08/27] SubscribeToSDKObservableEvent simplify --- .../SDKObservableEventsEngineApiWrapper.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/SDKObservableEventsEngineApiWrapper.cs b/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/SDKObservableEventsEngineApiWrapper.cs index 6edd5aea7d..c01fe3ce6e 100644 --- a/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/SDKObservableEventsEngineApiWrapper.cs +++ b/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/SDKObservableEventsEngineApiWrapper.cs @@ -64,12 +64,14 @@ private void DetectSceneMessageBusCommsObservableEvent() [UsedImplicitly] public void SubscribeToSDKObservableEvent(string eventId) { - engineApi.SdkObservableEventSubscriptions.Add(eventId); - if (eventId == SDKObservableEventIds.PlayerClicked) + { ReportHub.LogWarning(new ReportData(ReportCategory.SDK_OBSERVABLES), "Scene subscribed to unsupported SDK Observable 'PlayerClicked'"); - else - engineApi.EnableSDKObservableMessagesDetection = true; + return; + } + + engineApi.SdkObservableEventSubscriptions.Add(eventId); + engineApi.EnableSDKObservableMessagesDetection = true; } [UsedImplicitly] From 3df5ceb56b9f5de5a02ce3151bf43572cb8dd2e8 Mon Sep 17 00:00:00 2001 From: nickkhalow Date: Tue, 27 Aug 2024 21:06:18 +0700 Subject: [PATCH 09/27] ISDKObservableEventsEngineApi simplify --- ...ObservableEventsEngineAPIImplementation.cs | 53 ++++++++++++++----- .../ISDKObservableEventsEngineApi.cs | 9 ++-- .../SDKObservableEventsEngineApiWrapper.cs | 19 ++----- .../Factory/SceneRuntimeFactory.cs | 2 +- 4 files changed, 50 insertions(+), 33 deletions(-) diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/SDKObservableEventsEngineAPIImplementation.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/SDKObservableEventsEngineAPIImplementation.cs index 1680c0167a..e8eaa359af 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/SDKObservableEventsEngineAPIImplementation.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/SDKObservableEventsEngineAPIImplementation.cs @@ -6,6 +6,7 @@ using CrdtEcsBridge.PoolsProviders; using CrdtEcsBridge.UpdateGate; using CrdtEcsBridge.WorldSynchronizer; +using DCL.Diagnostics; using DCL.ECS7; using DCL.ECSComponents; using SceneRunner.Scene.ExceptionsHandling; @@ -20,6 +21,8 @@ public class SDKObservableEventsEngineAPIImplementation : EngineAPIImplementatio { private readonly Dictionary userIdEntitiesMap = new (); private readonly List sdkObservableEvents = new (); + private readonly HashSet sdkObservableEventSubscriptions = new (); + private bool enableSDKObservableMessagesDetection; private bool reportedSceneReady; public SDKObservableEventsEngineAPIImplementation(ISharedPoolsProvider poolsProvider, IInstancePoolsProvider instancePoolsProvider, ICRDTProtocol crdtProtocol, ICRDTDeserializer crdtDeserializer, ICRDTSerializer crdtSerializer, @@ -29,9 +32,31 @@ public SDKObservableEventsEngineAPIImplementation(ISharedPoolsProvider poolsProv crdtDeserializer, crdtSerializer, crdtWorldSynchronizer, outgoingCrtdMessagesProvider, systemGroupsUpdateGate, exceptionsHandler, multithreadSync) { } - public bool EnableSDKObservableMessagesDetection { get; set; } = false; + public void TryAddSubscription(string eventId) + { + if (eventId == SDKObservableEventIds.PlayerClicked) + { + ReportHub.LogWarning(new ReportData(ReportCategory.SDK_OBSERVABLES), "Scene subscribed to unsupported SDK Observable 'PlayerClicked'"); + return; + } + + sdkObservableEventSubscriptions.Add(eventId); + enableSDKObservableMessagesDetection = true; + } + + public void RemoveSubscriptionIfExists(string eventId) + { + sdkObservableEventSubscriptions.Remove(eventId); + + if (sdkObservableEventSubscriptions.Count == 0) + enableSDKObservableMessagesDetection = false; + } + + public bool HasSubscription(string eventId) => + sdkObservableEventSubscriptions.Contains(eventId); - public HashSet SdkObservableEventSubscriptions { get; } = new (); + public bool IsAnySubscription() => + sdkObservableEventSubscriptions.Count > 0; public void AddSDKObservableEvent(SDKObservableEvent sdkObservableEvent) { @@ -60,7 +85,7 @@ public override void SetIsDisposing() { userIdEntitiesMap.Clear(); sdkObservableEvents.Clear(); - SdkObservableEventSubscriptions.Clear(); + sdkObservableEventSubscriptions.Clear(); base.SetIsDisposing(); } @@ -77,7 +102,7 @@ private void DetectObservableEventsFromComponents(OutgoingCRDTMessagesProvider.P // for scenes that may subscribe to observables later in their execution DetectPlayerIdentityDataComponent(pendingMessage); - if (!EnableSDKObservableMessagesDetection) return; + if (!enableSDKObservableMessagesDetection) return; DetectOtherObservableComponents(pendingMessage); } @@ -92,10 +117,10 @@ private void DetectPlayerIdentityDataComponent(OutgoingCRDTMessagesProvider.Pend { case CRDTMessageType.PUT_COMPONENT: // onEnterScene + playerConnected observables - if (EnableSDKObservableMessagesDetection) + if (enableSDKObservableMessagesDetection) { - bool onEnterSceneSubscribed = SdkObservableEventSubscriptions.Contains(SDKObservableEventIds.EnterScene); - bool onPlayerConnectedSubscribed = SdkObservableEventSubscriptions.Contains(SDKObservableEventIds.PlayerConnected); + bool onEnterSceneSubscribed = sdkObservableEventSubscriptions.Contains(SDKObservableEventIds.EnterScene); + bool onPlayerConnectedSubscribed = sdkObservableEventSubscriptions.Contains(SDKObservableEventIds.PlayerConnected); if (onEnterSceneSubscribed) sdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.EnterScene, new UserIdPayload @@ -116,15 +141,15 @@ private void DetectPlayerIdentityDataComponent(OutgoingCRDTMessagesProvider.Pend if (userIdEntitiesMap.ContainsKey(pendingMessage.Entity)) // we may get more than 1 DELETE_COMPONENT of the same component { // onLeaveScene + playerDisconnected observables - if (EnableSDKObservableMessagesDetection) + if (enableSDKObservableMessagesDetection) { - if (SdkObservableEventSubscriptions.Contains(SDKObservableEventIds.LeaveScene)) + if (sdkObservableEventSubscriptions.Contains(SDKObservableEventIds.LeaveScene)) sdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.LeaveScene, new UserIdPayload { userId = userIdEntitiesMap[pendingMessage.Entity], })); - if (SdkObservableEventSubscriptions.Contains(SDKObservableEventIds.PlayerDisconnected)) + if (sdkObservableEventSubscriptions.Contains(SDKObservableEventIds.PlayerDisconnected)) sdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.PlayerDisconnected, new UserIdPayload { userId = userIdEntitiesMap[pendingMessage.Entity], @@ -148,7 +173,7 @@ private void DetectOtherObservableComponents(OutgoingCRDTMessagesProvider.Pendin case ComponentID.ENGINE_INFO: // onSceneReady observable if (reportedSceneReady) break; - if (SdkObservableEventSubscriptions.Contains(SDKObservableEventIds.SceneReady)) + if (sdkObservableEventSubscriptions.Contains(SDKObservableEventIds.SceneReady)) { sdkObservableEvents.Add(SDKObservableUtils.NewSDKObservableEventFromData(SDKObservableEventIds.SceneReady, new SceneReadyPayload())); reportedSceneReady = true; @@ -156,7 +181,7 @@ private void DetectOtherObservableComponents(OutgoingCRDTMessagesProvider.Pendin break; case ComponentID.REALM_INFO: // onRealmChanged observables - if (SdkObservableEventSubscriptions.Contains(SDKObservableEventIds.RealmChanged)) + if (sdkObservableEventSubscriptions.Contains(SDKObservableEventIds.RealmChanged)) { var realmInfo = (PBRealmInfo)message.Message; @@ -172,7 +197,7 @@ private void DetectOtherObservableComponents(OutgoingCRDTMessagesProvider.Pendin break; case ComponentID.AVATAR_EQUIPPED_DATA: // profileChanged observable case ComponentID.AVATAR_BASE: // profileChanged observable - if (SdkObservableEventSubscriptions.Contains(SDKObservableEventIds.ProfileChanged)) + if (sdkObservableEventSubscriptions.Contains(SDKObservableEventIds.ProfileChanged)) { if (!userIdEntitiesMap.ContainsKey(message.Entity)) break; @@ -191,7 +216,7 @@ private void DetectOtherObservableComponents(OutgoingCRDTMessagesProvider.Pendin break; case CRDTMessageType.APPEND_COMPONENT: if (message.Bridge.Id == ComponentID.AVATAR_EMOTE_COMMAND) - if (SdkObservableEventSubscriptions.Contains(SDKObservableEventIds.PlayerExpression)) + if (sdkObservableEventSubscriptions.Contains(SDKObservableEventIds.PlayerExpression)) { var avatarEmoteCommand = (PBAvatarEmoteCommand)message.Message; diff --git a/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/ISDKObservableEventsEngineApi.cs b/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/ISDKObservableEventsEngineApi.cs index 9c60f916e6..07b1bf30b9 100644 --- a/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/ISDKObservableEventsEngineApi.cs +++ b/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/ISDKObservableEventsEngineApi.cs @@ -1,6 +1,5 @@ using CrdtEcsBridge.PoolsProviders; using SceneRuntime.Apis.Modules.EngineApi.SDKObservableEvents.Events; -using System.Collections.Generic; namespace SceneRuntime.Apis.Modules.EngineApi.SDKObservableEvents { @@ -12,8 +11,12 @@ public interface ISDKObservableEventsEngineApi : IEngineApi PoolableSDKObservableEventArray? ConsumeSDKObservableEvents(); - HashSet SdkObservableEventSubscriptions { get; } + void TryAddSubscription(string eventId); - bool EnableSDKObservableMessagesDetection { set; } + void RemoveSubscriptionIfExists(string eventId); + + bool HasSubscription(string eventId); + + bool IsAnySubscription(); } } diff --git a/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/SDKObservableEventsEngineApiWrapper.cs b/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/SDKObservableEventsEngineApiWrapper.cs index c01fe3ce6e..ed1b76aa37 100644 --- a/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/SDKObservableEventsEngineApiWrapper.cs +++ b/Explorer/Assets/Scripts/SceneRuntime/Apis/Modules/EngineApi/SDKObservableEvents/SDKObservableEventsEngineApiWrapper.cs @@ -1,5 +1,4 @@ using CrdtEcsBridge.PoolsProviders; -using DCL.Diagnostics; using JetBrains.Annotations; using SceneRunner.Scene.ExceptionsHandling; using SceneRuntime.Apis.Modules.CommunicationsControllerApi.SDKMessageBus; @@ -24,7 +23,7 @@ public SDKObservableEventsEngineApiWrapper(ISDKObservableEventsEngineApi api, IS public override ScriptableSDKObservableEventArray? SendBatch() { // If there are no subscriptions at all there is nothing to handle - if (engineApi.SdkObservableEventSubscriptions.Count == 0) + if (engineApi.IsAnySubscription() == false) { engineApi.ClearSDKObservableEvents(); commsApi.ClearMessages(); @@ -50,7 +49,7 @@ public SDKObservableEventsEngineApiWrapper(ISDKObservableEventsEngineApi api, IS private void DetectSceneMessageBusCommsObservableEvent() { - if (!engineApi.SdkObservableEventSubscriptions.Contains(SDKObservableEventIds.Comms)) + if (!engineApi.HasSubscription(SDKObservableEventIds.Comms)) return; if (commsApi.SceneCommsMessages.Count == 0) return; @@ -64,23 +63,13 @@ private void DetectSceneMessageBusCommsObservableEvent() [UsedImplicitly] public void SubscribeToSDKObservableEvent(string eventId) { - if (eventId == SDKObservableEventIds.PlayerClicked) - { - ReportHub.LogWarning(new ReportData(ReportCategory.SDK_OBSERVABLES), "Scene subscribed to unsupported SDK Observable 'PlayerClicked'"); - return; - } - - engineApi.SdkObservableEventSubscriptions.Add(eventId); - engineApi.EnableSDKObservableMessagesDetection = true; + engineApi.TryAddSubscription(eventId); } [UsedImplicitly] public void UnsubscribeFromSDKObservableEvent(string eventId) { - engineApi.SdkObservableEventSubscriptions.Remove(eventId); - - if (engineApi.SdkObservableEventSubscriptions.Count == 0) - engineApi.EnableSDKObservableMessagesDetection = false; + engineApi.RemoveSubscriptionIfExists(eventId); } } } diff --git a/Explorer/Assets/Scripts/SceneRuntime/Factory/SceneRuntimeFactory.cs b/Explorer/Assets/Scripts/SceneRuntime/Factory/SceneRuntimeFactory.cs index 046936faf0..65a30dda27 100644 --- a/Explorer/Assets/Scripts/SceneRuntime/Factory/SceneRuntimeFactory.cs +++ b/Explorer/Assets/Scripts/SceneRuntime/Factory/SceneRuntimeFactory.cs @@ -94,7 +94,7 @@ internal async UniTask CreateBySourceCodeAsync( // Provide basic Thread Pool synchronization context SynchronizationContext.SetSynchronizationContext(new SynchronizationContext()); string wrappedSource = WrapInModuleCommonJs(jsSceneLocalSourceCode.CodeForScene(sceneShortInfo.BaseParcel) ?? sourceCode); - return new SceneRuntimeImpl(wrappedSource, pair, moduleDictionary, engineFactory, activeEngines); + return new SceneRuntimeImpl(wrappedSource, pair, moduleDictionary, sceneShortInfo, engineFactory, activeEngines); } /// From c4fee02b2dbf9a063996e34c8f94cb15d988f4cb Mon Sep 17 00:00:00 2001 From: nickkhalow Date: Tue, 27 Aug 2024 21:16:49 +0700 Subject: [PATCH 10/27] messagePipesHub rename to communicationControllerHub --- .../CommunicationsControllerAPIImplementationBase.cs | 12 ++++++------ .../SDKMessageBusCommsAPIImplementation.cs | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs index f8d2ac6b1b..95a293dfb4 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs @@ -19,7 +19,7 @@ internal enum MsgType } protected readonly CancellationTokenSource cancellationTokenSource = new (); - protected readonly ICommunicationControllerHub messagePipesHub; + protected readonly ICommunicationControllerHub communicationControllerHub; protected readonly ISceneData sceneData; protected readonly ISceneStateProvider sceneStateProvider; protected readonly IJsOperations jsOperations; @@ -29,12 +29,12 @@ internal enum MsgType public CommunicationsControllerAPIImplementationBase( ISceneData sceneData, - ICommunicationControllerHub messagePipesHub, + ICommunicationControllerHub communicationControllerHub, IJsOperations jsOperations, ISceneStateProvider sceneStateProvider) { this.sceneData = sceneData; - this.messagePipesHub = messagePipesHub; + this.communicationControllerHub = communicationControllerHub; this.jsOperations = jsOperations; this.sceneStateProvider = sceneStateProvider; @@ -51,9 +51,9 @@ public void Dispose() public void OnSceneIsCurrentChanged(bool isCurrent) { if (isCurrent) - messagePipesHub.SetSceneMessageHandler(onMessageReceivedCached); + communicationControllerHub.SetSceneMessageHandler(onMessageReceivedCached); else - messagePipesHub.RemoveSceneMessageHandler(onMessageReceivedCached); + communicationControllerHub.RemoveSceneMessageHandler(onMessageReceivedCached); } public object SendBinary(IReadOnlyList data) @@ -98,7 +98,7 @@ private void CleanUpReceivedMessages() private void SendMessage(ReadOnlySpan message) { - messagePipesHub.SendMessage(message, sceneData.SceneEntityDefinition.id!, cancellationTokenSource.Token); + communicationControllerHub.SendMessage(message, sceneData.SceneEntityDefinition.id!, cancellationTokenSource.Token); } protected virtual void OnMessageReceived(ICommunicationControllerHub.SceneMessage receivedMessage) { } diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs index 719e8992c1..0a44139569 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs @@ -14,7 +14,7 @@ public class SDKMessageBusCommsAPIImplementation : CommunicationsControllerAPIIm public IReadOnlyList SceneCommsMessages => messages; - public SDKMessageBusCommsAPIImplementation(ISceneData sceneData, ICommunicationControllerHub messagePipesHub, IJsOperations jsOperations, ISceneStateProvider sceneStateProvider) : base(sceneData, messagePipesHub, jsOperations, sceneStateProvider) { } + public SDKMessageBusCommsAPIImplementation(ISceneData sceneData, ICommunicationControllerHub communicationControllerHub, IJsOperations jsOperations, ISceneStateProvider sceneStateProvider) : base(sceneData, communicationControllerHub, jsOperations, sceneStateProvider) { } public void ClearMessages() { @@ -28,7 +28,7 @@ public void Send(string data) encodedMessage[0] = (byte)MsgType.String; dataBytes.CopyTo(encodedMessage[1..]); - messagePipesHub.SendMessage(encodedMessage, sceneData.SceneEntityDefinition.id, cancellationTokenSource.Token); + communicationControllerHub.SendMessage(encodedMessage, sceneData.SceneEntityDefinition.id, cancellationTokenSource.Token); } protected override void OnMessageReceived(ICommunicationControllerHub.SceneMessage receivedMessage) From f69a1a404850745bd664282218fd51ab54e54d31 Mon Sep 17 00:00:00 2001 From: nickkhalow Date: Tue, 27 Aug 2024 21:19:18 +0700 Subject: [PATCH 11/27] CommunicationsControllerAPIImplementationBase as abstract --- ...ommunicationsControllerAPIImplementationBase.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs index 95a293dfb4..3e9a9b8594 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs @@ -10,7 +10,7 @@ namespace CrdtEcsBridge.JsModulesImplementation.Communications { - public class CommunicationsControllerAPIImplementationBase : ICommunicationsControllerAPI + public abstract class CommunicationsControllerAPIImplementationBase : ICommunicationsControllerAPI { internal enum MsgType { @@ -18,16 +18,18 @@ internal enum MsgType Uint8Array = 2, } + protected readonly List> eventsToProcess = new (); protected readonly CancellationTokenSource cancellationTokenSource = new (); protected readonly ICommunicationControllerHub communicationControllerHub; protected readonly ISceneData sceneData; - protected readonly ISceneStateProvider sceneStateProvider; - protected readonly IJsOperations jsOperations; - protected readonly Action onMessageReceivedCached; - protected readonly List> eventsToProcess = new (); + + private readonly ISceneStateProvider sceneStateProvider; + private readonly IJsOperations jsOperations; + private readonly Action onMessageReceivedCached; + internal IReadOnlyList> EventsToProcess => eventsToProcess; - public CommunicationsControllerAPIImplementationBase( + protected CommunicationsControllerAPIImplementationBase( ISceneData sceneData, ICommunicationControllerHub communicationControllerHub, IJsOperations jsOperations, From f8f72bb3859b4a2a8038520f3d7db8f8e43d442a Mon Sep 17 00:00:00 2001 From: nickkhalow Date: Tue, 27 Aug 2024 21:27:49 +0700 Subject: [PATCH 12/27] remove double decoding OnMessageReceived --- .../CommunicationsControllerAPIImplementation.cs | 11 ++++------- ...mmunicationsControllerAPIImplementationBase.cs | 15 +++++++++++++-- .../SDKMessageBusCommsAPIImplementation.cs | 9 +++------ 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementation.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementation.cs index adac8a0f14..14ab9b6664 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementation.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementation.cs @@ -25,18 +25,15 @@ public CommunicationsControllerAPIImplementation( this.crdtMemoryAllocator = crdtMemoryAllocator; } - protected override void OnMessageReceived(ICommunicationControllerHub.SceneMessage receivedMessage) + protected override void OnMessageReceived(MsgType messageType, ReadOnlySpan decodedMessage, string fromWalletId) { - ReadOnlySpan decodedMessage = receivedMessage.Data.Span; - MsgType msgType = DecodeMessage(ref decodedMessage); - - if (msgType != MsgType.Uint8Array || decodedMessage.Length == 0) + if (messageType != MsgType.Uint8Array) return; // Wallet Id - int walletBytesCount = Encoding.UTF8.GetByteCount(receivedMessage.FromWalletId); + int walletBytesCount = Encoding.UTF8.GetByteCount(fromWalletId); Span senderBytes = stackalloc byte[walletBytesCount]; - Encoding.UTF8.GetBytes(receivedMessage.FromWalletId, senderBytes); + Encoding.UTF8.GetBytes(fromWalletId, senderBytes); int messageLength = senderBytes.Length + decodedMessage.Length + 1; diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs index 3e9a9b8594..2a9abe8b0c 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs @@ -12,7 +12,7 @@ namespace CrdtEcsBridge.JsModulesImplementation.Communications { public abstract class CommunicationsControllerAPIImplementationBase : ICommunicationsControllerAPI { - internal enum MsgType + public enum MsgType { String = 1, // SDK scenes MessageBus messages Uint8Array = 2, @@ -103,7 +103,18 @@ private void SendMessage(ReadOnlySpan message) communicationControllerHub.SendMessage(message, sceneData.SceneEntityDefinition.id!, cancellationTokenSource.Token); } - protected virtual void OnMessageReceived(ICommunicationControllerHub.SceneMessage receivedMessage) { } + private void OnMessageReceived(ICommunicationControllerHub.SceneMessage receivedMessage) + { + ReadOnlySpan decodedMessage = receivedMessage.Data.Span; + MsgType msgType = DecodeMessage(ref decodedMessage); + + if (decodedMessage.Length == 0) + return; + + OnMessageReceived(msgType, decodedMessage, receivedMessage.FromWalletId); + } + + protected abstract void OnMessageReceived(MsgType messageType, ReadOnlySpan decodedMessage, string fromWalletId); internal static MsgType DecodeMessage(ref ReadOnlySpan value) { diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs index 0a44139569..4b1d08ad66 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs @@ -31,17 +31,14 @@ public void Send(string data) communicationControllerHub.SendMessage(encodedMessage, sceneData.SceneEntityDefinition.id, cancellationTokenSource.Token); } - protected override void OnMessageReceived(ICommunicationControllerHub.SceneMessage receivedMessage) + protected override void OnMessageReceived(MsgType messageType, ReadOnlySpan decodedMessage, string fromWalletId) { - ReadOnlySpan decodedMessage = receivedMessage.Data.Span; - MsgType msgType = DecodeMessage(ref decodedMessage); - - if (msgType != MsgType.String || decodedMessage.Length == 0) + if (messageType != MsgType.String) return; messages.Add(new CommsPayload { - sender = receivedMessage.FromWalletId, + sender = fromWalletId, message = Encoding.UTF8.GetString(decodedMessage) }); } From d3c0cb036f90c69b84f118ca92a093f4e9aedc09 Mon Sep 17 00:00:00 2001 From: nickkhalow Date: Tue, 27 Aug 2024 21:35:18 +0700 Subject: [PATCH 13/27] remove logic duplication for encoding --- ...icationsControllerAPIImplementationBase.cs | 30 ++++++------------- .../SDKMessageBusCommsAPIImplementation.cs | 6 +--- 2 files changed, 10 insertions(+), 26 deletions(-) diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs index 2a9abe8b0c..02d17610a5 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs @@ -19,10 +19,9 @@ public enum MsgType } protected readonly List> eventsToProcess = new (); - protected readonly CancellationTokenSource cancellationTokenSource = new (); - protected readonly ICommunicationControllerHub communicationControllerHub; - protected readonly ISceneData sceneData; - + private readonly CancellationTokenSource cancellationTokenSource = new (); + private readonly ICommunicationControllerHub communicationControllerHub; + private readonly ISceneData sceneData; private readonly ISceneStateProvider sceneStateProvider; private readonly IJsOperations jsOperations; private readonly Action onMessageReceivedCached; @@ -64,22 +63,8 @@ public object SendBinary(IReadOnlyList data) return jsOperations.ConvertToScriptTypedArrays(Array.Empty>()); foreach (PoolableByteArray poolable in data) - { - if (poolable.Length == 0) - continue; - - Memory message = poolable.Memory; - - EncodeAndSend(); - - void EncodeAndSend() - { - Span encodedMessage = stackalloc byte[message.Length + 1]; - encodedMessage[0] = (byte)MsgType.Uint8Array; - message.Span.CopyTo(encodedMessage[1..]); - SendMessage(encodedMessage); - } - } + if (poolable.Length > 0) + EncodeAndSendMessage(MsgType.Uint8Array, poolable.Memory.Span); lock (eventsToProcess) { @@ -98,8 +83,11 @@ private void CleanUpReceivedMessages() eventsToProcess.Clear(); } - private void SendMessage(ReadOnlySpan message) + protected void EncodeAndSendMessage(MsgType msgType, ReadOnlySpan message) { + Span encodedMessage = stackalloc byte[message.Length + 1]; + encodedMessage[0] = (byte)msgType; + message.CopyTo(encodedMessage[1..]); communicationControllerHub.SendMessage(message, sceneData.SceneEntityDefinition.id!, cancellationTokenSource.Token); } diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs index 4b1d08ad66..edcb839030 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs @@ -24,11 +24,7 @@ public void ClearMessages() public void Send(string data) { byte[] dataBytes = Encoding.UTF8.GetBytes(data); - Span encodedMessage = stackalloc byte[dataBytes.Length + 1]; - encodedMessage[0] = (byte)MsgType.String; - dataBytes.CopyTo(encodedMessage[1..]); - - communicationControllerHub.SendMessage(encodedMessage, sceneData.SceneEntityDefinition.id, cancellationTokenSource.Token); + EncodeAndSendMessage(MsgType.String, dataBytes); } protected override void OnMessageReceived(MsgType messageType, ReadOnlySpan decodedMessage, string fromWalletId) From 94621521fbfab717a13ddd524163684cf2840da7 Mon Sep 17 00:00:00 2001 From: nickkhalow Date: Tue, 27 Aug 2024 21:36:40 +0700 Subject: [PATCH 14/27] decode message private static --- ...ommunicationsControllerAPIImplementationBase.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs index 02d17610a5..624e99f27a 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs @@ -91,6 +91,13 @@ protected void EncodeAndSendMessage(MsgType msgType, ReadOnlySpan message) communicationControllerHub.SendMessage(message, sceneData.SceneEntityDefinition.id!, cancellationTokenSource.Token); } + private static MsgType DecodeMessage(ref ReadOnlySpan value) + { + var msgType = (MsgType)value[0]; + value = value[1..]; + return msgType; + } + private void OnMessageReceived(ICommunicationControllerHub.SceneMessage receivedMessage) { ReadOnlySpan decodedMessage = receivedMessage.Data.Span; @@ -103,12 +110,5 @@ private void OnMessageReceived(ICommunicationControllerHub.SceneMessage received } protected abstract void OnMessageReceived(MsgType messageType, ReadOnlySpan decodedMessage, string fromWalletId); - - internal static MsgType DecodeMessage(ref ReadOnlySpan value) - { - var msgType = (MsgType)value[0]; - value = value[1..]; - return msgType; - } } } From 8196ad0b5b2f685dc04d2c1b99dbcfe0399774a6 Mon Sep 17 00:00:00 2001 From: nickkhalow Date: Tue, 27 Aug 2024 21:39:33 +0700 Subject: [PATCH 15/27] ICommunicationControllerHub to ISceneCommunicationPipe --- ...ommunicationsControllerAPIImplementation.cs | 2 +- ...nicationsControllerAPIImplementationBase.cs | 16 ++++++++-------- ...rollerHub.cs => ISceneCommunicationPipe.cs} | 2 +- ...cs.meta => ISceneCommunicationPipe.cs.meta} | 0 .../SDKMessageBusCommsAPIImplementation.cs | 2 +- ...trollerHub.cs => SceneCommunicationPipe.cs} | 12 ++++++------ ....cs.meta => SceneCommunicationPipe.cs.meta} | 0 ...icationControllerAPIImplementationShould.cs | 18 +++++++++--------- .../Scripts/Global/SceneSharedContainer.cs | 2 +- .../Assets/Scripts/SceneRunner/SceneFactory.cs | 4 ++-- .../Scripts/SceneRunner/SceneInstanceDeps.cs | 6 +++--- .../SceneRunner/Tests/SceneFacadeShould.cs | 2 +- .../SceneRunner/Tests/SceneFactoryShould.cs | 2 +- 13 files changed, 34 insertions(+), 34 deletions(-) rename Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/{ICommunicationControllerHub.cs => ISceneCommunicationPipe.cs} (95%) rename Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/{ICommunicationControllerHub.cs.meta => ISceneCommunicationPipe.cs.meta} (100%) rename Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/{CommunicationControllerHub.cs => SceneCommunicationPipe.cs} (72%) rename Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/{CommunicationControllerHub.cs.meta => SceneCommunicationPipe.cs.meta} (100%) diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementation.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementation.cs index 14ab9b6664..141253ea43 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementation.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementation.cs @@ -13,7 +13,7 @@ public class CommunicationsControllerAPIImplementation : CommunicationsControlle public CommunicationsControllerAPIImplementation( ISceneData sceneData, - ICommunicationControllerHub messagePipesHub, + ISceneCommunicationPipe messagePipesHub, IJsOperations jsOperations, ICRDTMemoryAllocator crdtMemoryAllocator, ISceneStateProvider sceneStateProvider) : base( diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs index 624e99f27a..5088e825d3 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs @@ -20,22 +20,22 @@ public enum MsgType protected readonly List> eventsToProcess = new (); private readonly CancellationTokenSource cancellationTokenSource = new (); - private readonly ICommunicationControllerHub communicationControllerHub; + private readonly ISceneCommunicationPipe sceneCommunicationPipe; private readonly ISceneData sceneData; private readonly ISceneStateProvider sceneStateProvider; private readonly IJsOperations jsOperations; - private readonly Action onMessageReceivedCached; + private readonly Action onMessageReceivedCached; internal IReadOnlyList> EventsToProcess => eventsToProcess; protected CommunicationsControllerAPIImplementationBase( ISceneData sceneData, - ICommunicationControllerHub communicationControllerHub, + ISceneCommunicationPipe sceneCommunicationPipe, IJsOperations jsOperations, ISceneStateProvider sceneStateProvider) { this.sceneData = sceneData; - this.communicationControllerHub = communicationControllerHub; + this.sceneCommunicationPipe = sceneCommunicationPipe; this.jsOperations = jsOperations; this.sceneStateProvider = sceneStateProvider; @@ -52,9 +52,9 @@ public void Dispose() public void OnSceneIsCurrentChanged(bool isCurrent) { if (isCurrent) - communicationControllerHub.SetSceneMessageHandler(onMessageReceivedCached); + sceneCommunicationPipe.SetSceneMessageHandler(onMessageReceivedCached); else - communicationControllerHub.RemoveSceneMessageHandler(onMessageReceivedCached); + sceneCommunicationPipe.RemoveSceneMessageHandler(onMessageReceivedCached); } public object SendBinary(IReadOnlyList data) @@ -88,7 +88,7 @@ protected void EncodeAndSendMessage(MsgType msgType, ReadOnlySpan message) Span encodedMessage = stackalloc byte[message.Length + 1]; encodedMessage[0] = (byte)msgType; message.CopyTo(encodedMessage[1..]); - communicationControllerHub.SendMessage(message, sceneData.SceneEntityDefinition.id!, cancellationTokenSource.Token); + sceneCommunicationPipe.SendMessage(message, sceneData.SceneEntityDefinition.id!, cancellationTokenSource.Token); } private static MsgType DecodeMessage(ref ReadOnlySpan value) @@ -98,7 +98,7 @@ private static MsgType DecodeMessage(ref ReadOnlySpan value) return msgType; } - private void OnMessageReceived(ICommunicationControllerHub.SceneMessage receivedMessage) + private void OnMessageReceived(ISceneCommunicationPipe.SceneMessage receivedMessage) { ReadOnlySpan decodedMessage = receivedMessage.Data.Span; MsgType msgType = DecodeMessage(ref decodedMessage); diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/ICommunicationControllerHub.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/ISceneCommunicationPipe.cs similarity index 95% rename from Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/ICommunicationControllerHub.cs rename to Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/ISceneCommunicationPipe.cs index 62dd608cd1..86a4d44733 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/ICommunicationControllerHub.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/ISceneCommunicationPipe.cs @@ -5,7 +5,7 @@ namespace CrdtEcsBridge.JsModulesImplementation.Communications { - public interface ICommunicationControllerHub + public interface ISceneCommunicationPipe { void SetSceneMessageHandler(Action onSceneMessage); diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/ICommunicationControllerHub.cs.meta b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/ISceneCommunicationPipe.cs.meta similarity index 100% rename from Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/ICommunicationControllerHub.cs.meta rename to Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/ISceneCommunicationPipe.cs.meta diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs index edcb839030..5dc46a0529 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs @@ -14,7 +14,7 @@ public class SDKMessageBusCommsAPIImplementation : CommunicationsControllerAPIIm public IReadOnlyList SceneCommsMessages => messages; - public SDKMessageBusCommsAPIImplementation(ISceneData sceneData, ICommunicationControllerHub communicationControllerHub, IJsOperations jsOperations, ISceneStateProvider sceneStateProvider) : base(sceneData, communicationControllerHub, jsOperations, sceneStateProvider) { } + public SDKMessageBusCommsAPIImplementation(ISceneData sceneData, ISceneCommunicationPipe sceneCommunicationPipe, IJsOperations jsOperations, ISceneStateProvider sceneStateProvider) : base(sceneData, sceneCommunicationPipe, jsOperations, sceneStateProvider) { } public void ClearMessages() { diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationControllerHub.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SceneCommunicationPipe.cs similarity index 72% rename from Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationControllerHub.cs rename to Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SceneCommunicationPipe.cs index 09217d1c25..53927a5c75 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationControllerHub.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SceneCommunicationPipe.cs @@ -13,12 +13,12 @@ namespace CrdtEcsBridge.JsModulesImplementation.Communications /// We can't subscribe to the `Scene` message multiple times /// so Hub handles the subscription and the API implementation handles the message processing /// - public class CommunicationControllerHub : ICommunicationControllerHub + public class SceneCommunicationPipe : ISceneCommunicationPipe { - private Action? onSceneMessage; + private Action? onSceneMessage; private readonly IMessagePipe messagePipe; - public CommunicationControllerHub(IMessagePipesHub messagePipesHub) + public SceneCommunicationPipe(IMessagePipesHub messagePipesHub) { messagePipe = messagePipesHub.ScenePipe(); messagePipe.Subscribe(Packet.MessageOneofCase.Scene, InvokeCurrentHandler); @@ -27,15 +27,15 @@ public CommunicationControllerHub(IMessagePipesHub messagePipesHub) private void InvokeCurrentHandler(ReceivedMessage message) { using (message) - onSceneMessage?.Invoke(ICommunicationControllerHub.SceneMessage.CopyFrom(in message)); + onSceneMessage?.Invoke(ISceneCommunicationPipe.SceneMessage.CopyFrom(in message)); } - public void RemoveSceneMessageHandler(Action onSceneMessage) + public void RemoveSceneMessageHandler(Action onSceneMessage) { lock (this) { this.onSceneMessage -= onSceneMessage; } } - public void SetSceneMessageHandler(Action onSceneMessage) + public void SetSceneMessageHandler(Action onSceneMessage) { lock (this) { this.onSceneMessage += onSceneMessage; } } diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationControllerHub.cs.meta b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SceneCommunicationPipe.cs.meta similarity index 100% rename from Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationControllerHub.cs.meta rename to Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SceneCommunicationPipe.cs.meta diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Tests/CommunicationControllerAPIImplementationShould.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Tests/CommunicationControllerAPIImplementationShould.cs index 53cdabffdc..794c0bd951 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Tests/CommunicationControllerAPIImplementationShould.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Tests/CommunicationControllerAPIImplementationShould.cs @@ -22,7 +22,7 @@ namespace CrdtEcsBridge.JsModulesImplementation.Tests public class CommunicationControllerAPIImplementationShould { private CommunicationsControllerAPIImplementation api; - private TestCommunicationControllerHub communicationControllerHub; + private TestSceneCommunicationPipe sceneCommunicationPipe; private IMessagePipe messagePipe; private IJsOperations jsOperations; private ICRDTMemoryAllocator crdtMemoryAllocator; @@ -30,7 +30,7 @@ public class CommunicationControllerAPIImplementationShould [SetUp] public void SetUp() { - communicationControllerHub = new TestCommunicationControllerHub(); + sceneCommunicationPipe = new TestSceneCommunicationPipe(); ISceneData sceneData = Substitute.For(); sceneData.SceneEntityDefinition.Returns(new SceneEntityDefinition { id = "TEST_SCENE" }); @@ -39,7 +39,7 @@ public void SetUp() var sceneStateProvider = Substitute.For(); sceneStateProvider.IsCurrent.Returns(true); - api = new CommunicationsControllerAPIImplementation(sceneData, communicationControllerHub, jsOperations = Substitute.For(), crdtMemoryAllocator, sceneStateProvider); + api = new CommunicationsControllerAPIImplementation(sceneData, sceneCommunicationPipe, jsOperations = Substitute.For(), crdtMemoryAllocator, sceneStateProvider); api.OnSceneIsCurrentChanged(true); } @@ -58,7 +58,7 @@ public void SendBinary([Range(0, 5)] int outerArraySize, [Range(1, 500, 50)] int var expectedCalls = outerArray.Select(o => o.Prepend((byte)CommunicationsControllerAPIImplementation.MsgType.Uint8Array).ToArray()).ToList(); // Assert the 2d array is equal - CollectionAssert.AreEqual(expectedCalls, communicationControllerHub.sendMessageCalls); + CollectionAssert.AreEqual(expectedCalls, sceneCommunicationPipe.sendMessageCalls); // Assert JSOperations called jsOperations.Received().ConvertToScriptTypedArrays(api.EventsToProcess); @@ -73,7 +73,7 @@ public void OnMessageReceived() byte[] data = GetRandomBytes(50).Prepend((byte)CommunicationsControllerAPIImplementation.MsgType.Uint8Array).ToArray(); var receivedMessage = new ReceivedMessage(new Scene { Data = ByteString.CopyFrom(data), SceneId = SCENE_ID }, new Packet(), WALLET_ID, Substitute.For()); - communicationControllerHub.onSceneMessage.Invoke(ICommunicationControllerHub.SceneMessage.CopyFrom(receivedMessage)); + sceneCommunicationPipe.onSceneMessage.Invoke(ISceneCommunicationPipe.SceneMessage.CopyFrom(receivedMessage)); byte[] walletBytes = Encoding.UTF8.GetBytes(receivedMessage.FromWalletId); @@ -101,17 +101,17 @@ public void TearDown() } // This class exists because we can't mock ReadOnlySpan (ref structs) - private class TestCommunicationControllerHub : ICommunicationControllerHub + private class TestSceneCommunicationPipe : ISceneCommunicationPipe { internal readonly List sendMessageCalls = new (); - internal Action onSceneMessage; + internal Action onSceneMessage; - public void SetSceneMessageHandler(Action onSceneMessage) + public void SetSceneMessageHandler(Action onSceneMessage) { this.onSceneMessage = onSceneMessage; } - public void RemoveSceneMessageHandler(Action onSceneMessage) + public void RemoveSceneMessageHandler(Action onSceneMessage) { } diff --git a/Explorer/Assets/Scripts/Global/SceneSharedContainer.cs b/Explorer/Assets/Scripts/Global/SceneSharedContainer.cs index 345b12c85b..232a1b91b3 100644 --- a/Explorer/Assets/Scripts/Global/SceneSharedContainer.cs +++ b/Explorer/Assets/Scripts/Global/SceneSharedContainer.cs @@ -66,7 +66,7 @@ public static SceneSharedContainer Create(in StaticContainer staticContainer, webRequestController, roomHub, realmData, - new CommunicationControllerHub(messagePipesHub) + new SceneCommunicationPipe(messagePipesHub) ), }; } diff --git a/Explorer/Assets/Scripts/SceneRunner/SceneFactory.cs b/Explorer/Assets/Scripts/SceneRunner/SceneFactory.cs index c015124736..337b8c42d7 100644 --- a/Explorer/Assets/Scripts/SceneRunner/SceneFactory.cs +++ b/Explorer/Assets/Scripts/SceneRunner/SceneFactory.cs @@ -50,7 +50,7 @@ public class SceneFactory : ISceneFactory private readonly ISharedPoolsProvider sharedPoolsProvider; private readonly IMVCManager mvcManager; private readonly IRealmData? realmData; - private readonly ICommunicationControllerHub messagePipesHub; + private readonly ISceneCommunicationPipe messagePipesHub; private IGlobalWorldActions globalWorldActions = null!; @@ -70,7 +70,7 @@ public SceneFactory( IWebRequestController webRequestController, IRoomHub roomHub, IRealmData? realmData, - ICommunicationControllerHub messagePipesHub) + ISceneCommunicationPipe messagePipesHub) { this.ecsWorldFactory = ecsWorldFactory; this.sceneRuntimeFactory = sceneRuntimeFactory; diff --git a/Explorer/Assets/Scripts/SceneRunner/SceneInstanceDeps.cs b/Explorer/Assets/Scripts/SceneRunner/SceneInstanceDeps.cs index b1348162c8..5741208cf8 100644 --- a/Explorer/Assets/Scripts/SceneRunner/SceneInstanceDeps.cs +++ b/Explorer/Assets/Scripts/SceneRunner/SceneInstanceDeps.cs @@ -226,7 +226,7 @@ protected WithRuntimeAndJsAPIBase( IMVCManager mvcManager, IGlobalWorldActions globalWorldActions, IRealmData realmData, - ICommunicationControllerHub messagePipesHub) + ISceneCommunicationPipe messagePipesHub) : this( engineApi, new RestrictedActionsAPIImplementation(mvcManager, syncDeps.ecsWorldSharedDependencies.SceneStateProvider, globalWorldActions, syncDeps.sceneData), @@ -251,7 +251,7 @@ internal class WithRuntimeAndJsAPI : WithRuntimeAndJsAPIBase { public WithRuntimeAndJsAPI (SceneInstanceDependencies syncDeps, SceneRuntimeImpl sceneRuntime, ISharedPoolsProvider sharedPoolsProvider, ICRDTSerializer crdtSerializer, IMVCManager mvcManager, - IGlobalWorldActions globalWorldActions, IRealmData realmData, ICommunicationControllerHub messagePipesHub) + IGlobalWorldActions globalWorldActions, IRealmData realmData, ISceneCommunicationPipe messagePipesHub) : base(new EngineAPIImplementation( sharedPoolsProvider, syncDeps.PoolsProvider, @@ -270,7 +270,7 @@ internal class WithRuntimeJsAndSDKObservablesEngineAPI : WithRuntimeAndJsAPIBase { public WithRuntimeJsAndSDKObservablesEngineAPI (SceneInstanceDependencies syncDeps, SceneRuntimeImpl sceneRuntime, ISharedPoolsProvider sharedPoolsProvider, ICRDTSerializer crdtSerializer, IMVCManager mvcManager, - IGlobalWorldActions globalWorldActions, IRealmData realmData, ICommunicationControllerHub messagePipesHub) + IGlobalWorldActions globalWorldActions, IRealmData realmData, ISceneCommunicationPipe messagePipesHub) : base(new SDKObservableEventsEngineAPIImplementation( sharedPoolsProvider, syncDeps.PoolsProvider, diff --git a/Explorer/Assets/Scripts/SceneRunner/Tests/SceneFacadeShould.cs b/Explorer/Assets/Scripts/SceneRunner/Tests/SceneFacadeShould.cs index 089f5b5122..8d3cb0806e 100644 --- a/Explorer/Assets/Scripts/SceneRunner/Tests/SceneFacadeShould.cs +++ b/Explorer/Assets/Scripts/SceneRunner/Tests/SceneFacadeShould.cs @@ -106,7 +106,7 @@ public void SetUp() IWebRequestController.DEFAULT, new IRoomHub.Fake(), Substitute.For(), - Substitute.For() + Substitute.For() ); } diff --git a/Explorer/Assets/Scripts/SceneRunner/Tests/SceneFactoryShould.cs b/Explorer/Assets/Scripts/SceneRunner/Tests/SceneFactoryShould.cs index 94811976e8..2ee5017520 100644 --- a/Explorer/Assets/Scripts/SceneRunner/Tests/SceneFactoryShould.cs +++ b/Explorer/Assets/Scripts/SceneRunner/Tests/SceneFactoryShould.cs @@ -66,7 +66,7 @@ public void SetUp() IWebRequestController.DEFAULT, new IRoomHub.Fake(), Substitute.For(), - Substitute.For() + Substitute.For() ); } From 8822345c911fce0f5cfd56aab7031e89829105c8 Mon Sep 17 00:00:00 2001 From: nickkhalow Date: Wed, 28 Aug 2024 16:31:35 +0700 Subject: [PATCH 16/27] InitialSceneSyncMessagePipe --- .../DCL.Multiplayer.Connections.asmdef | 5 +- .../Messaging/Hubs/MessagePipesHub.cs | 10 ++-- .../Messaging/Pipe/IMessagePipe.cs | 5 ++ .../Pipe/InitialSceneSyncMessagePipe.cs | 53 +++++++++++++++++++ .../Pipe/InitialSceneSyncMessagePipe.cs.meta | 3 ++ .../Global/Dynamic/DynamicWorldContainer.cs | 3 +- .../EmptyScene/EmptySceneFacade.cs | 2 + .../Scripts/SceneRunner/Scene/ISceneFacade.cs | 1 + 8 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs create mode 100644 Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs.meta diff --git a/Explorer/Assets/DCL/Multiplayer/Connections/DCL.Multiplayer.Connections.asmdef b/Explorer/Assets/DCL/Multiplayer/Connections/DCL.Multiplayer.Connections.asmdef index 18767fef3b..0cc08eb50b 100644 --- a/Explorer/Assets/DCL/Multiplayer/Connections/DCL.Multiplayer.Connections.asmdef +++ b/Explorer/Assets/DCL/Multiplayer/Connections/DCL.Multiplayer.Connections.asmdef @@ -16,7 +16,10 @@ "GUID:e0eedfa2deb9406daf86fd8368728e39", "GUID:3640f3c0b42946b0b8794a1ed8e06ca5", "GUID:d414ef88f3b15f746a4b97636b50dfb4", - "GUID:ca4e81cdd6a34d1aa54c32ad41fc5b3b" + "GUID:ca4e81cdd6a34d1aa54c32ad41fc5b3b", + "GUID:1d2c76eb8b48e0b40940e8b31a679ce1", + "GUID:286980af24684da6acc1caa413039811", + "GUID:e25ef972de004615a22937e739de2def" ], "includePlatforms": [], "excludePlatforms": [], diff --git a/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Hubs/MessagePipesHub.cs b/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Hubs/MessagePipesHub.cs index b1d6865865..2e76cb9631 100644 --- a/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Hubs/MessagePipesHub.cs +++ b/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Hubs/MessagePipesHub.cs @@ -1,5 +1,6 @@ using DCL.Multiplayer.Connections.Messaging.Pipe; using DCL.Multiplayer.Connections.RoomHubs; +using ECS.SceneLifeCycle; using LiveKit.Internal.FFIClients.Pools; using LiveKit.Internal.FFIClients.Pools.Memory; @@ -10,9 +11,12 @@ public class MessagePipesHub : IMessagePipesHub private readonly IMessagePipe scenePipe; private readonly IMessagePipe islandPipe; - public MessagePipesHub(IRoomHub roomHub, IMultiPool sendingMultiPool, IMultiPool receivingMultiPool, IMemoryPool memoryPool) : this( - new MessagePipe(roomHub.SceneRoom().DataPipe, sendingMultiPool, receivingMultiPool, memoryPool).WithLog("Scene"), - new MessagePipe(roomHub.IslandRoom().DataPipe, sendingMultiPool, receivingMultiPool, memoryPool).WithLog("Island") + public MessagePipesHub(IRoomHub roomHub, IMultiPool sendingMultiPool, IMultiPool receivingMultiPool, IMemoryPool memoryPool, IScenesCache scenesCache) : this( + new MessagePipe(roomHub.SceneRoom().DataPipe, sendingMultiPool, receivingMultiPool, memoryPool) + .WithLog("Scene") + .WithInitialSceneSync(roomHub.SceneRoom(), scenesCache), + new MessagePipe(roomHub.IslandRoom().DataPipe, sendingMultiPool, receivingMultiPool, memoryPool) + .WithLog("Island") ) { } public MessagePipesHub(IMessagePipe scenePipe, IMessagePipe islandPipe) diff --git a/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/IMessagePipe.cs b/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/IMessagePipe.cs index e2299bbbf9..ed0087f4d8 100644 --- a/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/IMessagePipe.cs +++ b/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/IMessagePipe.cs @@ -1,5 +1,7 @@ using Decentraland.Kernel.Comms.Rfc4; +using ECS.SceneLifeCycle; using Google.Protobuf; +using LiveKit.Rooms; using System; namespace DCL.Multiplayer.Connections.Messaging.Pipe @@ -30,5 +32,8 @@ public static class MessagePipeExtensions { public static LogMessagePipe WithLog(this IMessagePipe messagePipe, string fromPipe) => new (messagePipe, fromPipe); + + public static InitialSceneSyncMessagePipe WithInitialSceneSync(this IMessagePipe messagePipe, IRoom room, IScenesCache scenesCache) => + new (messagePipe, room, scenesCache); } } diff --git a/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs b/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs new file mode 100644 index 0000000000..1ebce3430b --- /dev/null +++ b/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs @@ -0,0 +1,53 @@ +using Decentraland.Kernel.Comms.Rfc4; +using ECS.SceneLifeCycle; +using Google.Protobuf; +using LiveKit.Rooms; +using System; +using System.Threading; +using Utility; + +namespace DCL.Multiplayer.Connections.Messaging.Pipe +{ + public class InitialSceneSyncMessagePipe : IMessagePipe + { + private readonly IMessagePipe origin; + private readonly IRoom room; + private readonly IScenesCache scenesCache; + private readonly CancellationTokenSource cancellationTokenSource = new (); + + public InitialSceneSyncMessagePipe(IMessagePipe origin, IRoom room, IScenesCache scenesCache) + { + room.ConnectionUpdated += RoomOnConnectionUpdated; + this.origin = origin; + this.room = room; + this.scenesCache = scenesCache; + } + + private void RoomOnConnectionUpdated(IRoom room, ConnectionUpdate connectionupdate) + { + var message = origin.NewMessage(); + message.Payload.SceneId = SceneId(); + ReadOnlySpan span = stackalloc byte[] { 2 }; + message.Payload.Data = ByteString.CopyFrom(span); + message.SendAndDisposeAsync(cancellationTokenSource.Token).Forget(); + } + + private string SceneId() => + scenesCache.CurrentScene.SceneData.SceneEntityDefinition.id ?? string.Empty; + + public void Dispose() + { + cancellationTokenSource.SafeCancelAndDispose(); + room.ConnectionUpdated -= RoomOnConnectionUpdated; + origin.Dispose(); + } + + public MessageWrap NewMessage() where T: class, IMessage, new() => + origin.NewMessage(); + + public void Subscribe(Packet.MessageOneofCase ofCase, Action> onMessageReceived) where T: class, IMessage, new() + { + origin.Subscribe(ofCase, onMessageReceived); + } + } +} diff --git a/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs.meta b/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs.meta new file mode 100644 index 0000000000..5a215e722c --- /dev/null +++ b/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: e27a63c8469e436e9c1f007d13dd3eea +timeCreated: 1724835663 \ No newline at end of file diff --git a/Explorer/Assets/Scripts/Global/Dynamic/DynamicWorldContainer.cs b/Explorer/Assets/Scripts/Global/Dynamic/DynamicWorldContainer.cs index ac48c5c840..ab3e45e0bf 100644 --- a/Explorer/Assets/Scripts/Global/Dynamic/DynamicWorldContainer.cs +++ b/Explorer/Assets/Scripts/Global/Dynamic/DynamicWorldContainer.cs @@ -75,6 +75,7 @@ using MVC; using MVC.PopupsController.PopupCloser; using SceneRunner.Debugging.Hub; +using SceneRunner.Scene; using System; using System.Buffers; using System.Collections.Generic; @@ -285,7 +286,7 @@ async UniTask InitializeContainersAsync(IPluginSettingsContainer settingsContain container.localSceneDevelopmentController = new LocalSceneDevelopmentController(container.reloadSceneController, dynamicWorldParams.LocalSceneDevelopmentRealm); container.RoomHub = localSceneDevelopment ? NullRoomHub.INSTANCE : new RoomHub(archipelagoIslandRoom, gateKeeperSceneRoom); - container.MessagePipesHub = new MessagePipesHub(container.RoomHub, multiPoolFactory()!, multiPoolFactory()!, memoryPool); + container.MessagePipesHub = new MessagePipesHub(container.RoomHub, multiPoolFactory()!, multiPoolFactory()!, memoryPool, staticContainer.ScenesCache); RoomsStatus roomsStatus = new RoomsStatus( container.RoomHub, diff --git a/Explorer/Assets/Scripts/SceneRunner/EmptyScene/EmptySceneFacade.cs b/Explorer/Assets/Scripts/SceneRunner/EmptyScene/EmptySceneFacade.cs index f0bcad07d3..024ab9d91e 100644 --- a/Explorer/Assets/Scripts/SceneRunner/EmptyScene/EmptySceneFacade.cs +++ b/Explorer/Assets/Scripts/SceneRunner/EmptyScene/EmptySceneFacade.cs @@ -5,6 +5,7 @@ using DCL.PluginSystem.World; using SceneRunner.Scene; using System; +using System.Collections.Generic; using System.Threading; using UnityEngine; using UnityEngine.Pool; @@ -25,6 +26,7 @@ private EmptySceneFacade() { } public PersistentEntities PersistentEntities => default; public bool IsEmpty => true; + public ISceneData SceneData { get; } = new EmptySceneData(new List()); public void Dispose() { diff --git a/Explorer/Assets/Scripts/SceneRunner/Scene/ISceneFacade.cs b/Explorer/Assets/Scripts/SceneRunner/Scene/ISceneFacade.cs index cfea145130..87941da5ee 100644 --- a/Explorer/Assets/Scripts/SceneRunner/Scene/ISceneFacade.cs +++ b/Explorer/Assets/Scripts/SceneRunner/Scene/ISceneFacade.cs @@ -14,6 +14,7 @@ public interface ISceneFacade : IUniTaskAsyncDisposable, IDisposable SceneEcsExecutor EcsExecutor { get; } PersistentEntities PersistentEntities { get; } bool IsEmpty { get; } + ISceneData SceneData { get; } void Initialize(); From 5b66a1ee0f7d3408fa60132d964fe5fdee5fb785 Mon Sep 17 00:00:00 2001 From: nickkhalow Date: Wed, 28 Aug 2024 16:47:38 +0700 Subject: [PATCH 17/27] constant sync --- .../Pipe/InitialSceneSyncMessagePipe.cs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs b/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs index 1ebce3430b..d7c07c01ee 100644 --- a/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs +++ b/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs @@ -1,3 +1,4 @@ +using Cysharp.Threading.Tasks; using Decentraland.Kernel.Comms.Rfc4; using ECS.SceneLifeCycle; using Google.Protobuf; @@ -21,9 +22,19 @@ public InitialSceneSyncMessagePipe(IMessagePipe origin, IRoom room, IScenesCache this.origin = origin; this.room = room; this.scenesCache = scenesCache; + SendEvent().Forget(); } - private void RoomOnConnectionUpdated(IRoom room, ConnectionUpdate connectionupdate) + private async UniTaskVoid SendEvent() + { + while (cancellationTokenSource.IsCancellationRequested == false) + { + await UniTask.Delay(TimeSpan.FromSeconds(0.5f)); + SyncScene(); + } + } + + private void SyncScene() { var message = origin.NewMessage(); message.Payload.SceneId = SceneId(); @@ -32,6 +43,11 @@ private void RoomOnConnectionUpdated(IRoom room, ConnectionUpdate connectionupda message.SendAndDisposeAsync(cancellationTokenSource.Token).Forget(); } + private void RoomOnConnectionUpdated(IRoom room, ConnectionUpdate connectionupdate) + { + SyncScene(); + } + private string SceneId() => scenesCache.CurrentScene.SceneData.SceneEntityDefinition.id ?? string.Empty; From e240c5d6e80831f71d34721c33698ae875cb65dc Mon Sep 17 00:00:00 2001 From: nickkhalow Date: Wed, 28 Aug 2024 18:10:57 +0700 Subject: [PATCH 18/27] fix nullables --- .../Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs b/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs index d7c07c01ee..7981af68a6 100644 --- a/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs +++ b/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs @@ -49,7 +49,7 @@ private void RoomOnConnectionUpdated(IRoom room, ConnectionUpdate connectionupda } private string SceneId() => - scenesCache.CurrentScene.SceneData.SceneEntityDefinition.id ?? string.Empty; + scenesCache?.CurrentScene?.SceneData?.SceneEntityDefinition?.id ?? string.Empty; public void Dispose() { From 6a9ee973ec65cfaf13353f97459632d84be5db8a Mon Sep 17 00:00:00 2001 From: nickkhalow Date: Wed, 28 Aug 2024 18:25:16 +0700 Subject: [PATCH 19/27] check zero byte --- .../Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs b/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs index 7981af68a6..c3085354fd 100644 --- a/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs +++ b/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs @@ -38,7 +38,7 @@ private void SyncScene() { var message = origin.NewMessage(); message.Payload.SceneId = SceneId(); - ReadOnlySpan span = stackalloc byte[] { 2 }; + ReadOnlySpan span = stackalloc byte[] { 2, 0 }; message.Payload.Data = ByteString.CopyFrom(span); message.SendAndDisposeAsync(cancellationTokenSource.Token).Forget(); } From f11bbe4a00b62d8f1cc8b674aefcf49d4794c313 Mon Sep 17 00:00:00 2001 From: nickkhalow Date: Wed, 28 Aug 2024 18:39:30 +0700 Subject: [PATCH 20/27] Revert "check zero byte" This reverts commit 6a9ee973ec65cfaf13353f97459632d84be5db8a. --- .../Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs b/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs index c3085354fd..7981af68a6 100644 --- a/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs +++ b/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs @@ -38,7 +38,7 @@ private void SyncScene() { var message = origin.NewMessage(); message.Payload.SceneId = SceneId(); - ReadOnlySpan span = stackalloc byte[] { 2, 0 }; + ReadOnlySpan span = stackalloc byte[] { 2 }; message.Payload.Data = ByteString.CopyFrom(span); message.SendAndDisposeAsync(cancellationTokenSource.Token).Forget(); } From a44efec9f02c46c2d80326be12c22de0bd84a7a7 Mon Sep 17 00:00:00 2001 From: nickkhalow Date: Wed, 28 Aug 2024 18:41:14 +0700 Subject: [PATCH 21/27] remove content size check --- .../CommunicationsControllerAPIImplementationBase.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs index 5088e825d3..e7dc5cd6de 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs @@ -103,8 +103,9 @@ private void OnMessageReceived(ISceneCommunicationPipe.SceneMessage receivedMess ReadOnlySpan decodedMessage = receivedMessage.Data.Span; MsgType msgType = DecodeMessage(ref decodedMessage); - if (decodedMessage.Length == 0) - return; + //TODO solve + // if (decodedMessage.Length == 0) + // return; OnMessageReceived(msgType, decodedMessage, receivedMessage.FromWalletId); } From fb820103f111fa48263ca5882fe43a824d2f996b Mon Sep 17 00:00:00 2001 From: nickkhalow Date: Wed, 28 Aug 2024 18:44:12 +0700 Subject: [PATCH 22/27] Revert "remove content size check" This reverts commit a44efec9f02c46c2d80326be12c22de0bd84a7a7. --- .../CommunicationsControllerAPIImplementationBase.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs index e7dc5cd6de..5088e825d3 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs @@ -103,9 +103,8 @@ private void OnMessageReceived(ISceneCommunicationPipe.SceneMessage receivedMess ReadOnlySpan decodedMessage = receivedMessage.Data.Span; MsgType msgType = DecodeMessage(ref decodedMessage); - //TODO solve - // if (decodedMessage.Length == 0) - // return; + if (decodedMessage.Length == 0) + return; OnMessageReceived(msgType, decodedMessage, receivedMessage.FromWalletId); } From 400b270844ba7544426167f2e1e5073692739cce Mon Sep 17 00:00:00 2001 From: nickkhalow Date: Wed, 28 Aug 2024 18:47:02 +0700 Subject: [PATCH 23/27] special msg signal send --- .../Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs b/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs index 7981af68a6..5a6eb9f12c 100644 --- a/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs +++ b/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs @@ -38,7 +38,7 @@ private void SyncScene() { var message = origin.NewMessage(); message.Payload.SceneId = SceneId(); - ReadOnlySpan span = stackalloc byte[] { 2 }; + ReadOnlySpan span = stackalloc byte[] { 2, 2 }; message.Payload.Data = ByteString.CopyFrom(span); message.SendAndDisposeAsync(cancellationTokenSource.Token).Forget(); } From bac87718572c86f84fd63da1c04b7a610a400c4d Mon Sep 17 00:00:00 2001 From: nickkhalow Date: Wed, 28 Aug 2024 18:52:11 +0700 Subject: [PATCH 24/27] Revert "special msg signal send" This reverts commit 400b270844ba7544426167f2e1e5073692739cce. --- .../Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs b/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs index 5a6eb9f12c..7981af68a6 100644 --- a/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs +++ b/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs @@ -38,7 +38,7 @@ private void SyncScene() { var message = origin.NewMessage(); message.Payload.SceneId = SceneId(); - ReadOnlySpan span = stackalloc byte[] { 2, 2 }; + ReadOnlySpan span = stackalloc byte[] { 2 }; message.Payload.Data = ByteString.CopyFrom(span); message.SendAndDisposeAsync(cancellationTokenSource.Token).Forget(); } From 13d550d4bc97b05b58ea69fe65c23ac02c613ff1 Mon Sep 17 00:00:00 2001 From: nickkhalow Date: Thu, 29 Aug 2024 15:17:49 +0700 Subject: [PATCH 25/27] remove unused pipe --- .../Messaging/Hubs/MessagePipesHub.cs | 6 +- .../Messaging/Pipe/IMessagePipe.cs | 3 - .../Pipe/InitialSceneSyncMessagePipe.cs | 69 ------------------- .../Pipe/InitialSceneSyncMessagePipe.cs.meta | 3 - .../Global/Dynamic/DynamicWorldContainer.cs | 2 +- 5 files changed, 3 insertions(+), 80 deletions(-) delete mode 100644 Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs delete mode 100644 Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs.meta diff --git a/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Hubs/MessagePipesHub.cs b/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Hubs/MessagePipesHub.cs index 2e76cb9631..b7a77c483e 100644 --- a/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Hubs/MessagePipesHub.cs +++ b/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Hubs/MessagePipesHub.cs @@ -1,6 +1,5 @@ using DCL.Multiplayer.Connections.Messaging.Pipe; using DCL.Multiplayer.Connections.RoomHubs; -using ECS.SceneLifeCycle; using LiveKit.Internal.FFIClients.Pools; using LiveKit.Internal.FFIClients.Pools.Memory; @@ -11,10 +10,9 @@ public class MessagePipesHub : IMessagePipesHub private readonly IMessagePipe scenePipe; private readonly IMessagePipe islandPipe; - public MessagePipesHub(IRoomHub roomHub, IMultiPool sendingMultiPool, IMultiPool receivingMultiPool, IMemoryPool memoryPool, IScenesCache scenesCache) : this( + public MessagePipesHub(IRoomHub roomHub, IMultiPool sendingMultiPool, IMultiPool receivingMultiPool, IMemoryPool memoryPool) : this( new MessagePipe(roomHub.SceneRoom().DataPipe, sendingMultiPool, receivingMultiPool, memoryPool) - .WithLog("Scene") - .WithInitialSceneSync(roomHub.SceneRoom(), scenesCache), + .WithLog("Scene"), new MessagePipe(roomHub.IslandRoom().DataPipe, sendingMultiPool, receivingMultiPool, memoryPool) .WithLog("Island") ) { } diff --git a/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/IMessagePipe.cs b/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/IMessagePipe.cs index 84be2b1aa3..14c02216a8 100644 --- a/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/IMessagePipe.cs +++ b/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/IMessagePipe.cs @@ -30,8 +30,5 @@ public static class MessagePipeBuilder { public static LogMessagePipe WithLog(this IMessagePipe messagePipe, string fromPipe) => new (messagePipe, fromPipe); - - public static InitialSceneSyncMessagePipe WithInitialSceneSync(this IMessagePipe messagePipe, IRoom room, IScenesCache scenesCache) => - new (messagePipe, room, scenesCache); } } diff --git a/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs b/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs deleted file mode 100644 index 7981af68a6..0000000000 --- a/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs +++ /dev/null @@ -1,69 +0,0 @@ -using Cysharp.Threading.Tasks; -using Decentraland.Kernel.Comms.Rfc4; -using ECS.SceneLifeCycle; -using Google.Protobuf; -using LiveKit.Rooms; -using System; -using System.Threading; -using Utility; - -namespace DCL.Multiplayer.Connections.Messaging.Pipe -{ - public class InitialSceneSyncMessagePipe : IMessagePipe - { - private readonly IMessagePipe origin; - private readonly IRoom room; - private readonly IScenesCache scenesCache; - private readonly CancellationTokenSource cancellationTokenSource = new (); - - public InitialSceneSyncMessagePipe(IMessagePipe origin, IRoom room, IScenesCache scenesCache) - { - room.ConnectionUpdated += RoomOnConnectionUpdated; - this.origin = origin; - this.room = room; - this.scenesCache = scenesCache; - SendEvent().Forget(); - } - - private async UniTaskVoid SendEvent() - { - while (cancellationTokenSource.IsCancellationRequested == false) - { - await UniTask.Delay(TimeSpan.FromSeconds(0.5f)); - SyncScene(); - } - } - - private void SyncScene() - { - var message = origin.NewMessage(); - message.Payload.SceneId = SceneId(); - ReadOnlySpan span = stackalloc byte[] { 2 }; - message.Payload.Data = ByteString.CopyFrom(span); - message.SendAndDisposeAsync(cancellationTokenSource.Token).Forget(); - } - - private void RoomOnConnectionUpdated(IRoom room, ConnectionUpdate connectionupdate) - { - SyncScene(); - } - - private string SceneId() => - scenesCache?.CurrentScene?.SceneData?.SceneEntityDefinition?.id ?? string.Empty; - - public void Dispose() - { - cancellationTokenSource.SafeCancelAndDispose(); - room.ConnectionUpdated -= RoomOnConnectionUpdated; - origin.Dispose(); - } - - public MessageWrap NewMessage() where T: class, IMessage, new() => - origin.NewMessage(); - - public void Subscribe(Packet.MessageOneofCase ofCase, Action> onMessageReceived) where T: class, IMessage, new() - { - origin.Subscribe(ofCase, onMessageReceived); - } - } -} diff --git a/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs.meta b/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs.meta deleted file mode 100644 index 5a215e722c..0000000000 --- a/Explorer/Assets/DCL/Multiplayer/Connections/Messaging/Pipe/InitialSceneSyncMessagePipe.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: e27a63c8469e436e9c1f007d13dd3eea -timeCreated: 1724835663 \ No newline at end of file diff --git a/Explorer/Assets/Scripts/Global/Dynamic/DynamicWorldContainer.cs b/Explorer/Assets/Scripts/Global/Dynamic/DynamicWorldContainer.cs index bfd26cc4cf..a76c2c1ec6 100644 --- a/Explorer/Assets/Scripts/Global/Dynamic/DynamicWorldContainer.cs +++ b/Explorer/Assets/Scripts/Global/Dynamic/DynamicWorldContainer.cs @@ -292,7 +292,7 @@ async UniTask InitializeContainersAsync(IPluginSettingsContainer settingsContain container.localSceneDevelopmentController = new LocalSceneDevelopmentController(container.reloadSceneController, dynamicWorldParams.LocalSceneDevelopmentRealm); container.RoomHub = localSceneDevelopment ? NullRoomHub.INSTANCE : new RoomHub(archipelagoIslandRoom, gateKeeperSceneRoom); - container.MessagePipesHub = new MessagePipesHub(container.RoomHub, multiPoolFactory()!, multiPoolFactory()!, memoryPool, staticContainer.ScenesCache); + container.MessagePipesHub = new MessagePipesHub(container.RoomHub, multiPoolFactory()!, multiPoolFactory()!, memoryPool); RoomsStatus roomsStatus = new RoomsStatus( container.RoomHub, From e48ba502aeae56ef5a6117c2d60442ca4e0957e4 Mon Sep 17 00:00:00 2001 From: nickkhalow Date: Thu, 29 Aug 2024 20:24:14 +0700 Subject: [PATCH 26/27] fix encoding EncodeAndSendMessage --- .../CommunicationsControllerAPIImplementationBase.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs index 5088e825d3..7118a6ace0 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/CommunicationsControllerAPIImplementationBase.cs @@ -88,7 +88,7 @@ protected void EncodeAndSendMessage(MsgType msgType, ReadOnlySpan message) Span encodedMessage = stackalloc byte[message.Length + 1]; encodedMessage[0] = (byte)msgType; message.CopyTo(encodedMessage[1..]); - sceneCommunicationPipe.SendMessage(message, sceneData.SceneEntityDefinition.id!, cancellationTokenSource.Token); + sceneCommunicationPipe.SendMessage(encodedMessage, sceneData.SceneEntityDefinition.id!, cancellationTokenSource.Token); } private static MsgType DecodeMessage(ref ReadOnlySpan value) From 9d3806484d37d50282de01bda68b43aa05fd5136 Mon Sep 17 00:00:00 2001 From: nickkhalow Date: Mon, 9 Sep 2024 22:00:25 +0700 Subject: [PATCH 27/27] fix unsigned commits --- .../SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs index 2a7573c96e..8d940df14b 100644 --- a/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs +++ b/Explorer/Assets/Scripts/CrdtEcsBridge/JsModulesImplementation/Communications/SDKMessageBus/SDKMessageBusCommsAPIImplementation.cs @@ -15,7 +15,8 @@ public class SDKMessageBusCommsAPIImplementation : CommunicationsControllerAPIIm public IReadOnlyList SceneCommsMessages => messages; - public SDKMessageBusCommsAPIImplementation(ISceneData sceneData, ISceneCommunicationPipe sceneCommunicationPipe, IJsOperations jsOperations, ISceneStateProvider sceneStateProvider) : base(sceneData, sceneCommunicationPipe, jsOperations, sceneStateProvider) { } + public SDKMessageBusCommsAPIImplementation(IRealmData realmData, ISceneData sceneData, ISceneCommunicationPipe sceneCommunicationPipe, IJsOperations jsOperations, ISceneStateProvider sceneStateProvider) + : base(realmData, sceneData, sceneCommunicationPipe, jsOperations, sceneStateProvider) { } public void ClearMessages() {