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