< Summary

Class:DCL.MessageDecoder
Assembly:DCL.Runtime
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/Messaging/MessageDecoder.cs
Covered lines:30
Uncovered lines:58
Coverable lines:88
Total lines:189
Line coverage:34% (30 of 88)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
DecodePayloadChunk(...)0%5115045.71%
DecodeSceneMessage(...)0%9923100%
DecodeQueryMessage(...)0%110100%
DumpMessage(...)0%22.946022.22%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/Messaging/MessageDecoder.cs

#LineLine coverage
 1using System.Net;
 2using UnityEngine;
 3using DCL.Interface;
 4using DCL.Models;
 5using DCL.Components;
 6
 7namespace DCL
 8{
 9    public static class MessageDecoder
 10    {
 11#if UNITY_EDITOR
 12        public const string DUMP_PATH = "TestResources/SceneMessages";
 13        public const string MESSAGES_FILENAME = "messages.txt";
 14        public const string TRANSFORM_FILENAME = "transform.txt";
 15        public const string QUERY_FILENAME = "query.txt";
 16
 17        static bool DUMP_MESSAGES_FOR_PERFORMANCE_TESTS = false;
 18        const int MESSAGES_COUNT = 1000;
 19        static int messagesCount = 0;
 20        static int transformCount = 0;
 21        static int queryCount = 0;
 22        static string messageDumpStr;
 23        static string transformDumpStr;
 24        static string queryDumpStr;
 25#endif
 26
 27        public static bool DecodePayloadChunk(string payload, out int sceneNumber, out string message, out string tag,
 28            out PB_SendSceneMessage sendSceneMessage)
 29        {
 30#if UNITY_EDITOR
 13000031            DumpMessage(payload, MESSAGES_FILENAME, ref messageDumpStr, ref messagesCount);
 32#endif
 13000033            byte[] bytes = System.Convert.FromBase64String(payload);
 34
 13000035            sendSceneMessage = DCL.Interface.PB_SendSceneMessage.Parser.ParseFrom(bytes);
 13000036            sceneNumber = sendSceneMessage.SceneNumber;
 13000037            tag = sendSceneMessage.Tag;
 38
 13000039            message = null;
 40
 13000041            switch (sendSceneMessage.PayloadCase)
 42            {
 43                case PB_SendSceneMessage.PayloadOneofCase.CreateEntity:
 044                    message = MessagingTypes.ENTITY_CREATE;
 045                    break;
 46                case PB_SendSceneMessage.PayloadOneofCase.RemoveEntity:
 047                    message = MessagingTypes.ENTITY_DESTROY;
 048                    break;
 49                case PB_SendSceneMessage.PayloadOneofCase.UpdateEntityComponent:
 6656050                    message = MessagingTypes.ENTITY_COMPONENT_CREATE_OR_UPDATE;
 6656051                    break;
 52                case PB_SendSceneMessage.PayloadOneofCase.AttachEntityComponent:
 520053                    message = MessagingTypes.SHARED_COMPONENT_ATTACH;
 520054                    break;
 55                case PB_SendSceneMessage.PayloadOneofCase.ComponentCreated:
 056                    message = MessagingTypes.SHARED_COMPONENT_CREATE;
 057                    break;
 58                case PB_SendSceneMessage.PayloadOneofCase.ComponentDisposed:
 059                    message = MessagingTypes.SHARED_COMPONENT_DISPOSE;
 060                    break;
 61                case PB_SendSceneMessage.PayloadOneofCase.ComponentRemoved:
 062                    message = MessagingTypes.ENTITY_COMPONENT_DESTROY;
 063                    break;
 64                case PB_SendSceneMessage.PayloadOneofCase.ComponentUpdated:
 4524065                    message = MessagingTypes.SHARED_COMPONENT_UPDATE;
 4524066                    break;
 67                case PB_SendSceneMessage.PayloadOneofCase.SceneStarted:
 068                    message = MessagingTypes.INIT_DONE;
 069                    break;
 70                case PB_SendSceneMessage.PayloadOneofCase.SetEntityParent:
 071                    message = MessagingTypes.ENTITY_REPARENT;
 072                    break;
 73                case PB_SendSceneMessage.PayloadOneofCase.Query:
 1300074                    message = MessagingTypes.QUERY;
 1300075                    break;
 76                case PB_SendSceneMessage.PayloadOneofCase.OpenExternalUrl:
 077                    message = MessagingTypes.OPEN_EXTERNAL_URL;
 078                    break;
 79                case PB_SendSceneMessage.PayloadOneofCase.OpenNFTDialog:
 080                    message = MessagingTypes.OPEN_NFT_DIALOG;
 081                    break;
 82                default:
 083                    Debug.Log("Error: " + payload);
 84                    break;
 85            }
 86
 13000087            return true;
 88        }
 89
 90        public static void DecodeSceneMessage(int sceneNumber, string method, string tag,
 91            PB_SendSceneMessage sendSceneMessage, ref QueuedSceneMessage_Scene queuedMessage)
 92        {
 093            queuedMessage.type = QueuedSceneMessage.Type.SCENE_MESSAGE;
 094            queuedMessage.sceneNumber = sceneNumber;
 095            queuedMessage.method = method;
 096            queuedMessage.tag = tag;
 97
 98            switch (method)
 99            {
 100                case MessagingTypes.INIT_DONE:
 0101                    queuedMessage.payload = new Protocol.SceneReady();
 0102                    break;
 103                case MessagingTypes.QUERY:
 0104                    QueryMessage query = new QueryMessage();
 0105                    DecodeQueryMessage(sendSceneMessage.Query.QueryId, sendSceneMessage.Query.Payload, ref query);
 0106                    queuedMessage.payload = query;
 0107                    break;
 108                case MessagingTypes.ENTITY_CREATE:
 0109                    queuedMessage.payload = Protocol.CreateEntity.FromPB(sendSceneMessage.CreateEntity);
 0110                    break;
 111                case MessagingTypes.ENTITY_DESTROY:
 0112                    queuedMessage.payload = Protocol.RemoveEntity.FromPB(sendSceneMessage.RemoveEntity);
 0113                    break;
 114                case MessagingTypes.ENTITY_REPARENT:
 0115                    queuedMessage.payload = Protocol.SetEntityParent.FromPB(sendSceneMessage.SetEntityParent);
 0116                    break;
 117                case MessagingTypes.SHARED_COMPONENT_CREATE:
 0118                    queuedMessage.payload = Protocol.SharedComponentCreate.FromPB(sendSceneMessage.ComponentCreated);
 0119                    break;
 120                case MessagingTypes.SHARED_COMPONENT_ATTACH:
 0121                    queuedMessage.payload =
 122                        Protocol.SharedComponentAttach.FromPB(sendSceneMessage.AttachEntityComponent);
 0123                    break;
 124                case MessagingTypes.SHARED_COMPONENT_UPDATE:
 0125                    queuedMessage.payload = Protocol.SharedComponentUpdate.FromPB(sendSceneMessage.ComponentUpdated);
 0126                    break;
 127                case MessagingTypes.SHARED_COMPONENT_DISPOSE:
 0128                    queuedMessage.payload = Protocol.SharedComponentDispose.FromPB(sendSceneMessage.ComponentDisposed);
 0129                    break;
 130                case MessagingTypes.ENTITY_COMPONENT_CREATE_OR_UPDATE:
 0131                    queuedMessage.payload =
 132                        Protocol.EntityComponentCreateOrUpdate.FromPB(sendSceneMessage.UpdateEntityComponent);
 0133                    break;
 134                case MessagingTypes.ENTITY_COMPONENT_DESTROY:
 0135                    queuedMessage.payload = Protocol.EntityComponentDestroy.FromPB(sendSceneMessage.ComponentRemoved);
 0136                    break;
 137                case MessagingTypes.OPEN_NFT_DIALOG:
 0138                    queuedMessage.payload = Protocol.OpenNftDialog.FromPB(sendSceneMessage.OpenNFTDialog);
 0139                    break;
 140                case MessagingTypes.OPEN_EXTERNAL_URL:
 0141                    queuedMessage.payload = Protocol.OpenExternalUrl.FromPB(sendSceneMessage.OpenExternalUrl);
 142                    break;
 143            }
 0144        }
 145
 146        public static void DecodeQueryMessage(string queryId, string payload, ref QueryMessage query)
 147        {
 148#if UNITY_EDITOR
 130000149            DumpMessage(payload, QUERY_FILENAME, ref queryDumpStr, ref queryCount);
 150#endif
 151
 130000152            byte[] bytes = System.Convert.FromBase64String(payload);
 130000153            PB_RayQuery pbRayQuery = PB_RayQuery.Parser.ParseFrom(bytes);
 154
 130000155            query.queryType = queryId;
 130000156            query.payload = new RaycastQuery();
 130000157            query.payload.id = pbRayQuery.QueryId;
 130000158            query.payload.raycastType = Protocol.RaycastLiteralToType(pbRayQuery.QueryType);
 130000159            query.payload.ray = new DCL.Models.Ray();
 130000160            query.payload.ray.direction = new Vector3(pbRayQuery.Ray.Direction.X, pbRayQuery.Ray.Direction.Y,
 161                pbRayQuery.Ray.Direction.Z);
 130000162            query.payload.ray.distance = pbRayQuery.Ray.Distance;
 130000163            query.payload.ray.origin =
 164                new Vector3(pbRayQuery.Ray.Origin.X, pbRayQuery.Ray.Origin.Y, pbRayQuery.Ray.Origin.Z);
 130000165        }
 166
 167#if UNITY_EDITOR
 168        public static void DumpMessage(string payload, string filename, ref string dumpString, ref int counter)
 169        {
 260000170            if (DUMP_MESSAGES_FOR_PERFORMANCE_TESTS && CommonScriptableObjects.rendererState.Get())
 171            {
 0172                if (counter < MESSAGES_COUNT)
 173                {
 0174                    dumpString += payload + "\n";
 175
 0176                    counter++;
 0177                    if (counter >= MESSAGES_COUNT)
 178                    {
 0179                        string fullFilename = System.IO.Path.Combine(DUMP_PATH, filename);
 0180                        System.IO.File.WriteAllText($"{fullFilename}", dumpString);
 181
 0182                        Debug.Log($"{filename} file dumped!");
 183                    }
 184                }
 185            }
 260000186        }
 187#endif
 188    }
 189}