| | 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 | | { |
| 147 | 12 | | public virtual string componentName => GetType().Name; |
| 64 | 13 | | public string id { get; private set; } |
| 64 | 14 | | public IParcelScene scene { get; private set; } |
| | 15 | |
|
| | 16 | | public abstract int GetClassId(); |
| | 17 | |
|
| | 18 | | public virtual void Initialize(IParcelScene scene, string id) |
| | 19 | | { |
| 543 | 20 | | this.scene = scene; |
| 543 | 21 | | this.id = id; |
| 543 | 22 | | } |
| | 23 | |
|
| | 24 | | ComponentUpdateHandler updateHandler; |
| 0 | 25 | | public CustomYieldInstruction yieldInstruction => updateHandler.yieldInstruction; |
| 67 | 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 Action<BaseDisposable> OnAppliedChanges; |
| | 32 | |
|
| 543 | 33 | | public HashSet<IDCLEntity> attachedEntities = new HashSet<IDCLEntity>(); |
| | 34 | |
|
| | 35 | | protected BaseModel model; |
| | 36 | |
|
| 1 | 37 | | public HashSet<IDCLEntity> GetAttachedEntities() { return attachedEntities; } |
| | 38 | |
|
| 1136 | 39 | | public virtual void UpdateFromJSON(string json) { UpdateFromModel(model.GetDataFromJSON(json)); } |
| | 40 | |
|
| | 41 | | public virtual void UpdateFromModel(BaseModel newModel) |
| | 42 | | { |
| 619 | 43 | | model = newModel; |
| 619 | 44 | | updateHandler.ApplyChangesIfModified(model); |
| 619 | 45 | | } |
| | 46 | |
|
| 1629 | 47 | | public BaseDisposable() { updateHandler = CreateUpdateHandler(); } |
| | 48 | |
|
| 629 | 49 | | public virtual void RaiseOnAppliedChanges() { OnAppliedChanges?.Invoke(this); } |
| | 50 | |
|
| | 51 | | public virtual void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null) |
| | 52 | | { |
| 346 | 53 | | if (attachedEntities.Contains(entity)) |
| | 54 | | { |
| 0 | 55 | | return; |
| | 56 | | } |
| | 57 | |
|
| 346 | 58 | | System.Type thisType = overridenAttachedType != null ? overridenAttachedType : GetType(); |
| 346 | 59 | | entity.AddSharedComponent(thisType, this); |
| | 60 | |
|
| 346 | 61 | | attachedEntities.Add(entity); |
| | 62 | |
|
| 346 | 63 | | entity.OnRemoved += OnEntityRemoved; |
| | 64 | |
|
| 346 | 65 | | OnAttach?.Invoke(entity); |
| 272 | 66 | | } |
| | 67 | |
|
| 68 | 68 | | private void OnEntityRemoved(IDCLEntity entity) { DetachFrom(entity); } |
| | 69 | |
|
| | 70 | | public virtual void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) |
| | 71 | | { |
| 339 | 72 | | if (!attachedEntities.Contains(entity)) |
| 0 | 73 | | return; |
| | 74 | |
|
| 339 | 75 | | entity.OnRemoved -= OnEntityRemoved; |
| | 76 | |
|
| 339 | 77 | | System.Type thisType = overridenAttachedType != null ? overridenAttachedType : GetType(); |
| 339 | 78 | | entity.RemoveSharedComponent(thisType, false); |
| | 79 | |
|
| 339 | 80 | | attachedEntities.Remove(entity); |
| | 81 | |
|
| 339 | 82 | | OnDetach?.Invoke(entity); |
| 265 | 83 | | } |
| | 84 | |
|
| | 85 | | public void DetachFromEveryEntity() |
| | 86 | | { |
| 501 | 87 | | IDCLEntity[] attachedEntitiesArray = new IDCLEntity[attachedEntities.Count]; |
| | 88 | |
|
| 501 | 89 | | attachedEntities.CopyTo(attachedEntitiesArray); |
| | 90 | |
|
| 1554 | 91 | | for (int i = 0; i < attachedEntitiesArray.Length; i++) |
| | 92 | | { |
| 276 | 93 | | DetachFrom(attachedEntitiesArray[i]); |
| | 94 | | } |
| 501 | 95 | | } |
| | 96 | |
|
| 1002 | 97 | | public virtual void Dispose() { DetachFromEveryEntity(); } |
| | 98 | |
|
| 243 | 99 | | public virtual BaseModel GetModel() => model; |
| | 100 | |
|
| | 101 | | public abstract IEnumerator ApplyChanges(BaseModel model); |
| | 102 | |
|
| 465 | 103 | | public virtual ComponentUpdateHandler CreateUpdateHandler() { return new ComponentUpdateHandler(this); } |
| | 104 | |
|
| 619 | 105 | | public bool IsValid() { return true; } |
| | 106 | |
|
| | 107 | | public void Cleanup() |
| | 108 | | { |
| 0 | 109 | | if (isRoutineRunning) |
| | 110 | | { |
| 0 | 111 | | CoroutineStarter.Stop(routine); |
| | 112 | | } |
| 0 | 113 | | } |
| | 114 | |
|
| | 115 | | public virtual void CallWhenReady(Action<ISharedComponent> callback) |
| | 116 | | { |
| | 117 | | //By default there's no initialization process and we call back as soon as we get the suscription |
| 6 | 118 | | callback.Invoke(this); |
| 6 | 119 | | } |
| | 120 | | } |
| | 121 | | } |