| | 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 | |
|
| 0 | 25 | | private static readonly Model limitReachedNotification = new Model() |
| | 26 | | { |
| | 27 | | type = Type.WARNING, |
| | 28 | | groupID = NOTIFICATION_GROUP, |
| | 29 | | message = NOTIFICATION_MESSAGE |
| | 30 | | }; |
| | 31 | |
|
| 0 | 32 | | public PreviewSceneLimitsWarning(IWorldState worldState) |
| | 33 | | { |
| 0 | 34 | | this.worldState = worldState; |
| 0 | 35 | | } |
| | 36 | |
|
| | 37 | | public void Dispose() |
| | 38 | | { |
| 0 | 39 | | KernelConfig.i.OnChange -= OnKernelConfigChanged; |
| 0 | 40 | | StopChecking(); |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | public void SetActive(bool active) |
| | 44 | | { |
| 0 | 45 | | if (active && !isActive) |
| | 46 | | { |
| 0 | 47 | | sceneId = KernelConfig.i.Get().debugConfig.sceneLimitsWarningSceneId; |
| 0 | 48 | | KernelConfig.i.OnChange += OnKernelConfigChanged; |
| 0 | 49 | | updateRoutine = CoroutineStarter.Start(UpdateRoutine()); |
| | 50 | | } |
| | 51 | |
|
| 0 | 52 | | if (!active && isActive) |
| | 53 | | { |
| 0 | 54 | | KernelConfig.i.OnChange -= OnKernelConfigChanged; |
| 0 | 55 | | StopChecking(); |
| | 56 | | } |
| | 57 | |
|
| 0 | 58 | | isActive = active; |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | private void StopChecking() |
| | 62 | | { |
| 0 | 63 | | CoroutineStarter.Stop(updateRoutine); |
| 0 | 64 | | ShowNotification(false); |
| 0 | 65 | | } |
| | 66 | |
|
| | 67 | | internal void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous) |
| | 68 | | { |
| 0 | 69 | | sceneId = current.debugConfig.sceneLimitsWarningSceneId; |
| 0 | 70 | | if (string.IsNullOrEmpty(sceneId)) |
| | 71 | | { |
| 0 | 72 | | StopChecking(); |
| | 73 | | } |
| 0 | 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 | | { |
| 0 | 81 | | HandleWarningNotification(); |
| 0 | 82 | | yield return WaitForSecondsCache.Get(CHECK_INTERVAL); |
| | 83 | | } |
| | 84 | | } |
| | 85 | |
|
| | 86 | | internal void HandleWarningNotification() |
| | 87 | | { |
| 0 | 88 | | bool isLimitReached = false; |
| 0 | 89 | | bool isLimitReachedAndMessageChanged = false; |
| | 90 | |
|
| 0 | 91 | | if (!string.IsNullOrEmpty(sceneId)) |
| | 92 | | { |
| 0 | 93 | | worldState.TryGetScene(sceneId, out IParcelScene scene); |
| 0 | 94 | | ISceneMetricsCounter metricsController = scene?.metricsCounter; |
| 0 | 95 | | SceneMetricsModel currentMetrics = metricsController?.currentCount; |
| 0 | 96 | | SceneMetricsModel limit = metricsController?.maxCount; |
| | 97 | |
|
| 0 | 98 | | string warningMessage = null; |
| 0 | 99 | | isLimitReached = IsLimitReached(currentMetrics, limit, ref warningMessage); |
| 0 | 100 | | if (isLimitReached) |
| | 101 | | { |
| 0 | 102 | | string prevMessage = limitReachedNotification.message; |
| 0 | 103 | | string newMessage = string.Format(NOTIFICATION_MESSAGE, warningMessage); |
| 0 | 104 | | limitReachedNotification.message = newMessage; |
| 0 | 105 | | isLimitReachedAndMessageChanged = prevMessage != newMessage; |
| | 106 | | } |
| | 107 | | } |
| | 108 | |
|
| 0 | 109 | | if (isShowingNotification != isLimitReached || isLimitReachedAndMessageChanged) |
| | 110 | | { |
| 0 | 111 | | ShowNotification(isLimitReached); |
| | 112 | | } |
| 0 | 113 | | } |
| | 114 | |
|
| | 115 | | private static bool IsLimitReached(SceneMetricsModel currentMetrics, SceneMetricsModel limit, |
| | 116 | | ref string message) |
| | 117 | | { |
| 0 | 118 | | if (currentMetrics == null || limit == null) |
| 0 | 119 | | return false; |
| | 120 | |
|
| 0 | 121 | | if (currentMetrics.materials > limit.materials) |
| | 122 | | { |
| 0 | 123 | | message = $"Materials ({currentMetrics.materials}/{limit.materials})"; |
| 0 | 124 | | return true; |
| | 125 | | } |
| | 126 | |
|
| 0 | 127 | | if (currentMetrics.triangles > limit.triangles) |
| | 128 | | { |
| 0 | 129 | | message = $"Triangles ({currentMetrics.triangles}/{limit.triangles})"; |
| 0 | 130 | | return true; |
| | 131 | | } |
| | 132 | |
|
| 0 | 133 | | if (currentMetrics.meshes > limit.meshes) |
| | 134 | | { |
| 0 | 135 | | message = $"Meshes ({currentMetrics.meshes}/{limit.meshes})"; |
| 0 | 136 | | return true; |
| | 137 | | } |
| | 138 | |
|
| 0 | 139 | | if (currentMetrics.entities > limit.entities) |
| | 140 | | { |
| 0 | 141 | | message = $"Entities ({currentMetrics.entities}/{limit.entities})"; |
| 0 | 142 | | return true; |
| | 143 | | } |
| | 144 | |
|
| 0 | 145 | | if (currentMetrics.bodies > limit.bodies) |
| | 146 | | { |
| 0 | 147 | | message = $"Bodies ({currentMetrics.bodies}/{limit.bodies})"; |
| 0 | 148 | | return true; |
| | 149 | | } |
| | 150 | |
|
| 0 | 151 | | if (currentMetrics.sceneHeight > limit.sceneHeight) |
| | 152 | | { |
| 0 | 153 | | message = $"Height ({currentMetrics.sceneHeight}/{limit.sceneHeight})"; |
| 0 | 154 | | return true; |
| | 155 | | } |
| | 156 | |
|
| 0 | 157 | | return false; |
| | 158 | | } |
| | 159 | |
|
| | 160 | | private void ShowNotification(bool show) |
| | 161 | | { |
| 0 | 162 | | isShowingNotification = show; |
| | 163 | |
|
| 0 | 164 | | var notificationsController = NotificationsController.i; |
| | 165 | |
|
| 0 | 166 | | if (notificationsController == null) |
| 0 | 167 | | return; |
| | 168 | |
|
| 0 | 169 | | if (show) |
| | 170 | | { |
| 0 | 171 | | notificationsController.ShowNotification(limitReachedNotification); |
| 0 | 172 | | return; |
| | 173 | | } |
| | 174 | |
|
| 0 | 175 | | notificationsController.DismissAllNotifications(limitReachedNotification.groupID); |
| 0 | 176 | | } |
| | 177 | | } |
| | 178 | | } |