| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL; |
| | 4 | | using DCL.WorldRuntime; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | public class ResourcesLoadTrackerECS : IResourcesLoadTracker |
| | 8 | | { |
| 18 | 9 | | public int pendingResourcesCount => resourcesNotReady.Count; |
| | 10 | |
|
| | 11 | | public float loadingProgress |
| | 12 | | { |
| | 13 | | get |
| | 14 | | { |
| 4 | 15 | | if (resourcesReady < pendingResourcesCount) |
| 0 | 16 | | return 0; |
| 4 | 17 | | return resourcesReady > 0 ? (resourcesReady - pendingResourcesCount) * 100f / resourcesReady : 100f; |
| | 18 | | } |
| | 19 | | } |
| | 20 | |
|
| | 21 | | public event Action OnResourcesLoaded; |
| | 22 | | public event Action OnStatusUpdate; |
| | 23 | |
|
| | 24 | | private int resourcesReady; |
| | 25 | |
|
| 9 | 26 | | private readonly List<object> resourcesNotReady = new List<object>(); |
| | 27 | | private readonly int sceneNumber; |
| | 28 | | private readonly DataStore_ECS7 dataStore; |
| | 29 | |
|
| 9 | 30 | | public ResourcesLoadTrackerECS(DataStore_ECS7 dataStoreEcs7, int sceneNumber) |
| | 31 | | { |
| 9 | 32 | | this.dataStore = dataStoreEcs7; |
| 9 | 33 | | this.sceneNumber = sceneNumber; |
| 9 | 34 | | dataStore.pendingSceneResources.AddOrSet(sceneNumber, new BaseRefCountedCollection<object>()); |
| 9 | 35 | | dataStore.pendingSceneResources[sceneNumber].OnRefCountUpdated += ResourcesUpdate; |
| 9 | 36 | | } |
| | 37 | |
|
| | 38 | | public void Dispose() |
| | 39 | | { |
| 0 | 40 | | dataStore.pendingSceneResources[sceneNumber].OnRefCountUpdated -= ResourcesUpdate; |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | public void PrintWaitingResourcesDebugInfo() |
| | 44 | | { |
| 0 | 45 | | foreach (var model in resourcesNotReady) |
| | 46 | | { |
| 0 | 47 | | Debug.Log($"This model is waiting to be loaded: " + model); |
| | 48 | | } |
| 0 | 49 | | } |
| | 50 | |
|
| | 51 | | public string GetStateString() |
| | 52 | | { |
| 0 | 53 | | int totalComponents = resourcesNotReady.Count + resourcesReady; |
| 0 | 54 | | if (totalComponents > 0) |
| 0 | 55 | | return $"left to ready:{totalComponents - resourcesReady}/{totalComponents} ({loadingProgress}%)"; |
| | 56 | |
|
| 0 | 57 | | return $"no components. waiting..."; |
| | 58 | | } |
| | 59 | |
|
| | 60 | | public bool CheckPendingResources() |
| | 61 | | { |
| 2 | 62 | | return pendingResourcesCount > 0; |
| | 63 | | } |
| | 64 | |
|
| | 65 | | private void ResourcesUpdate(object model, int refCount) |
| | 66 | | { |
| 12 | 67 | | if (refCount > 0) |
| 6 | 68 | | PendingResourceAdded(model); |
| | 69 | | else |
| 6 | 70 | | ResourceReady(model); |
| 6 | 71 | | } |
| | 72 | |
|
| | 73 | | private void PendingResourceAdded(object model) |
| | 74 | | { |
| 6 | 75 | | if(!resourcesNotReady.Contains(model)) |
| 6 | 76 | | resourcesNotReady.Add(model); |
| 6 | 77 | | } |
| | 78 | |
|
| | 79 | | private void ResourceReady(object model) |
| | 80 | | { |
| 6 | 81 | | if(resourcesNotReady.Contains(model)) |
| 6 | 82 | | resourcesNotReady.Remove(model); |
| | 83 | |
|
| 6 | 84 | | resourcesReady++; |
| | 85 | |
|
| 6 | 86 | | if (resourcesNotReady.Count == 0) |
| 5 | 87 | | OnResourcesLoaded?.Invoke(); |
| | 88 | | else |
| 1 | 89 | | OnStatusUpdate?.Invoke(); |
| 1 | 90 | | } |
| | 91 | | } |