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