< Summary

Class:NativeBridgeCommunication
Assembly:NativeBridgeCommunication
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/KernelCommunication/NativeBridgeCommunication/NativeBridgeCommunication.cs
Covered lines:31
Uncovered lines:13
Coverable lines:44
Total lines:231
Line coverage:70.4% (31 of 44)
Covered branches:0
Total branches:0
Covered methods:17
Total methods:22
Method coverage:77.2% (17 of 22)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
NativeBridgeCommunication()0%110100%
NativeBridgeCommunication(...)0%110100%
Dispose()0%2100%
Sdk6BinaryMessage(...)0%6200%
SetTag(...)0%110100%
SetSceneNumber(...)0%110100%
SceneReady()0%110100%
CreateEntity()0%110100%
RemoveEntity()0%110100%
SetEntityId(...)0%110100%
SetEntityParent(...)0%110100%
EntityComponentCreateOrUpdate(...)0%110100%
EntityComponentDestroy(...)0%110100%
SharedComponentCreate(...)0%110100%
SharedComponentDispose(...)0%110100%
SharedComponentAttach(...)0%110100%
SharedComponentUpdate(...)0%110100%
OpenExternalUrl(...)0%2100%
OpenNftDialog(...)0%2100%
Query(...)0%110100%
EnqueueSceneMessage(...)0%110100%
SetSceneId(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/KernelCommunication/NativeBridgeCommunication/NativeBridgeCommunication.cs

#LineLine coverage
 1using DCL;
 2using DCL.Models;
 3using Decentraland.Renderer.RendererServices;
 4using MainScripts.DCL.Components;
 5using System;
 6using System.Collections.Concurrent;
 7using System.Runtime.InteropServices;
 8using UnityEngine;
 9using Ray = DCL.Models.Ray;
 10
 11public 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
 124    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
 532    public NativeBridgeCommunication(IMessageQueueHandler queueHandler)
 33    {
 534        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
 563    }
 64
 065    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    {
 072        var ptr = new IntPtr(intPtr);
 73
 074        var readonlySpan = new ReadOnlySpan<byte>(ptr.ToPointer(), length);
 075        readonlySpan.CopyTo(PREALLOCATED_READER_BUFFER);
 76
 77        try
 78        {
 079            RendererManyEntityActions sceneRequest = RendererManyEntityActions.Parser.ParseFrom(PREALLOCATED_READER_BUFF
 80
 081            for (var i = 0; i < sceneRequest.Actions.Count; i++)
 082                queueHandler.EnqueueSceneMessage(
 83                    SDK6DataMapExtensions.SceneMessageFromSdk6Message(sceneRequest.Actions[i], currentSceneNumber)
 84                );
 085        }
 086        catch (Exception e) { Debug.LogError(e); }
 087    }
 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) =>
 593        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) =>
 599        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() =>
 1105        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() =>
 4111        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() =>
 2117        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) =>
 6123        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) =>
 1129        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) =>
 1135        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) =>
 1141        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) =>
 1147        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) =>
 1153        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) =>
 1159        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) =>
 1165        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) =>
 0171        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) =>
 0177        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    {
 1184        var ray = new Ray
 185        {
 186            origin = payload.raycastPayload.origin,
 187            direction = payload.raycastPayload.direction,
 188            distance = payload.raycastPayload.distance,
 189        };
 190
 1191        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
 1202        EnqueueSceneMessage(MessagingTypes.QUERY, resultPayload);
 1203    }
 204
 205    private static void EnqueueSceneMessage(string messageType, object payload)
 206    {
 15207        QueuedSceneMessage_Scene queuedMessage = GetSceneMessageInstance();
 15208        queuedMessage.method = messageType;
 15209        queuedMessage.payload = payload;
 210
 15211        queueHandler.EnqueueSceneMessage(queuedMessage);
 212
 213        QueuedSceneMessage_Scene GetSceneMessageInstance()
 214        {
 15215            var message = new QueuedSceneMessage_Scene();
 15216            message.sceneNumber = currentSceneNumber;
 15217            message.tag = currentTag;
 15218            message.type = QueuedSceneMessage.Type.SCENE_MESSAGE;
 219
 15220            return message;
 221        }
 15222    }
 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
 0229    [MonoPInvokeCallback(typeof(JS_Delegate_VI))] internal static void SetSceneId(string _) { }
 230#endregion
 231}