| | 1 | | using DCL.Models; |
| | 2 | | using System; |
| | 3 | | using System.Collections; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using DCL.Controllers; |
| | 6 | | using UnityEngine; |
| | 7 | | using Decentraland.Sdk.Ecs6; |
| | 8 | |
|
| | 9 | | namespace DCL.Components |
| | 10 | | { |
| | 11 | | public abstract class BaseDisposable : IDelayedComponent, ISharedComponent |
| | 12 | | { |
| 191 | 13 | | public virtual string componentName => GetType().Name; |
| | 14 | | public event Action<IDCLEntity> OnAttach; |
| | 15 | | public event Action<IDCLEntity> OnDetach; |
| 1490 | 16 | | public string id { get; private set; } |
| 3198 | 17 | | public IParcelScene scene { get; private set; } |
| | 18 | |
|
| | 19 | | public abstract int GetClassId(); |
| | 20 | |
|
| | 21 | | public virtual void Initialize(IParcelScene scene, string id) |
| | 22 | | { |
| 579 | 23 | | this.scene = scene; |
| 579 | 24 | | this.id = id; |
| 579 | 25 | | } |
| | 26 | |
|
| | 27 | | ComponentUpdateHandler updateHandler; |
| 0 | 28 | | public CustomYieldInstruction yieldInstruction => updateHandler.yieldInstruction; |
| 652 | 29 | | public Coroutine routine => updateHandler.routine; |
| 10 | 30 | | public bool isRoutineRunning => updateHandler.isRoutineRunning; |
| | 31 | |
|
| | 32 | | public event Action<BaseDisposable> OnDispose; |
| | 33 | | public event Action<BaseDisposable> OnAppliedChanges; |
| | 34 | |
|
| 579 | 35 | | public HashSet<IDCLEntity> attachedEntities = new HashSet<IDCLEntity>(); |
| | 36 | |
|
| | 37 | | protected BaseModel model; |
| | 38 | |
|
| | 39 | | public HashSet<IDCLEntity> GetAttachedEntities() => |
| 199 | 40 | | attachedEntities; |
| | 41 | |
|
| | 42 | | public virtual void UpdateFromJSON(string json) => |
| 727 | 43 | | UpdateFromModel(model.GetDataFromJSON(json)); |
| | 44 | |
|
| | 45 | | public virtual void UpdateFromPb(ComponentBodyPayload payload) => |
| 0 | 46 | | UpdateFromModel(model.GetDataFromPb(payload)); |
| | 47 | |
|
| | 48 | | public virtual void UpdateFromModel(BaseModel newModel) |
| | 49 | | { |
| 759 | 50 | | model = newModel; |
| 759 | 51 | | updateHandler.ApplyChangesIfModified(model); |
| 759 | 52 | | } |
| | 53 | |
|
| 1737 | 54 | | public BaseDisposable() { updateHandler = CreateUpdateHandler(); } |
| | 55 | |
|
| 767 | 56 | | public virtual void RaiseOnAppliedChanges() { OnAppliedChanges?.Invoke(this); } |
| | 57 | |
|
| | 58 | | public virtual void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null) |
| | 59 | | { |
| 376 | 60 | | if (attachedEntities.Contains(entity)) |
| | 61 | | { |
| 0 | 62 | | return; |
| | 63 | | } |
| | 64 | |
|
| 376 | 65 | | System.Type thisType = overridenAttachedType != null ? overridenAttachedType : GetType(); |
| 376 | 66 | | scene.componentsManagerLegacy.AddSharedComponent(entity, thisType, this); |
| | 67 | |
|
| 376 | 68 | | attachedEntities.Add(entity); |
| | 69 | |
|
| 376 | 70 | | entity.OnRemoved += OnEntityRemoved; |
| | 71 | |
|
| 376 | 72 | | OnAttach?.Invoke(entity); |
| 326 | 73 | | } |
| | 74 | |
|
| 380 | 75 | | private void OnEntityRemoved(IDCLEntity entity) { DetachFrom(entity); } |
| | 76 | |
|
| | 77 | | public virtual void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) |
| | 78 | | { |
| 309 | 79 | | if (!attachedEntities.Contains(entity)) |
| 0 | 80 | | return; |
| | 81 | |
|
| 309 | 82 | | entity.OnRemoved -= OnEntityRemoved; |
| | 83 | |
|
| 309 | 84 | | System.Type thisType = overridenAttachedType != null ? overridenAttachedType : GetType(); |
| 309 | 85 | | scene.componentsManagerLegacy.RemoveSharedComponent(entity, thisType, false); |
| | 86 | |
|
| 309 | 87 | | attachedEntities.Remove(entity); |
| | 88 | |
|
| 309 | 89 | | OnDetach?.Invoke(entity); |
| 259 | 90 | | } |
| | 91 | |
|
| | 92 | | public void DetachFromEveryEntity() |
| | 93 | | { |
| 157 | 94 | | if (attachedEntities == null) |
| 0 | 95 | | return; |
| | 96 | |
|
| 157 | 97 | | IDCLEntity[] attachedEntitiesArray = new IDCLEntity[attachedEntities.Count]; |
| | 98 | |
|
| 157 | 99 | | attachedEntities.CopyTo(attachedEntitiesArray); |
| | 100 | |
|
| 390 | 101 | | for (int i = 0; i < attachedEntitiesArray.Length; i++) |
| | 102 | | { |
| 38 | 103 | | DetachFrom(attachedEntitiesArray[i]); |
| | 104 | | } |
| 157 | 105 | | } |
| | 106 | |
|
| | 107 | | public virtual void Dispose() |
| | 108 | | { |
| 157 | 109 | | OnDispose?.Invoke(this); |
| 157 | 110 | | DetachFromEveryEntity(); |
| 157 | 111 | | } |
| | 112 | |
|
| 114 | 113 | | public virtual BaseModel GetModel() => model; |
| | 114 | |
|
| | 115 | | public abstract IEnumerator ApplyChanges(BaseModel model); |
| | 116 | |
|
| 499 | 117 | | public virtual ComponentUpdateHandler CreateUpdateHandler() { return new ComponentUpdateHandler(this); } |
| | 118 | |
|
| 759 | 119 | | public bool IsValid() { return true; } |
| | 120 | |
|
| | 121 | | public void Cleanup() |
| | 122 | | { |
| 0 | 123 | | if (isRoutineRunning) |
| | 124 | | { |
| 0 | 125 | | CoroutineStarter.Stop(routine); |
| | 126 | | } |
| 0 | 127 | | } |
| | 128 | |
|
| | 129 | | public virtual void CallWhenReady(Action<ISharedComponent> callback) |
| | 130 | | { |
| | 131 | | //By default there's no initialization process and we call back as soon as we get the suscription |
| 6 | 132 | | callback.Invoke(this); |
| 6 | 133 | | } |
| | 134 | | } |
| | 135 | | } |