| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using DCL.Components; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.WorldRuntime |
| | 8 | | { |
| | 9 | | internal class ResourcesLoadTrackerLegacyECS : IResourcesLoadTracker |
| | 10 | | { |
| 2 | 11 | | public int pendingResourcesCount => disposableNotReady?.Count ?? 0; |
| | 12 | | public float loadingProgress |
| | 13 | | { |
| | 14 | | get |
| | 15 | | { |
| 503 | 16 | | int sharedComponentsCount = componentsManager.GetSceneSharedComponentsCount(); |
| 503 | 17 | | return sharedComponentsCount > 0 ? (sharedComponentsCount - pendingResourcesCount) * 100f / sharedCompon |
| | 18 | | } |
| | 19 | | } |
| | 20 | |
|
| | 21 | | public event Action OnResourcesLoaded; |
| | 22 | | public event Action OnStatusUpdate; |
| | 23 | |
|
| | 24 | | private readonly IECSComponentsManagerLegacy componentsManager; |
| | 25 | | private readonly IWorldState worldState; |
| | 26 | |
|
| 544 | 27 | | private List<string> disposableNotReady = new List<string>(); |
| | 28 | |
|
| 544 | 29 | | public ResourcesLoadTrackerLegacyECS(IECSComponentsManagerLegacy componentsManager, IWorldState worldState) |
| | 30 | | { |
| 544 | 31 | | this.componentsManager = componentsManager; |
| 544 | 32 | | this.worldState = worldState; |
| | 33 | |
|
| 544 | 34 | | componentsManager.OnAddSharedComponent += OnSharedComponentAdded; |
| 544 | 35 | | } |
| | 36 | |
|
| | 37 | | public void Dispose() |
| | 38 | | { |
| 502 | 39 | | componentsManager.OnAddSharedComponent -= OnSharedComponentAdded; |
| 502 | 40 | | disposableNotReady = null; |
| 502 | 41 | | } |
| | 42 | |
|
| | 43 | | public bool CheckPendingResources() |
| | 44 | | { |
| 3 | 45 | | List<ISharedComponent> disposableComponents = componentsManager.GetSceneSharedComponentsDictionary().Values. |
| 20 | 46 | | for (int i = 0; i < disposableComponents.Count; i++) |
| | 47 | | { |
| 7 | 48 | | if (!disposableNotReady.Contains(disposableComponents[i].id)) |
| | 49 | | { |
| 0 | 50 | | disposableNotReady.Add(disposableComponents[i].id); |
| | 51 | | } |
| 7 | 52 | | disposableComponents[i].CallWhenReady(OnDisposableReady); |
| | 53 | | } |
| 3 | 54 | | return disposableNotReady.Count > 0; |
| | 55 | | } |
| | 56 | |
|
| | 57 | | public void PrintWaitingResourcesDebugInfo() |
| | 58 | | { |
| 0 | 59 | | foreach (string componentId in disposableNotReady) |
| | 60 | | { |
| 0 | 61 | | if (componentsManager.HasSceneSharedComponent(componentId)) |
| | 62 | | { |
| 0 | 63 | | var component = componentsManager.GetSceneSharedComponent(componentId); |
| | 64 | |
|
| 0 | 65 | | Debug.Log($"Waiting for: {component.ToString()}"); |
| | 66 | |
|
| 0 | 67 | | foreach (var entity in component.GetAttachedEntities()) |
| | 68 | | { |
| 0 | 69 | | var loader = worldState.GetLoaderForEntity(entity); |
| | 70 | |
|
| 0 | 71 | | string loadInfo = "No loader"; |
| | 72 | |
|
| 0 | 73 | | if (loader != null) |
| | 74 | | { |
| 0 | 75 | | loadInfo = loader.ToString(); |
| | 76 | | } |
| | 77 | |
|
| 0 | 78 | | Debug.Log($"This shape is attached to {entity.entityId} entity. Click here for highlight it.\nLo |
| | 79 | | } |
| | 80 | | } |
| | 81 | | else |
| | 82 | | { |
| 0 | 83 | | Debug.Log($"Waiting for missing component? id: {componentId}"); |
| | 84 | | } |
| | 85 | | } |
| 0 | 86 | | } |
| | 87 | |
|
| | 88 | | public string GetStateString() |
| | 89 | | { |
| 0 | 90 | | int sharedComponentsCount = componentsManager.GetSceneSharedComponentsCount(); |
| 0 | 91 | | if (sharedComponentsCount > 0) |
| 0 | 92 | | return $"left to ready:{sharedComponentsCount - pendingResourcesCount}/{sharedComponentsCount} ({loading |
| | 93 | |
|
| 0 | 94 | | return $"no components. waiting..."; |
| | 95 | | } |
| | 96 | |
|
| | 97 | | private void OnSharedComponentAdded(string id, ISharedComponent component) |
| | 98 | | { |
| 4 | 99 | | disposableNotReady.Add(id); |
| 4 | 100 | | } |
| | 101 | |
|
| | 102 | | private void OnDisposableReady(ISharedComponent component) |
| | 103 | | { |
| 7 | 104 | | if (disposableNotReady == null) |
| 0 | 105 | | return; |
| | 106 | |
|
| 7 | 107 | | if (!disposableNotReady.Remove(component.id)) |
| 3 | 108 | | return; |
| | 109 | |
|
| 4 | 110 | | if (disposableNotReady.Count == 0) |
| | 111 | | { |
| 2 | 112 | | OnResourcesLoaded?.Invoke(); |
| 2 | 113 | | } |
| | 114 | | else |
| | 115 | | { |
| 2 | 116 | | OnStatusUpdate?.Invoke(); |
| | 117 | | } |
| 2 | 118 | | } |
| | 119 | | } |
| | 120 | | } |