SRServiceManager.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using SRF.Components;
  6. using SRF.Helpers;
  7. using UnityEngine;
  8. namespace SRF.Service
  9. {
  10. [AddComponentMenu("SRF/Service/Service Manager")]
  11. public class SRServiceManager : SRAutoSingleton<SRServiceManager>
  12. {
  13. public static bool IsLoading
  14. {
  15. get
  16. {
  17. return SRServiceManager.LoadingCount > 0;
  18. }
  19. }
  20. public static T GetService<T>() where T : class
  21. {
  22. T t = SRServiceManager.GetServiceInternal(typeof(T)) as T;
  23. if (t == null && (!SRServiceManager._hasQuit || false))
  24. {
  25. UnityEngine.Debug.LogWarning("Service {0} not found. (HasQuit: {1})".Fmt(new object[]
  26. {
  27. typeof(T).Name,
  28. SRServiceManager._hasQuit
  29. }));
  30. }
  31. return t;
  32. }
  33. public static object GetService(Type t)
  34. {
  35. object serviceInternal = SRServiceManager.GetServiceInternal(t);
  36. if (serviceInternal == null && (!SRServiceManager._hasQuit || false))
  37. {
  38. UnityEngine.Debug.LogWarning("Service {0} not found. (HasQuit: {1})".Fmt(new object[]
  39. {
  40. t.Name,
  41. SRServiceManager._hasQuit
  42. }));
  43. }
  44. return serviceInternal;
  45. }
  46. private static object GetServiceInternal(Type t)
  47. {
  48. if (SRServiceManager._hasQuit || !Application.isPlaying)
  49. {
  50. return null;
  51. }
  52. SRList<SRServiceManager.Service> services = SRAutoSingleton<SRServiceManager>.Instance._services;
  53. int i = 0;
  54. while (i < services.Count)
  55. {
  56. SRServiceManager.Service service = services[i];
  57. if (t.IsAssignableFrom(service.Type))
  58. {
  59. if (service.Object == null)
  60. {
  61. SRServiceManager.UnRegisterService(t);
  62. break;
  63. }
  64. return service.Object;
  65. }
  66. else
  67. {
  68. i++;
  69. }
  70. }
  71. return SRAutoSingleton<SRServiceManager>.Instance.AutoCreateService(t);
  72. }
  73. public static bool HasService<T>() where T : class
  74. {
  75. return SRServiceManager.HasService(typeof(T));
  76. }
  77. public static bool HasService(Type t)
  78. {
  79. if (SRServiceManager._hasQuit || !Application.isPlaying)
  80. {
  81. return false;
  82. }
  83. SRList<SRServiceManager.Service> services = SRAutoSingleton<SRServiceManager>.Instance._services;
  84. for (int i = 0; i < services.Count; i++)
  85. {
  86. SRServiceManager.Service service = services[i];
  87. if (t.IsAssignableFrom(service.Type))
  88. {
  89. return service.Object != null;
  90. }
  91. }
  92. return false;
  93. }
  94. public static void RegisterService<T>(object service) where T : class
  95. {
  96. SRServiceManager.RegisterService(typeof(T), service);
  97. }
  98. private static void RegisterService(Type t, object service)
  99. {
  100. if (SRServiceManager._hasQuit)
  101. {
  102. return;
  103. }
  104. if (SRServiceManager.HasService(t))
  105. {
  106. if (SRServiceManager.GetServiceInternal(t) == service)
  107. {
  108. return;
  109. }
  110. throw new Exception("Service already registered for type " + t.Name);
  111. }
  112. else
  113. {
  114. SRServiceManager.UnRegisterService(t);
  115. if (!t.IsInstanceOfType(service))
  116. {
  117. throw new ArgumentException("service {0} must be assignable from type {1}".Fmt(new object[]
  118. {
  119. service.GetType(),
  120. t
  121. }));
  122. }
  123. SRAutoSingleton<SRServiceManager>.Instance._services.Add(new SRServiceManager.Service
  124. {
  125. Object = service,
  126. Type = t
  127. });
  128. return;
  129. }
  130. }
  131. public static void UnRegisterService<T>() where T : class
  132. {
  133. SRServiceManager.UnRegisterService(typeof(T));
  134. }
  135. private static void UnRegisterService(Type t)
  136. {
  137. if (SRServiceManager._hasQuit || !SRAutoSingleton<SRServiceManager>.HasInstance)
  138. {
  139. return;
  140. }
  141. if (!SRServiceManager.HasService(t))
  142. {
  143. return;
  144. }
  145. SRList<SRServiceManager.Service> services = SRAutoSingleton<SRServiceManager>.Instance._services;
  146. for (int i = services.Count - 1; i >= 0; i--)
  147. {
  148. SRServiceManager.Service service = services[i];
  149. if (service.Type == t)
  150. {
  151. services.RemoveAt(i);
  152. }
  153. }
  154. }
  155. protected override void Awake()
  156. {
  157. SRServiceManager._hasQuit = false;
  158. base.Awake();
  159. UnityEngine.Object.DontDestroyOnLoad(base.CachedGameObject);
  160. base.CachedGameObject.hideFlags = HideFlags.NotEditable;
  161. }
  162. protected void UpdateStubs()
  163. {
  164. if (this._serviceStubs != null)
  165. {
  166. return;
  167. }
  168. this._serviceStubs = new List<SRServiceManager.ServiceStub>();
  169. List<Type> list = new List<Type>();
  170. Assembly assembly = typeof(SRServiceManager).Assembly;
  171. try
  172. {
  173. list.AddRange(assembly.GetExportedTypes());
  174. }
  175. catch (Exception exception)
  176. {
  177. UnityEngine.Debug.LogError("[SRServiceManager] Error loading assembly {0}".Fmt(new object[]
  178. {
  179. assembly.FullName
  180. }), this);
  181. UnityEngine.Debug.LogException(exception);
  182. }
  183. foreach (Type type in list)
  184. {
  185. this.ScanType(type);
  186. }
  187. }
  188. protected object AutoCreateService(Type t)
  189. {
  190. this.UpdateStubs();
  191. foreach (SRServiceManager.ServiceStub serviceStub in this._serviceStubs)
  192. {
  193. if (serviceStub.InterfaceType == t)
  194. {
  195. object obj;
  196. if (serviceStub.Constructor != null)
  197. {
  198. obj = serviceStub.Constructor();
  199. }
  200. else
  201. {
  202. Type type = serviceStub.Type;
  203. if (type == null)
  204. {
  205. type = serviceStub.Selector();
  206. }
  207. obj = SRServiceManager.DefaultServiceConstructor(t, type);
  208. }
  209. if (!SRServiceManager.HasService(t))
  210. {
  211. SRServiceManager.RegisterService(t, obj);
  212. }
  213. return obj;
  214. }
  215. }
  216. return null;
  217. }
  218. protected void OnApplicationQuit()
  219. {
  220. SRServiceManager._hasQuit = true;
  221. }
  222. private static object DefaultServiceConstructor(Type serviceIntType, Type implType)
  223. {
  224. if (typeof(MonoBehaviour).IsAssignableFrom(implType))
  225. {
  226. GameObject gameObject = new GameObject("_S_" + serviceIntType.Name);
  227. return gameObject.AddComponent(implType);
  228. }
  229. if (typeof(ScriptableObject).IsAssignableFrom(implType))
  230. {
  231. return ScriptableObject.CreateInstance(implType);
  232. }
  233. return Activator.CreateInstance(implType);
  234. }
  235. private void ScanType(Type type)
  236. {
  237. ServiceAttribute attribute = SRReflection.GetAttribute<ServiceAttribute>(type);
  238. if (attribute != null)
  239. {
  240. this._serviceStubs.Add(new SRServiceManager.ServiceStub
  241. {
  242. Type = type,
  243. InterfaceType = attribute.ServiceType
  244. });
  245. }
  246. SRServiceManager.ScanTypeForConstructors(type, this._serviceStubs);
  247. SRServiceManager.ScanTypeForSelectors(type, this._serviceStubs);
  248. }
  249. private static void ScanTypeForSelectors(Type t, List<SRServiceManager.ServiceStub> stubs)
  250. {
  251. MethodInfo[] staticMethods = SRServiceManager.GetStaticMethods(t);
  252. MethodInfo[] array = staticMethods;
  253. for (int i = 0; i < array.Length; i++)
  254. {
  255. MethodInfo methodInfo = array[i];
  256. ServiceSelectorAttribute attrib = SRReflection.GetAttribute<ServiceSelectorAttribute>(methodInfo);
  257. if (attrib != null)
  258. {
  259. if (methodInfo.ReturnType != typeof(Type))
  260. {
  261. UnityEngine.Debug.LogError("ServiceSelector must have return type of Type ({0}.{1}())".Fmt(new object[]
  262. {
  263. t.Name,
  264. methodInfo.Name
  265. }));
  266. }
  267. else if (methodInfo.GetParameters().Length > 0)
  268. {
  269. UnityEngine.Debug.LogError("ServiceSelector must have no parameters ({0}.{1}())".Fmt(new object[]
  270. {
  271. t.Name,
  272. methodInfo.Name
  273. }));
  274. }
  275. else
  276. {
  277. SRServiceManager.ServiceStub serviceStub = stubs.FirstOrDefault((SRServiceManager.ServiceStub p) => p.InterfaceType == attrib.ServiceType);
  278. if (serviceStub == null)
  279. {
  280. serviceStub = new SRServiceManager.ServiceStub
  281. {
  282. InterfaceType = attrib.ServiceType
  283. };
  284. stubs.Add(serviceStub);
  285. }
  286. serviceStub.Selector = (Func<Type>)Delegate.CreateDelegate(typeof(Func<Type>), methodInfo);
  287. }
  288. }
  289. }
  290. }
  291. private static void ScanTypeForConstructors(Type t, List<SRServiceManager.ServiceStub> stubs)
  292. {
  293. MethodInfo[] staticMethods = SRServiceManager.GetStaticMethods(t);
  294. MethodInfo[] array = staticMethods;
  295. for (int i = 0; i < array.Length; i++)
  296. {
  297. MethodInfo methodInfo = array[i];
  298. ServiceConstructorAttribute attrib = SRReflection.GetAttribute<ServiceConstructorAttribute>(methodInfo);
  299. if (attrib != null)
  300. {
  301. if (methodInfo.ReturnType != attrib.ServiceType)
  302. {
  303. UnityEngine.Debug.LogError("ServiceConstructor must have return type of {2} ({0}.{1}())".Fmt(new object[]
  304. {
  305. t.Name,
  306. methodInfo.Name,
  307. attrib.ServiceType
  308. }));
  309. }
  310. else if (methodInfo.GetParameters().Length > 0)
  311. {
  312. UnityEngine.Debug.LogError("ServiceConstructor must have no parameters ({0}.{1}())".Fmt(new object[]
  313. {
  314. t.Name,
  315. methodInfo.Name
  316. }));
  317. }
  318. else
  319. {
  320. SRServiceManager.ServiceStub serviceStub = stubs.FirstOrDefault((SRServiceManager.ServiceStub p) => p.InterfaceType == attrib.ServiceType);
  321. if (serviceStub == null)
  322. {
  323. serviceStub = new SRServiceManager.ServiceStub
  324. {
  325. InterfaceType = attrib.ServiceType
  326. };
  327. stubs.Add(serviceStub);
  328. }
  329. MethodInfo m = methodInfo;
  330. serviceStub.Constructor = (() => m.Invoke(null, null));
  331. }
  332. }
  333. }
  334. }
  335. private static MethodInfo[] GetStaticMethods(Type t)
  336. {
  337. return t.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
  338. }
  339. public const bool EnableLogging = false;
  340. public static int LoadingCount;
  341. private readonly SRList<SRServiceManager.Service> _services = new SRList<SRServiceManager.Service>();
  342. private List<SRServiceManager.ServiceStub> _serviceStubs;
  343. private static bool _hasQuit;
  344. [Serializable]
  345. private class Service
  346. {
  347. public object Object;
  348. public Type Type;
  349. }
  350. [Serializable]
  351. private class ServiceStub
  352. {
  353. public override string ToString()
  354. {
  355. string text = this.InterfaceType.Name + " (";
  356. if (this.Type != null)
  357. {
  358. text = text + "Type: " + this.Type;
  359. }
  360. else if (this.Selector != null)
  361. {
  362. text = text + "Selector: " + this.Selector;
  363. }
  364. else if (this.Constructor != null)
  365. {
  366. text = text + "Constructor: " + this.Constructor;
  367. }
  368. return text + ")";
  369. }
  370. public Func<object> Constructor;
  371. public Type InterfaceType;
  372. public Func<Type> Selector;
  373. public Type Type;
  374. }
  375. }
  376. }