| | 1 | | using System; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.Interface; |
| | 4 | | using DCL.Models; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.Assertions; |
| | 9 | |
|
| | 10 | | namespace DCL |
| | 11 | | { |
| | 12 | | public sealed class SceneMetricsCounter : ISceneMetricsCounter |
| | 13 | | { |
| | 14 | | public static class LimitsConfig |
| | 15 | | { |
| | 16 | | // number of entities |
| | 17 | | public const int entities = 200; |
| | 18 | |
|
| | 19 | | // Number of faces (per parcel) |
| | 20 | | public const int triangles = 10000; |
| | 21 | | public const int bodies = 300; |
| | 22 | | public const int textures = 10; |
| | 23 | | public const int materials = 20; |
| | 24 | | public const int meshes = 200; |
| | 25 | |
|
| | 26 | | public const float height = 20; |
| | 27 | | public const float visibleRadius = 10; |
| | 28 | | } |
| | 29 | |
|
| 1 | 30 | | private static bool VERBOSE = false; |
| 1 | 31 | | private static Logger logger = new Logger("SceneMetricsCounter") { verboseEnabled = VERBOSE }; |
| | 32 | | public event Action<ISceneMetricsCounter> OnMetricsUpdated; |
| | 33 | |
|
| 509 | 34 | | private SceneMetricsModel maxCountValue = new SceneMetricsModel(); |
| 509 | 35 | | private SceneMetricsModel currentCountValue = new SceneMetricsModel(); |
| | 36 | |
|
| | 37 | | public SceneMetricsModel currentCount |
| | 38 | | { |
| | 39 | | get |
| | 40 | | { |
| 82 | 41 | | UpdateMetrics(); |
| 82 | 42 | | return currentCountValue.Clone(); |
| | 43 | | } |
| | 44 | | } |
| | 45 | |
|
| 723 | 46 | | public SceneMetricsModel maxCount => maxCountValue.Clone(); |
| | 47 | |
|
| 0 | 48 | | public bool dirty { get; private set; } |
| | 49 | |
|
| | 50 | | private string sceneId; |
| | 51 | |
|
| | 52 | | private Vector2Int scenePosition; |
| | 53 | |
|
| | 54 | | private int sceneParcelCount; |
| | 55 | |
|
| | 56 | | private DataStore_WorldObjects data; |
| | 57 | | private bool enabled = false; |
| | 58 | |
|
| 6 | 59 | | public SceneMetricsCounter(DataStore_WorldObjects dataStore, string sceneId, Vector2Int scenePosition, int scene |
| | 60 | | { |
| 6 | 61 | | this.data = dataStore; |
| 6 | 62 | | Configure(sceneId, scenePosition, sceneParcelCount); |
| 6 | 63 | | } |
| | 64 | |
|
| 503 | 65 | | public SceneMetricsCounter(DataStore_WorldObjects dataStore) |
| | 66 | | { |
| 503 | 67 | | this.data = dataStore; |
| 503 | 68 | | } |
| | 69 | |
|
| | 70 | | public void Configure(string sceneId, Vector2Int scenePosition, int sceneParcelCount) |
| | 71 | | { |
| 506 | 72 | | this.sceneId = sceneId; |
| 506 | 73 | | this.scenePosition = scenePosition; |
| 506 | 74 | | this.sceneParcelCount = sceneParcelCount; |
| | 75 | |
|
| 506 | 76 | | Assert.IsTrue( !string.IsNullOrEmpty(sceneId), "Scene must have an ID!" ); |
| 506 | 77 | | maxCountValue = ComputeMaxCount(); |
| 506 | 78 | | } |
| | 79 | |
|
| | 80 | | public void Dispose() |
| | 81 | | { |
| 503 | 82 | | } |
| | 83 | |
|
| | 84 | |
|
| | 85 | | public void Enable() |
| | 86 | | { |
| 506 | 87 | | if ( enabled ) |
| 0 | 88 | | return; |
| | 89 | |
|
| 506 | 90 | | var sceneData = data.sceneData[sceneId]; |
| | 91 | |
|
| 506 | 92 | | sceneData.materials.OnAdded += OnDataChanged; |
| 506 | 93 | | sceneData.materials.OnRemoved += OnDataChanged; |
| | 94 | |
|
| 506 | 95 | | sceneData.textures.OnAdded += OnDataChanged; |
| 506 | 96 | | sceneData.textures.OnRemoved += OnDataChanged; |
| | 97 | |
|
| 506 | 98 | | sceneData.meshes.OnAdded += OnDataChanged; |
| 506 | 99 | | sceneData.meshes.OnRemoved += OnDataChanged; |
| | 100 | |
|
| 506 | 101 | | sceneData.renderers.OnAdded += OnDataChanged; |
| 506 | 102 | | sceneData.renderers.OnRemoved += OnDataChanged; |
| | 103 | |
|
| 506 | 104 | | sceneData.owners.OnAdded += OnDataChanged; |
| 506 | 105 | | sceneData.owners.OnRemoved += OnDataChanged; |
| | 106 | |
|
| 506 | 107 | | sceneData.triangles.OnChange += OnDataChanged; |
| | 108 | |
|
| 506 | 109 | | enabled = true; |
| 506 | 110 | | } |
| | 111 | |
|
| | 112 | | public void Disable() |
| | 113 | | { |
| 503 | 114 | | if ( !enabled ) |
| 3 | 115 | | return; |
| | 116 | |
|
| 500 | 117 | | var sceneData = data.sceneData[sceneId]; |
| | 118 | |
|
| 500 | 119 | | sceneData.materials.OnAdded -= OnDataChanged; |
| 500 | 120 | | sceneData.materials.OnRemoved -= OnDataChanged; |
| | 121 | |
|
| 500 | 122 | | sceneData.textures.OnAdded -= OnDataChanged; |
| 500 | 123 | | sceneData.textures.OnRemoved -= OnDataChanged; |
| | 124 | |
|
| 500 | 125 | | sceneData.meshes.OnAdded -= OnDataChanged; |
| 500 | 126 | | sceneData.meshes.OnRemoved -= OnDataChanged; |
| | 127 | |
|
| 500 | 128 | | sceneData.renderers.OnAdded -= OnDataChanged; |
| 500 | 129 | | sceneData.renderers.OnRemoved -= OnDataChanged; |
| | 130 | |
|
| 500 | 131 | | sceneData.owners.OnAdded -= OnDataChanged; |
| 500 | 132 | | sceneData.owners.OnRemoved -= OnDataChanged; |
| | 133 | |
|
| 500 | 134 | | sceneData.triangles.OnChange -= OnDataChanged; |
| | 135 | |
|
| 500 | 136 | | enabled = false; |
| 500 | 137 | | } |
| | 138 | |
|
| | 139 | | private SceneMetricsModel ComputeMaxCount() |
| | 140 | | { |
| 511 | 141 | | var result = new SceneMetricsModel(); |
| | 142 | |
|
| 511 | 143 | | float log = Mathf.Log(sceneParcelCount + 1, 2); |
| 511 | 144 | | float lineal = sceneParcelCount; |
| | 145 | |
|
| 511 | 146 | | result.triangles = (int) (lineal * LimitsConfig.triangles); |
| 511 | 147 | | result.bodies = (int) (lineal * LimitsConfig.bodies); |
| 511 | 148 | | result.entities = (int) (lineal * LimitsConfig.entities); |
| 511 | 149 | | result.materials = (int) (log * LimitsConfig.materials); |
| 511 | 150 | | result.textures = (int) (log * LimitsConfig.textures); |
| 511 | 151 | | result.meshes = (int) (log * LimitsConfig.meshes); |
| 511 | 152 | | result.sceneHeight = (int) (log * LimitsConfig.height); |
| | 153 | |
|
| 511 | 154 | | return result; |
| | 155 | | } |
| | 156 | |
|
| | 157 | | public bool IsInsideTheLimits() |
| | 158 | | { |
| 5 | 159 | | UpdateMetrics(); |
| 5 | 160 | | SceneMetricsModel limits = ComputeMaxCount(); |
| 5 | 161 | | SceneMetricsModel usage = currentCountValue; |
| | 162 | |
|
| 5 | 163 | | if (usage.triangles > limits.triangles) |
| 0 | 164 | | return false; |
| | 165 | |
|
| 5 | 166 | | if (usage.bodies > limits.bodies) |
| 0 | 167 | | return false; |
| | 168 | |
|
| 5 | 169 | | if (usage.entities > limits.entities) |
| 1 | 170 | | return false; |
| | 171 | |
|
| 4 | 172 | | if (usage.materials > limits.materials) |
| 0 | 173 | | return false; |
| | 174 | |
|
| 4 | 175 | | if (usage.textures > limits.textures) |
| 0 | 176 | | return false; |
| | 177 | |
|
| 4 | 178 | | if (usage.meshes > limits.meshes) |
| 0 | 179 | | return false; |
| | 180 | |
|
| 4 | 181 | | return true; |
| | 182 | | } |
| | 183 | |
|
| | 184 | | private void MarkDirty() |
| | 185 | | { |
| 0 | 186 | | dirty = true; |
| 0 | 187 | | } |
| | 188 | |
|
| | 189 | | public void SendEvent() |
| | 190 | | { |
| 669 | 191 | | if (!dirty) |
| 490 | 192 | | return; |
| | 193 | |
|
| 179 | 194 | | dirty = false; |
| | 195 | |
|
| 179 | 196 | | UpdateMetrics(); |
| | 197 | |
|
| 179 | 198 | | Interface.WebInterface.ReportOnMetricsUpdate(sceneId, currentCountValue.ToMetricsModel(), maxCount.ToMetrics |
| 179 | 199 | | } |
| | 200 | |
|
| | 201 | | void OnDataChanged<T>(T obj) |
| | 202 | | where T : class |
| | 203 | | { |
| 2686 | 204 | | MarkDirty(); |
| 2686 | 205 | | } |
| | 206 | |
|
| | 207 | | void OnDataChanged(int obj1, int obj2) |
| | 208 | | { |
| 449 | 209 | | MarkDirty(); |
| 449 | 210 | | } |
| | 211 | |
|
| | 212 | | private void UpdateMetrics() |
| | 213 | | { |
| 266 | 214 | | if (string.IsNullOrEmpty(sceneId)) |
| 0 | 215 | | return; |
| | 216 | |
|
| 266 | 217 | | var sceneData = data?.sceneData[sceneId]; |
| | 218 | |
|
| 266 | 219 | | if ( sceneData != null ) |
| | 220 | | { |
| 266 | 221 | | currentCountValue.materials = sceneData.materials.Count(); |
| 266 | 222 | | currentCountValue.textures = sceneData.textures.Count(); |
| 266 | 223 | | currentCountValue.meshes = sceneData.meshes.Count(); |
| 266 | 224 | | currentCountValue.entities = sceneData.owners.Count(); |
| 266 | 225 | | currentCountValue.bodies = sceneData.renderers.Count(); |
| 266 | 226 | | currentCountValue.triangles = sceneData.triangles.Get() / 3; |
| | 227 | | } |
| | 228 | |
|
| 266 | 229 | | logger.Verbose($"Current metrics: {currentCountValue}"); |
| 266 | 230 | | RaiseMetricsUpdate(); |
| 266 | 231 | | } |
| | 232 | |
|
| | 233 | | private void UpdateWorstMetricsOffense() |
| | 234 | | { |
| 266 | 235 | | DataStore_SceneMetrics metricsData = DataStore.i.Get<DataStore_SceneMetrics>(); |
| | 236 | |
|
| 266 | 237 | | if ( maxCountValue != null && metricsData.worstMetricOffenseComputeEnabled.Get() ) |
| | 238 | | { |
| 266 | 239 | | bool isOffending = currentCountValue > maxCountValue; |
| | 240 | |
|
| 266 | 241 | | if ( !isOffending ) |
| 265 | 242 | | return; |
| | 243 | |
|
| 1 | 244 | | bool firstOffense = false; |
| | 245 | |
|
| 1 | 246 | | if (!metricsData.worstMetricOffenses.ContainsKey(sceneId)) |
| | 247 | | { |
| 1 | 248 | | firstOffense = true; |
| 1 | 249 | | metricsData.worstMetricOffenses[sceneId] = currentCountValue.Clone(); |
| | 250 | | } |
| | 251 | |
|
| 1 | 252 | | SceneMetricsModel worstOffense = metricsData.worstMetricOffenses[sceneId]; |
| 1 | 253 | | SceneMetricsModel currentOffense = maxCountValue - currentCountValue; |
| | 254 | |
|
| 1 | 255 | | if ( firstOffense ) |
| 1 | 256 | | logger.Verbose($"New offending scene {sceneId} ({scenePosition})!\n{currentCountValue}"); |
| | 257 | |
|
| 1 | 258 | | if ( currentOffense < worstOffense ) |
| 1 | 259 | | return; |
| | 260 | |
|
| 0 | 261 | | metricsData.worstMetricOffenses[sceneId] = currentOffense; |
| 0 | 262 | | logger.Verbose($"New offending scene {sceneId} {scenePosition}!\nmetrics: {currentCountValue}\nlimits: { |
| | 263 | | } |
| 0 | 264 | | } |
| | 265 | |
|
| | 266 | | private void RaiseMetricsUpdate() |
| | 267 | | { |
| 266 | 268 | | UpdateWorstMetricsOffense(); |
| 266 | 269 | | OnMetricsUpdated?.Invoke(this); |
| 0 | 270 | | } |
| | 271 | | } |
| | 272 | | } |