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