| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.Assertions; |
| | 5 | |
|
| | 6 | | namespace DCL |
| | 7 | | { |
| | 8 | | public delegate IService ServiceBuilder(); |
| | 9 | |
|
| | 10 | | public class ServiceLocator : IDisposable |
| | 11 | | { |
| 1 | 12 | | private static bool VERBOSE = false; |
| 1 | 13 | | private static Logger logger = new Logger("ServiceLocator") { verboseEnabled = VERBOSE }; |
| | 14 | |
|
| 883 | 15 | | private Dictionary<Type, IService> services = new Dictionary<Type, IService>(); |
| 883 | 16 | | private Dictionary<Type, ServiceBuilder> serviceBuilders = new Dictionary<Type, ServiceBuilder>(); |
| | 17 | |
|
| | 18 | | public ServiceBuilder Register<T>(ServiceBuilder data) where T : IService |
| | 19 | | { |
| 19969 | 20 | | Type type = typeof(T); |
| 19969 | 21 | | Assert.IsTrue( type.IsInterface, "ServiceLocator's generic type should be an interface." ); |
| | 22 | |
|
| 19969 | 23 | | if (serviceBuilders.ContainsKey(type)) |
| | 24 | | { |
| 2842 | 25 | | serviceBuilders[type] = data; |
| 2842 | 26 | | return data; |
| | 27 | | } |
| | 28 | |
|
| 17127 | 29 | | serviceBuilders.Add(type, data); |
| 17127 | 30 | | return data; |
| | 31 | | } |
| | 32 | |
|
| | 33 | | public void Unregister<T>() where T : IService |
| | 34 | | { |
| 0 | 35 | | Type type = typeof(T); |
| 0 | 36 | | Assert.IsTrue( type.IsInterface, "ServiceLocator's generic type should be an interface." ); |
| | 37 | |
|
| 0 | 38 | | if (!serviceBuilders.ContainsKey(type)) |
| | 39 | | { |
| 0 | 40 | | logger.Warning($"Trying to unregister non-existent type! type: {type.FullName}"); |
| 0 | 41 | | return; |
| | 42 | | } |
| | 43 | |
|
| 0 | 44 | | serviceBuilders.Remove(type); |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | public T Get<T>() where T : class, IService |
| | 48 | | { |
| 45234 | 49 | | Type type = typeof(T); |
| 45234 | 50 | | Assert.IsTrue( type.IsInterface, "ServiceLocator's generic type should be an interface." ); |
| | 51 | |
|
| 45234 | 52 | | if (!serviceBuilders.ContainsKey(type)) |
| | 53 | | { |
| 57 | 54 | | logger.Verbose($"Not registered! use Register<T> to set the type builder first and then Initialize() to |
| 57 | 55 | | return null; |
| | 56 | | } |
| | 57 | |
|
| 45177 | 58 | | if (!services.ContainsKey(type)) |
| | 59 | | { |
| 0 | 60 | | logger.Warning($"Not initialized! use Initialize() to create this service. type: {type.FullName}"); |
| 0 | 61 | | return null; |
| | 62 | | } |
| | 63 | |
|
| 45177 | 64 | | return services[type] as T; |
| | 65 | | } |
| | 66 | |
|
| | 67 | | public void Initialize() |
| | 68 | | { |
| 1764 | 69 | | foreach ( var service in services ) |
| | 70 | | { |
| 0 | 71 | | service.Value.Dispose(); |
| | 72 | | } |
| | 73 | |
|
| 882 | 74 | | services.Clear(); |
| | 75 | |
|
| 36018 | 76 | | foreach ( var kvp in serviceBuilders ) |
| | 77 | | { |
| 17127 | 78 | | var builderType = kvp.Key; |
| 17127 | 79 | | var builder = kvp.Value; |
| 17127 | 80 | | services.Add( builderType, builder.Invoke() ); |
| | 81 | | } |
| | 82 | |
|
| 36018 | 83 | | foreach ( var service in services ) |
| | 84 | | { |
| 17127 | 85 | | service.Value.Initialize(); |
| | 86 | | } |
| 882 | 87 | | } |
| | 88 | |
|
| | 89 | | public void Dispose() |
| | 90 | | { |
| 37778 | 91 | | foreach ( var service in services ) |
| | 92 | | { |
| 17127 | 93 | | service.Value.Dispose(); |
| | 94 | | } |
| | 95 | |
|
| 1762 | 96 | | services.Clear(); |
| 1762 | 97 | | serviceBuilders.Clear(); |
| 1762 | 98 | | } |
| | 99 | | } |
| | 100 | | } |