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