| | 1 | | using System; |
| | 2 | | using System.Runtime.InteropServices; |
| | 3 | | using DCL; |
| | 4 | | using DCL.Models; |
| | 5 | |
|
| | 6 | | public class NativeBridgeCommunication : IKernelCommunication |
| | 7 | | { |
| | 8 | | private static string currentEntityId; |
| | 9 | | private static string currentSceneId; |
| | 10 | | private static int currentSceneNumber; |
| | 11 | | private static string currentTag; |
| | 12 | |
|
| | 13 | | private static IMessageQueueHandler queueHandler; |
| | 14 | |
|
| | 15 | | delegate void JS_Delegate_VI(int a); |
| | 16 | |
|
| | 17 | | delegate void JS_Delegate_VIS(int a, string b); |
| | 18 | |
|
| | 19 | | delegate void JS_Delegate_VSS(string a, string b); |
| | 20 | |
|
| | 21 | | delegate void JS_Delegate_VSSS(string a, string b, string c); |
| | 22 | |
|
| | 23 | | delegate void JS_Delegate_VS(string a); |
| | 24 | |
|
| | 25 | | delegate void JS_Delegate_Query(Protocol.QueryPayload a); |
| | 26 | |
|
| | 27 | | delegate void JS_Delegate_V(); |
| | 28 | |
|
| 5 | 29 | | public NativeBridgeCommunication(IMessageQueueHandler queueHandler) |
| | 30 | | { |
| 5 | 31 | | NativeBridgeCommunication.queueHandler = queueHandler; |
| | 32 | | #if UNITY_WEBGL && !UNITY_EDITOR |
| | 33 | | SetCallback_CreateEntity(CreateEntity); |
| | 34 | | SetCallback_RemoveEntity(RemoveEntity); |
| | 35 | | SetCallback_SceneReady(SceneReady); |
| | 36 | |
|
| | 37 | | SetCallback_SetEntityId(SetEntityId); |
| | 38 | | SetCallback_SetSceneId(SetSceneId); |
| | 39 | | SetCallback_SetSceneNumber(SetSceneNumber); |
| | 40 | | SetCallback_SetTag(SetTag); |
| | 41 | |
|
| | 42 | | SetCallback_SetEntityParent(SetEntityParent); |
| | 43 | |
|
| | 44 | | SetCallback_EntityComponentCreateOrUpdate(EntityComponentCreateOrUpdate); |
| | 45 | | SetCallback_EntityComponentDestroy(EntityComponentDestroy); |
| | 46 | |
|
| | 47 | | SetCallback_SharedComponentCreate(SharedComponentCreate); |
| | 48 | | SetCallback_SharedComponentAttach(SharedComponentAttach); |
| | 49 | | SetCallback_SharedComponentUpdate(SharedComponentUpdate); |
| | 50 | | SetCallback_SharedComponentDispose(SharedComponentDispose); |
| | 51 | |
|
| | 52 | | SetCallback_OpenExternalUrl(OpenExternalUrl); |
| | 53 | | SetCallback_OpenNftDialog(OpenNftDialog); |
| | 54 | |
|
| | 55 | | SetCallback_Query(Query); |
| | 56 | | #endif |
| 5 | 57 | | } |
| | 58 | | public void Dispose() |
| | 59 | | { |
| | 60 | |
|
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | [MonoPInvokeCallback(typeof(JS_Delegate_VSSS))] |
| | 64 | | internal static void OpenNftDialog(string contactAddress, string comment, string tokenId) |
| | 65 | | { |
| 0 | 66 | | QueuedSceneMessage_Scene queuedMessage = GetSceneMessageInstance(); |
| | 67 | |
|
| 0 | 68 | | Protocol.OpenNftDialog payload = new Protocol.OpenNftDialog |
| | 69 | | { |
| | 70 | | contactAddress = contactAddress, |
| | 71 | | comment = comment, |
| | 72 | | tokenId = tokenId |
| | 73 | | }; |
| | 74 | |
|
| 0 | 75 | | queuedMessage.payload = payload; |
| 0 | 76 | | queuedMessage.method = MessagingTypes.OPEN_NFT_DIALOG; |
| | 77 | |
|
| 0 | 78 | | queueHandler.EnqueueSceneMessage(queuedMessage); |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | [MonoPInvokeCallback(typeof(JS_Delegate_VS))] |
| | 82 | | internal static void OpenExternalUrl(string url) |
| | 83 | | { |
| 0 | 84 | | QueuedSceneMessage_Scene queuedMessage = GetSceneMessageInstance(); |
| | 85 | |
|
| 0 | 86 | | Protocol.OpenExternalUrl payload = new Protocol.OpenExternalUrl |
| | 87 | | { |
| | 88 | | url = url |
| | 89 | | }; |
| | 90 | |
|
| 0 | 91 | | queuedMessage.payload = payload; |
| 0 | 92 | | queuedMessage.method = MessagingTypes.OPEN_EXTERNAL_URL; |
| | 93 | |
|
| 0 | 94 | | queueHandler.EnqueueSceneMessage(queuedMessage); |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | [MonoPInvokeCallback(typeof(JS_Delegate_VS))] |
| | 98 | | internal static void EntityComponentDestroy(string name) |
| | 99 | | { |
| 1 | 100 | | QueuedSceneMessage_Scene queuedMessage = GetSceneMessageInstance(); |
| | 101 | |
|
| 1 | 102 | | Protocol.EntityComponentDestroy payload = new Protocol.EntityComponentDestroy |
| | 103 | | { |
| | 104 | | entityId = currentEntityId, |
| | 105 | | name = name |
| | 106 | | }; |
| | 107 | |
|
| 1 | 108 | | queuedMessage.payload = payload; |
| 1 | 109 | | queuedMessage.method = MessagingTypes.ENTITY_COMPONENT_DESTROY; |
| | 110 | |
|
| 1 | 111 | | queueHandler.EnqueueSceneMessage(queuedMessage); |
| 1 | 112 | | } |
| | 113 | |
|
| | 114 | | [MonoPInvokeCallback(typeof(JS_Delegate_VSS))] |
| | 115 | | internal static void SharedComponentAttach(string id, string name) |
| | 116 | | { |
| 1 | 117 | | QueuedSceneMessage_Scene queuedMessage = GetSceneMessageInstance(); |
| | 118 | |
|
| 1 | 119 | | Protocol.SharedComponentAttach payload = new Protocol.SharedComponentAttach |
| | 120 | | { |
| | 121 | | entityId = currentEntityId, |
| | 122 | | id = id, |
| | 123 | | name = name |
| | 124 | | }; |
| | 125 | |
|
| 1 | 126 | | queuedMessage.payload = payload; |
| 1 | 127 | | queuedMessage.method = MessagingTypes.SHARED_COMPONENT_ATTACH; |
| | 128 | |
|
| 1 | 129 | | queueHandler.EnqueueSceneMessage(queuedMessage); |
| 1 | 130 | | } |
| | 131 | |
|
| | 132 | | [MonoPInvokeCallback(typeof(JS_Delegate_Query))] |
| | 133 | | internal static void Query(Protocol.QueryPayload payload) |
| | 134 | | { |
| 1 | 135 | | QueuedSceneMessage_Scene queuedMessage = GetSceneMessageInstance(); |
| | 136 | |
|
| 1 | 137 | | string queryId = Convert.ToString(payload.raycastPayload.id); |
| | 138 | |
|
| 1 | 139 | | RaycastType raycastType = (RaycastType) payload.raycastPayload.raycastType; |
| | 140 | |
|
| 1 | 141 | | Ray ray = new Ray() |
| | 142 | | { |
| | 143 | | origin = payload.raycastPayload.origin, |
| | 144 | | direction = payload.raycastPayload.direction, |
| | 145 | | distance = payload.raycastPayload.distance |
| | 146 | | }; |
| | 147 | |
|
| 1 | 148 | | queuedMessage.method = MessagingTypes.QUERY; |
| 1 | 149 | | queuedMessage.payload = new QueryMessage() |
| | 150 | | { |
| | 151 | | payload = new RaycastQuery() |
| | 152 | | { |
| | 153 | | id = queryId, |
| | 154 | | raycastType = raycastType, |
| | 155 | | ray = ray, |
| | 156 | | sceneId = currentSceneId |
| | 157 | | } |
| | 158 | | }; |
| | 159 | |
|
| 1 | 160 | | queueHandler.EnqueueSceneMessage(queuedMessage); |
| 1 | 161 | | } |
| | 162 | |
|
| | 163 | | [MonoPInvokeCallback(typeof(JS_Delegate_VSS))] |
| | 164 | | internal static void SharedComponentUpdate(string id, string json) |
| | 165 | | { |
| 1 | 166 | | QueuedSceneMessage_Scene queuedMessage = GetSceneMessageInstance(); |
| | 167 | |
|
| 1 | 168 | | Protocol.SharedComponentUpdate payload = |
| | 169 | | new Protocol.SharedComponentUpdate |
| | 170 | | { |
| | 171 | | componentId = id, |
| | 172 | | json = json |
| | 173 | | }; |
| | 174 | |
|
| 1 | 175 | | queuedMessage.payload = payload; |
| 1 | 176 | | queuedMessage.method = MessagingTypes.SHARED_COMPONENT_UPDATE; |
| | 177 | |
|
| 1 | 178 | | queueHandler.EnqueueSceneMessage(queuedMessage); |
| 1 | 179 | | } |
| | 180 | |
|
| | 181 | | [MonoPInvokeCallback(typeof(JS_Delegate_VS))] |
| | 182 | | internal static void SharedComponentDispose(string id) |
| | 183 | | { |
| 1 | 184 | | QueuedSceneMessage_Scene queuedMessage = GetSceneMessageInstance(); |
| | 185 | |
|
| 1 | 186 | | Protocol.SharedComponentDispose payload = |
| | 187 | | new Protocol.SharedComponentDispose |
| | 188 | | { |
| | 189 | | id = id |
| | 190 | | }; |
| | 191 | |
|
| 1 | 192 | | queuedMessage.payload = payload; |
| 1 | 193 | | queuedMessage.method = MessagingTypes.SHARED_COMPONENT_DISPOSE; |
| | 194 | |
|
| 1 | 195 | | queueHandler.EnqueueSceneMessage(queuedMessage); |
| 1 | 196 | | } |
| | 197 | |
|
| | 198 | | [MonoPInvokeCallback(typeof(JS_Delegate_VIS))] |
| | 199 | | internal static void SharedComponentCreate(int classId, string id) |
| | 200 | | { |
| 1 | 201 | | QueuedSceneMessage_Scene queuedMessage = GetSceneMessageInstance(); |
| | 202 | |
|
| 1 | 203 | | Protocol.SharedComponentCreate payload = |
| | 204 | | new Protocol.SharedComponentCreate |
| | 205 | | { |
| | 206 | | id = id, |
| | 207 | | classId = classId |
| | 208 | | }; |
| | 209 | |
|
| 1 | 210 | | queuedMessage.payload = payload; |
| 1 | 211 | | queuedMessage.method = MessagingTypes.SHARED_COMPONENT_CREATE; |
| | 212 | |
|
| 1 | 213 | | queueHandler.EnqueueSceneMessage(queuedMessage); |
| 1 | 214 | | } |
| | 215 | |
|
| | 216 | | [MonoPInvokeCallback(typeof(JS_Delegate_VIS))] |
| | 217 | | internal static void EntityComponentCreateOrUpdate(int classId, string json) |
| | 218 | | { |
| 1 | 219 | | QueuedSceneMessage_Scene queuedMessage = GetSceneMessageInstance(); |
| | 220 | |
|
| 1 | 221 | | Protocol.EntityComponentCreateOrUpdate payload = |
| | 222 | | new Protocol.EntityComponentCreateOrUpdate |
| | 223 | | { |
| | 224 | | entityId = currentEntityId, |
| | 225 | | classId = classId, |
| | 226 | | json = json |
| | 227 | | }; |
| | 228 | |
|
| 1 | 229 | | queuedMessage.payload = payload; |
| 1 | 230 | | queuedMessage.method = MessagingTypes.ENTITY_COMPONENT_CREATE_OR_UPDATE; |
| | 231 | |
|
| 1 | 232 | | queueHandler.EnqueueSceneMessage(queuedMessage); |
| 1 | 233 | | } |
| | 234 | |
|
| | 235 | | [MonoPInvokeCallback(typeof(JS_Delegate_VS))] |
| | 236 | | internal static void SetEntityParent(string parentId) |
| | 237 | | { |
| 1 | 238 | | QueuedSceneMessage_Scene queuedMessage = GetSceneMessageInstance(); |
| | 239 | |
|
| 1 | 240 | | Protocol.SetEntityParent payload = |
| | 241 | | new Protocol.SetEntityParent |
| | 242 | | { |
| | 243 | | entityId = currentEntityId, |
| | 244 | | parentId = parentId |
| | 245 | | }; |
| | 246 | |
|
| 1 | 247 | | queuedMessage.payload = payload; |
| 1 | 248 | | queuedMessage.method = MessagingTypes.ENTITY_REPARENT; |
| | 249 | |
|
| 1 | 250 | | queueHandler.EnqueueSceneMessage(queuedMessage); |
| 1 | 251 | | } |
| | 252 | |
|
| | 253 | | [MonoPInvokeCallback(typeof(JS_Delegate_VS))] |
| 0 | 254 | | internal static void SetEntityId(string id) { currentEntityId = id; } |
| | 255 | |
|
| | 256 | | [MonoPInvokeCallback(typeof(JS_Delegate_VS))] |
| 10 | 257 | | internal static void SetSceneId(string id) { currentSceneId = id; } |
| | 258 | |
|
| | 259 | | [MonoPInvokeCallback(typeof(JS_Delegate_VI))] |
| 10 | 260 | | internal static void SetSceneNumber(int sceneNumber) { currentSceneNumber = sceneNumber; } |
| | 261 | |
|
| | 262 | | [MonoPInvokeCallback(typeof(JS_Delegate_VS))] |
| 10 | 263 | | internal static void SetTag(string id) { currentTag = id; } |
| | 264 | |
|
| | 265 | | [MonoPInvokeCallback(typeof(JS_Delegate_V))] |
| | 266 | | internal static void CreateEntity() |
| | 267 | | { |
| 4 | 268 | | QueuedSceneMessage_Scene queuedMessage = GetSceneMessageInstance(); |
| | 269 | |
|
| 4 | 270 | | Protocol.CreateEntity payload = |
| | 271 | | new Protocol.CreateEntity |
| | 272 | | { |
| | 273 | | entityId = currentEntityId |
| | 274 | | }; |
| | 275 | |
|
| 4 | 276 | | queuedMessage.payload = payload; |
| 4 | 277 | | queuedMessage.method = MessagingTypes.ENTITY_CREATE; |
| | 278 | |
|
| 4 | 279 | | queueHandler.EnqueueSceneMessage(queuedMessage); |
| 4 | 280 | | } |
| | 281 | |
|
| | 282 | | [MonoPInvokeCallback(typeof(JS_Delegate_V))] |
| | 283 | | internal static void RemoveEntity() |
| | 284 | | { |
| 2 | 285 | | QueuedSceneMessage_Scene queuedMessage = GetSceneMessageInstance(); |
| 2 | 286 | | Protocol.RemoveEntity payload = |
| | 287 | | new Protocol.RemoveEntity() |
| | 288 | | { |
| | 289 | | entityId = currentEntityId |
| | 290 | | }; |
| | 291 | |
|
| 2 | 292 | | queuedMessage.payload = payload; |
| 2 | 293 | | queuedMessage.method = MessagingTypes.ENTITY_DESTROY; |
| | 294 | |
|
| 2 | 295 | | queueHandler.EnqueueSceneMessage(queuedMessage); |
| 2 | 296 | | } |
| | 297 | |
|
| | 298 | | [MonoPInvokeCallback(typeof(JS_Delegate_V))] |
| | 299 | | internal static void SceneReady() |
| | 300 | | { |
| 1 | 301 | | QueuedSceneMessage_Scene queuedMessage = GetSceneMessageInstance(); |
| 1 | 302 | | queuedMessage.method = MessagingTypes.INIT_DONE; |
| 1 | 303 | | queuedMessage.payload = new Protocol.SceneReady(); |
| | 304 | |
|
| 1 | 305 | | queueHandler.EnqueueSceneMessage(queuedMessage); |
| 1 | 306 | | } |
| | 307 | |
|
| | 308 | | internal static QueuedSceneMessage_Scene GetSceneMessageInstance() |
| | 309 | | { |
| 15 | 310 | | var sceneMessagesPool = queueHandler.sceneMessagesPool; |
| | 311 | |
|
| 15 | 312 | | if (!sceneMessagesPool.TryDequeue(out QueuedSceneMessage_Scene message)) |
| | 313 | | { |
| 15 | 314 | | message = new QueuedSceneMessage_Scene(); |
| | 315 | | } |
| | 316 | |
|
| 15 | 317 | | message.sceneId = currentSceneId; |
| 15 | 318 | | message.sceneNumber = currentSceneNumber; |
| 15 | 319 | | message.tag = currentTag; |
| 15 | 320 | | message.type = QueuedSceneMessage.Type.SCENE_MESSAGE; |
| | 321 | |
|
| 15 | 322 | | return message; |
| | 323 | | } |
| | 324 | |
|
| | 325 | | [DllImport("__Internal")] |
| | 326 | | private static extern void SetCallback_CreateEntity(JS_Delegate_V callback); |
| | 327 | |
|
| | 328 | | [DllImport("__Internal")] |
| | 329 | | private static extern void SetCallback_RemoveEntity(JS_Delegate_V callback); |
| | 330 | |
|
| | 331 | | [DllImport("__Internal")] |
| | 332 | | private static extern void SetCallback_SceneReady(JS_Delegate_V callback); |
| | 333 | |
|
| | 334 | | [DllImport("__Internal")] |
| | 335 | | private static extern void SetCallback_SetEntityId(JS_Delegate_VS callback); |
| | 336 | |
|
| | 337 | | [DllImport("__Internal")] |
| | 338 | | private static extern void SetCallback_SetSceneId(JS_Delegate_VS callback); |
| | 339 | |
|
| | 340 | | [DllImport("__Internal")] |
| | 341 | | private static extern void SetCallback_SetSceneNumber(JS_Delegate_VI callback); |
| | 342 | |
|
| | 343 | | [DllImport("__Internal")] |
| | 344 | | private static extern void SetCallback_SetEntityParent(JS_Delegate_VS callback); |
| | 345 | |
|
| | 346 | | [DllImport("__Internal")] |
| | 347 | | private static extern void SetCallback_EntityComponentCreateOrUpdate(JS_Delegate_VIS callback); |
| | 348 | |
|
| | 349 | | [DllImport("__Internal")] |
| | 350 | | private static extern void SetCallback_SharedComponentAttach(JS_Delegate_VSS callback); |
| | 351 | |
|
| | 352 | | [DllImport("__Internal")] |
| | 353 | | private static extern void SetCallback_EntityComponentDestroy(JS_Delegate_VS callback); |
| | 354 | |
|
| | 355 | | [DllImport("__Internal")] |
| | 356 | | private static extern void SetCallback_OpenExternalUrl(JS_Delegate_VS callback); |
| | 357 | |
|
| | 358 | | [DllImport("__Internal")] |
| | 359 | | private static extern void SetCallback_OpenNftDialog(JS_Delegate_VSSS callback); |
| | 360 | |
|
| | 361 | | [DllImport("__Internal")] |
| | 362 | | private static extern void SetCallback_SharedComponentUpdate(JS_Delegate_VSS callback); |
| | 363 | |
|
| | 364 | | [DllImport("__Internal")] |
| | 365 | | private static extern void SetCallback_SharedComponentDispose(JS_Delegate_VS callback); |
| | 366 | |
|
| | 367 | | [DllImport("__Internal")] |
| | 368 | | private static extern void SetCallback_SharedComponentCreate(JS_Delegate_VIS callback); |
| | 369 | |
|
| | 370 | | [DllImport("__Internal")] |
| | 371 | | private static extern void SetCallback_SetTag(JS_Delegate_VS callback); |
| | 372 | |
|
| | 373 | | [DllImport("__Internal")] |
| | 374 | | private static extern void SetCallback_Query(JS_Delegate_Query callback); |
| | 375 | | } |