| | 1 | | using System.Net; |
| | 2 | | using UnityEngine; |
| | 3 | | using DCL.Interface; |
| | 4 | | using DCL.Models; |
| | 5 | | using DCL.Components; |
| | 6 | |
|
| | 7 | | namespace 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 |
| 0 | 30 | | DumpMessage(payload, MESSAGES_FILENAME, ref messageDumpStr, ref messagesCount); |
| | 31 | | #endif |
| 0 | 32 | | byte[] bytes = System.Convert.FromBase64String(payload); |
| | 33 | |
|
| 0 | 34 | | sendSceneMessage = DCL.Interface.PB_SendSceneMessage.Parser.ParseFrom(bytes); |
| 0 | 35 | | sceneId = sendSceneMessage.SceneId; |
| 0 | 36 | | tag = sendSceneMessage.Tag; |
| | 37 | |
|
| 0 | 38 | | message = null; |
| | 39 | |
|
| 0 | 40 | | switch (sendSceneMessage.PayloadCase) |
| | 41 | | { |
| | 42 | | case PB_SendSceneMessage.PayloadOneofCase.CreateEntity: |
| 0 | 43 | | message = MessagingTypes.ENTITY_CREATE; |
| 0 | 44 | | break; |
| | 45 | | case PB_SendSceneMessage.PayloadOneofCase.RemoveEntity: |
| 0 | 46 | | message = MessagingTypes.ENTITY_DESTROY; |
| 0 | 47 | | break; |
| | 48 | | case PB_SendSceneMessage.PayloadOneofCase.UpdateEntityComponent: |
| 0 | 49 | | message = MessagingTypes.ENTITY_COMPONENT_CREATE_OR_UPDATE; |
| 0 | 50 | | break; |
| | 51 | | case PB_SendSceneMessage.PayloadOneofCase.AttachEntityComponent: |
| 0 | 52 | | message = MessagingTypes.SHARED_COMPONENT_ATTACH; |
| 0 | 53 | | break; |
| | 54 | | case PB_SendSceneMessage.PayloadOneofCase.ComponentCreated: |
| 0 | 55 | | message = MessagingTypes.SHARED_COMPONENT_CREATE; |
| 0 | 56 | | break; |
| | 57 | | case PB_SendSceneMessage.PayloadOneofCase.ComponentDisposed: |
| 0 | 58 | | message = MessagingTypes.SHARED_COMPONENT_DISPOSE; |
| 0 | 59 | | break; |
| | 60 | | case PB_SendSceneMessage.PayloadOneofCase.ComponentRemoved: |
| 0 | 61 | | message = MessagingTypes.ENTITY_COMPONENT_DESTROY; |
| 0 | 62 | | break; |
| | 63 | | case PB_SendSceneMessage.PayloadOneofCase.ComponentUpdated: |
| 0 | 64 | | message = MessagingTypes.SHARED_COMPONENT_UPDATE; |
| 0 | 65 | | break; |
| | 66 | | case PB_SendSceneMessage.PayloadOneofCase.SceneStarted: |
| 0 | 67 | | message = MessagingTypes.INIT_DONE; |
| 0 | 68 | | break; |
| | 69 | | case PB_SendSceneMessage.PayloadOneofCase.SetEntityParent: |
| 0 | 70 | | message = MessagingTypes.ENTITY_REPARENT; |
| 0 | 71 | | break; |
| | 72 | | case PB_SendSceneMessage.PayloadOneofCase.Query: |
| 0 | 73 | | message = MessagingTypes.QUERY; |
| 0 | 74 | | break; |
| | 75 | | case PB_SendSceneMessage.PayloadOneofCase.OpenExternalUrl: |
| 0 | 76 | | message = MessagingTypes.OPEN_EXTERNAL_URL; |
| 0 | 77 | | break; |
| | 78 | | case PB_SendSceneMessage.PayloadOneofCase.OpenNFTDialog: |
| 0 | 79 | | message = MessagingTypes.OPEN_NFT_DIALOG; |
| 0 | 80 | | break; |
| | 81 | | default: |
| 0 | 82 | | Debug.Log("Error: " + payload); |
| | 83 | | break; |
| | 84 | | } |
| | 85 | |
|
| 0 | 86 | | return true; |
| | 87 | | } |
| | 88 | |
|
| | 89 | | public static void DecodeSceneMessage(string sceneId, string method, string tag, PB_SendSceneMessage sendSceneMe |
| | 90 | | { |
| 0 | 91 | | queuedMessage.type = QueuedSceneMessage.Type.SCENE_MESSAGE; |
| 0 | 92 | | queuedMessage.sceneId = sceneId; |
| 0 | 93 | | queuedMessage.method = method; |
| 0 | 94 | | queuedMessage.tag = tag; |
| | 95 | |
|
| | 96 | | switch (method) |
| | 97 | | { |
| | 98 | | case MessagingTypes.INIT_DONE: |
| 0 | 99 | | queuedMessage.payload = new Protocol.SceneReady(); |
| 0 | 100 | | break; |
| | 101 | | case MessagingTypes.QUERY: |
| 0 | 102 | | QueryMessage query = new QueryMessage(); |
| 0 | 103 | | DecodeQueryMessage(sendSceneMessage.Query.QueryId, sendSceneMessage.Query.Payload, ref query); |
| 0 | 104 | | queuedMessage.payload = query; |
| 0 | 105 | | break; |
| | 106 | | case MessagingTypes.ENTITY_CREATE: |
| 0 | 107 | | queuedMessage.payload = Protocol.CreateEntity.FromPB(sendSceneMessage.CreateEntity); |
| 0 | 108 | | break; |
| | 109 | | case MessagingTypes.ENTITY_DESTROY: |
| 0 | 110 | | queuedMessage.payload = Protocol.RemoveEntity.FromPB(sendSceneMessage.RemoveEntity); |
| 0 | 111 | | break; |
| | 112 | | case MessagingTypes.ENTITY_REPARENT: |
| 0 | 113 | | queuedMessage.payload = Protocol.SetEntityParent.FromPB(sendSceneMessage.SetEntityParent); |
| 0 | 114 | | break; |
| | 115 | | case MessagingTypes.SHARED_COMPONENT_CREATE: |
| 0 | 116 | | queuedMessage.payload = Protocol.SharedComponentCreate.FromPB(sendSceneMessage.ComponentCreated); |
| 0 | 117 | | break; |
| | 118 | | case MessagingTypes.SHARED_COMPONENT_ATTACH: |
| 0 | 119 | | queuedMessage.payload = Protocol.SharedComponentAttach.FromPB(sendSceneMessage.AttachEntityComponent |
| 0 | 120 | | break; |
| | 121 | | case MessagingTypes.SHARED_COMPONENT_UPDATE: |
| 0 | 122 | | queuedMessage.payload = Protocol.SharedComponentUpdate.FromPB(sendSceneMessage.ComponentUpdated); |
| 0 | 123 | | break; |
| | 124 | | case MessagingTypes.SHARED_COMPONENT_DISPOSE: |
| 0 | 125 | | queuedMessage.payload = Protocol.SharedComponentDispose.FromPB(sendSceneMessage.ComponentDisposed); |
| 0 | 126 | | break; |
| | 127 | | case MessagingTypes.ENTITY_COMPONENT_CREATE_OR_UPDATE: |
| 0 | 128 | | queuedMessage.payload = Protocol.EntityComponentCreateOrUpdate.FromPB(sendSceneMessage.UpdateEntityC |
| 0 | 129 | | break; |
| | 130 | | case MessagingTypes.ENTITY_COMPONENT_DESTROY: |
| 0 | 131 | | queuedMessage.payload = Protocol.EntityComponentDestroy.FromPB(sendSceneMessage.ComponentRemoved); |
| 0 | 132 | | break; |
| | 133 | | case MessagingTypes.OPEN_NFT_DIALOG: |
| 0 | 134 | | queuedMessage.payload = Protocol.OpenNftDialog.FromPB(sendSceneMessage.OpenNFTDialog); |
| 0 | 135 | | break; |
| | 136 | | case MessagingTypes.OPEN_EXTERNAL_URL: |
| 0 | 137 | | queuedMessage.payload = Protocol.OpenExternalUrl.FromPB(sendSceneMessage.OpenExternalUrl); |
| | 138 | | break; |
| | 139 | | } |
| 0 | 140 | | } |
| | 141 | |
|
| | 142 | | public static void DecodeTransform(string payload, ref DCLTransform.Model model) |
| | 143 | | { |
| | 144 | | #if UNITY_EDITOR |
| 230 | 145 | | DumpMessage(payload, TRANSFORM_FILENAME, ref transformDumpStr, ref transformCount); |
| | 146 | | #endif |
| 230 | 147 | | byte[] bytes = System.Convert.FromBase64String(payload); |
| | 148 | |
|
| 230 | 149 | | DCL.Interface.PB_Transform pbTransform = DCL.Interface.PB_Transform.Parser.ParseFrom(bytes); |
| 230 | 150 | | model.position = new Vector3(pbTransform.Position.X, pbTransform.Position.Y, pbTransform.Position.Z); |
| 230 | 151 | | model.scale = new Vector3(pbTransform.Scale.X, pbTransform.Scale.Y, pbTransform.Scale.Z); |
| 230 | 152 | | model.rotation = new Quaternion((float)pbTransform.Rotation.X, (float)pbTransform.Rotation.Y, (float)pbTrans |
| 230 | 153 | | } |
| | 154 | |
|
| | 155 | | public static void DecodeQueryMessage(string queryId, string payload, ref QueryMessage query) |
| | 156 | | { |
| | 157 | | #if UNITY_EDITOR |
| 0 | 158 | | DumpMessage(payload, QUERY_FILENAME, ref queryDumpStr, ref queryCount); |
| | 159 | | #endif |
| | 160 | |
|
| 0 | 161 | | byte[] bytes = System.Convert.FromBase64String(payload); |
| 0 | 162 | | PB_RayQuery pbRayQuery = PB_RayQuery.Parser.ParseFrom(bytes); |
| | 163 | |
|
| 0 | 164 | | query.queryType = queryId; |
| 0 | 165 | | query.payload = new RaycastQuery(); |
| 0 | 166 | | query.payload.id = pbRayQuery.QueryId; |
| 0 | 167 | | query.payload.raycastType = Protocol.RaycastLiteralToType(pbRayQuery.QueryType); |
| 0 | 168 | | query.payload.ray = new DCL.Models.Ray(); |
| 0 | 169 | | query.payload.ray.direction = new Vector3(pbRayQuery.Ray.Direction.X, pbRayQuery.Ray.Direction.Y, pbRayQuery |
| 0 | 170 | | query.payload.ray.distance = pbRayQuery.Ray.Distance; |
| 0 | 171 | | query.payload.ray.origin = new Vector3(pbRayQuery.Ray.Origin.X, pbRayQuery.Ray.Origin.Y, pbRayQuery.Ray.Orig |
| 0 | 172 | | } |
| | 173 | |
|
| | 174 | | #if UNITY_EDITOR |
| | 175 | | private static void DumpMessage(string payload, string filename, ref string dumpString, ref int counter) |
| | 176 | | { |
| 230 | 177 | | if (DUMP_MESSAGES_FOR_PERFORMANCE_TESTS && CommonScriptableObjects.rendererState.Get()) |
| | 178 | | { |
| 0 | 179 | | if (counter < MESSAGES_COUNT) |
| | 180 | | { |
| 0 | 181 | | dumpString += payload + "\n"; |
| | 182 | |
|
| 0 | 183 | | counter++; |
| 0 | 184 | | if (counter >= MESSAGES_COUNT) |
| | 185 | | { |
| 0 | 186 | | string fullFilename = System.IO.Path.Combine(DUMP_PATH, filename); |
| 0 | 187 | | System.IO.File.WriteAllText($"{fullFilename}", dumpString); |
| | 188 | |
|
| 0 | 189 | | Debug.Log($"{filename} file dumped!"); |
| | 190 | | } |
| | 191 | | } |
| | 192 | | } |
| 230 | 193 | | } |
| | 194 | | #endif |
| | 195 | | } |
| | 196 | | } |