< Summary

Class:PluginSystem
Assembly:FeatureController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/FeatureController/PluginSystem.cs
Covered lines:36
Uncovered lines:6
Coverable lines:42
Total lines:106
Line coverage:85.7% (36 of 42)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PluginSystem()0%110100%
GetCurrentConfig()0%2100%
OnGUI()0%220100%
Update()0%220100%
LateUpdate()0%220100%
OnDestroy()0%220100%
ApplyConfig(...)0%2100%
AddBasePlugins()0%110100%
ApplyFeaturesConfig(...)0%110100%
HandleFeature[T](...)0%220100%
InitializeFeature[T]()0%3.333066.67%
RemoveFeature[T]()0%3.213071.43%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/FeatureController/PluginSystem.cs

#LineLine coverage
 1using DCL;
 2using DCL.Tutorial;
 3using System;
 4using System.Collections.Generic;
 5
 6/// <summary>
 7/// This class is used to handle a feature that needs the MonoBehaviour callbacks.
 8///
 9/// You need to add it as a feature toggle in gitlab so kernel will understand when to activate it and deactivate it.
 10/// After that, your feature manager should inherit from 'Feature' so it can start receiving MonoBehaviour callbacks whe
 11/// </summary>
 12public class PluginSystem
 13{
 12514    private readonly List<PluginFeature> activeFeatures = new List<PluginFeature>();
 15
 16    private FeatureFlag currentConfig;
 17
 018    public FeatureFlag GetCurrentConfig() { return currentConfig; }
 19
 12520    public PluginSystem()
 21    {
 12522        DataStore.i.featureFlags.flags.OnChange += ApplyConfig;
 12523        AddBasePlugins();
 12524    }
 25
 26    public void OnGUI()
 27    {
 1351228        foreach (PluginFeature feature in activeFeatures)
 29        {
 337830            feature.OnGUI();
 31        }
 337832    }
 33
 34    public void Update()
 35    {
 8501236        foreach (PluginFeature feature in activeFeatures)
 37        {
 2125338            feature.Update();
 39        }
 2125340    }
 41
 42    public void LateUpdate()
 43    {
 8500844        foreach (PluginFeature feature in activeFeatures)
 45        {
 2125246            feature.LateUpdate();
 47        }
 2125248    }
 49
 50    public void OnDestroy()
 51    {
 12252        DataStore.i.featureFlags.flags.OnChange -= ApplyConfig;
 53
 48854        foreach (PluginFeature feature in activeFeatures)
 55        {
 12256            feature.Dispose();
 57        }
 12258    }
 59
 060    public void ApplyConfig(FeatureFlag newConfig, FeatureFlag oldConfig) { ApplyFeaturesConfig(newConfig); }
 61
 25062    public void AddBasePlugins() { HandleFeature<DebugPluginFeature>(true); }
 63
 64    public void ApplyFeaturesConfig(FeatureFlag featureFlag)
 65    {
 366        HandleFeature<BuilderInWorld>(featureFlag.IsFeatureEnabled("builder_in_world"));
 367        HandleFeature<TutorialController>(featureFlag.IsFeatureEnabled("tutorial"));
 368        HandleFeature<ExploreV2Feature>(featureFlag.IsFeatureEnabled("explorev2"));
 369        currentConfig = featureFlag;
 370    }
 71
 72    private void HandleFeature<T>(bool isActive) where T : PluginFeature, new ()
 73    {
 13474        if (isActive)
 12575            InitializeFeature<T>();
 76        else
 977            RemoveFeature<T>();
 978    }
 79
 80    private void InitializeFeature<T>() where T : PluginFeature, new ()
 81    {
 25082        for (int i = 0; i < activeFeatures.Count; i++)
 83        {
 084            if (activeFeatures[i].GetType() == typeof(T))
 085                return;
 86        }
 87
 88        //NOTE: We should revise this code on the future, because we'd want to use custom constructors to DI.
 89        //      So here we most likely need to use an abstract factory, or just pass the new Feature object by argument.
 12590        PluginFeature pluginFeature = new T();
 12591        pluginFeature.Initialize();
 12592        activeFeatures.Add(pluginFeature);
 12593    }
 94
 95    private void RemoveFeature<T>() where T : PluginFeature
 96    {
 3697        for (int i = 0; i < activeFeatures.Count; i++)
 98        {
 999            if (activeFeatures[i].GetType() == typeof(T))
 100            {
 0101                activeFeatures[i].Dispose();
 0102                activeFeatures.Remove(activeFeatures[i]);
 103            }
 104        }
 9105    }
 106}