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