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