/FishingPrototype/Assets/ThirdParty/Zenject/Source/Providers/FactoryProvider.cs

https://gitlab.com/ChicK00o/JaneiousTests · C# · 321 lines · 243 code · 48 blank · 30 comment · 12 complexity · 95bc989cecb780f23118983d2dcfed39 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using ModestTree;
  4. namespace Zenject
  5. {
  6. public abstract class FactoryProviderBase<TValue, TFactory> : IProvider
  7. where TFactory : IFactory
  8. {
  9. readonly List<TypeValuePair> _factoryArgs;
  10. public FactoryProviderBase(DiContainer container, List<TypeValuePair> factoryArgs)
  11. {
  12. Container = container;
  13. _factoryArgs = factoryArgs;
  14. }
  15. protected DiContainer Container
  16. {
  17. get;
  18. private set;
  19. }
  20. public Type GetInstanceType(InjectContext context)
  21. {
  22. return typeof(TValue);
  23. }
  24. public abstract IEnumerator<List<object>> GetAllInstancesWithInjectSplit(
  25. InjectContext context, List<TypeValuePair> args);
  26. protected object CreateFactory()
  27. {
  28. return Container.InstantiateExplicit(typeof(TFactory), _factoryArgs);
  29. }
  30. }
  31. // Zero parameters
  32. public class FactoryProvider<TValue, TFactory> : FactoryProviderBase<TValue, TFactory>
  33. where TFactory : IFactory<TValue>
  34. {
  35. public FactoryProvider(DiContainer container, List<TypeValuePair> factoryArgs)
  36. : base(container, factoryArgs)
  37. {
  38. }
  39. public override IEnumerator<List<object>> GetAllInstancesWithInjectSplit(
  40. InjectContext context, List<TypeValuePair> args)
  41. {
  42. Assert.IsEmpty(args);
  43. Assert.IsNotNull(context);
  44. Assert.That(typeof(TValue).DerivesFromOrEqual(context.MemberType));
  45. // Do this even when validating in case it has its own dependencies
  46. var factory = CreateFactory();
  47. if (Container.IsValidating)
  48. {
  49. // In case users define a custom IFactory that needs to be validated
  50. if (factory is IValidatable)
  51. {
  52. ((IValidatable)factory).Validate();
  53. }
  54. // We assume here that we are creating a user-defined factory so there's
  55. // nothing else we can validate here
  56. yield return new List<object>() { new ValidationMarker(typeof(TValue)) };
  57. }
  58. else
  59. {
  60. yield return new List<object>() { ((TFactory)factory).Create() };
  61. }
  62. }
  63. }
  64. // One parameters
  65. public class FactoryProvider<TParam1, TValue, TFactory> : FactoryProviderBase<TValue, TFactory>
  66. where TFactory : IFactory<TParam1, TValue>
  67. {
  68. public FactoryProvider(DiContainer container, List<TypeValuePair> factoryArgs)
  69. : base(container, factoryArgs)
  70. {
  71. }
  72. public override IEnumerator<List<object>> GetAllInstancesWithInjectSplit(
  73. InjectContext context, List<TypeValuePair> args)
  74. {
  75. Assert.IsEqual(args.Count, 1);
  76. Assert.IsNotNull(context);
  77. Assert.That(typeof(TValue).DerivesFromOrEqual(context.MemberType));
  78. Assert.That(args[0].Type.DerivesFromOrEqual<TParam1>());
  79. // Do this even when validating in case it has its own dependencies
  80. var factory = CreateFactory();
  81. if (Container.IsValidating)
  82. {
  83. // In case users define a custom IFactory that needs to be validated
  84. if (factory is IValidatable)
  85. {
  86. ((IValidatable)factory).Validate();
  87. }
  88. // We assume here that we are creating a user-defined factory so there's
  89. // nothing else we can validate here
  90. yield return new List<object>() { new ValidationMarker(typeof(TValue)) };
  91. }
  92. else
  93. {
  94. yield return new List<object>()
  95. {
  96. ((TFactory)factory).Create((TParam1)args[0].Value)
  97. };
  98. }
  99. }
  100. }
  101. // Two parameters
  102. public class FactoryProvider<TParam1, TParam2, TValue, TFactory> : FactoryProviderBase<TValue, TFactory>
  103. where TFactory : IFactory<TParam1, TParam2, TValue>
  104. {
  105. public FactoryProvider(DiContainer container, List<TypeValuePair> factoryArgs)
  106. : base(container, factoryArgs)
  107. {
  108. }
  109. public override IEnumerator<List<object>> GetAllInstancesWithInjectSplit(
  110. InjectContext context, List<TypeValuePair> args)
  111. {
  112. Assert.IsEqual(args.Count, 2);
  113. Assert.IsNotNull(context);
  114. Assert.That(typeof(TValue).DerivesFromOrEqual(context.MemberType));
  115. Assert.That(args[0].Type.DerivesFromOrEqual<TParam1>());
  116. Assert.That(args[1].Type.DerivesFromOrEqual<TParam2>());
  117. // Do this even when validating in case it has its own dependencies
  118. var factory = CreateFactory();
  119. if (Container.IsValidating)
  120. {
  121. // In case users define a custom IFactory that needs to be validated
  122. if (factory is IValidatable)
  123. {
  124. ((IValidatable)factory).Validate();
  125. }
  126. // We assume here that we are creating a user-defined factory so there's
  127. // nothing else we can validate here
  128. yield return new List<object>() { new ValidationMarker(typeof(TValue)) };
  129. }
  130. else
  131. {
  132. yield return new List<object>()
  133. {
  134. ((TFactory)factory).Create(
  135. (TParam1)args[0].Value,
  136. (TParam2)args[1].Value)
  137. };
  138. }
  139. }
  140. }
  141. // Three parameters
  142. public class FactoryProvider<TParam1, TParam2, TParam3, TValue, TFactory> : FactoryProviderBase<TValue, TFactory>
  143. where TFactory : IFactory<TParam1, TParam2, TParam3, TValue>
  144. {
  145. public FactoryProvider(DiContainer container, List<TypeValuePair> factoryArgs)
  146. : base(container, factoryArgs)
  147. {
  148. }
  149. public override IEnumerator<List<object>> GetAllInstancesWithInjectSplit(
  150. InjectContext context, List<TypeValuePair> args)
  151. {
  152. Assert.IsEqual(args.Count, 3);
  153. Assert.IsNotNull(context);
  154. Assert.That(typeof(TValue).DerivesFromOrEqual(context.MemberType));
  155. Assert.That(args[0].Type.DerivesFromOrEqual<TParam1>());
  156. Assert.That(args[1].Type.DerivesFromOrEqual<TParam2>());
  157. Assert.That(args[2].Type.DerivesFromOrEqual<TParam3>());
  158. // Do this even when validating in case it has its own dependencies
  159. var factory = CreateFactory();
  160. if (Container.IsValidating)
  161. {
  162. // In case users define a custom IFactory that needs to be validated
  163. if (factory is IValidatable)
  164. {
  165. ((IValidatable)factory).Validate();
  166. }
  167. // We assume here that we are creating a user-defined factory so there's
  168. // nothing else we can validate here
  169. yield return new List<object>() { new ValidationMarker(typeof(TValue)) };
  170. }
  171. else
  172. {
  173. yield return new List<object>()
  174. {
  175. ((TFactory)factory).Create(
  176. (TParam1)args[0].Value,
  177. (TParam2)args[1].Value,
  178. (TParam3)args[2].Value)
  179. };
  180. }
  181. }
  182. }
  183. // Four parameters
  184. public class FactoryProvider<TParam1, TParam2, TParam3, TParam4, TValue, TFactory> : FactoryProviderBase<TValue, TFactory>
  185. where TFactory : IFactory<TParam1, TParam2, TParam3, TParam4, TValue>
  186. {
  187. public FactoryProvider(DiContainer container, List<TypeValuePair> factoryArgs)
  188. : base(container, factoryArgs)
  189. {
  190. }
  191. public override IEnumerator<List<object>> GetAllInstancesWithInjectSplit(
  192. InjectContext context, List<TypeValuePair> args)
  193. {
  194. Assert.IsEqual(args.Count, 4);
  195. Assert.IsNotNull(context);
  196. Assert.That(typeof(TValue).DerivesFromOrEqual(context.MemberType));
  197. Assert.That(args[0].Type.DerivesFromOrEqual<TParam1>());
  198. Assert.That(args[1].Type.DerivesFromOrEqual<TParam2>());
  199. Assert.That(args[2].Type.DerivesFromOrEqual<TParam3>());
  200. Assert.That(args[3].Type.DerivesFromOrEqual<TParam4>());
  201. // Do this even when validating in case it has its own dependencies
  202. var factory = CreateFactory();
  203. if (Container.IsValidating)
  204. {
  205. // In case users define a custom IFactory that needs to be validated
  206. if (factory is IValidatable)
  207. {
  208. ((IValidatable)factory).Validate();
  209. }
  210. // We assume here that we are creating a user-defined factory so there's
  211. // nothing else we can validate here
  212. yield return new List<object>() { new ValidationMarker(typeof(TValue)) };
  213. }
  214. else
  215. {
  216. yield return new List<object>()
  217. {
  218. ((TFactory)factory).Create(
  219. (TParam1)args[0].Value,
  220. (TParam2)args[1].Value,
  221. (TParam3)args[2].Value,
  222. (TParam4)args[3].Value)
  223. };
  224. }
  225. }
  226. }
  227. // Five parameters
  228. public class FactoryProvider<TParam1, TParam2, TParam3, TParam4, TParam5, TValue, TFactory> : FactoryProviderBase<TValue, TFactory>
  229. where TFactory : IFactory<TParam1, TParam2, TParam3, TParam4, TParam5, TValue>
  230. {
  231. public FactoryProvider(DiContainer container, List<TypeValuePair> factoryArgs)
  232. : base(container, factoryArgs)
  233. {
  234. }
  235. public override IEnumerator<List<object>> GetAllInstancesWithInjectSplit(
  236. InjectContext context, List<TypeValuePair> args)
  237. {
  238. Assert.IsEqual(args.Count, 5);
  239. Assert.IsNotNull(context);
  240. Assert.That(typeof(TValue).DerivesFromOrEqual(context.MemberType));
  241. Assert.That(args[0].Type.DerivesFromOrEqual<TParam1>());
  242. Assert.That(args[1].Type.DerivesFromOrEqual<TParam2>());
  243. Assert.That(args[2].Type.DerivesFromOrEqual<TParam3>());
  244. Assert.That(args[3].Type.DerivesFromOrEqual<TParam4>());
  245. Assert.That(args[4].Type.DerivesFromOrEqual<TParam5>());
  246. // Do this even when validating in case it has its own dependencies
  247. var factory = CreateFactory();
  248. if (Container.IsValidating)
  249. {
  250. // In case users define a custom IFactory that needs to be validated
  251. if (factory is IValidatable)
  252. {
  253. ((IValidatable)factory).Validate();
  254. }
  255. // We assume here that we are creating a user-defined factory so there's
  256. // nothing else we can validate here
  257. yield return new List<object>() { new ValidationMarker(typeof(TValue)) };
  258. }
  259. else
  260. {
  261. yield return new List<object>()
  262. {
  263. ((TFactory)factory).Create(
  264. (TParam1)args[0].Value,
  265. (TParam2)args[1].Value,
  266. (TParam3)args[2].Value,
  267. (TParam4)args[3].Value,
  268. (TParam5)args[4].Value)
  269. };
  270. }
  271. }
  272. }
  273. }