| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using DCL; |
| | 5 | | using DCL.Tutorial; |
| | 6 | | using 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> |
| | 14 | | public class PluginSystem |
| | 15 | | { |
| 123 | 16 | | private List<PluginFeature> activeFeatures = new List<PluginFeature>(); |
| | 17 | |
|
| | 18 | | private KernelConfigModel currentConfig; |
| | 19 | |
|
| 0 | 20 | | public KernelConfigModel GetCurrentConfig() { return currentConfig; } |
| | 21 | |
|
| | 22 | | public void Start() |
| | 23 | | { |
| 123 | 24 | | KernelConfig.i.EnsureConfigInitialized().Then(ApplyFeaturesConfig); |
| 123 | 25 | | KernelConfig.i.OnChange += OnKernelConfigChanged; |
| 123 | 26 | | } |
| | 27 | |
|
| | 28 | | public void OnGUI() |
| | 29 | | { |
| 7244 | 30 | | foreach (PluginFeature feature in activeFeatures) |
| | 31 | | { |
| 0 | 32 | | feature.OnGUI(); |
| | 33 | | } |
| 3622 | 34 | | } |
| | 35 | |
|
| | 36 | | public void Update() |
| | 37 | | { |
| 37532 | 38 | | foreach (PluginFeature feature in activeFeatures) |
| | 39 | | { |
| 0 | 40 | | feature.Update(); |
| | 41 | | } |
| 18766 | 42 | | } |
| | 43 | |
|
| | 44 | | public void LateUpdate() |
| | 45 | | { |
| 37530 | 46 | | foreach (PluginFeature feature in activeFeatures) |
| | 47 | | { |
| 0 | 48 | | feature.LateUpdate(); |
| | 49 | | } |
| 18765 | 50 | | } |
| | 51 | |
|
| | 52 | | public void OnDestroy() |
| | 53 | | { |
| 244 | 54 | | foreach (PluginFeature feature in activeFeatures) |
| | 55 | | { |
| 0 | 56 | | feature.Dispose(); |
| | 57 | | } |
| 122 | 58 | | } |
| | 59 | |
|
| 592 | 60 | | public void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous) { ApplyFeaturesConfig(curre |
| | 61 | |
|
| | 62 | | public void ApplyFeaturesConfig(KernelConfigModel config) |
| | 63 | | { |
| 421 | 64 | | HandleFeature<BIWMainController>(config.features.enableBuilderInWorld); |
| 421 | 65 | | HandleFeature<TutorialController>(config.features.enableTutorial); |
| 421 | 66 | | currentConfig = config; |
| 421 | 67 | | } |
| | 68 | |
|
| | 69 | | private void HandleFeature<T>(bool isActive) where T : PluginFeature, new () |
| | 70 | | { |
| 842 | 71 | | if (isActive) |
| 177 | 72 | | InitializeFeature<T>(); |
| | 73 | | else |
| 665 | 74 | | RemoveFeature<T>(); |
| 665 | 75 | | } |
| | 76 | |
|
| | 77 | | private void InitializeFeature<T>() where T : PluginFeature, new () |
| | 78 | | { |
| 354 | 79 | | for (int i = 0; i < activeFeatures.Count; i++) |
| | 80 | | { |
| 59 | 81 | | if (activeFeatures[i].GetType() == typeof(T)) |
| 59 | 82 | | 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. |
| 118 | 87 | | PluginFeature pluginFeature = new T(); |
| 118 | 88 | | pluginFeature.Initialize(); |
| 118 | 89 | | activeFeatures.Add(pluginFeature); |
| 118 | 90 | | } |
| | 91 | |
|
| | 92 | | private void RemoveFeature<T>() where T : PluginFeature |
| | 93 | | { |
| 1920 | 94 | | for (int i = 0; i < activeFeatures.Count; i++) |
| | 95 | | { |
| 295 | 96 | | if (activeFeatures[i].GetType() == typeof(T)) |
| | 97 | | { |
| 118 | 98 | | activeFeatures[i].Dispose(); |
| 118 | 99 | | activeFeatures.Remove(activeFeatures[i]); |
| | 100 | | } |
| | 101 | | } |
| 665 | 102 | | } |
| | 103 | |
|
| | 104 | | } |