< Summary

Class:DCL.SettingsCommon.SettingsControllers.AutoQualitySettingsComponent
Assembly:SettingsControllers
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/Settings/SettingsControllers/AutoQualitySettingsComponent.cs
Covered lines:24
Uncovered lines:17
Coverable lines:41
Total lines:101
Line coverage:58.5% (24 of 41)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Start()0%440100%
OnDestroy()0%220100%
OnQualitySettingsChanged(...)0%3.033085.71%
SetAutoSettings(...)0%4.123050%
AutoSettingsLoop()0%30500%
UpdateQuality(...)0%20400%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/Settings/SettingsControllers/AutoQualitySettingsComponent.cs

#LineLine coverage
 1using System.Collections;
 2using DCL.Helpers;
 3using UnityEngine;
 4
 5namespace DCL.SettingsCommon.SettingsControllers
 6{
 7    /// <summary>
 8    /// Controller to change the quality settings automatically based on performance
 9    /// </summary>
 10    public class AutoQualitySettingsComponent : MonoBehaviour
 11    {
 12        private const float LOOP_TIME_SECONDS = 1f;
 13        private const string LAST_QUALITY_INDEX = "LAST_QUALITY_INDEX";
 14        private const int TARGET_FPS = 30;
 15
 16        [SerializeField] internal BooleanVariable autoQualityEnabled;
 17        [SerializeField] internal PerformanceMetricsDataVariable performanceMetricsData;
 018        internal QualitySettingsData qualitySettings => Settings.i.autoqualitySettings;
 19
 20        internal int currentQualityIndex;
 21        private Coroutine settingsCheckerCoroutine;
 22        internal IAutoQualityController controller;
 23        internal bool fpsCapped;
 24
 25        void Start()
 26        {
 12627            if (autoQualityEnabled == null || qualitySettings == null || qualitySettings.Length == 0)
 228                return;
 29
 12430            currentQualityIndex = PlayerPrefsUtils.GetInt(LAST_QUALITY_INDEX, (qualitySettings.Length - 1) / 2);
 31
 12432            controller = new AutoQualityUncappedFPSController(currentQualityIndex, qualitySettings);
 33
 12434            Settings.i.qualitySettings.OnChanged += OnQualitySettingsChanged;
 12435            autoQualityEnabled.OnChange += SetAutoSettings;
 36
 12437            fpsCapped = !Settings.i.qualitySettings.Data.fpsCap;
 12438            OnQualitySettingsChanged(Settings.i.qualitySettings.Data);
 12439            SetAutoSettings(autoQualityEnabled.Get(), !autoQualityEnabled.Get());
 12440        }
 41
 42        private void OnDestroy()
 43        {
 12544            if (autoQualityEnabled != null)
 12345                autoQualityEnabled.OnChange -= SetAutoSettings;
 12546            Settings.i.qualitySettings.OnChanged -= OnQualitySettingsChanged;
 12547        }
 48
 49        internal void OnQualitySettingsChanged(QualitySettings settings)
 50        {
 12851            if (fpsCapped == settings.fpsCap)
 052                return;
 53
 12854            fpsCapped = settings.fpsCap;
 12855            if (settings.fpsCap)
 7856                controller = new AutoQualityCappedFPSController(TARGET_FPS, currentQualityIndex, qualitySettings);
 57            else
 5058                controller = new AutoQualityUncappedFPSController(currentQualityIndex, qualitySettings);
 5059        }
 60
 61        private void SetAutoSettings(bool newValue, bool oldValue)
 62        {
 12463            if (settingsCheckerCoroutine != null)
 64            {
 065                StopCoroutine(settingsCheckerCoroutine);
 066                settingsCheckerCoroutine = null;
 67            }
 68
 12469            if (newValue)
 70            {
 071                settingsCheckerCoroutine = StartCoroutine(AutoSettingsLoop());
 072            }
 73            else
 74            {
 12475                controller.ResetEvaluation();
 76            }
 12477        }
 78
 79        private IEnumerator AutoSettingsLoop()
 80        {
 081            while (true)
 82            {
 083                UpdateQuality(controller.EvaluateQuality(performanceMetricsData?.Get()));
 084                yield return new WaitForSeconds(LOOP_TIME_SECONDS);
 85            }
 86        }
 87
 88        private void UpdateQuality(int newQualityIndex)
 89        {
 090            if (newQualityIndex == currentQualityIndex)
 091                return;
 92
 093            if (newQualityIndex <= 0 || newQualityIndex >= qualitySettings.Length)
 094                return;
 95
 096            PlayerPrefsUtils.SetInt(LAST_QUALITY_INDEX, currentQualityIndex);
 097            currentQualityIndex = newQualityIndex;
 098            Settings.i.ApplyAutoQualitySettings(currentQualityIndex);
 099        }
 100    }
 101}