-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceContainer.cs
More file actions
147 lines (119 loc) · 5.61 KB
/
Copy pathServiceContainer.cs
File metadata and controls
147 lines (119 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using UnityEngine;
namespace ppl.ServiceManagement
{
public static class ServiceContainer
{
static string ERROR_NotInitializedOrImplemented => "{0} is not implemented or not initialized in ServiceContainer";
static Dictionary<string, IService> _services = new Dictionary<string, IService>();
public delegate void CallbackEventHandler();
public static async Task<TServiceModel> AddService<TServiceModel, TImplementation>(Dictionary<string, object> args = null)
where TServiceModel : IService
where TImplementation : TServiceModel
{
TImplementation instance = (TImplementation)Activator.CreateInstance(typeof(TImplementation));
await instance.AsyncSetup(args);
_services.Add(typeof(TServiceModel).Name, instance);
return instance;
}
public static async Task<object> AddService(Type targetInterface, Type implementation)
{
object instance = Activator.CreateInstance(implementation);
Type instanceType = instance.GetType();
MethodInfo setupMethod = instanceType.GetMethod("Setup");
MethodInfo asyncSetupMethod = instanceType.GetMethod("AsyncSetup");
if(null != setupMethod)
setupMethod.Invoke(instance, null);
else if(null != asyncSetupMethod)
await (Task)asyncSetupMethod.Invoke(instance, null);
Debug.Log("Added " + targetInterface.Name);
_services.Add(targetInterface.Name, (IService)instance);
return instance;
}
public static void UseService<TServiceModel>(Action<TServiceModel> onServiceLoad)
where TServiceModel: IService
{
string name = typeof(TServiceModel).Name;
if(!_services.ContainsKey(name))
throw new NotImplementedException(string.Format(ERROR_NotInitializedOrImplemented, name));
onServiceLoad.Invoke((TServiceModel)_services[name]);
}
public static void UseServices<TService1, TService2>(Action<TService1, TService2> onServicesLoad)
where TService1 : IService
where TService2 : IService
{
List<IService> foundServices = new List<IService>();
UseService<TService1>(service => foundServices.Add(service));
UseService<TService2>(service => foundServices.Add(service));
onServicesLoad.Invoke((TService1)foundServices[0], (TService2)foundServices[1]);
}
public static void UseServices<TService1, TService2, TService3>(Action<TService1, TService2, TService3> onServicesLoad)
where TService1 : IService
where TService2 : IService
where TService3 : IService
{
List<IService> foundServices = new List<IService>();
UseServices<TService1, TService2>((service1, service2) =>
{
foundServices.Add(service1);
foundServices.Add(service2);
});
UseService<TService3>(service3 => { foundServices.Add(service3); });
onServicesLoad.Invoke((TService1)foundServices[0], (TService2)foundServices[1], (TService3)foundServices[2]);
}
public static void UseServices<TService1, TService2, TService3, TService4>(Action<TService1, TService2, TService3, TService4> onServicesLoad)
where TService1 : IService
where TService2 : IService
where TService3 : IService
where TService4 : IService
{
List<IService> foundServices = new List<IService>();
UseServices<TService1, TService2, TService3>((service1, service2, service3) =>
{
foundServices.Add(service1);
foundServices.Add(service2);
foundServices.Add(service3);
});
UseService<TService4>(service4 => { foundServices.Add(service4); });
onServicesLoad.Invoke((TService1)foundServices[0], (TService2)foundServices[1], (TService3)foundServices[2], (TService4)foundServices[3]);
}
public static void UseService(Type service, Action<IService> onServiceLoad)
{
string name = service.Name;
try
{
onServiceLoad.Invoke(_services[name]);
}
catch
{
throw new NotImplementedException(string.Format(ERROR_NotInitializedOrImplemented, name));
}
}
#if UNITY_EDITOR
[System.Obsolete("You should not be using this function. It's only for the debug window. It will not be included in the final build")]
public static void EDITOR_DrawServiceSelector(Action<int> onService)
{
foreach (var service in _services)
{
onService(service.Key.GetHashCode());
}
}
[System.Obsolete("You should not be using this function. It's only for the debug window. It will not be included in the final build")]
public static IService EDITOR_GetServiceByHash(int hash)
{
return _services.First(service => service.Key.GetHashCode() == hash).Value;
}
#endif
}
internal static class InterfaceUtils
{
internal static bool Implements<TImplementation, TService>()
{
return typeof(TService).IsAssignableFrom(typeof(TImplementation));
}
}
}