< Summary

Class:BuilderInWorldBridge
Assembly:BIWBridge
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/BuilderInWorldBridge/BuilderInWorldBridge.cs
Covered lines:96
Uncovered lines:41
Coverable lines:137
Total lines:294
Line coverage:70% (96 of 137)
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%6200%
BuilderInWorldCatalogHeaders(...)0%6200%
BuilderProjectInfo(...)0%6200%
AskKernelForCatalogHeadersWithParams(...)0%110100%
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/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, string> OnBuilderProjectInfo;
 26    public event Action<string> OnCatalogHeadersReceived;
 27
 28    //This is done for optimization purposes, recreating new objects can increase garbage collection
 25529    private TransformComponent entityTransformComponentModel = new TransformComponent();
 30
 25531    private StoreSceneStateEvent storeSceneState = new StoreSceneStateEvent();
 25532    private SaveSceneStateEvent saveSceneState = new SaveSceneStateEvent();
 25533    private SaveProjectInfoEvent saveProjectInfo = new SaveProjectInfoEvent();
 25534    private ModifyEntityComponentEvent modifyEntityComponentEvent = new ModifyEntityComponentEvent();
 25535    private EntityPayload entityPayload = new EntityPayload();
 25536    private EntitySingleComponentPayload entitySingleComponentPayload = new EntitySingleComponentPayload();
 25537    private 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        foreach (var catalogItem in DataStore.i.builderInWorld.currentSceneCatalogItemDict.GetValues())
 63        {
 064            AssetCatalogBridge.i.RemoveSceneObjectToSceneCatalog(catalogItem.id);
 65        }
 066        DataStore.i.builderInWorld.currentSceneCatalogItemDict.Clear();
 67
 068        AssetCatalogBridge.i.AddSceneObjectToSceneCatalog(JsonConvert.DeserializeObject<SceneObject[]>(payload));
 069    }
 70
 071    public void BuilderInWorldCatalogHeaders(string payload) { OnCatalogHeadersReceived?.Invoke(payload); }
 72
 73    public void BuilderProjectInfo(string payload)
 74    {
 075        builderProjectPayload = JsonUtility.FromJson<BuilderProjectPayload>(payload);
 076        OnBuilderProjectInfo?.Invoke(builderProjectPayload.title, builderProjectPayload.description);
 077    }
 78
 79    #endregion
 80
 81    #region MessagesToKernel
 82
 5483    public void AskKernelForCatalogHeadersWithParams(string method, string url) { WebInterface.SendRequestHeadersForUrl(
 84
 85    public void UpdateSmartItemComponent(BIWEntity entity, ParcelScene scene)
 86    {
 187        SmartItemComponent smartItemComponent = entity.rootEntity.TryGetComponent<SmartItemComponent>();
 188        if (smartItemComponent == null)
 089            return;
 90
 191        entitySingleComponentPayload.entityId = entity.rootEntity.entityId;
 192        entitySingleComponentPayload.componentId = (int) CLASS_ID_COMPONENT.SMART_ITEM;
 93
 194        entitySingleComponentPayload.data = smartItemComponent.GetValues();
 95
 196        ChangeEntityComponent(entitySingleComponentPayload, scene);
 197    }
 98
 99    public void SaveSceneInfo(ParcelScene scene, string sceneName, string sceneDescription, string sceneScreenshot)
 100    {
 0101        saveProjectInfo.payload.title = sceneName;
 0102        saveProjectInfo.payload.description = sceneDescription;
 0103        saveProjectInfo.payload.screenshot = sceneScreenshot;
 104
 0105        WebInterface.SendSceneEvent(scene.sceneData.id, BIWSettings.STATE_EVENT_NAME, saveProjectInfo);
 0106    }
 107
 108    public void SaveSceneState(ParcelScene scene)
 109    {
 3110        saveSceneState.payload = JsonUtility.ToJson(builderProjectPayload);
 3111        WebInterface.SendSceneEvent(scene.sceneData.id, BIWSettings.STATE_EVENT_NAME, saveSceneState);
 3112    }
 113
 114    public void ChangeEntityLockStatus(BIWEntity entity, ParcelScene scene)
 115    {
 1116        entitySingleComponentPayload.entityId = entity.rootEntity.entityId;
 1117        entitySingleComponentPayload.componentId = (int) CLASS_ID.LOCKED_ON_EDIT;
 118
 4119        foreach (KeyValuePair<Type, ISharedComponent> keyValuePairBaseDisposable in entity.rootEntity.sharedComponents)
 120        {
 1121            if (keyValuePairBaseDisposable.Value.GetClassId() == (int) CLASS_ID.LOCKED_ON_EDIT)
 122            {
 1123                entitySingleComponentPayload.data = ((DCLLockedOnEdit) keyValuePairBaseDisposable.Value).GetModel();
 124            }
 125        }
 126
 1127        ChangeEntityComponent(entitySingleComponentPayload, scene);
 1128    }
 129
 130    public void ChangedEntityName(BIWEntity entity, ParcelScene scene)
 131    {
 1132        entitySingleComponentPayload.entityId = entity.rootEntity.entityId;
 1133        entitySingleComponentPayload.componentId = (int) CLASS_ID.NAME;
 134
 4135        foreach (KeyValuePair<Type, ISharedComponent> keyValuePairBaseDisposable in entity.rootEntity.sharedComponents)
 136        {
 1137            if (keyValuePairBaseDisposable.Value.GetClassId() == (int) CLASS_ID.NAME)
 138            {
 1139                entitySingleComponentPayload.data = ((DCLName) keyValuePairBaseDisposable.Value).GetModel();
 140            }
 141        }
 142
 1143        ChangeEntityComponent(entitySingleComponentPayload, scene);
 1144    }
 145
 146    void ChangeEntityComponent(EntitySingleComponentPayload payload, ParcelScene scene)
 147    {
 3148        modifyEntityComponentEvent.payload = payload;
 149
 3150        WebInterface.SceneEvent<ModifyEntityComponentEvent> sceneEvent = new WebInterface.SceneEvent<ModifyEntityCompone
 3151        sceneEvent.sceneId = scene.sceneData.id;
 3152        sceneEvent.eventType = BIWSettings.STATE_EVENT_NAME;
 3153        sceneEvent.payload = modifyEntityComponentEvent;
 154
 155        //Note (Adrian): We use Newtonsoft instead of JsonUtility because we need to deal with super classes, JsonUtilit
 3156        string message = JsonConvert.SerializeObject(sceneEvent, Formatting.None, new JsonSerializerSettings
 157        {
 158            ReferenceLoopHandling = ReferenceLoopHandling.Ignore
 159        });
 160
 3161        WebInterface.BuilderInWorldMessage(BIWSettings.SCENE_EVENT_NAME, message);
 3162        OnKernelUpdated?.Invoke();
 0163    }
 164
 165    public void AddEntityOnKernel(IDCLEntity entity, ParcelScene scene)
 166    {
 1167        if (scene == null)
 0168            return;
 169
 1170        List<ComponentPayload> list = new List<ComponentPayload>();
 171
 4172        foreach (KeyValuePair<CLASS_ID_COMPONENT, IEntityComponent> keyValuePair in entity.components)
 173        {
 1174            ComponentPayload componentPayLoad = new ComponentPayload();
 1175            componentPayLoad.componentId = Convert.ToInt32(keyValuePair.Key);
 176
 1177            if (keyValuePair.Key == CLASS_ID_COMPONENT.TRANSFORM)
 178            {
 1179                TransformComponent entityComponentModel = new TransformComponent();
 180
 1181                entityComponentModel.position = WorldStateUtils.ConvertUnityToScenePosition(entity.gameObject.transform.
 1182                entityComponentModel.rotation = new QuaternionRepresentation(entity.gameObject.transform.rotation);
 1183                entityComponentModel.scale = entity.gameObject.transform.lossyScale;
 184
 1185                componentPayLoad.data = entityComponentModel;
 1186            }
 187            else
 188            {
 0189                componentPayLoad.data = keyValuePair.Value.GetModel();
 190            }
 191
 1192            list.Add(componentPayLoad);
 193        }
 194
 2195        foreach (KeyValuePair<Type, ISharedComponent> keyValuePairBaseDisposable in entity.sharedComponents)
 196        {
 0197            ComponentPayload componentPayLoad = new ComponentPayload();
 198
 0199            componentPayLoad.componentId = keyValuePairBaseDisposable.Value.GetClassId();
 200
 0201            if (keyValuePairBaseDisposable.Value.GetClassId() == (int) CLASS_ID.NFT_SHAPE)
 202            {
 0203                NFTComponent nftComponent = new NFTComponent();
 0204                NFTShape.Model model = (NFTShape.Model) keyValuePairBaseDisposable.Value.GetModel();
 205
 0206                nftComponent.color = new ColorRepresentation(model.color);
 0207                nftComponent.assetId = model.assetId;
 0208                nftComponent.src = model.src;
 0209                nftComponent.style = model.style;
 210
 0211                componentPayLoad.data = nftComponent;
 0212            }
 213            else
 214            {
 0215                componentPayLoad.data = keyValuePairBaseDisposable.Value.GetModel();
 216            }
 217
 0218            list.Add(componentPayLoad);
 219        }
 220
 1221        SendNewEntityToKernel(scene.sceneData.id, entity.entityId, list.ToArray());
 1222    }
 223
 224    public void EntityTransformReport(IDCLEntity entity, ParcelScene scene)
 225    {
 1226        entitySingleComponentPayload.entityId = entity.entityId;
 1227        entitySingleComponentPayload.componentId = (int) CLASS_ID_COMPONENT.TRANSFORM;
 228
 1229        entityTransformComponentModel.position = WorldStateUtils.ConvertUnityToScenePosition(entity.gameObject.transform
 1230        entityTransformComponentModel.rotation = new QuaternionRepresentation(entity.gameObject.transform.rotation);
 1231        entityTransformComponentModel.scale = entity.gameObject.transform.lossyScale;
 232
 1233        entitySingleComponentPayload.data = entityTransformComponentModel;
 234
 1235        modifyEntityComponentEvent.payload = entitySingleComponentPayload;
 236
 1237        WebInterface.SceneEvent<ModifyEntityComponentEvent> sceneEvent = new WebInterface.SceneEvent<ModifyEntityCompone
 1238        sceneEvent.sceneId = scene.sceneData.id;
 1239        sceneEvent.eventType = BIWSettings.STATE_EVENT_NAME;
 1240        sceneEvent.payload = modifyEntityComponentEvent;
 241
 242        //Note (Adrian): We use Newtonsoft instead of JsonUtility because we need to deal with super classes, JsonUtilit
 1243        string message = JsonConvert.SerializeObject(sceneEvent);
 1244        WebInterface.BuilderInWorldMessage(BIWSettings.SCENE_EVENT_NAME, message);
 1245    }
 246
 247    public void RemoveEntityOnKernel(string entityId, ParcelScene scene)
 248    {
 2249        RemoveEntityEvent removeEntityEvent = new RemoveEntityEvent();
 2250        RemoveEntityPayload removeEntityPayLoad = new RemoveEntityPayload();
 2251        removeEntityPayLoad.entityId = entityId;
 2252        removeEntityEvent.payload = removeEntityPayLoad;
 253
 2254        WebInterface.SendSceneEvent(scene.sceneData.id, BIWSettings.STATE_EVENT_NAME, removeEntityEvent);
 2255        OnKernelUpdated?.Invoke();
 1256    }
 257
 16258    public void StartKernelEditMode(ParcelScene scene) { WebInterface.ReportControlEvent(new WebInterface.StartStatefulM
 259
 10260    public void ExitKernelEditMode(ParcelScene scene) { WebInterface.ReportControlEvent(new WebInterface.StopStatefulMod
 261
 262    public void PublishScene(ParcelScene scene, string sceneName, string sceneDescription, string sceneScreenshot)
 263    {
 1264        storeSceneState.payload.title = sceneName;
 1265        storeSceneState.payload.description = sceneDescription;
 1266        storeSceneState.payload.screenshot = sceneScreenshot;
 267
 1268        WebInterface.SendSceneEvent(scene.sceneData.id, BIWSettings.STATE_EVENT_NAME, storeSceneState);
 1269    }
 270
 271    // ReSharper disable Unity.PerformanceAnalysis
 272    void SendNewEntityToKernel(string sceneId, string entityId, ComponentPayload[] componentsPayload)
 273    {
 1274        AddEntityEvent addEntityEvent = new AddEntityEvent();
 1275        entityPayload.entityId = entityId;
 1276        entityPayload.components = componentsPayload;
 277
 1278        addEntityEvent.payload = entityPayload;
 279
 1280        WebInterface.SceneEvent<AddEntityEvent> sceneEvent = new WebInterface.SceneEvent<AddEntityEvent>();
 1281        sceneEvent.sceneId = sceneId;
 1282        sceneEvent.eventType = BIWSettings.STATE_EVENT_NAME;
 1283        sceneEvent.payload = addEntityEvent;
 284
 285
 286        //Note(Adrian): We use Newtonsoft instead of JsonUtility because we need to deal with super classes, JsonUtility
 1287        string message = JsonConvert.SerializeObject(sceneEvent);
 1288        WebInterface.BuilderInWorldMessage(BIWSettings.SCENE_EVENT_NAME, message);
 1289        OnKernelUpdated?.Invoke();
 0290    }
 291
 292    #endregion
 293
 294}