| | 1 | | using System.Collections.Generic; |
| | 2 | | using UnityEngine; |
| | 3 | | using DCL.Helpers; |
| | 4 | |
|
| | 5 | | public class KernelConfig |
| | 6 | | { |
| | 7 | | public delegate void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous); |
| | 8 | |
|
| | 9 | | public event OnKernelConfigChanged OnChange; |
| | 10 | |
|
| | 11 | | public static KernelConfig i |
| | 12 | | { |
| | 13 | | get |
| | 14 | | { |
| 1404 | 15 | | if (config == null) |
| | 16 | | { |
| 1 | 17 | | config = new KernelConfig(); |
| | 18 | | } |
| 1404 | 19 | | return config; |
| | 20 | | } |
| | 21 | | } |
| | 22 | | static KernelConfig config = null; |
| | 23 | |
|
| 1 | 24 | | KernelConfigModel value = new KernelConfigModel(); |
| | 25 | | List<Promise<KernelConfigModel>> initializationPromises; |
| | 26 | |
|
| | 27 | | internal bool initialized = false; |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Set a new Kernel Configuration |
| | 31 | | /// </summary> |
| | 32 | | /// <param name="newValue">New Configuration</param> |
| | 33 | | public void Set(KernelConfigModel newValue) |
| | 34 | | { |
| 6 | 35 | | if (newValue == null) |
| | 36 | | { |
| 0 | 37 | | return; |
| | 38 | | } |
| | 39 | |
|
| 6 | 40 | | if (!initialized) |
| | 41 | | { |
| 2 | 42 | | Initialize(newValue); |
| | 43 | | } |
| | 44 | |
|
| 6 | 45 | | if (newValue.Equals(value)) |
| 1 | 46 | | return; |
| | 47 | |
|
| 5 | 48 | | var previous = value; |
| 5 | 49 | | value = newValue; |
| 5 | 50 | | OnChange?.Invoke(value, previous); |
| 5 | 51 | | } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Get Kernel Configuration |
| | 55 | | /// </summary> |
| | 56 | | /// <returns>Kernel Configuration</returns> |
| 0 | 57 | | public KernelConfigModel Get() { return value; } |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// Get a promise that is resolved when KernelConfig is initialized. |
| | 61 | | /// Usefeul when you need to fetch the configuration once and make sure it values were set and are not it defaults. |
| | 62 | | /// i.e: EnsureConfigInitialized.Then(config => //do stuff) |
| | 63 | | /// </summary> |
| | 64 | | /// <returns>Promise with latest values</returns> |
| | 65 | | public Promise<KernelConfigModel> EnsureConfigInitialized() |
| | 66 | | { |
| 343 | 67 | | var newPromise = new Promise<KernelConfigModel>(); |
| 343 | 68 | | if (initialized) |
| | 69 | | { |
| 182 | 70 | | newPromise.Resolve(value); |
| 182 | 71 | | } |
| | 72 | | else |
| | 73 | | { |
| 161 | 74 | | if (initializationPromises == null) |
| | 75 | | { |
| 2 | 76 | | initializationPromises = new List<Promise<KernelConfigModel>>(); |
| | 77 | | } |
| 161 | 78 | | initializationPromises.Add(newPromise); |
| | 79 | | } |
| 343 | 80 | | return newPromise; |
| | 81 | | } |
| | 82 | |
|
| | 83 | | private void Initialize(KernelConfigModel newValue) |
| | 84 | | { |
| 2 | 85 | | if (initializationPromises?.Count > 0) |
| | 86 | | { |
| 326 | 87 | | for (int i = 0; i < initializationPromises.Count; i++) |
| | 88 | | { |
| 161 | 89 | | initializationPromises[i].Resolve(newValue); |
| | 90 | | } |
| 2 | 91 | | initializationPromises.Clear(); |
| 2 | 92 | | initializationPromises = null; |
| | 93 | | } |
| 2 | 94 | | initialized = true; |
| 2 | 95 | | } |
| | 96 | |
|
| | 97 | | internal void Set(string json) |
| | 98 | | { |
| | 99 | | try |
| | 100 | | { |
| 1 | 101 | | var newConfig = value.Clone(); |
| 1 | 102 | | JsonUtility.FromJsonOverwrite(json, newConfig); |
| 1 | 103 | | Set(newConfig); |
| 1 | 104 | | } |
| 0 | 105 | | catch (System.Exception e) |
| | 106 | | { |
| 0 | 107 | | Debug.LogError($"Error setting KernelConfig {e.Message}"); |
| 0 | 108 | | } |
| 1 | 109 | | } |
| | 110 | |
|
| 2 | 111 | | private KernelConfig() { } |
| | 112 | | } |