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