| | 1 | | using DCL; |
| | 2 | | using DCL.Components; |
| | 3 | | using DCL.Configuration; |
| | 4 | | using DCL.Controllers; |
| | 5 | | using DCL.Interface; |
| | 6 | | using DCL.Models; |
| | 7 | | using Newtonsoft.Json; |
| | 8 | | using System; |
| | 9 | | using System.Collections.Generic; |
| | 10 | | using UnityEngine; |
| | 11 | | using static ProtocolV2; |
| | 12 | | using Environment = DCL.Environment; |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// This class will handle all the messages that will be sent to kernel. |
| | 16 | | /// </summary> |
| | 17 | | public class BuilderInWorldBridge : MonoBehaviour |
| | 18 | | { |
| 0 | 19 | | 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 |
| 38 | 29 | | private TransformComponent entityTransformComponentModel = new TransformComponent(); |
| | 30 | |
|
| 38 | 31 | | private StoreSceneStateEvent storeSceneState = new StoreSceneStateEvent(); |
| 38 | 32 | | private SaveSceneStateEvent saveSceneState = new SaveSceneStateEvent(); |
| 38 | 33 | | private SaveProjectInfoEvent saveProjectInfo = new SaveProjectInfoEvent(); |
| 38 | 34 | | private ModifyEntityComponentEvent modifyEntityComponentEvent = new ModifyEntityComponentEvent(); |
| 38 | 35 | | private EntityPayload entityPayload = new EntityPayload(); |
| 38 | 36 | | private EntitySingleComponentPayload entitySingleComponentPayload = new EntitySingleComponentPayload(); |
| 38 | 37 | | internal BuilderProjectPayload builderProjectPayload = new BuilderProjectPayload(); |
| | 38 | |
|
| | 39 | | #region MessagesFromKernel |
| | 40 | |
|
| | 41 | | public void PublishSceneResult(string payload) |
| | 42 | | { |
| 0 | 43 | | PublishSceneResultPayload publishSceneResultPayload = JsonUtility.FromJson<PublishSceneResultPayload>(payload); |
| | 44 | |
|
| 0 | 45 | | if (publishSceneResultPayload.ok) |
| | 46 | | { |
| 0 | 47 | | OnPublishEnd?.Invoke(true, ""); |
| | 48 | |
|
| 0 | 49 | | AudioScriptableObjects.confirm.Play(); |
| 0 | 50 | | } |
| | 51 | | else |
| | 52 | | { |
| 0 | 53 | | OnPublishEnd?.Invoke(false, publishSceneResultPayload.error); |
| | 54 | |
|
| 0 | 55 | | AudioScriptableObjects.error.Play(); |
| | 56 | | } |
| 0 | 57 | | } |
| | 58 | |
|
| | 59 | | public void AddAssets(string payload) |
| | 60 | | { |
| | 61 | | //We remove the old assets to they don't collide with the new ones |
| 0 | 62 | | BIWUtils.RemoveAssetsFromCurrentScene(); |
| | 63 | |
|
| 0 | 64 | | AssetCatalogBridge.i.AddScenesObjectToSceneCatalog(payload); |
| 0 | 65 | | } |
| | 66 | |
|
| 0 | 67 | | public void RequestedHeaders(string payload) { OnHeadersReceived?.Invoke(JsonConvert.DeserializeObject<RequestHeader |
| | 68 | |
|
| | 69 | | public void BuilderProjectInfo(string payload) |
| | 70 | | { |
| 0 | 71 | | builderProjectPayload = JsonUtility.FromJson<BuilderProjectPayload>(payload); |
| 0 | 72 | | OnBuilderProjectInfo?.Invoke(builderProjectPayload.title, builderProjectPayload.description); |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | #endregion |
| | 76 | |
|
| | 77 | | #region MessagesToKernel |
| | 78 | |
|
| 0 | 79 | | public void AskKernelForCatalogHeadersWithParams(string method, string url) { WebInterface.SendRequestHeadersForUrl( |
| | 80 | |
|
| | 81 | | public void UpdateSmartItemComponent(BIWEntity entity, ParcelScene scene) |
| | 82 | | { |
| 1 | 83 | | SmartItemComponent smartItemComponent = entity.rootEntity.TryGetComponent<SmartItemComponent>(); |
| 1 | 84 | | if (smartItemComponent == null) |
| 0 | 85 | | return; |
| | 86 | |
|
| 1 | 87 | | entitySingleComponentPayload.entityId = entity.rootEntity.entityId; |
| 1 | 88 | | entitySingleComponentPayload.componentId = (int) CLASS_ID_COMPONENT.SMART_ITEM; |
| | 89 | |
|
| 1 | 90 | | entitySingleComponentPayload.data = smartItemComponent.GetValues(); |
| | 91 | |
|
| 1 | 92 | | ChangeEntityComponent(entitySingleComponentPayload, scene); |
| 1 | 93 | | } |
| | 94 | |
|
| | 95 | | public void SaveSceneInfo(ParcelScene scene, string sceneName, string sceneDescription, string sceneScreenshot) |
| | 96 | | { |
| 0 | 97 | | saveProjectInfo.payload.title = sceneName; |
| 0 | 98 | | saveProjectInfo.payload.description = sceneDescription; |
| 0 | 99 | | saveProjectInfo.payload.screenshot = sceneScreenshot; |
| | 100 | |
|
| 0 | 101 | | WebInterface.SendSceneEvent(scene.sceneData.id, BIWSettings.STATE_EVENT_NAME, saveProjectInfo); |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | public void SaveSceneState(ParcelScene scene) |
| | 105 | | { |
| 2 | 106 | | saveSceneState.payload = JsonUtility.ToJson(builderProjectPayload); |
| 2 | 107 | | WebInterface.SendSceneEvent(scene.sceneData.id, BIWSettings.STATE_EVENT_NAME, saveSceneState); |
| 2 | 108 | | } |
| | 109 | |
|
| | 110 | | public void ChangeEntityLockStatus(BIWEntity entity, ParcelScene scene) |
| | 111 | | { |
| 1 | 112 | | entitySingleComponentPayload.entityId = entity.rootEntity.entityId; |
| 1 | 113 | | entitySingleComponentPayload.componentId = (int) CLASS_ID.LOCKED_ON_EDIT; |
| | 114 | |
|
| 4 | 115 | | foreach (KeyValuePair<Type, ISharedComponent> keyValuePairBaseDisposable in entity.rootEntity.sharedComponents) |
| | 116 | | { |
| 1 | 117 | | if (keyValuePairBaseDisposable.Value.GetClassId() == (int) CLASS_ID.LOCKED_ON_EDIT) |
| | 118 | | { |
| 1 | 119 | | entitySingleComponentPayload.data = ((DCLLockedOnEdit) keyValuePairBaseDisposable.Value).GetModel(); |
| | 120 | | } |
| | 121 | | } |
| | 122 | |
|
| 1 | 123 | | ChangeEntityComponent(entitySingleComponentPayload, scene); |
| 1 | 124 | | } |
| | 125 | |
|
| | 126 | | public void ChangedEntityName(BIWEntity entity, ParcelScene scene) |
| | 127 | | { |
| 1 | 128 | | entitySingleComponentPayload.entityId = entity.rootEntity.entityId; |
| 1 | 129 | | entitySingleComponentPayload.componentId = (int) CLASS_ID.NAME; |
| | 130 | |
|
| 4 | 131 | | foreach (KeyValuePair<Type, ISharedComponent> keyValuePairBaseDisposable in entity.rootEntity.sharedComponents) |
| | 132 | | { |
| 1 | 133 | | if (keyValuePairBaseDisposable.Value.GetClassId() == (int) CLASS_ID.NAME) |
| | 134 | | { |
| 1 | 135 | | entitySingleComponentPayload.data = ((DCLName) keyValuePairBaseDisposable.Value).GetModel(); |
| | 136 | | } |
| | 137 | | } |
| | 138 | |
|
| 1 | 139 | | ChangeEntityComponent(entitySingleComponentPayload, scene); |
| 1 | 140 | | } |
| | 141 | |
|
| | 142 | | void ChangeEntityComponent(EntitySingleComponentPayload payload, ParcelScene scene) |
| | 143 | | { |
| 3 | 144 | | modifyEntityComponentEvent.payload = payload; |
| | 145 | |
|
| 3 | 146 | | WebInterface.SceneEvent<ModifyEntityComponentEvent> sceneEvent = new WebInterface.SceneEvent<ModifyEntityCompone |
| 3 | 147 | | sceneEvent.sceneId = scene.sceneData.id; |
| 3 | 148 | | sceneEvent.eventType = BIWSettings.STATE_EVENT_NAME; |
| 3 | 149 | | sceneEvent.payload = modifyEntityComponentEvent; |
| | 150 | |
|
| | 151 | | //Note (Adrian): We use Newtonsoft instead of JsonUtility because we need to deal with super classes, JsonUtilit |
| 3 | 152 | | string message = JsonConvert.SerializeObject(sceneEvent, Formatting.None, new JsonSerializerSettings |
| | 153 | | { |
| | 154 | | ReferenceLoopHandling = ReferenceLoopHandling.Ignore |
| | 155 | | }); |
| | 156 | |
|
| 3 | 157 | | WebInterface.BuilderInWorldMessage(BIWSettings.SCENE_EVENT_NAME, message); |
| 3 | 158 | | OnKernelUpdated?.Invoke(); |
| 0 | 159 | | } |
| | 160 | |
|
| | 161 | | public void AddEntityOnKernel(IDCLEntity entity, ParcelScene scene) |
| | 162 | | { |
| 1 | 163 | | if (scene == null) |
| 0 | 164 | | return; |
| | 165 | |
|
| 1 | 166 | | List<ComponentPayload> list = new List<ComponentPayload>(); |
| | 167 | |
|
| 4 | 168 | | foreach (KeyValuePair<CLASS_ID_COMPONENT, IEntityComponent> keyValuePair in entity.components) |
| | 169 | | { |
| 1 | 170 | | ComponentPayload componentPayLoad = new ComponentPayload(); |
| 1 | 171 | | componentPayLoad.componentId = Convert.ToInt32(keyValuePair.Key); |
| | 172 | |
|
| 1 | 173 | | if (keyValuePair.Key == CLASS_ID_COMPONENT.TRANSFORM) |
| | 174 | | { |
| 1 | 175 | | TransformComponent entityComponentModel = new TransformComponent(); |
| | 176 | |
|
| 1 | 177 | | entityComponentModel.position = WorldStateUtils.ConvertUnityToScenePosition(entity.gameObject.transform. |
| 1 | 178 | | entityComponentModel.rotation = new QuaternionRepresentation(entity.gameObject.transform.rotation); |
| 1 | 179 | | entityComponentModel.scale = entity.gameObject.transform.lossyScale; |
| | 180 | |
|
| 1 | 181 | | componentPayLoad.data = entityComponentModel; |
| 1 | 182 | | } |
| | 183 | | else |
| | 184 | | { |
| 0 | 185 | | componentPayLoad.data = keyValuePair.Value.GetModel(); |
| | 186 | | } |
| | 187 | |
|
| 1 | 188 | | list.Add(componentPayLoad); |
| | 189 | | } |
| | 190 | |
|
| 2 | 191 | | foreach (KeyValuePair<Type, ISharedComponent> keyValuePairBaseDisposable in entity.sharedComponents) |
| | 192 | | { |
| 0 | 193 | | ComponentPayload componentPayLoad = new ComponentPayload(); |
| | 194 | |
|
| 0 | 195 | | componentPayLoad.componentId = keyValuePairBaseDisposable.Value.GetClassId(); |
| | 196 | |
|
| 0 | 197 | | if (keyValuePairBaseDisposable.Value.GetClassId() == (int) CLASS_ID.NFT_SHAPE) |
| | 198 | | { |
| 0 | 199 | | NFTComponent nftComponent = new NFTComponent(); |
| 0 | 200 | | NFTShape.Model model = (NFTShape.Model) keyValuePairBaseDisposable.Value.GetModel(); |
| | 201 | |
|
| 0 | 202 | | nftComponent.color = new ColorRepresentation(model.color); |
| 0 | 203 | | nftComponent.assetId = model.assetId; |
| 0 | 204 | | nftComponent.src = model.src; |
| 0 | 205 | | nftComponent.style = model.style; |
| | 206 | |
|
| 0 | 207 | | componentPayLoad.data = nftComponent; |
| 0 | 208 | | } |
| | 209 | | else |
| | 210 | | { |
| 0 | 211 | | componentPayLoad.data = keyValuePairBaseDisposable.Value.GetModel(); |
| | 212 | | } |
| | 213 | |
|
| 0 | 214 | | list.Add(componentPayLoad); |
| | 215 | | } |
| | 216 | |
|
| 1 | 217 | | SendNewEntityToKernel(scene.sceneData.id, entity.entityId, list.ToArray()); |
| 1 | 218 | | } |
| | 219 | |
|
| | 220 | | public void EntityTransformReport(IDCLEntity entity, ParcelScene scene) |
| | 221 | | { |
| 1 | 222 | | entitySingleComponentPayload.entityId = entity.entityId; |
| 1 | 223 | | entitySingleComponentPayload.componentId = (int) CLASS_ID_COMPONENT.TRANSFORM; |
| | 224 | |
|
| 1 | 225 | | entityTransformComponentModel.position = WorldStateUtils.ConvertUnityToScenePosition(entity.gameObject.transform |
| 1 | 226 | | entityTransformComponentModel.rotation = new QuaternionRepresentation(entity.gameObject.transform.rotation); |
| 1 | 227 | | entityTransformComponentModel.scale = entity.gameObject.transform.lossyScale; |
| | 228 | |
|
| 1 | 229 | | entitySingleComponentPayload.data = entityTransformComponentModel; |
| | 230 | |
|
| 1 | 231 | | modifyEntityComponentEvent.payload = entitySingleComponentPayload; |
| | 232 | |
|
| 1 | 233 | | WebInterface.SceneEvent<ModifyEntityComponentEvent> sceneEvent = new WebInterface.SceneEvent<ModifyEntityCompone |
| 1 | 234 | | sceneEvent.sceneId = scene.sceneData.id; |
| 1 | 235 | | sceneEvent.eventType = BIWSettings.STATE_EVENT_NAME; |
| 1 | 236 | | sceneEvent.payload = modifyEntityComponentEvent; |
| | 237 | |
|
| | 238 | | //Note (Adrian): We use Newtonsoft instead of JsonUtility because we need to deal with super classes, JsonUtilit |
| 1 | 239 | | string message = JsonConvert.SerializeObject(sceneEvent); |
| 1 | 240 | | WebInterface.BuilderInWorldMessage(BIWSettings.SCENE_EVENT_NAME, message); |
| 1 | 241 | | } |
| | 242 | |
|
| | 243 | | public void RemoveEntityOnKernel(string entityId, ParcelScene scene) |
| | 244 | | { |
| 2 | 245 | | RemoveEntityEvent removeEntityEvent = new RemoveEntityEvent(); |
| 2 | 246 | | RemoveEntityPayload removeEntityPayLoad = new RemoveEntityPayload(); |
| 2 | 247 | | removeEntityPayLoad.entityId = entityId; |
| 2 | 248 | | removeEntityEvent.payload = removeEntityPayLoad; |
| | 249 | |
|
| 2 | 250 | | WebInterface.SendSceneEvent(scene.sceneData.id, BIWSettings.STATE_EVENT_NAME, removeEntityEvent); |
| 2 | 251 | | OnKernelUpdated?.Invoke(); |
| 1 | 252 | | } |
| | 253 | |
|
| 4 | 254 | | public void StartKernelEditMode(IParcelScene scene) { WebInterface.ReportControlEvent(new WebInterface.StartStateful |
| | 255 | |
|
| 4 | 256 | | public void ExitKernelEditMode(IParcelScene scene) { WebInterface.ReportControlEvent(new WebInterface.StopStatefulMo |
| | 257 | |
|
| | 258 | | public void PublishScene(ParcelScene scene, string sceneName, string sceneDescription, string sceneScreenshot) |
| | 259 | | { |
| 1 | 260 | | storeSceneState.payload.title = sceneName; |
| 1 | 261 | | storeSceneState.payload.description = sceneDescription; |
| 1 | 262 | | storeSceneState.payload.screenshot = sceneScreenshot; |
| | 263 | |
|
| 1 | 264 | | WebInterface.SendSceneEvent(scene.sceneData.id, BIWSettings.STATE_EVENT_NAME, storeSceneState); |
| 1 | 265 | | } |
| | 266 | |
|
| | 267 | | // ReSharper disable Unity.PerformanceAnalysis |
| | 268 | | void SendNewEntityToKernel(string sceneId, string entityId, ComponentPayload[] componentsPayload) |
| | 269 | | { |
| 1 | 270 | | AddEntityEvent addEntityEvent = new AddEntityEvent(); |
| 1 | 271 | | entityPayload.entityId = entityId; |
| 1 | 272 | | entityPayload.components = componentsPayload; |
| | 273 | |
|
| 1 | 274 | | addEntityEvent.payload = entityPayload; |
| | 275 | |
|
| 1 | 276 | | WebInterface.SceneEvent<AddEntityEvent> sceneEvent = new WebInterface.SceneEvent<AddEntityEvent>(); |
| 1 | 277 | | sceneEvent.sceneId = sceneId; |
| 1 | 278 | | sceneEvent.eventType = BIWSettings.STATE_EVENT_NAME; |
| 1 | 279 | | sceneEvent.payload = addEntityEvent; |
| | 280 | |
|
| | 281 | | //Note(Adrian): We use Newtonsoft instead of JsonUtility because we need to deal with super classes, JsonUtility |
| 1 | 282 | | string message = JsonConvert.SerializeObject(sceneEvent); |
| 1 | 283 | | WebInterface.BuilderInWorldMessage(BIWSettings.SCENE_EVENT_NAME, message); |
| 1 | 284 | | OnKernelUpdated?.Invoke(); |
| 0 | 285 | | } |
| | 286 | |
|
| | 287 | | #endregion |
| | 288 | | } |