| | 1 | | using DCL; |
| | 2 | | using DCL.Models; |
| | 3 | | using Decentraland.Renderer.RendererServices; |
| | 4 | | using MainScripts.DCL.Components; |
| | 5 | | using System; |
| | 6 | | using System.Collections.Concurrent; |
| | 7 | | using System.Runtime.InteropServices; |
| | 8 | | using UnityEngine; |
| | 9 | | using Ray = DCL.Models.Ray; |
| | 10 | |
|
| | 11 | | public class NativeBridgeCommunication : IKernelCommunication |
| | 12 | | { |
| | 13 | | private delegate void JS_Delegate_V(); |
| | 14 | | private delegate void JS_Delegate_Query(Protocol.QueryPayload a); |
| | 15 | | private delegate void JS_Delegate_VS(string a); |
| | 16 | | private delegate void JS_Delegate_VI(int a); |
| | 17 | | private delegate void JS_Delegate_VIS(int a, string b); |
| | 18 | | private delegate void JS_Delegate_VSS(string a, string b); |
| | 19 | | private delegate void JS_Delegate_VSSS(string a, string b, string c); |
| | 20 | |
|
| | 21 | | // TODO: this is repeated |
| | 22 | | private delegate void JS_Delegate_VII(int a, int b); |
| | 23 | |
|
| 1 | 24 | | private static readonly byte[] PREALLOCATED_READER_BUFFER = new byte[88388608]; |
| | 25 | |
|
| | 26 | | private static string currentEntityId; |
| | 27 | | private static int currentSceneNumber; |
| | 28 | | private static string currentTag; |
| | 29 | |
|
| | 30 | | private static IMessageQueueHandler queueHandler; |
| | 31 | |
|
| 5 | 32 | | public NativeBridgeCommunication(IMessageQueueHandler queueHandler) |
| | 33 | | { |
| 5 | 34 | | NativeBridgeCommunication.queueHandler = queueHandler; |
| | 35 | |
|
| | 36 | | #if UNITY_WEBGL && !UNITY_EDITOR |
| | 37 | | SetCallback_CreateEntity(CreateEntity); |
| | 38 | | SetCallback_RemoveEntity(RemoveEntity); |
| | 39 | | SetCallback_SceneReady(SceneReady); |
| | 40 | |
|
| | 41 | | SetCallback_SetEntityId(SetEntityId); |
| | 42 | | // @deprecated use SetSceneNumber |
| | 43 | | SetCallback_SetSceneId(SetSceneId); |
| | 44 | | SetCallback_SetSceneNumber(SetSceneNumber); |
| | 45 | | SetCallback_SetTag(SetTag); |
| | 46 | |
|
| | 47 | | SetCallback_SetEntityParent(SetEntityParent); |
| | 48 | |
|
| | 49 | | SetCallback_EntityComponentCreateOrUpdate(EntityComponentCreateOrUpdate); |
| | 50 | | SetCallback_EntityComponentDestroy(EntityComponentDestroy); |
| | 51 | |
|
| | 52 | | SetCallback_SharedComponentCreate(SharedComponentCreate); |
| | 53 | | SetCallback_SharedComponentAttach(SharedComponentAttach); |
| | 54 | | SetCallback_SharedComponentUpdate(SharedComponentUpdate); |
| | 55 | | SetCallback_SharedComponentDispose(SharedComponentDispose); |
| | 56 | |
|
| | 57 | | SetCallback_OpenExternalUrl(OpenExternalUrl); |
| | 58 | | SetCallback_OpenNftDialog(OpenNftDialog); |
| | 59 | |
|
| | 60 | | SetCallback_Query(Query); |
| | 61 | | SetCallback_Sdk6BinaryMessage(Sdk6BinaryMessage); |
| | 62 | | #endif |
| 5 | 63 | | } |
| | 64 | |
|
| 0 | 65 | | public void Dispose() { } |
| | 66 | |
|
| | 67 | | [DllImport("__Internal")] private static extern void SetCallback_Sdk6BinaryMessage(JS_Delegate_VII callback); |
| | 68 | |
|
| | 69 | | [MonoPInvokeCallback(typeof(JS_Delegate_VII))] |
| | 70 | | internal static unsafe void Sdk6BinaryMessage(int intPtr, int length) |
| | 71 | | { |
| 0 | 72 | | var ptr = new IntPtr(intPtr); |
| | 73 | |
|
| 0 | 74 | | var readonlySpan = new ReadOnlySpan<byte>(ptr.ToPointer(), length); |
| 0 | 75 | | readonlySpan.CopyTo(PREALLOCATED_READER_BUFFER); |
| | 76 | |
|
| | 77 | | try |
| | 78 | | { |
| 0 | 79 | | RendererManyEntityActions sceneRequest = RendererManyEntityActions.Parser.ParseFrom(PREALLOCATED_READER_BUFF |
| | 80 | |
|
| 0 | 81 | | for (var i = 0; i < sceneRequest.Actions.Count; i++) |
| 0 | 82 | | queueHandler.EnqueueSceneMessage( |
| | 83 | | SDK6DataMapExtensions.SceneMessageFromSdk6Message(sceneRequest.Actions[i], currentSceneNumber) |
| | 84 | | ); |
| 0 | 85 | | } |
| 0 | 86 | | catch (Exception e) { Debug.LogError(e); } |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | [DllImport("__Internal")] private static extern void SetCallback_SetTag(JS_Delegate_VS callback); |
| | 90 | |
|
| | 91 | | [MonoPInvokeCallback(typeof(JS_Delegate_VS))] |
| | 92 | | internal static void SetTag(string id) => |
| 5 | 93 | | currentTag = id; |
| | 94 | |
|
| | 95 | | [DllImport("__Internal")] private static extern void SetCallback_SetSceneNumber(JS_Delegate_VI callback); |
| | 96 | |
|
| | 97 | | [MonoPInvokeCallback(typeof(JS_Delegate_VI))] |
| | 98 | | internal static void SetSceneNumber(int sceneNumber) => |
| 5 | 99 | | currentSceneNumber = sceneNumber; |
| | 100 | |
|
| | 101 | | [DllImport("__Internal")] private static extern void SetCallback_SceneReady(JS_Delegate_V callback); |
| | 102 | |
|
| | 103 | | [MonoPInvokeCallback(typeof(JS_Delegate_V))] |
| | 104 | | internal static void SceneReady() => |
| 1 | 105 | | EnqueueSceneMessage(MessagingTypes.INIT_DONE, new Protocol.SceneReady()); |
| | 106 | |
|
| | 107 | | [DllImport("__Internal")] private static extern void SetCallback_CreateEntity(JS_Delegate_V callback); |
| | 108 | |
|
| | 109 | | [MonoPInvokeCallback(typeof(JS_Delegate_V))] |
| | 110 | | internal static void CreateEntity() => |
| 4 | 111 | | EnqueueSceneMessage(MessagingTypes.ENTITY_CREATE, new Protocol.CreateEntity { entityId = currentEntityId }); |
| | 112 | |
|
| | 113 | | [DllImport("__Internal")] private static extern void SetCallback_RemoveEntity(JS_Delegate_V callback); |
| | 114 | |
|
| | 115 | | [MonoPInvokeCallback(typeof(JS_Delegate_V))] |
| | 116 | | internal static void RemoveEntity() => |
| 2 | 117 | | EnqueueSceneMessage(MessagingTypes.ENTITY_DESTROY, new Protocol.RemoveEntity { entityId = currentEntityId }); |
| | 118 | |
|
| | 119 | | [DllImport("__Internal")] private static extern void SetCallback_SetEntityId(JS_Delegate_VS callback); |
| | 120 | |
|
| | 121 | | [MonoPInvokeCallback(typeof(JS_Delegate_VS))] |
| | 122 | | internal static void SetEntityId(string id) => |
| 6 | 123 | | currentEntityId = id; |
| | 124 | |
|
| | 125 | | [DllImport("__Internal")] private static extern void SetCallback_SetEntityParent(JS_Delegate_VS callback); |
| | 126 | |
|
| | 127 | | [MonoPInvokeCallback(typeof(JS_Delegate_VS))] |
| | 128 | | internal static void SetEntityParent(string parentId) => |
| 1 | 129 | | EnqueueSceneMessage(MessagingTypes.ENTITY_REPARENT, new Protocol.SetEntityParent { entityId = currentEntityId, p |
| | 130 | |
|
| | 131 | | [DllImport("__Internal")] private static extern void SetCallback_EntityComponentCreateOrUpdate(JS_Delegate_VIS callb |
| | 132 | |
|
| | 133 | | [MonoPInvokeCallback(typeof(JS_Delegate_VIS))] |
| | 134 | | internal static void EntityComponentCreateOrUpdate(int classId, string json) => |
| 1 | 135 | | EnqueueSceneMessage(MessagingTypes.ENTITY_COMPONENT_CREATE_OR_UPDATE, new Protocol.EntityComponentCreateOrUpdate |
| | 136 | |
|
| | 137 | | [DllImport("__Internal")] private static extern void SetCallback_EntityComponentDestroy(JS_Delegate_VS callback); |
| | 138 | |
|
| | 139 | | [MonoPInvokeCallback(typeof(JS_Delegate_VS))] |
| | 140 | | internal static void EntityComponentDestroy(string name) => |
| 1 | 141 | | EnqueueSceneMessage(MessagingTypes.ENTITY_COMPONENT_DESTROY, new Protocol.EntityComponentDestroy { entityId = cu |
| | 142 | |
|
| | 143 | | [DllImport("__Internal")] private static extern void SetCallback_SharedComponentCreate(JS_Delegate_VIS callback); |
| | 144 | |
|
| | 145 | | [MonoPInvokeCallback(typeof(JS_Delegate_VIS))] |
| | 146 | | internal static void SharedComponentCreate(int classId, string id) => |
| 1 | 147 | | EnqueueSceneMessage(MessagingTypes.SHARED_COMPONENT_CREATE, new Protocol.SharedComponentCreate { id = id, classI |
| | 148 | |
|
| | 149 | | [DllImport("__Internal")] private static extern void SetCallback_SharedComponentDispose(JS_Delegate_VS callback); |
| | 150 | |
|
| | 151 | | [MonoPInvokeCallback(typeof(JS_Delegate_VS))] |
| | 152 | | internal static void SharedComponentDispose(string id) => |
| 1 | 153 | | EnqueueSceneMessage(MessagingTypes.SHARED_COMPONENT_DISPOSE, new Protocol.SharedComponentDispose { id = id }); |
| | 154 | |
|
| | 155 | | [DllImport("__Internal")] private static extern void SetCallback_SharedComponentAttach(JS_Delegate_VSS callback); |
| | 156 | |
|
| | 157 | | [MonoPInvokeCallback(typeof(JS_Delegate_VSS))] |
| | 158 | | internal static void SharedComponentAttach(string id, string name) => |
| 1 | 159 | | EnqueueSceneMessage(MessagingTypes.SHARED_COMPONENT_ATTACH, new Protocol.SharedComponentAttach { entityId = curr |
| | 160 | |
|
| | 161 | | [DllImport("__Internal")] private static extern void SetCallback_SharedComponentUpdate(JS_Delegate_VSS callback); |
| | 162 | |
|
| | 163 | | [MonoPInvokeCallback(typeof(JS_Delegate_VSS))] |
| | 164 | | internal static void SharedComponentUpdate(string id, string json) => |
| 1 | 165 | | EnqueueSceneMessage(MessagingTypes.SHARED_COMPONENT_UPDATE, new Protocol.SharedComponentUpdate { componentId = i |
| | 166 | |
|
| | 167 | | [DllImport("__Internal")] private static extern void SetCallback_OpenExternalUrl(JS_Delegate_VS callback); |
| | 168 | |
|
| | 169 | | [MonoPInvokeCallback(typeof(JS_Delegate_VS))] |
| | 170 | | internal static void OpenExternalUrl(string url) => |
| 0 | 171 | | EnqueueSceneMessage(MessagingTypes.OPEN_EXTERNAL_URL, new Protocol.OpenExternalUrl { url = url }); |
| | 172 | |
|
| | 173 | | [DllImport("__Internal")] private static extern void SetCallback_OpenNftDialog(JS_Delegate_VSSS callback); |
| | 174 | |
|
| | 175 | | [MonoPInvokeCallback(typeof(JS_Delegate_VSSS))] |
| | 176 | | internal static void OpenNftDialog(string contactAddress, string comment, string tokenId) => |
| 0 | 177 | | EnqueueSceneMessage(MessagingTypes.OPEN_NFT_DIALOG, new Protocol.OpenNftDialog { contactAddress = contactAddress |
| | 178 | |
|
| | 179 | | [DllImport("__Internal")] private static extern void SetCallback_Query(JS_Delegate_Query callback); |
| | 180 | |
|
| | 181 | | [MonoPInvokeCallback(typeof(JS_Delegate_Query))] |
| | 182 | | internal static void Query(Protocol.QueryPayload payload) |
| | 183 | | { |
| 1 | 184 | | var ray = new Ray |
| | 185 | | { |
| | 186 | | origin = payload.raycastPayload.origin, |
| | 187 | | direction = payload.raycastPayload.direction, |
| | 188 | | distance = payload.raycastPayload.distance, |
| | 189 | | }; |
| | 190 | |
|
| 1 | 191 | | var resultPayload = new QueryMessage |
| | 192 | | { |
| | 193 | | payload = new RaycastQuery |
| | 194 | | { |
| | 195 | | id = Convert.ToString(payload.raycastPayload.id), |
| | 196 | | raycastType = (RaycastType)payload.raycastPayload.raycastType, |
| | 197 | | ray = ray, |
| | 198 | | sceneNumber = currentSceneNumber, |
| | 199 | | }, |
| | 200 | | }; |
| | 201 | |
|
| 1 | 202 | | EnqueueSceneMessage(MessagingTypes.QUERY, resultPayload); |
| 1 | 203 | | } |
| | 204 | |
|
| | 205 | | private static void EnqueueSceneMessage(string messageType, object payload) |
| | 206 | | { |
| 15 | 207 | | QueuedSceneMessage_Scene queuedMessage = GetSceneMessageInstance(); |
| 15 | 208 | | queuedMessage.method = messageType; |
| 15 | 209 | | queuedMessage.payload = payload; |
| | 210 | |
|
| 15 | 211 | | queueHandler.EnqueueSceneMessage(queuedMessage); |
| | 212 | |
|
| | 213 | | QueuedSceneMessage_Scene GetSceneMessageInstance() |
| | 214 | | { |
| 15 | 215 | | var message = new QueuedSceneMessage_Scene(); |
| 15 | 216 | | message.sceneNumber = currentSceneNumber; |
| 15 | 217 | | message.tag = currentTag; |
| 15 | 218 | | message.type = QueuedSceneMessage.Type.SCENE_MESSAGE; |
| | 219 | |
|
| 15 | 220 | | return message; |
| | 221 | | } |
| 15 | 222 | | } |
| | 223 | |
|
| | 224 | | #region DEPRECATED |
| | 225 | | // @deprecated use SetSceneNumber |
| | 226 | | [DllImport("__Internal")] private static extern void SetCallback_SetSceneId(JS_Delegate_VS callback); |
| | 227 | |
|
| | 228 | | // @deprecated use SetSceneNumber |
| 0 | 229 | | [MonoPInvokeCallback(typeof(JS_Delegate_VI))] internal static void SetSceneId(string _) { } |
| | 230 | | #endregion |
| | 231 | | } |