< Summary

Class:BuilderInWorldBridge
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/BuilderInWorldBridge/BuilderInWorldBridge.cs
Covered lines:94
Uncovered lines:36
Coverable lines:130
Total lines:279
Line coverage:72.3% (94 of 130)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BuilderInWorldBridge()0%110100%
PublishSceneResult(...)0%20400%
BuilderInWorldCatalogHeaders(...)0%6200%
BuilderProjectInfo(...)0%2100%
AskKernelForCatalogHeaders()0%2100%
UpdateSmartItemComponent(...)0%2.012087.5%
SaveSceneInfo(...)0%2100%
SaveSceneState(...)0%110100%
ChangeEntityLockStatus(...)0%330100%
ChangedEntityName(...)0%330100%
ChangeEntityComponent(...)0%2.012088.89%
AddEntityOnKernel(...)0%7.355054.55%
EntityTransformReport(...)0%110100%
RemoveEntityOnKernel(...)0%220100%
StartKernelEditMode(...)0%110100%
ExitKernelEditMode(...)0%110100%
PublishScene(...)0%110100%
SendNewEntityToKernel(...)0%22091.67%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/BuilderInWorldBridge/BuilderInWorldBridge.cs

#LineLine coverage
 1using DCL;
 2using DCL.Components;
 3using DCL.Configuration;
 4using DCL.Controllers;
 5using DCL.Interface;
 6using DCL.Models;
 7using Newtonsoft.Json;
 8using System;
 9using System.Collections.Generic;
 10using UnityEngine;
 11using static ProtocolV2;
 12using Environment = DCL.Environment;
 13
 14/// <summary>
 15/// This class will handle all the messages that will be sent to kernel.
 16/// </summary>
 17public class BuilderInWorldBridge : MonoBehaviour
 18{
 019    public BuilderProjectPayload builderProject { get => builderProjectPayload; }
 20
 21    //Note Adrian: OnKernelUpdated in not called in the update of the transform, since it will give a lot of
 22    //events and probably dont need to get called with that frecuency
 23    public event Action OnKernelUpdated;
 24    public event Action<bool, string> OnPublishEnd;
 25    public event Action<string> OnCatalogHeadersReceived;
 26
 27    //This is done for optimization purposes, recreating new objects can increase garbage collection
 22228    TransformComponent entityTransformComponentModel = new TransformComponent();
 29
 22230    StoreSceneStateEvent storeSceneState = new StoreSceneStateEvent();
 22231    SaveSceneStateEvent saveSceneState = new SaveSceneStateEvent();
 22232    SaveProjectInfoEvent saveProjectInfo = new SaveProjectInfoEvent();
 22233    ModifyEntityComponentEvent modifyEntityComponentEvent = new ModifyEntityComponentEvent();
 22234    EntityPayload entityPayload = new EntityPayload();
 22235    EntitySingleComponentPayload entitySingleComponentPayload = new EntitySingleComponentPayload();
 22236    BuilderProjectPayload builderProjectPayload = new BuilderProjectPayload();
 37
 38    #region MessagesFromKernel
 39
 40    public void PublishSceneResult(string payload)
 41    {
 042        PublishSceneResultPayload publishSceneResultPayload = JsonUtility.FromJson<PublishSceneResultPayload>(payload);
 43
 044        if (publishSceneResultPayload.ok)
 45        {
 046            OnPublishEnd?.Invoke(true, "");
 47
 048            AudioScriptableObjects.confirm.Play();
 049        }
 50        else
 51        {
 052            OnPublishEnd?.Invoke(false, publishSceneResultPayload.error);
 53
 054            AudioScriptableObjects.error.Play();
 55        }
 056    }
 57
 058    public void BuilderInWorldCatalogHeaders(string payload) { OnCatalogHeadersReceived?.Invoke(payload); }
 59
 60    public void BuilderProjectInfo(string payload)
 61    {
 062        builderProjectPayload = JsonUtility.FromJson<BuilderProjectPayload>(payload);
 063        HUDController.i.builderInWorldMainHud.SetBuilderProjectInfo(builderProjectPayload.title, builderProjectPayload.d
 064    }
 65
 66    #endregion
 67
 68    #region MessagesToKernel
 69
 070    public void AskKernelForCatalogHeaders() { WebInterface.SendMessage(BuilderInWorldSettings.BIW_HEADER_REQUEST_EVENT_
 71
 72    public void UpdateSmartItemComponent(DCLBuilderInWorldEntity entity, ParcelScene scene)
 73    {
 174        SmartItemComponent smartItemComponent = entity.rootEntity.TryGetComponent<SmartItemComponent>();
 175        if (smartItemComponent == null)
 076            return;
 77
 178        entitySingleComponentPayload.entityId = entity.rootEntity.entityId;
 179        entitySingleComponentPayload.componentId = (int) CLASS_ID_COMPONENT.SMART_ITEM;
 80
 181        entitySingleComponentPayload.data = smartItemComponent.GetValues();
 82
 183        ChangeEntityComponent(entitySingleComponentPayload, scene);
 184    }
 85
 86    public void SaveSceneInfo(ParcelScene scene, string sceneName, string sceneDescription, string sceneScreenshot)
 87    {
 088        saveProjectInfo.payload.title = sceneName;
 089        saveProjectInfo.payload.description = sceneDescription;
 090        saveProjectInfo.payload.screenshot = sceneScreenshot;
 91
 092        WebInterface.SendSceneEvent(scene.sceneData.id, BuilderInWorldSettings.STATE_EVENT_NAME, saveProjectInfo);
 093    }
 94
 95    public void SaveSceneState(ParcelScene scene)
 96    {
 297        saveSceneState.payload = JsonUtility.ToJson(builderProjectPayload);
 298        WebInterface.SendSceneEvent(scene.sceneData.id, BuilderInWorldSettings.STATE_EVENT_NAME, saveSceneState);
 299    }
 100
 101    public void ChangeEntityLockStatus(DCLBuilderInWorldEntity entity, ParcelScene scene)
 102    {
 1103        entitySingleComponentPayload.entityId = entity.rootEntity.entityId;
 1104        entitySingleComponentPayload.componentId = (int) CLASS_ID.LOCKED_ON_EDIT;
 105
 4106        foreach (KeyValuePair<Type, ISharedComponent> keyValuePairBaseDisposable in entity.rootEntity.sharedComponents)
 107        {
 1108            if (keyValuePairBaseDisposable.Value.GetClassId() == (int) CLASS_ID.LOCKED_ON_EDIT)
 109            {
 1110                entitySingleComponentPayload.data = ((DCLLockedOnEdit) keyValuePairBaseDisposable.Value).GetModel();
 111            }
 112        }
 113
 1114        ChangeEntityComponent(entitySingleComponentPayload, scene);
 1115    }
 116
 117    public void ChangedEntityName(DCLBuilderInWorldEntity entity, ParcelScene scene)
 118    {
 1119        entitySingleComponentPayload.entityId = entity.rootEntity.entityId;
 1120        entitySingleComponentPayload.componentId = (int) CLASS_ID.NAME;
 121
 4122        foreach (KeyValuePair<Type, ISharedComponent> keyValuePairBaseDisposable in entity.rootEntity.sharedComponents)
 123        {
 1124            if (keyValuePairBaseDisposable.Value.GetClassId() == (int) CLASS_ID.NAME)
 125            {
 1126                entitySingleComponentPayload.data = ((DCLName) keyValuePairBaseDisposable.Value).GetModel();
 127            }
 128        }
 129
 1130        ChangeEntityComponent(entitySingleComponentPayload, scene);
 1131    }
 132
 133    void ChangeEntityComponent(EntitySingleComponentPayload payload, ParcelScene scene)
 134    {
 3135        modifyEntityComponentEvent.payload = payload;
 136
 3137        WebInterface.SceneEvent<ModifyEntityComponentEvent> sceneEvent = new WebInterface.SceneEvent<ModifyEntityCompone
 3138        sceneEvent.sceneId = scene.sceneData.id;
 3139        sceneEvent.eventType = BuilderInWorldSettings.STATE_EVENT_NAME;
 3140        sceneEvent.payload = modifyEntityComponentEvent;
 141
 142
 143        //Note (Adrian): We use Newtonsoft instead of JsonUtility because we need to deal with super classes, JsonUtilit
 3144        string message = JsonConvert.SerializeObject(sceneEvent, Formatting.None, new JsonSerializerSettings
 145        {
 146            ReferenceLoopHandling = ReferenceLoopHandling.Ignore
 147        });
 148
 3149        WebInterface.BuilderInWorldMessage(BuilderInWorldSettings.SCENE_EVENT_NAME, message);
 3150        OnKernelUpdated?.Invoke();
 0151    }
 152
 153    public void AddEntityOnKernel(IDCLEntity entity, ParcelScene scene)
 154    {
 1155        List<ComponentPayload> list = new List<ComponentPayload>();
 156
 4157        foreach (KeyValuePair<CLASS_ID_COMPONENT, IEntityComponent> keyValuePair in entity.components)
 158        {
 1159            ComponentPayload componentPayLoad = new ComponentPayload();
 1160            componentPayLoad.componentId = Convert.ToInt32(keyValuePair.Key);
 161
 1162            if (keyValuePair.Key == CLASS_ID_COMPONENT.TRANSFORM)
 163            {
 1164                TransformComponent entityComponentModel = new TransformComponent();
 165
 1166                entityComponentModel.position = WorldStateUtils.ConvertUnityToScenePosition(entity.gameObject.transform.
 1167                entityComponentModel.rotation = new QuaternionRepresentation(entity.gameObject.transform.rotation);
 1168                entityComponentModel.scale = entity.gameObject.transform.lossyScale;
 169
 1170                componentPayLoad.data = entityComponentModel;
 1171            }
 172            else
 173            {
 0174                componentPayLoad.data = keyValuePair.Value.GetModel();
 175            }
 176
 1177            list.Add(componentPayLoad);
 178        }
 179
 2180        foreach (KeyValuePair<Type, ISharedComponent> keyValuePairBaseDisposable in entity.sharedComponents)
 181        {
 0182            ComponentPayload componentPayLoad = new ComponentPayload();
 183
 0184            componentPayLoad.componentId = keyValuePairBaseDisposable.Value.GetClassId();
 185
 0186            if (keyValuePairBaseDisposable.Value.GetClassId() == (int) CLASS_ID.NFT_SHAPE)
 187            {
 0188                NFTComponent nftComponent = new NFTComponent();
 0189                NFTShape.Model model = (NFTShape.Model) keyValuePairBaseDisposable.Value.GetModel();
 190
 0191                nftComponent.color = new ColorRepresentation(model.color);
 0192                nftComponent.assetId = model.assetId;
 0193                nftComponent.src = model.src;
 0194                nftComponent.style = model.style;
 195
 0196                componentPayLoad.data = nftComponent;
 0197            }
 198            else
 199            {
 0200                componentPayLoad.data = keyValuePairBaseDisposable.Value.GetModel();
 201            }
 202
 0203            list.Add(componentPayLoad);
 204        }
 205
 1206        SendNewEntityToKernel(scene.sceneData.id, entity.entityId, list.ToArray());
 1207    }
 208
 209    public void EntityTransformReport(IDCLEntity entity, ParcelScene scene)
 210    {
 1211        entitySingleComponentPayload.entityId = entity.entityId;
 1212        entitySingleComponentPayload.componentId = (int) CLASS_ID_COMPONENT.TRANSFORM;
 213
 1214        entityTransformComponentModel.position = WorldStateUtils.ConvertUnityToScenePosition(entity.gameObject.transform
 1215        entityTransformComponentModel.rotation = new QuaternionRepresentation(entity.gameObject.transform.rotation);
 1216        entityTransformComponentModel.scale = entity.gameObject.transform.lossyScale;
 217
 1218        entitySingleComponentPayload.data = entityTransformComponentModel;
 219
 1220        modifyEntityComponentEvent.payload = entitySingleComponentPayload;
 221
 1222        WebInterface.SceneEvent<ModifyEntityComponentEvent> sceneEvent = new WebInterface.SceneEvent<ModifyEntityCompone
 1223        sceneEvent.sceneId = scene.sceneData.id;
 1224        sceneEvent.eventType = BuilderInWorldSettings.STATE_EVENT_NAME;
 1225        sceneEvent.payload = modifyEntityComponentEvent;
 226
 227        //Note (Adrian): We use Newtonsoft instead of JsonUtility because we need to deal with super classes, JsonUtilit
 1228        string message = JsonConvert.SerializeObject(sceneEvent);
 1229        WebInterface.BuilderInWorldMessage(BuilderInWorldSettings.SCENE_EVENT_NAME, message);
 1230    }
 231
 232    public void RemoveEntityOnKernel(string entityId, ParcelScene scene)
 233    {
 2234        RemoveEntityEvent removeEntityEvent = new RemoveEntityEvent();
 2235        RemoveEntityPayload removeEntityPayLoad = new RemoveEntityPayload();
 2236        removeEntityPayLoad.entityId = entityId;
 2237        removeEntityEvent.payload = removeEntityPayLoad;
 238
 2239        WebInterface.SendSceneEvent(scene.sceneData.id, BuilderInWorldSettings.STATE_EVENT_NAME, removeEntityEvent);
 2240        OnKernelUpdated?.Invoke();
 1241    }
 242
 2243    public void StartKernelEditMode(ParcelScene scene) { WebInterface.ReportControlEvent(new WebInterface.StartStatefulM
 244
 2245    public void ExitKernelEditMode(ParcelScene scene) { WebInterface.ReportControlEvent(new WebInterface.StopStatefulMod
 246
 247    public void PublishScene(ParcelScene scene, string sceneName, string sceneDescription, string sceneScreenshot)
 248    {
 1249        storeSceneState.payload.title = sceneName;
 1250        storeSceneState.payload.description = sceneDescription;
 1251        storeSceneState.payload.screenshot = sceneScreenshot;
 252
 1253        WebInterface.SendSceneEvent(scene.sceneData.id, BuilderInWorldSettings.STATE_EVENT_NAME, storeSceneState);
 1254    }
 255
 256    // ReSharper disable Unity.PerformanceAnalysis
 257    void SendNewEntityToKernel(string sceneId, string entityId, ComponentPayload[] componentsPayload)
 258    {
 1259        AddEntityEvent addEntityEvent = new AddEntityEvent();
 1260        entityPayload.entityId = entityId;
 1261        entityPayload.components = componentsPayload;
 262
 1263        addEntityEvent.payload = entityPayload;
 264
 1265        WebInterface.SceneEvent<AddEntityEvent> sceneEvent = new WebInterface.SceneEvent<AddEntityEvent>();
 1266        sceneEvent.sceneId = sceneId;
 1267        sceneEvent.eventType = BuilderInWorldSettings.STATE_EVENT_NAME;
 1268        sceneEvent.payload = addEntityEvent;
 269
 270
 271        //Note(Adrian): We use Newtonsoft instead of JsonUtility because we need to deal with super classes, JsonUtility
 1272        string message = JsonConvert.SerializeObject(sceneEvent);
 1273        WebInterface.BuilderInWorldMessage(BuilderInWorldSettings.SCENE_EVENT_NAME, message);
 1274        OnKernelUpdated?.Invoke();
 0275    }
 276
 277    #endregion
 278
 279}