| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using DCL.Components; |
| | 5 | | using DCL.Controllers; |
| | 6 | | using DCL.Helpers; |
| | 7 | | using DCL.Models; |
| | 8 | | using DCL.Rendering; |
| | 9 | | using UnityEngine; |
| | 10 | | using Decentraland.Sdk.Ecs6; |
| | 11 | |
|
| | 12 | | namespace DCL |
| | 13 | | { |
| | 14 | | public class ECSComponentsManagerLegacy : IECSComponentsManagerLegacy |
| | 15 | | { |
| 438 | 16 | | private readonly Dictionary<string, ISharedComponent> disposableComponents = new Dictionary<string, ISharedCompo |
| | 17 | |
|
| 438 | 18 | | private readonly Dictionary<long, Dictionary<Type, ISharedComponent>> entitiesSharedComponents = |
| | 19 | | new Dictionary<long, Dictionary<Type, ISharedComponent>>(); |
| | 20 | |
|
| 438 | 21 | | private readonly Dictionary<long, Dictionary<CLASS_ID_COMPONENT, IEntityComponent>> entitiesComponents = |
| | 22 | | new Dictionary<long, Dictionary<CLASS_ID_COMPONENT, IEntityComponent>>(); |
| | 23 | |
|
| | 24 | | private readonly IParcelScene scene; |
| | 25 | | private readonly IRuntimeComponentFactory componentFactory; |
| | 26 | | private readonly IParcelScenesCleaner parcelScenesCleaner; |
| | 27 | | private readonly ISceneBoundsChecker sceneBoundsChecker; |
| | 28 | | private readonly IPhysicsSyncController physicsSyncController; |
| | 29 | | private readonly ICullingController cullingController; |
| | 30 | | private readonly DataStore_FeatureFlag dataStoreFeatureFlag; |
| | 31 | | private readonly bool isSBCNerfEnabled; |
| | 32 | |
|
| | 33 | | public event Action<string, ISharedComponent> OnAddSharedComponent; |
| | 34 | |
|
| | 35 | | public ECSComponentsManagerLegacy(IParcelScene scene) |
| 438 | 36 | | : this(scene, |
| | 37 | | Environment.i.world.componentFactory, |
| | 38 | | Environment.i.platform.parcelScenesCleaner, |
| | 39 | | Environment.i.world.sceneBoundsChecker, |
| | 40 | | Environment.i.platform.physicsSyncController, |
| | 41 | | Environment.i.platform.cullingController, |
| 438 | 42 | | DataStore.i.featureFlags.flags.Get()) { } |
| | 43 | |
|
| 438 | 44 | | public ECSComponentsManagerLegacy(IParcelScene scene, |
| | 45 | | IRuntimeComponentFactory componentFactory, |
| | 46 | | IParcelScenesCleaner parcelScenesCleaner, |
| | 47 | | ISceneBoundsChecker sceneBoundsChecker, |
| | 48 | | IPhysicsSyncController physicsSyncController, |
| | 49 | | ICullingController cullingController, |
| | 50 | | FeatureFlag featureFlags) |
| | 51 | | { |
| 438 | 52 | | this.scene = scene; |
| 438 | 53 | | this.componentFactory = componentFactory; |
| 438 | 54 | | this.parcelScenesCleaner = parcelScenesCleaner; |
| 438 | 55 | | this.sceneBoundsChecker = sceneBoundsChecker; |
| 438 | 56 | | this.physicsSyncController = physicsSyncController; |
| 438 | 57 | | this.cullingController = cullingController; |
| | 58 | |
|
| 438 | 59 | | isSBCNerfEnabled = featureFlags.IsFeatureEnabled("NERF_SBC"); |
| 438 | 60 | | } |
| | 61 | |
|
| | 62 | | public void AddSharedComponent(IDCLEntity entity, Type componentType, ISharedComponent component) |
| | 63 | | { |
| 376 | 64 | | if (component == null) |
| | 65 | | { |
| 0 | 66 | | return; |
| | 67 | | } |
| | 68 | |
|
| 376 | 69 | | RemoveSharedComponent(entity, componentType); |
| | 70 | |
|
| 376 | 71 | | if (!entitiesSharedComponents.TryGetValue(entity.entityId, out Dictionary<Type, ISharedComponent> entityComp |
| | 72 | | { |
| 270 | 73 | | entityComponents = new Dictionary<Type, ISharedComponent>(); |
| 270 | 74 | | entitiesSharedComponents.Add(entity.entityId, entityComponents); |
| | 75 | | } |
| | 76 | |
|
| 376 | 77 | | entityComponents.Add(componentType, component); |
| 376 | 78 | | } |
| | 79 | |
|
| | 80 | | public void RemoveSharedComponent(IDCLEntity entity, Type targetType, bool triggerDetaching = true) |
| | 81 | | { |
| 758 | 82 | | if (!entitiesSharedComponents.TryGetValue(entity.entityId, out Dictionary<Type, ISharedComponent> entityComp |
| | 83 | | { |
| 296 | 84 | | return; |
| | 85 | | } |
| 462 | 86 | | if (!entityComponents.TryGetValue(targetType, out ISharedComponent component)) |
| | 87 | | { |
| 153 | 88 | | return; |
| | 89 | | } |
| 309 | 90 | | if (component == null) |
| 0 | 91 | | return; |
| | 92 | |
|
| 309 | 93 | | entityComponents.Remove(targetType); |
| | 94 | |
|
| 309 | 95 | | if (entityComponents.Count == 0) |
| | 96 | | { |
| 210 | 97 | | entitiesSharedComponents.Remove(entity.entityId); |
| | 98 | | } |
| | 99 | |
|
| 309 | 100 | | if (triggerDetaching) |
| 37 | 101 | | component.DetachFrom(entity, targetType); |
| 309 | 102 | | } |
| | 103 | |
|
| | 104 | | public T TryGetComponent<T>(IDCLEntity entity) where T : class |
| | 105 | | { |
| 0 | 106 | | T component = null; |
| 0 | 107 | | if (entitiesComponents.TryGetValue(entity.entityId, out Dictionary<CLASS_ID_COMPONENT, IEntityComponent> ent |
| | 108 | | { |
| 0 | 109 | | component = entityComponents.Values.FirstOrDefault(x => x is T) as T; |
| | 110 | |
|
| 0 | 111 | | if (component != null) |
| 0 | 112 | | return component; |
| | 113 | | } |
| | 114 | |
|
| 0 | 115 | | if (entitiesSharedComponents.TryGetValue(entity.entityId, out Dictionary<Type, ISharedComponent> entityShare |
| | 116 | | { |
| 0 | 117 | | component = entitySharedComponents.Values.FirstOrDefault(x => x is T) as T; |
| | 118 | |
|
| 0 | 119 | | if (component != null) |
| 0 | 120 | | return component; |
| | 121 | | } |
| | 122 | |
|
| 0 | 123 | | return null; |
| | 124 | | } |
| | 125 | |
|
| | 126 | | public bool TryGetBaseComponent(IDCLEntity entity, CLASS_ID_COMPONENT componentId, out IEntityComponent componen |
| | 127 | | { |
| 236 | 128 | | if (entitiesComponents.TryGetValue(entity.entityId, out Dictionary<CLASS_ID_COMPONENT, IEntityComponent> ent |
| | 129 | | { |
| 65 | 130 | | return entityComponents.TryGetValue(componentId, out component); |
| | 131 | | } |
| 171 | 132 | | component = null; |
| 171 | 133 | | return false; |
| | 134 | | } |
| | 135 | |
|
| | 136 | | public bool TryGetSharedComponent(IDCLEntity entity, CLASS_ID componentId, out ISharedComponent component) |
| | 137 | | { |
| 0 | 138 | | component = null; |
| 0 | 139 | | if (!entitiesSharedComponents.TryGetValue(entity.entityId, out Dictionary<Type, ISharedComponent> entityComp |
| | 140 | | { |
| 0 | 141 | | return false; |
| | 142 | | } |
| | 143 | |
|
| 0 | 144 | | using (var iterator = entityComponents.GetEnumerator()) |
| | 145 | | { |
| 0 | 146 | | while (iterator.MoveNext()) |
| | 147 | | { |
| 0 | 148 | | if (iterator.Current.Value.GetClassId() != (int)componentId) |
| | 149 | | continue; |
| | 150 | |
|
| 0 | 151 | | component = iterator.Current.Value; |
| 0 | 152 | | break; |
| | 153 | | } |
| 0 | 154 | | } |
| | 155 | |
|
| 0 | 156 | | return component != null; |
| | 157 | | } |
| | 158 | |
|
| | 159 | | public ISharedComponent GetSharedComponent(IDCLEntity entity, Type targetType) |
| | 160 | | { |
| 68 | 161 | | if (!entitiesSharedComponents.TryGetValue(entity.entityId, out Dictionary<Type, ISharedComponent> entityComp |
| | 162 | | { |
| 11 | 163 | | return null; |
| | 164 | | } |
| | 165 | |
|
| 57 | 166 | | if (entityComponents.TryGetValue(targetType, out ISharedComponent component)) |
| | 167 | | { |
| 57 | 168 | | return component; |
| | 169 | | } |
| | 170 | |
|
| 0 | 171 | | return null; |
| | 172 | | } |
| | 173 | |
|
| | 174 | | public bool HasComponent(IDCLEntity entity, CLASS_ID_COMPONENT componentId) |
| | 175 | | { |
| 748 | 176 | | if (entitiesComponents.TryGetValue(entity.entityId, out Dictionary<CLASS_ID_COMPONENT, IEntityComponent> ent |
| | 177 | | { |
| 456 | 178 | | return entityComponents.ContainsKey(componentId); |
| | 179 | | } |
| 292 | 180 | | return false; |
| | 181 | | } |
| | 182 | |
|
| | 183 | | public bool HasSharedComponent(IDCLEntity entity, CLASS_ID componentId) |
| | 184 | | { |
| 0 | 185 | | if (TryGetSharedComponent(entity, componentId, out ISharedComponent component)) |
| | 186 | | { |
| 0 | 187 | | return component != null; |
| | 188 | | } |
| 0 | 189 | | return false; |
| | 190 | | } |
| | 191 | |
|
| | 192 | | public void RemoveComponent(IDCLEntity entity, CLASS_ID_COMPONENT componentId) |
| | 193 | | { |
| 21 | 194 | | if (entitiesComponents.TryGetValue(entity.entityId, out Dictionary<CLASS_ID_COMPONENT, IEntityComponent> ent |
| | 195 | | { |
| 21 | 196 | | entityComponents.Remove(componentId); |
| | 197 | |
|
| 21 | 198 | | if (entityComponents.Count == 0) |
| | 199 | | { |
| 16 | 200 | | entitiesComponents.Remove(entity.entityId); |
| | 201 | | } |
| | 202 | | } |
| 21 | 203 | | } |
| | 204 | |
|
| | 205 | | public void AddComponent(IDCLEntity entity, CLASS_ID_COMPONENT componentId, IEntityComponent component) |
| | 206 | | { |
| 286 | 207 | | if (!entitiesComponents.TryGetValue(entity.entityId, out Dictionary<CLASS_ID_COMPONENT, IEntityComponent> en |
| | 208 | | { |
| 244 | 209 | | entityComponents = new Dictionary<CLASS_ID_COMPONENT, IEntityComponent>(); |
| 244 | 210 | | entitiesComponents.Add(entity.entityId, entityComponents); |
| | 211 | |
|
| 244 | 212 | | entity.OnBaseComponentAdded?.Invoke(componentId, entity); |
| | 213 | | } |
| 286 | 214 | | entityComponents.Add(componentId, component); |
| 286 | 215 | | } |
| | 216 | |
|
| | 217 | | public IEntityComponent GetComponent(IDCLEntity entity, CLASS_ID_COMPONENT componentId) |
| | 218 | | { |
| 103 | 219 | | if (!entitiesComponents.TryGetValue(entity.entityId, out Dictionary<CLASS_ID_COMPONENT, IEntityComponent> en |
| | 220 | | { |
| 0 | 221 | | return null; |
| | 222 | | } |
| 103 | 223 | | if (entityComponents.TryGetValue(componentId, out IEntityComponent component)) |
| | 224 | | { |
| 103 | 225 | | return component; |
| | 226 | | } |
| 0 | 227 | | return null; |
| | 228 | | } |
| | 229 | |
|
| | 230 | | public IEnumerator<IEntityComponent> GetComponents(IDCLEntity entity) |
| | 231 | | { |
| 0 | 232 | | if (!entitiesComponents.TryGetValue(entity.entityId, out Dictionary<CLASS_ID_COMPONENT, IEntityComponent> en |
| | 233 | | { |
| 0 | 234 | | yield break; |
| | 235 | | } |
| | 236 | |
|
| 0 | 237 | | using (var iterator = entityComponents.GetEnumerator()) |
| | 238 | | { |
| 0 | 239 | | while (iterator.MoveNext()) |
| | 240 | | { |
| 0 | 241 | | yield return iterator.Current.Value; |
| | 242 | | } |
| 0 | 243 | | } |
| 0 | 244 | | } |
| | 245 | |
|
| | 246 | | public IEnumerator<ISharedComponent> GetSharedComponents(IDCLEntity entity) |
| | 247 | | { |
| 0 | 248 | | if (!entitiesSharedComponents.TryGetValue(entity.entityId, out Dictionary<Type, ISharedComponent> entityComp |
| | 249 | | { |
| 0 | 250 | | yield break; |
| | 251 | | } |
| | 252 | |
|
| 0 | 253 | | using (var iterator = entityComponents.GetEnumerator()) |
| | 254 | | { |
| 0 | 255 | | while (iterator.MoveNext()) |
| | 256 | | { |
| 0 | 257 | | yield return iterator.Current.Value; |
| | 258 | | } |
| 0 | 259 | | } |
| 0 | 260 | | } |
| | 261 | |
|
| | 262 | | public IReadOnlyDictionary<CLASS_ID_COMPONENT, IEntityComponent> GetComponentsDictionary(IDCLEntity entity) |
| | 263 | | { |
| 29 | 264 | | entitiesComponents.TryGetValue(entity.entityId, out Dictionary<CLASS_ID_COMPONENT, IEntityComponent> entityC |
| 29 | 265 | | return entityComponents; |
| | 266 | | } |
| | 267 | |
|
| | 268 | | public IReadOnlyDictionary<Type, ISharedComponent> GetSharedComponentsDictionary(IDCLEntity entity) |
| | 269 | | { |
| 0 | 270 | | entitiesSharedComponents.TryGetValue(entity.entityId, out Dictionary<Type, ISharedComponent> entityComponent |
| 0 | 271 | | return entityComponents; |
| | 272 | | } |
| | 273 | |
|
| | 274 | | public int GetComponentsCount() |
| | 275 | | { |
| 0 | 276 | | int count = 0; |
| 0 | 277 | | using (var entityIterator = entitiesComponents.GetEnumerator()) |
| | 278 | | { |
| 0 | 279 | | while (entityIterator.MoveNext()) |
| | 280 | | { |
| 0 | 281 | | count += entityIterator.Current.Value.Count; |
| | 282 | | } |
| 0 | 283 | | } |
| 0 | 284 | | return count; |
| | 285 | | } |
| | 286 | |
|
| | 287 | | public void CleanComponents(IDCLEntity entity) |
| | 288 | | { |
| 240 | 289 | | if (!entitiesComponents.TryGetValue(entity.entityId, out Dictionary<CLASS_ID_COMPONENT, IEntityComponent> en |
| | 290 | | { |
| 76 | 291 | | return; |
| | 292 | | } |
| | 293 | |
|
| 164 | 294 | | using (var iterator = entityComponents.GetEnumerator()) |
| | 295 | | { |
| 341 | 296 | | while (iterator.MoveNext()) |
| | 297 | | { |
| 177 | 298 | | if (iterator.Current.Value == null) |
| | 299 | | continue; |
| | 300 | |
|
| 177 | 301 | | if (iterator.Current.Value is ICleanable cleanableComponent) |
| 177 | 302 | | cleanableComponent.Cleanup(); |
| | 303 | |
|
| 177 | 304 | | if (!(iterator.Current.Value is IPoolableObjectContainer poolableContainer)) |
| | 305 | | continue; |
| | 306 | |
|
| 43 | 307 | | if (poolableContainer.poolableObject == null) |
| | 308 | | continue; |
| | 309 | |
|
| 7 | 310 | | poolableContainer.poolableObject.Release(); |
| | 311 | | } |
| 164 | 312 | | } |
| 164 | 313 | | entitiesComponents.Remove(entity.entityId); |
| 164 | 314 | | } |
| | 315 | |
|
| | 316 | | public bool HasSceneSharedComponent(string component) |
| | 317 | | { |
| 213 | 318 | | return disposableComponents.ContainsKey(component); |
| | 319 | | } |
| | 320 | |
|
| | 321 | | public ISharedComponent GetSceneSharedComponent(string component) |
| | 322 | | { |
| 843 | 323 | | disposableComponents.TryGetValue(component, out ISharedComponent sharedComponent); |
| 843 | 324 | | return sharedComponent; |
| | 325 | | } |
| | 326 | |
|
| | 327 | | public bool TryGetSceneSharedComponent(string component, out ISharedComponent sharedComponent) |
| | 328 | | { |
| 0 | 329 | | return disposableComponents.TryGetValue(component, out sharedComponent); |
| | 330 | | } |
| | 331 | |
|
| | 332 | | public IReadOnlyDictionary<string, ISharedComponent> GetSceneSharedComponentsDictionary() |
| | 333 | | { |
| 3 | 334 | | return disposableComponents; |
| | 335 | | } |
| | 336 | |
|
| | 337 | | public int GetSceneSharedComponentsCount() |
| | 338 | | { |
| 398 | 339 | | return disposableComponents.Count; |
| | 340 | | } |
| | 341 | |
|
| | 342 | | public void AddSceneSharedComponent(string component, ISharedComponent sharedComponent) |
| | 343 | | { |
| 579 | 344 | | disposableComponents.Add(component, sharedComponent); |
| 579 | 345 | | OnAddSharedComponent?.Invoke(component, sharedComponent); |
| 4 | 346 | | } |
| | 347 | |
|
| | 348 | | public bool RemoveSceneSharedComponent(string component) |
| | 349 | | { |
| 0 | 350 | | return disposableComponents.Remove(component); |
| | 351 | | } |
| | 352 | |
|
| | 353 | | public ISharedComponent SceneSharedComponentCreate(string id, int classId) |
| | 354 | | { |
| 576 | 355 | | if (disposableComponents.TryGetValue(id, out ISharedComponent component)) |
| 1 | 356 | | return component; |
| | 357 | |
|
| 575 | 358 | | IRuntimeComponentFactory factory = componentFactory; |
| | 359 | |
|
| 575 | 360 | | if (factory.createConditions.ContainsKey(classId)) |
| | 361 | | { |
| 46 | 362 | | if (!factory.createConditions[(int)classId].Invoke(scene.sceneData.sceneNumber, classId)) |
| 0 | 363 | | return null; |
| | 364 | | } |
| | 365 | |
|
| 575 | 366 | | ISharedComponent newComponent = componentFactory.CreateComponent(classId) as ISharedComponent; |
| | 367 | |
|
| 575 | 368 | | if (newComponent == null) |
| 0 | 369 | | return null; |
| | 370 | |
|
| 575 | 371 | | AddSceneSharedComponent(id, newComponent); |
| | 372 | |
|
| 575 | 373 | | newComponent.Initialize(scene, id); |
| | 374 | |
|
| 575 | 375 | | return newComponent; |
| | 376 | | } |
| | 377 | |
|
| | 378 | | public T GetSceneSharedComponent<T>() where T : class |
| | 379 | | { |
| 128 | 380 | | return disposableComponents.Values.FirstOrDefault(x => x is T) as T; |
| | 381 | | } |
| | 382 | |
|
| | 383 | | /** |
| | 384 | | * This method is called when we need to attach a disposable component to the entity |
| | 385 | | */ |
| | 386 | | public void SceneSharedComponentAttach(long entityId, string componentId) |
| | 387 | | { |
| 376 | 388 | | IDCLEntity entity = scene.GetEntityById(entityId); |
| | 389 | |
|
| 376 | 390 | | if (entity == null) |
| 0 | 391 | | return; |
| | 392 | |
|
| 376 | 393 | | if (disposableComponents.TryGetValue(componentId, out ISharedComponent sharedComponent)) |
| | 394 | | { |
| 376 | 395 | | sharedComponent.AttachTo(entity); |
| | 396 | | } |
| 376 | 397 | | } |
| | 398 | |
|
| | 399 | | public IEntityComponent EntityComponentCreateOrUpdate(long entityId, CLASS_ID_COMPONENT classId, object data) |
| | 400 | | { |
| 362 | 401 | | IDCLEntity entity = scene.GetEntityById(entityId); |
| | 402 | |
|
| 362 | 403 | | if (entity == null) |
| | 404 | | { |
| 0 | 405 | | Debug.LogError($"scene '{scene.sceneData.sceneNumber}': Can't create entity component if the entity {ent |
| 0 | 406 | | return null; |
| | 407 | | } |
| | 408 | |
|
| 362 | 409 | | IEntityComponent targetComponent = null; |
| | 410 | |
|
| 362 | 411 | | var overrideCreate = Environment.i.world.componentFactory.createOverrides; |
| | 412 | |
|
| 362 | 413 | | if (overrideCreate.ContainsKey((int)classId)) |
| | 414 | | { |
| 299 | 415 | | int classIdAsInt = (int)classId; |
| 299 | 416 | | overrideCreate[(int)classId].Invoke(scene.sceneData.sceneNumber, entityId, ref classIdAsInt, data); |
| 299 | 417 | | classId = (CLASS_ID_COMPONENT)classIdAsInt; |
| | 418 | | } |
| | 419 | |
|
| 362 | 420 | | bool wasCreated = false; |
| 362 | 421 | | if (!HasComponent(entity, classId)) |
| | 422 | | { |
| 285 | 423 | | targetComponent = componentFactory.CreateComponent((int) classId) as IEntityComponent; |
| | 424 | |
|
| 285 | 425 | | if (targetComponent != null) |
| | 426 | | { |
| 285 | 427 | | AddComponent(entity, classId, targetComponent); |
| | 428 | |
|
| 285 | 429 | | targetComponent.Initialize(scene, entity); |
| | 430 | |
|
| | 431 | | switch (data) |
| | 432 | | { |
| | 433 | | case string json: |
| 285 | 434 | | targetComponent.UpdateFromJSON(json); |
| 285 | 435 | | break; |
| | 436 | | case ComponentBodyPayload payload: |
| 0 | 437 | | targetComponent.UpdateFromPb(payload); |
| 0 | 438 | | break; |
| | 439 | | default: |
| 0 | 440 | | targetComponent.UpdateFromModel(data as BaseModel); |
| | 441 | | break; |
| | 442 | | } |
| | 443 | |
|
| 285 | 444 | | wasCreated = true; |
| | 445 | | } |
| | 446 | | } |
| | 447 | | else |
| | 448 | | { |
| 77 | 449 | | targetComponent = EntityComponentUpdate(entity, classId, data); |
| | 450 | | } |
| | 451 | |
|
| 362 | 452 | | var isTransform = classId == CLASS_ID_COMPONENT.TRANSFORM; |
| 362 | 453 | | var avoidThrottling = isSBCNerfEnabled ? isTransform && wasCreated : isTransform; |
| | 454 | |
|
| 362 | 455 | | if (targetComponent != null && targetComponent is IOutOfSceneBoundariesHandler) |
| 305 | 456 | | sceneBoundsChecker?.AddEntityToBeChecked(entity, runPreliminaryEvaluation: avoidThrottling); |
| | 457 | |
|
| 362 | 458 | | physicsSyncController.MarkDirty(); |
| 362 | 459 | | cullingController.MarkDirty(); |
| | 460 | |
|
| 362 | 461 | | return targetComponent; |
| | 462 | | } |
| | 463 | |
|
| | 464 | | public IEntityComponent EntityComponentUpdate(IDCLEntity entity, CLASS_ID_COMPONENT classId, |
| | 465 | | object data) |
| | 466 | | { |
| 101 | 467 | | if (entity == null) |
| | 468 | | { |
| 0 | 469 | | Debug.LogError($"Can't update the {classId} component of a nonexistent entity!", scene.GetSceneTransform |
| 0 | 470 | | return null; |
| | 471 | | } |
| | 472 | |
|
| 101 | 473 | | if (!HasComponent(entity, classId)) |
| | 474 | | { |
| 0 | 475 | | Debug.LogError($"Entity {entity.entityId} doesn't have a {classId} component to update!", scene.GetScene |
| 0 | 476 | | return null; |
| | 477 | | } |
| | 478 | |
|
| 101 | 479 | | var targetComponent = GetComponent(entity, classId); |
| 101 | 480 | | if (data is string json) |
| 101 | 481 | | targetComponent.UpdateFromJSON(json); |
| 0 | 482 | | else if (data is Decentraland.Sdk.Ecs6.ComponentBodyPayload payload) |
| 0 | 483 | | targetComponent.UpdateFromPb(payload); |
| | 484 | | else |
| 0 | 485 | | targetComponent.UpdateFromModel(data as BaseModel); |
| | 486 | |
|
| 101 | 487 | | return targetComponent; |
| | 488 | | } |
| | 489 | |
|
| | 490 | | public void SceneSharedComponentDispose(string id) |
| | 491 | | { |
| 93 | 492 | | if (disposableComponents.TryGetValue(id, out ISharedComponent sharedComponent)) |
| | 493 | | { |
| 93 | 494 | | sharedComponent?.Dispose(); |
| 93 | 495 | | disposableComponents.Remove(id); |
| | 496 | | } |
| 93 | 497 | | } |
| | 498 | |
|
| | 499 | | public ISharedComponent SceneSharedComponentUpdate(string id, BaseModel model) |
| | 500 | | { |
| 28 | 501 | | if (disposableComponents.TryGetValue(id, out ISharedComponent sharedComponent)) |
| | 502 | | { |
| 28 | 503 | | sharedComponent.UpdateFromModel(model); |
| 28 | 504 | | return sharedComponent; |
| | 505 | | } |
| | 506 | |
|
| 0 | 507 | | return null; |
| | 508 | | } |
| | 509 | |
|
| | 510 | | public ISharedComponent SceneSharedComponentUpdate(string id, string json) |
| | 511 | | { |
| 727 | 512 | | if (disposableComponents.TryGetValue(id, out ISharedComponent disposableComponent)) |
| | 513 | | { |
| 727 | 514 | | disposableComponent.UpdateFromJSON(json); |
| 727 | 515 | | return disposableComponent; |
| | 516 | | } |
| | 517 | |
|
| 0 | 518 | | return null; |
| | 519 | | } |
| | 520 | |
|
| | 521 | | public ISharedComponent SceneSharedComponentUpdate(string id, ComponentBodyPayload payload) |
| | 522 | | { |
| 0 | 523 | | if (!disposableComponents.TryGetValue(id, out ISharedComponent disposableComponent)) return null; |
| | 524 | |
|
| 0 | 525 | | disposableComponent.UpdateFromPb(payload); |
| 0 | 526 | | return disposableComponent; |
| | 527 | | } |
| | 528 | |
|
| | 529 | | public void EntityComponentRemove(long entityId, string componentName) |
| | 530 | | { |
| 36 | 531 | | IDCLEntity entity = scene.GetEntityById(entityId); |
| | 532 | |
|
| 36 | 533 | | if (entity == null) |
| | 534 | | { |
| 0 | 535 | | return; |
| | 536 | | } |
| | 537 | |
|
| | 538 | | switch (componentName) |
| | 539 | | { |
| | 540 | | case ComponentNameLiterals.Shape: |
| 0 | 541 | | if (entity.meshesInfo.currentShape is BaseShape baseShape) |
| | 542 | | { |
| 0 | 543 | | baseShape.DetachFrom(entity); |
| | 544 | | } |
| | 545 | |
|
| 0 | 546 | | return; |
| | 547 | |
|
| | 548 | | case ComponentNameLiterals.OnClick: |
| | 549 | | { |
| 1 | 550 | | if (TryGetBaseComponent(entity, CLASS_ID_COMPONENT.UUID_ON_CLICK, out IEntityComponent component |
| | 551 | | { |
| 1 | 552 | | Utils.SafeDestroy(component.GetTransform().gameObject); |
| 1 | 553 | | RemoveComponent(entity, CLASS_ID_COMPONENT.UUID_ON_CLICK); |
| | 554 | | } |
| | 555 | |
|
| 1 | 556 | | return; |
| | 557 | | } |
| | 558 | | case ComponentNameLiterals.OnPointerDown: |
| | 559 | | { |
| 1 | 560 | | if (TryGetBaseComponent(entity, CLASS_ID_COMPONENT.UUID_ON_DOWN, out IEntityComponent component) |
| | 561 | | { |
| 1 | 562 | | Utils.SafeDestroy(component.GetTransform().gameObject); |
| 1 | 563 | | RemoveComponent(entity, CLASS_ID_COMPONENT.UUID_ON_DOWN); |
| | 564 | | } |
| | 565 | | } |
| 1 | 566 | | return; |
| | 567 | | case ComponentNameLiterals.OnPointerUp: |
| | 568 | | { |
| 1 | 569 | | if (TryGetBaseComponent(entity, CLASS_ID_COMPONENT.UUID_ON_UP, out IEntityComponent component)) |
| | 570 | | { |
| 1 | 571 | | Utils.SafeDestroy(component.GetTransform().gameObject); |
| 1 | 572 | | RemoveComponent(entity, CLASS_ID_COMPONENT.UUID_ON_UP); |
| | 573 | | } |
| | 574 | | } |
| 1 | 575 | | return; |
| | 576 | | case ComponentNameLiterals.OnPointerHoverEnter: |
| | 577 | | { |
| 2 | 578 | | if (TryGetBaseComponent(entity, CLASS_ID_COMPONENT.UUID_ON_HOVER_ENTER, out IEntityComponent com |
| | 579 | | { |
| 2 | 580 | | Utils.SafeDestroy(component.GetTransform().gameObject); |
| 2 | 581 | | RemoveComponent(entity, CLASS_ID_COMPONENT.UUID_ON_HOVER_ENTER); |
| | 582 | | } |
| | 583 | | } |
| 2 | 584 | | return; |
| | 585 | | case ComponentNameLiterals.OnPointerHoverExit: |
| | 586 | | { |
| 2 | 587 | | if (TryGetBaseComponent(entity, CLASS_ID_COMPONENT.UUID_ON_HOVER_EXIT, out IEntityComponent comp |
| | 588 | | { |
| 2 | 589 | | Utils.SafeDestroy(component.GetTransform().gameObject); |
| 2 | 590 | | RemoveComponent(entity, CLASS_ID_COMPONENT.UUID_ON_HOVER_EXIT); |
| | 591 | | } |
| | 592 | | } |
| 2 | 593 | | return; |
| | 594 | | case ComponentNameLiterals.Transform: |
| | 595 | | { |
| 0 | 596 | | if (TryGetBaseComponent(entity, CLASS_ID_COMPONENT.AVATAR_ATTACH, out IEntityComponent component |
| | 597 | | { |
| 0 | 598 | | component.Cleanup(); |
| 0 | 599 | | RemoveComponent(entity, CLASS_ID_COMPONENT.AVATAR_ATTACH); |
| | 600 | | } |
| | 601 | | } |
| 0 | 602 | | return; |
| | 603 | |
|
| | 604 | | default: |
| | 605 | | { |
| 73 | 606 | | IEntityComponent component = GetComponentsDictionary(entity).FirstOrDefault(kp => kp.Value.compo |
| 29 | 607 | | if (component == null) |
| | 608 | | break; |
| | 609 | |
|
| 14 | 610 | | RemoveComponent(entity, (CLASS_ID_COMPONENT)component.GetClassId()); |
| | 611 | |
|
| 14 | 612 | | if (component is ICleanable cleanableComponent) |
| 14 | 613 | | cleanableComponent.Cleanup(); |
| | 614 | |
|
| 14 | 615 | | bool released = false; |
| 14 | 616 | | if (component is IPoolableObjectContainer poolableContainer) |
| | 617 | | { |
| 12 | 618 | | if (poolableContainer.poolableObject != null) |
| | 619 | | { |
| 2 | 620 | | poolableContainer.poolableObject.Release(); |
| 2 | 621 | | released = true; |
| | 622 | | } |
| | 623 | | } |
| 14 | 624 | | if (!released) |
| | 625 | | { |
| 12 | 626 | | Utils.SafeDestroy(component.GetTransform()?.gameObject); |
| | 627 | | } |
| | 628 | | break; |
| | 629 | | } |
| | 630 | | } |
| 29 | 631 | | } |
| | 632 | |
|
| | 633 | | public void DisposeAllSceneComponents() |
| | 634 | | { |
| 736 | 635 | | List<string> allDisposableComponents = disposableComponents.Select(x => x.Key).ToList(); |
| 1472 | 636 | | foreach (string id in allDisposableComponents) |
| | 637 | | { |
| 443 | 638 | | parcelScenesCleaner.MarkDisposableComponentForCleanup(scene, id); |
| | 639 | | } |
| 293 | 640 | | } |
| | 641 | | } |
| | 642 | | } |