| | 1 | | using DCL; |
| | 2 | | using DCL.Tutorial; |
| | 3 | | using System; |
| | 4 | | using 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> |
| | 12 | | public class PluginSystem |
| | 13 | | { |
| 125 | 14 | | private readonly List<PluginFeature> activeFeatures = new List<PluginFeature>(); |
| | 15 | |
|
| | 16 | | private FeatureFlag currentConfig; |
| | 17 | |
|
| 0 | 18 | | public FeatureFlag GetCurrentConfig() { return currentConfig; } |
| | 19 | |
|
| 125 | 20 | | public PluginSystem() |
| | 21 | | { |
| 125 | 22 | | DataStore.i.featureFlags.flags.OnChange += ApplyConfig; |
| 125 | 23 | | AddBasePlugins(); |
| 125 | 24 | | } |
| | 25 | |
|
| | 26 | | public void OnGUI() |
| | 27 | | { |
| 13512 | 28 | | foreach (PluginFeature feature in activeFeatures) |
| | 29 | | { |
| 3378 | 30 | | feature.OnGUI(); |
| | 31 | | } |
| 3378 | 32 | | } |
| | 33 | |
|
| | 34 | | public void Update() |
| | 35 | | { |
| 85012 | 36 | | foreach (PluginFeature feature in activeFeatures) |
| | 37 | | { |
| 21253 | 38 | | feature.Update(); |
| | 39 | | } |
| 21253 | 40 | | } |
| | 41 | |
|
| | 42 | | public void LateUpdate() |
| | 43 | | { |
| 85008 | 44 | | foreach (PluginFeature feature in activeFeatures) |
| | 45 | | { |
| 21252 | 46 | | feature.LateUpdate(); |
| | 47 | | } |
| 21252 | 48 | | } |
| | 49 | |
|
| | 50 | | public void OnDestroy() |
| | 51 | | { |
| 122 | 52 | | DataStore.i.featureFlags.flags.OnChange -= ApplyConfig; |
| | 53 | |
|
| 488 | 54 | | foreach (PluginFeature feature in activeFeatures) |
| | 55 | | { |
| 122 | 56 | | feature.Dispose(); |
| | 57 | | } |
| 122 | 58 | | } |
| | 59 | |
|
| 0 | 60 | | public void ApplyConfig(FeatureFlag newConfig, FeatureFlag oldConfig) { ApplyFeaturesConfig(newConfig); } |
| | 61 | |
|
| 250 | 62 | | public void AddBasePlugins() { HandleFeature<DebugPluginFeature>(true); } |
| | 63 | |
|
| | 64 | | public void ApplyFeaturesConfig(FeatureFlag featureFlag) |
| | 65 | | { |
| 3 | 66 | | HandleFeature<BuilderInWorld>(featureFlag.IsFeatureEnabled("builder_in_world")); |
| 3 | 67 | | HandleFeature<TutorialController>(featureFlag.IsFeatureEnabled("tutorial")); |
| 3 | 68 | | HandleFeature<ExploreV2Feature>(featureFlag.IsFeatureEnabled("explorev2")); |
| 3 | 69 | | currentConfig = featureFlag; |
| 3 | 70 | | } |
| | 71 | |
|
| | 72 | | private void HandleFeature<T>(bool isActive) where T : PluginFeature, new () |
| | 73 | | { |
| 134 | 74 | | if (isActive) |
| 125 | 75 | | InitializeFeature<T>(); |
| | 76 | | else |
| 9 | 77 | | RemoveFeature<T>(); |
| 9 | 78 | | } |
| | 79 | |
|
| | 80 | | private void InitializeFeature<T>() where T : PluginFeature, new () |
| | 81 | | { |
| 250 | 82 | | for (int i = 0; i < activeFeatures.Count; i++) |
| | 83 | | { |
| 0 | 84 | | if (activeFeatures[i].GetType() == typeof(T)) |
| 0 | 85 | | 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. |
| 125 | 90 | | PluginFeature pluginFeature = new T(); |
| 125 | 91 | | pluginFeature.Initialize(); |
| 125 | 92 | | activeFeatures.Add(pluginFeature); |
| 125 | 93 | | } |
| | 94 | |
|
| | 95 | | private void RemoveFeature<T>() where T : PluginFeature |
| | 96 | | { |
| 36 | 97 | | for (int i = 0; i < activeFeatures.Count; i++) |
| | 98 | | { |
| 9 | 99 | | if (activeFeatures[i].GetType() == typeof(T)) |
| | 100 | | { |
| 0 | 101 | | activeFeatures[i].Dispose(); |
| 0 | 102 | | activeFeatures.Remove(activeFeatures[i]); |
| | 103 | | } |
| | 104 | | } |
| 9 | 105 | | } |
| | 106 | | } |