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