< Summary

Class:DCL.MessageDecoder
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/Scene/Messaging/MessageDecoder.cs
Covered lines:9
Uncovered lines:86
Coverable lines:95
Total lines:196
Line coverage:9.4% (9 of 95)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
DecodePayloadChunk(...)0%2401500%
DecodeSceneMessage(...)0%10563200%
DecodeTransform(...)0%110100%
DecodeQueryMessage(...)0%2100%
DumpMessage(...)0%22.946022.22%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/Scene/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 string sceneId, out string message, out string tag, ou
 28        {
 29#if UNITY_EDITOR
 030            DumpMessage(payload, MESSAGES_FILENAME, ref messageDumpStr, ref messagesCount);
 31#endif
 032            byte[] bytes = System.Convert.FromBase64String(payload);
 33
 034            sendSceneMessage = DCL.Interface.PB_SendSceneMessage.Parser.ParseFrom(bytes);
 035            sceneId = sendSceneMessage.SceneId;
 036            tag = sendSceneMessage.Tag;
 37
 038            message = null;
 39
 040            switch (sendSceneMessage.PayloadCase)
 41            {
 42                case PB_SendSceneMessage.PayloadOneofCase.CreateEntity:
 043                    message = MessagingTypes.ENTITY_CREATE;
 044                    break;
 45                case PB_SendSceneMessage.PayloadOneofCase.RemoveEntity:
 046                    message = MessagingTypes.ENTITY_DESTROY;
 047                    break;
 48                case PB_SendSceneMessage.PayloadOneofCase.UpdateEntityComponent:
 049                    message = MessagingTypes.ENTITY_COMPONENT_CREATE_OR_UPDATE;
 050                    break;
 51                case PB_SendSceneMessage.PayloadOneofCase.AttachEntityComponent:
 052                    message = MessagingTypes.SHARED_COMPONENT_ATTACH;
 053                    break;
 54                case PB_SendSceneMessage.PayloadOneofCase.ComponentCreated:
 055                    message = MessagingTypes.SHARED_COMPONENT_CREATE;
 056                    break;
 57                case PB_SendSceneMessage.PayloadOneofCase.ComponentDisposed:
 058                    message = MessagingTypes.SHARED_COMPONENT_DISPOSE;
 059                    break;
 60                case PB_SendSceneMessage.PayloadOneofCase.ComponentRemoved:
 061                    message = MessagingTypes.ENTITY_COMPONENT_DESTROY;
 062                    break;
 63                case PB_SendSceneMessage.PayloadOneofCase.ComponentUpdated:
 064                    message = MessagingTypes.SHARED_COMPONENT_UPDATE;
 065                    break;
 66                case PB_SendSceneMessage.PayloadOneofCase.SceneStarted:
 067                    message = MessagingTypes.INIT_DONE;
 068                    break;
 69                case PB_SendSceneMessage.PayloadOneofCase.SetEntityParent:
 070                    message = MessagingTypes.ENTITY_REPARENT;
 071                    break;
 72                case PB_SendSceneMessage.PayloadOneofCase.Query:
 073                    message = MessagingTypes.QUERY;
 074                    break;
 75                case PB_SendSceneMessage.PayloadOneofCase.OpenExternalUrl:
 076                    message = MessagingTypes.OPEN_EXTERNAL_URL;
 077                    break;
 78                case PB_SendSceneMessage.PayloadOneofCase.OpenNFTDialog:
 079                    message = MessagingTypes.OPEN_NFT_DIALOG;
 080                    break;
 81                default:
 082                    Debug.Log("Error: " + payload);
 83                    break;
 84            }
 85
 086            return true;
 87        }
 88
 89        public static void DecodeSceneMessage(string sceneId, string method, string tag, PB_SendSceneMessage sendSceneMe
 90        {
 091            queuedMessage.type = QueuedSceneMessage.Type.SCENE_MESSAGE;
 092            queuedMessage.sceneId = sceneId;
 093            queuedMessage.method = method;
 094            queuedMessage.tag = tag;
 95
 96            switch (method)
 97            {
 98                case MessagingTypes.INIT_DONE:
 099                    queuedMessage.payload = new Protocol.SceneReady();
 0100                    break;
 101                case MessagingTypes.QUERY:
 0102                    QueryMessage query = new QueryMessage();
 0103                    DecodeQueryMessage(sendSceneMessage.Query.QueryId, sendSceneMessage.Query.Payload, ref query);
 0104                    queuedMessage.payload = query;
 0105                    break;
 106                case MessagingTypes.ENTITY_CREATE:
 0107                    queuedMessage.payload = Protocol.CreateEntity.FromPB(sendSceneMessage.CreateEntity);
 0108                    break;
 109                case MessagingTypes.ENTITY_DESTROY:
 0110                    queuedMessage.payload = Protocol.RemoveEntity.FromPB(sendSceneMessage.RemoveEntity);
 0111                    break;
 112                case MessagingTypes.ENTITY_REPARENT:
 0113                    queuedMessage.payload = Protocol.SetEntityParent.FromPB(sendSceneMessage.SetEntityParent);
 0114                    break;
 115                case MessagingTypes.SHARED_COMPONENT_CREATE:
 0116                    queuedMessage.payload = Protocol.SharedComponentCreate.FromPB(sendSceneMessage.ComponentCreated);
 0117                    break;
 118                case MessagingTypes.SHARED_COMPONENT_ATTACH:
 0119                    queuedMessage.payload = Protocol.SharedComponentAttach.FromPB(sendSceneMessage.AttachEntityComponent
 0120                    break;
 121                case MessagingTypes.SHARED_COMPONENT_UPDATE:
 0122                    queuedMessage.payload = Protocol.SharedComponentUpdate.FromPB(sendSceneMessage.ComponentUpdated);
 0123                    break;
 124                case MessagingTypes.SHARED_COMPONENT_DISPOSE:
 0125                    queuedMessage.payload = Protocol.SharedComponentDispose.FromPB(sendSceneMessage.ComponentDisposed);
 0126                    break;
 127                case MessagingTypes.ENTITY_COMPONENT_CREATE_OR_UPDATE:
 0128                    queuedMessage.payload = Protocol.EntityComponentCreateOrUpdate.FromPB(sendSceneMessage.UpdateEntityC
 0129                    break;
 130                case MessagingTypes.ENTITY_COMPONENT_DESTROY:
 0131                    queuedMessage.payload = Protocol.EntityComponentDestroy.FromPB(sendSceneMessage.ComponentRemoved);
 0132                    break;
 133                case MessagingTypes.OPEN_NFT_DIALOG:
 0134                    queuedMessage.payload = Protocol.OpenNftDialog.FromPB(sendSceneMessage.OpenNFTDialog);
 0135                    break;
 136                case MessagingTypes.OPEN_EXTERNAL_URL:
 0137                    queuedMessage.payload = Protocol.OpenExternalUrl.FromPB(sendSceneMessage.OpenExternalUrl);
 138                    break;
 139            }
 0140        }
 141
 142        public static void DecodeTransform(string payload, ref DCLTransform.Model model)
 143        {
 144#if UNITY_EDITOR
 230145            DumpMessage(payload, TRANSFORM_FILENAME, ref transformDumpStr, ref transformCount);
 146#endif
 230147            byte[] bytes = System.Convert.FromBase64String(payload);
 148
 230149            DCL.Interface.PB_Transform pbTransform = DCL.Interface.PB_Transform.Parser.ParseFrom(bytes);
 230150            model.position = new Vector3(pbTransform.Position.X, pbTransform.Position.Y, pbTransform.Position.Z);
 230151            model.scale = new Vector3(pbTransform.Scale.X, pbTransform.Scale.Y, pbTransform.Scale.Z);
 230152            model.rotation = new Quaternion((float)pbTransform.Rotation.X, (float)pbTransform.Rotation.Y, (float)pbTrans
 230153        }
 154
 155        public static void DecodeQueryMessage(string queryId, string payload, ref QueryMessage query)
 156        {
 157#if UNITY_EDITOR
 0158            DumpMessage(payload, QUERY_FILENAME, ref queryDumpStr, ref queryCount);
 159#endif
 160
 0161            byte[] bytes = System.Convert.FromBase64String(payload);
 0162            PB_RayQuery pbRayQuery = PB_RayQuery.Parser.ParseFrom(bytes);
 163
 0164            query.queryType = queryId;
 0165            query.payload = new RaycastQuery();
 0166            query.payload.id = pbRayQuery.QueryId;
 0167            query.payload.raycastType = Protocol.RaycastLiteralToType(pbRayQuery.QueryType);
 0168            query.payload.ray = new DCL.Models.Ray();
 0169            query.payload.ray.direction = new Vector3(pbRayQuery.Ray.Direction.X, pbRayQuery.Ray.Direction.Y, pbRayQuery
 0170            query.payload.ray.distance = pbRayQuery.Ray.Distance;
 0171            query.payload.ray.origin = new Vector3(pbRayQuery.Ray.Origin.X, pbRayQuery.Ray.Origin.Y, pbRayQuery.Ray.Orig
 0172        }
 173
 174#if UNITY_EDITOR
 175        private static void DumpMessage(string payload, string filename, ref string dumpString, ref int counter)
 176        {
 230177            if (DUMP_MESSAGES_FOR_PERFORMANCE_TESTS && CommonScriptableObjects.rendererState.Get())
 178            {
 0179                if (counter < MESSAGES_COUNT)
 180                {
 0181                    dumpString += payload + "\n";
 182
 0183                    counter++;
 0184                    if (counter >= MESSAGES_COUNT)
 185                    {
 0186                        string fullFilename = System.IO.Path.Combine(DUMP_PATH, filename);
 0187                        System.IO.File.WriteAllText($"{fullFilename}", dumpString);
 188
 0189                        Debug.Log($"{filename} file dumped!");
 190                    }
 191                }
 192            }
 230193        }
 194#endif
 195    }
 196}