< Summary

Class:BuilderInWorldBridge
Assembly:BIWBridge
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/BuilderInWorldBridge/BuilderInWorldBridge.cs
Covered lines:95
Uncovered lines:40
Coverable lines:135
Total lines:288
Line coverage:70.3% (95 of 135)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BuilderInWorldBridge()0%110100%
PublishSceneResult(...)0%20400%
AddAssets(...)0%2100%
RequestedHeaders(...)0%6200%
BuilderProjectInfo(...)0%6200%
AskKernelForCatalogHeadersWithParams(...)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%9.446054.29%
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/DCLPlugins/BuilderInWorld/Scripts/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, string> OnBuilderProjectInfo;
 26    public event Action<RequestHeader> OnHeadersReceived;
 27
 28    //This is done for optimization purposes, recreating new objects can increase garbage collection
 3829    private TransformComponent entityTransformComponentModel = new TransformComponent();
 30
 3831    private StoreSceneStateEvent storeSceneState = new StoreSceneStateEvent();
 3832    private SaveSceneStateEvent saveSceneState = new SaveSceneStateEvent();
 3833    private SaveProjectInfoEvent saveProjectInfo = new SaveProjectInfoEvent();
 3834    private ModifyEntityComponentEvent modifyEntityComponentEvent = new ModifyEntityComponentEvent();
 3835    private EntityPayload entityPayload = new EntityPayload();
 3836    private EntitySingleComponentPayload entitySingleComponentPayload = new EntitySingleComponentPayload();
 3837    internal BuilderProjectPayload builderProjectPayload = new BuilderProjectPayload();
 38
 39    #region MessagesFromKernel
 40
 41    public void PublishSceneResult(string payload)
 42    {
 043        PublishSceneResultPayload publishSceneResultPayload = JsonUtility.FromJson<PublishSceneResultPayload>(payload);
 44
 045        if (publishSceneResultPayload.ok)
 46        {
 047            OnPublishEnd?.Invoke(true, "");
 48
 049            AudioScriptableObjects.confirm.Play();
 050        }
 51        else
 52        {
 053            OnPublishEnd?.Invoke(false, publishSceneResultPayload.error);
 54
 055            AudioScriptableObjects.error.Play();
 56        }
 057    }
 58
 59    public void AddAssets(string payload)
 60    {
 61        //We remove the old assets to they don't collide with the new ones
 062        BIWUtils.RemoveAssetsFromCurrentScene();
 63
 064        AssetCatalogBridge.i.AddScenesObjectToSceneCatalog(payload);
 065    }
 66
 067    public void RequestedHeaders(string payload) { OnHeadersReceived?.Invoke(JsonConvert.DeserializeObject<RequestHeader
 68
 69    public void BuilderProjectInfo(string payload)
 70    {
 071        builderProjectPayload = JsonUtility.FromJson<BuilderProjectPayload>(payload);
 072        OnBuilderProjectInfo?.Invoke(builderProjectPayload.title, builderProjectPayload.description);
 073    }
 74
 75    #endregion
 76
 77    #region MessagesToKernel
 78
 079    public void AskKernelForCatalogHeadersWithParams(string method, string url) { WebInterface.SendRequestHeadersForUrl(
 80
 81    public void UpdateSmartItemComponent(BIWEntity entity, ParcelScene scene)
 82    {
 183        SmartItemComponent smartItemComponent = entity.rootEntity.TryGetComponent<SmartItemComponent>();
 184        if (smartItemComponent == null)
 085            return;
 86
 187        entitySingleComponentPayload.entityId = entity.rootEntity.entityId;
 188        entitySingleComponentPayload.componentId = (int) CLASS_ID_COMPONENT.SMART_ITEM;
 89
 190        entitySingleComponentPayload.data = smartItemComponent.GetValues();
 91
 192        ChangeEntityComponent(entitySingleComponentPayload, scene);
 193    }
 94
 95    public void SaveSceneInfo(ParcelScene scene, string sceneName, string sceneDescription, string sceneScreenshot)
 96    {
 097        saveProjectInfo.payload.title = sceneName;
 098        saveProjectInfo.payload.description = sceneDescription;
 099        saveProjectInfo.payload.screenshot = sceneScreenshot;
 100
 0101        WebInterface.SendSceneEvent(scene.sceneData.id, BIWSettings.STATE_EVENT_NAME, saveProjectInfo);
 0102    }
 103
 104    public void SaveSceneState(ParcelScene scene)
 105    {
 2106        saveSceneState.payload = JsonUtility.ToJson(builderProjectPayload);
 2107        WebInterface.SendSceneEvent(scene.sceneData.id, BIWSettings.STATE_EVENT_NAME, saveSceneState);
 2108    }
 109
 110    public void ChangeEntityLockStatus(BIWEntity entity, ParcelScene scene)
 111    {
 1112        entitySingleComponentPayload.entityId = entity.rootEntity.entityId;
 1113        entitySingleComponentPayload.componentId = (int) CLASS_ID.LOCKED_ON_EDIT;
 114
 4115        foreach (KeyValuePair<Type, ISharedComponent> keyValuePairBaseDisposable in entity.rootEntity.sharedComponents)
 116        {
 1117            if (keyValuePairBaseDisposable.Value.GetClassId() == (int) CLASS_ID.LOCKED_ON_EDIT)
 118            {
 1119                entitySingleComponentPayload.data = ((DCLLockedOnEdit) keyValuePairBaseDisposable.Value).GetModel();
 120            }
 121        }
 122
 1123        ChangeEntityComponent(entitySingleComponentPayload, scene);
 1124    }
 125
 126    public void ChangedEntityName(BIWEntity entity, ParcelScene scene)
 127    {
 1128        entitySingleComponentPayload.entityId = entity.rootEntity.entityId;
 1129        entitySingleComponentPayload.componentId = (int) CLASS_ID.NAME;
 130
 4131        foreach (KeyValuePair<Type, ISharedComponent> keyValuePairBaseDisposable in entity.rootEntity.sharedComponents)
 132        {
 1133            if (keyValuePairBaseDisposable.Value.GetClassId() == (int) CLASS_ID.NAME)
 134            {
 1135                entitySingleComponentPayload.data = ((DCLName) keyValuePairBaseDisposable.Value).GetModel();
 136            }
 137        }
 138
 1139        ChangeEntityComponent(entitySingleComponentPayload, scene);
 1140    }
 141
 142    void ChangeEntityComponent(EntitySingleComponentPayload payload, ParcelScene scene)
 143    {
 3144        modifyEntityComponentEvent.payload = payload;
 145
 3146        WebInterface.SceneEvent<ModifyEntityComponentEvent> sceneEvent = new WebInterface.SceneEvent<ModifyEntityCompone
 3147        sceneEvent.sceneId = scene.sceneData.id;
 3148        sceneEvent.eventType = BIWSettings.STATE_EVENT_NAME;
 3149        sceneEvent.payload = modifyEntityComponentEvent;
 150
 151        //Note (Adrian): We use Newtonsoft instead of JsonUtility because we need to deal with super classes, JsonUtilit
 3152        string message = JsonConvert.SerializeObject(sceneEvent, Formatting.None, new JsonSerializerSettings
 153        {
 154            ReferenceLoopHandling = ReferenceLoopHandling.Ignore
 155        });
 156
 3157        WebInterface.BuilderInWorldMessage(BIWSettings.SCENE_EVENT_NAME, message);
 3158        OnKernelUpdated?.Invoke();
 0159    }
 160
 161    public void AddEntityOnKernel(IDCLEntity entity, ParcelScene scene)
 162    {
 1163        if (scene == null)
 0164            return;
 165
 1166        List<ComponentPayload> list = new List<ComponentPayload>();
 167
 4168        foreach (KeyValuePair<CLASS_ID_COMPONENT, IEntityComponent> keyValuePair in entity.components)
 169        {
 1170            ComponentPayload componentPayLoad = new ComponentPayload();
 1171            componentPayLoad.componentId = Convert.ToInt32(keyValuePair.Key);
 172
 1173            if (keyValuePair.Key == CLASS_ID_COMPONENT.TRANSFORM)
 174            {
 1175                TransformComponent entityComponentModel = new TransformComponent();
 176
 1177                entityComponentModel.position = WorldStateUtils.ConvertUnityToScenePosition(entity.gameObject.transform.
 1178                entityComponentModel.rotation = new QuaternionRepresentation(entity.gameObject.transform.rotation);
 1179                entityComponentModel.scale = entity.gameObject.transform.lossyScale;
 180
 1181                componentPayLoad.data = entityComponentModel;
 1182            }
 183            else
 184            {
 0185                componentPayLoad.data = keyValuePair.Value.GetModel();
 186            }
 187
 1188            list.Add(componentPayLoad);
 189        }
 190
 2191        foreach (KeyValuePair<Type, ISharedComponent> keyValuePairBaseDisposable in entity.sharedComponents)
 192        {
 0193            ComponentPayload componentPayLoad = new ComponentPayload();
 194
 0195            componentPayLoad.componentId = keyValuePairBaseDisposable.Value.GetClassId();
 196
 0197            if (keyValuePairBaseDisposable.Value.GetClassId() == (int) CLASS_ID.NFT_SHAPE)
 198            {
 0199                NFTComponent nftComponent = new NFTComponent();
 0200                NFTShape.Model model = (NFTShape.Model) keyValuePairBaseDisposable.Value.GetModel();
 201
 0202                nftComponent.color = new ColorRepresentation(model.color);
 0203                nftComponent.assetId = model.assetId;
 0204                nftComponent.src = model.src;
 0205                nftComponent.style = model.style;
 206
 0207                componentPayLoad.data = nftComponent;
 0208            }
 209            else
 210            {
 0211                componentPayLoad.data = keyValuePairBaseDisposable.Value.GetModel();
 212            }
 213
 0214            list.Add(componentPayLoad);
 215        }
 216
 1217        SendNewEntityToKernel(scene.sceneData.id, entity.entityId, list.ToArray());
 1218    }
 219
 220    public void EntityTransformReport(IDCLEntity entity, ParcelScene scene)
 221    {
 1222        entitySingleComponentPayload.entityId = entity.entityId;
 1223        entitySingleComponentPayload.componentId = (int) CLASS_ID_COMPONENT.TRANSFORM;
 224
 1225        entityTransformComponentModel.position = WorldStateUtils.ConvertUnityToScenePosition(entity.gameObject.transform
 1226        entityTransformComponentModel.rotation = new QuaternionRepresentation(entity.gameObject.transform.rotation);
 1227        entityTransformComponentModel.scale = entity.gameObject.transform.lossyScale;
 228
 1229        entitySingleComponentPayload.data = entityTransformComponentModel;
 230
 1231        modifyEntityComponentEvent.payload = entitySingleComponentPayload;
 232
 1233        WebInterface.SceneEvent<ModifyEntityComponentEvent> sceneEvent = new WebInterface.SceneEvent<ModifyEntityCompone
 1234        sceneEvent.sceneId = scene.sceneData.id;
 1235        sceneEvent.eventType = BIWSettings.STATE_EVENT_NAME;
 1236        sceneEvent.payload = modifyEntityComponentEvent;
 237
 238        //Note (Adrian): We use Newtonsoft instead of JsonUtility because we need to deal with super classes, JsonUtilit
 1239        string message = JsonConvert.SerializeObject(sceneEvent);
 1240        WebInterface.BuilderInWorldMessage(BIWSettings.SCENE_EVENT_NAME, message);
 1241    }
 242
 243    public void RemoveEntityOnKernel(string entityId, ParcelScene scene)
 244    {
 2245        RemoveEntityEvent removeEntityEvent = new RemoveEntityEvent();
 2246        RemoveEntityPayload removeEntityPayLoad = new RemoveEntityPayload();
 2247        removeEntityPayLoad.entityId = entityId;
 2248        removeEntityEvent.payload = removeEntityPayLoad;
 249
 2250        WebInterface.SendSceneEvent(scene.sceneData.id, BIWSettings.STATE_EVENT_NAME, removeEntityEvent);
 2251        OnKernelUpdated?.Invoke();
 1252    }
 253
 4254    public void StartKernelEditMode(IParcelScene scene) { WebInterface.ReportControlEvent(new WebInterface.StartStateful
 255
 4256    public void ExitKernelEditMode(IParcelScene scene) { WebInterface.ReportControlEvent(new WebInterface.StopStatefulMo
 257
 258    public void PublishScene(ParcelScene scene, string sceneName, string sceneDescription, string sceneScreenshot)
 259    {
 1260        storeSceneState.payload.title = sceneName;
 1261        storeSceneState.payload.description = sceneDescription;
 1262        storeSceneState.payload.screenshot = sceneScreenshot;
 263
 1264        WebInterface.SendSceneEvent(scene.sceneData.id, BIWSettings.STATE_EVENT_NAME, storeSceneState);
 1265    }
 266
 267    // ReSharper disable Unity.PerformanceAnalysis
 268    void SendNewEntityToKernel(string sceneId, string entityId, ComponentPayload[] componentsPayload)
 269    {
 1270        AddEntityEvent addEntityEvent = new AddEntityEvent();
 1271        entityPayload.entityId = entityId;
 1272        entityPayload.components = componentsPayload;
 273
 1274        addEntityEvent.payload = entityPayload;
 275
 1276        WebInterface.SceneEvent<AddEntityEvent> sceneEvent = new WebInterface.SceneEvent<AddEntityEvent>();
 1277        sceneEvent.sceneId = sceneId;
 1278        sceneEvent.eventType = BIWSettings.STATE_EVENT_NAME;
 1279        sceneEvent.payload = addEntityEvent;
 280
 281        //Note(Adrian): We use Newtonsoft instead of JsonUtility because we need to deal with super classes, JsonUtility
 1282        string message = JsonConvert.SerializeObject(sceneEvent);
 1283        WebInterface.BuilderInWorldMessage(BIWSettings.SCENE_EVENT_NAME, message);
 1284        OnKernelUpdated?.Invoke();
 0285    }
 286
 287    #endregion
 288}