| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using DCL.NotificationModel; |
| | 5 | | using UnityEngine; |
| | 6 | | using Type = DCL.NotificationModel.Type; |
| | 7 | |
|
| | 8 | | namespace DCL |
| | 9 | | { |
| | 10 | | public class PreviewSceneLimitsWarning : IDisposable |
| | 11 | | { |
| | 12 | | private const string NOTIFICATION_GROUP = "SceneLimitationExceeded"; |
| | 13 | | private const string NOTIFICATION_MESSAGE = "Scene's limits exceeded: {0}"; |
| | 14 | |
|
| | 15 | | internal const float CHECK_INTERVAL = 0.2f; |
| | 16 | |
|
| | 17 | | private string sceneId; |
| | 18 | | private Coroutine updateRoutine; |
| | 19 | | private bool isActive = false; |
| | 20 | |
|
| | 21 | | internal bool isShowingNotification = false; |
| | 22 | |
|
| | 23 | | private readonly IWorldState worldState; |
| | 24 | |
|
| 1 | 25 | | private static readonly Model limitReachedNotification = new Model() |
| | 26 | | { |
| | 27 | | type = Type.WARNING, |
| | 28 | | groupID = NOTIFICATION_GROUP, |
| | 29 | | message = NOTIFICATION_MESSAGE |
| | 30 | | }; |
| | 31 | |
|
| 5 | 32 | | public PreviewSceneLimitsWarning(IWorldState worldState) |
| | 33 | | { |
| 5 | 34 | | this.worldState = worldState; |
| 5 | 35 | | } |
| | 36 | |
|
| | 37 | | public void Dispose() |
| | 38 | | { |
| 6 | 39 | | KernelConfig.i.OnChange -= OnKernelConfigChanged; |
| 6 | 40 | | StopChecking(); |
| 6 | 41 | | } |
| | 42 | |
|
| | 43 | | public void SetActive(bool active) |
| | 44 | | { |
| 6 | 45 | | if (active && !isActive) |
| | 46 | | { |
| 5 | 47 | | sceneId = KernelConfig.i.Get().debugConfig.sceneLimitsWarningSceneId; |
| 5 | 48 | | KernelConfig.i.OnChange += OnKernelConfigChanged; |
| 5 | 49 | | updateRoutine = CoroutineStarter.Start(UpdateRoutine()); |
| | 50 | | } |
| | 51 | |
|
| 6 | 52 | | if (!active && isActive) |
| | 53 | | { |
| 1 | 54 | | KernelConfig.i.OnChange -= OnKernelConfigChanged; |
| 1 | 55 | | StopChecking(); |
| | 56 | | } |
| | 57 | |
|
| 6 | 58 | | isActive = active; |
| 6 | 59 | | } |
| | 60 | |
|
| | 61 | | private void StopChecking() |
| | 62 | | { |
| 8 | 63 | | CoroutineStarter.Stop(updateRoutine); |
| 8 | 64 | | ShowNotification(false); |
| 8 | 65 | | } |
| | 66 | |
|
| | 67 | | internal void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous) |
| | 68 | | { |
| 6 | 69 | | sceneId = current.debugConfig.sceneLimitsWarningSceneId; |
| 6 | 70 | | if (string.IsNullOrEmpty(sceneId)) |
| | 71 | | { |
| 1 | 72 | | StopChecking(); |
| | 73 | | } |
| 6 | 74 | | } |
| | 75 | |
|
| | 76 | | // NOTE: we are doing the check inside a coroutine since scene might be unloaded and loaded again |
| | 77 | | private IEnumerator UpdateRoutine() |
| | 78 | | { |
| 0 | 79 | | while (true) |
| | 80 | | { |
| 5 | 81 | | HandleWarningNotification(); |
| 5 | 82 | | yield return WaitForSecondsCache.Get(CHECK_INTERVAL); |
| | 83 | | } |
| | 84 | | } |
| | 85 | |
|
| | 86 | | internal void HandleWarningNotification() |
| | 87 | | { |
| 11 | 88 | | bool isLimitReached = false; |
| 11 | 89 | | bool isLimitReachedAndMessageChanged = false; |
| | 90 | |
|
| 11 | 91 | | if (!string.IsNullOrEmpty(sceneId)) |
| | 92 | | { |
| 6 | 93 | | worldState.loadedScenes.TryGetValue(sceneId, out IParcelScene scene); |
| 6 | 94 | | ISceneMetricsCounter metricsController = scene?.metricsCounter; |
| 6 | 95 | | SceneMetricsModel currentMetrics = metricsController?.currentCount; |
| 6 | 96 | | SceneMetricsModel limit = metricsController?.maxCount; |
| | 97 | |
|
| 6 | 98 | | string warningMessage = null; |
| 6 | 99 | | isLimitReached = IsLimitReached(currentMetrics, limit, ref warningMessage); |
| 6 | 100 | | if (isLimitReached) |
| | 101 | | { |
| 5 | 102 | | string prevMessage = limitReachedNotification.message; |
| 5 | 103 | | string newMessage = string.Format(NOTIFICATION_MESSAGE, warningMessage); |
| 5 | 104 | | limitReachedNotification.message = newMessage; |
| 5 | 105 | | isLimitReachedAndMessageChanged = prevMessage != newMessage; |
| | 106 | | } |
| | 107 | | } |
| | 108 | |
|
| 11 | 109 | | if (isShowingNotification != isLimitReached || isLimitReachedAndMessageChanged) |
| | 110 | | { |
| 6 | 111 | | ShowNotification(isLimitReached); |
| | 112 | | } |
| 11 | 113 | | } |
| | 114 | |
|
| | 115 | | private static bool IsLimitReached(SceneMetricsModel currentMetrics, SceneMetricsModel limit, ref string message |
| | 116 | | { |
| 6 | 117 | | if (currentMetrics == null || limit == null) |
| 0 | 118 | | return false; |
| | 119 | |
|
| 6 | 120 | | if (currentMetrics.materials > limit.materials) |
| | 121 | | { |
| 0 | 122 | | message = $"Materials ({currentMetrics.materials}/{limit.materials})"; |
| 0 | 123 | | return true; |
| | 124 | | } |
| | 125 | |
|
| 6 | 126 | | if (currentMetrics.triangles > limit.triangles) |
| | 127 | | { |
| 0 | 128 | | message = $"Triangles ({currentMetrics.triangles}/{limit.triangles})"; |
| 0 | 129 | | return true; |
| | 130 | | } |
| | 131 | |
|
| 6 | 132 | | if (currentMetrics.meshes > limit.meshes) |
| | 133 | | { |
| 0 | 134 | | message = $"Meshes ({currentMetrics.meshes}/{limit.meshes})"; |
| 0 | 135 | | return true; |
| | 136 | | } |
| | 137 | |
|
| 6 | 138 | | if (currentMetrics.entities > limit.entities) |
| | 139 | | { |
| 5 | 140 | | message = $"Entities ({currentMetrics.entities}/{limit.entities})"; |
| 5 | 141 | | return true; |
| | 142 | | } |
| | 143 | |
|
| 1 | 144 | | if (currentMetrics.bodies > limit.bodies) |
| | 145 | | { |
| 0 | 146 | | message = $"Bodies ({currentMetrics.bodies}/{limit.bodies})"; |
| 0 | 147 | | return true; |
| | 148 | | } |
| | 149 | |
|
| 1 | 150 | | if (currentMetrics.sceneHeight > limit.sceneHeight) |
| | 151 | | { |
| 0 | 152 | | message = $"Height ({currentMetrics.sceneHeight}/{limit.sceneHeight})"; |
| 0 | 153 | | return true; |
| | 154 | | } |
| | 155 | |
|
| 1 | 156 | | return false; |
| | 157 | | } |
| | 158 | |
|
| | 159 | | private void ShowNotification(bool show) |
| | 160 | | { |
| 14 | 161 | | isShowingNotification = show; |
| | 162 | |
|
| 14 | 163 | | var notificationsController = NotificationsController.i; |
| | 164 | |
|
| 14 | 165 | | if (notificationsController == null) |
| 14 | 166 | | return; |
| | 167 | |
|
| 0 | 168 | | if (show) |
| | 169 | | { |
| 0 | 170 | | notificationsController.ShowNotification(limitReachedNotification); |
| 0 | 171 | | return; |
| | 172 | | } |
| | 173 | |
|
| 0 | 174 | | notificationsController.DismissAllNotifications(limitReachedNotification.groupID); |
| 0 | 175 | | } |
| | 176 | | } |
| | 177 | | } |