< Summary

Class:PluginSystem
Assembly:FeatureController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/FeatureController/PluginSystem.cs
Covered lines:33
Uncovered lines:5
Coverable lines:38
Total lines:104
Line coverage:86.8% (33 of 38)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PluginSystem()0%110100%
GetCurrentConfig()0%2100%
Start()0%110100%
OnGUI()0%2.262060%
Update()0%2.262060%
LateUpdate()0%2.262060%
OnDestroy()0%2.262060%
OnKernelConfigChanged(...)0%110100%
ApplyFeaturesConfig(...)0%110100%
HandleFeature[T](...)0%220100%
InitializeFeature[T]()0%3.013088.89%
RemoveFeature[T]()0%330100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using DCL;
 5using DCL.Tutorial;
 6using UnityEngine;
 7
 8/// <summary>
 9/// This class is used to handle a feature that needs the MonoBehaviour callbacks.
 10///
 11/// You need to add it as a feature toggle in gitlab so kernel will understand when to activate it and deactivate it.
 12/// After that, your feature manager should inherit from 'Feature' so it can start receiving MonoBehaviour callbacks whe
 13/// </summary>
 14public class PluginSystem
 15{
 12316    private List<PluginFeature> activeFeatures = new List<PluginFeature>();
 17
 18    private KernelConfigModel currentConfig;
 19
 020    public KernelConfigModel GetCurrentConfig() { return currentConfig; }
 21
 22    public void Start()
 23    {
 12324        KernelConfig.i.EnsureConfigInitialized().Then(ApplyFeaturesConfig);
 12325        KernelConfig.i.OnChange += OnKernelConfigChanged;
 12326    }
 27
 28    public void OnGUI()
 29    {
 724430        foreach (PluginFeature feature in activeFeatures)
 31        {
 032            feature.OnGUI();
 33        }
 362234    }
 35
 36    public void Update()
 37    {
 3753238        foreach (PluginFeature feature in activeFeatures)
 39        {
 040            feature.Update();
 41        }
 1876642    }
 43
 44    public void LateUpdate()
 45    {
 3753046        foreach (PluginFeature feature in activeFeatures)
 47        {
 048            feature.LateUpdate();
 49        }
 1876550    }
 51
 52    public void OnDestroy()
 53    {
 24454        foreach (PluginFeature feature in activeFeatures)
 55        {
 056            feature.Dispose();
 57        }
 12258    }
 59
 59260    public void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous) { ApplyFeaturesConfig(curre
 61
 62    public void ApplyFeaturesConfig(KernelConfigModel config)
 63    {
 42164        HandleFeature<BIWMainController>(config.features.enableBuilderInWorld);
 42165        HandleFeature<TutorialController>(config.features.enableTutorial);
 42166        currentConfig = config;
 42167    }
 68
 69    private void HandleFeature<T>(bool isActive) where T : PluginFeature, new ()
 70    {
 84271        if (isActive)
 17772            InitializeFeature<T>();
 73        else
 66574            RemoveFeature<T>();
 66575    }
 76
 77    private void InitializeFeature<T>() where T : PluginFeature, new ()
 78    {
 35479        for (int i = 0; i < activeFeatures.Count; i++)
 80        {
 5981            if (activeFeatures[i].GetType() == typeof(T))
 5982                return;
 83        }
 84
 85        //NOTE: We should revise this code on the future, because we'd want to use custom constructors to DI.
 86        //      So here we most likely need to use an abstract factory, or just pass the new Feature object by argument.
 11887        PluginFeature pluginFeature = new T();
 11888        pluginFeature.Initialize();
 11889        activeFeatures.Add(pluginFeature);
 11890    }
 91
 92    private void RemoveFeature<T>() where T : PluginFeature
 93    {
 192094        for (int i = 0; i < activeFeatures.Count; i++)
 95        {
 29596            if (activeFeatures[i].GetType() == typeof(T))
 97            {
 11898                activeFeatures[i].Dispose();
 11899                activeFeatures.Remove(activeFeatures[i]);
 100            }
 101        }
 665102    }
 103
 104}