ServiceMap.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. using FlutterCodeGenerator.Helper;
  2. using FlutterCodeGenerator.Map.Interface;
  3. using FlutterCodeGenerator.Model;
  4. using FlutterCodeGenerator.ModelTypes;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. namespace FlutterCodeGenerator.Map
  11. {
  12. internal class ServiceMap : IServiceMap
  13. {
  14. private List<string> _userDefinedComplexReturnTypeList;
  15. private List<MethodMap> _methodMapList;
  16. public string ServiceName { get; private set; }
  17. public List<ComplexModelType> UsedComplexModelTypeList { get; }
  18. public bool HasMethod => _methodMapList.Count > 0;
  19. public ServiceMap(Type type, bool isFISLib)
  20. {
  21. ServiceName = type.Name[1..];
  22. UsedComplexModelTypeList = new List<ComplexModelType>();
  23. GenerateDataCache.Instance.SetCurrentServiceMap(this);
  24. _methodMapList = new List<MethodMap>();
  25. var methodsList = type.GetMethods().ToList();
  26. var eventList = type.GetEvents().ToList();
  27. var eventMethodList = new List<MethodInfo>();
  28. foreach (var eventType in eventList)
  29. {
  30. eventMethodList.Add(eventType.RemoveMethod);
  31. eventMethodList.Add(eventType.AddMethod);
  32. }
  33. methodsList = methodsList.Where(x => !eventMethodList.Contains(x)).ToList();//将Event的Method屏蔽
  34. if (isFISLib)
  35. {
  36. methodsList = methodsList.Where(x => x.CustomAttributes.Any(y => y.AttributeType.FullName == "FISLib.FISAttribute" && y.ConstructorArguments.FirstOrDefault().ArgumentType == typeof(string) && (string)y.ConstructorArguments.FirstOrDefault().Value == "ForFlutter")).ToList();
  37. }
  38. foreach (var method in methodsList)
  39. {
  40. var methodMap = new MethodMap(method);
  41. _methodMapList.Add(methodMap);
  42. }
  43. }
  44. public string GetServiceModelDartString()
  45. {
  46. var dartString = new StringBuilder();
  47. var importServiceList = new List<string>();
  48. var alreadyGeneratedList = GenerateDataCache.Instance.AlreadyGeneratedList;
  49. foreach (var modelType in UsedComplexModelTypeList)
  50. {
  51. if (!alreadyGeneratedList.Any(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
  52. {
  53. alreadyGeneratedList.Add(modelType, ServiceName);
  54. dartString.AppendLine(modelType.GetDartString());
  55. }
  56. else
  57. {
  58. var importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
  59. if (importService == null)
  60. {
  61. throw new Exception("Import Service is null");
  62. }
  63. if (importService != ServiceName && !importServiceList.Contains(importService))
  64. {
  65. importServiceList.Add(importService);
  66. }
  67. }
  68. }
  69. if (string.IsNullOrWhiteSpace(dartString.ToString()))
  70. {
  71. return null;
  72. }
  73. var serviceModelDartString = new StringBuilder();
  74. foreach (var importService in importServiceList)
  75. {
  76. serviceModelDartString.AppendLine($"import '{LetterConverterHelper.FirstCharToLower(importService[0..^7])}.m.dart';");
  77. }
  78. if (importServiceList.Count > 0)
  79. {
  80. serviceModelDartString.AppendLine();
  81. }
  82. var dartStringInfo = dartString.ToString();
  83. if (dartStringInfo.Contains("JsonRpcUtils"))
  84. {
  85. serviceModelDartString.AppendLine(CommonParameters.StringUtils);
  86. serviceModelDartString.AppendLine();
  87. }
  88. if (dartStringInfo.Contains("FJsonConvert"))
  89. {
  90. serviceModelDartString.AppendLine(CommonParameters.StringJsonConvert);
  91. serviceModelDartString.AppendLine();
  92. }
  93. serviceModelDartString.AppendLine(dartStringInfo);
  94. return serviceModelDartString.ToString();
  95. }
  96. public string GetServiceDartString()
  97. {
  98. var serviceDartString = new StringBuilder();
  99. var importServiceList = new List<string>();
  100. var alreadyGeneratedList = GenerateDataCache.Instance.AlreadyGeneratedList;
  101. foreach (var methodMap in _methodMapList)
  102. {
  103. GenerateDataCache.Instance.CurrentMethod = methodMap.MethodName;
  104. string importService = null;
  105. foreach (var modelType in methodMap.ParameterModelTypes)
  106. {
  107. if (modelType is ComplexModelType)
  108. {
  109. importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
  110. if (importService == null)
  111. {
  112. throw new Exception("Import Service is null");
  113. }
  114. if (importService != ServiceName && !importServiceList.Contains(importService))
  115. {
  116. importServiceList.Add(importService);
  117. }
  118. }
  119. }
  120. var returnModelType = methodMap.ReturnParameterModelType;
  121. if (returnModelType is ComplexModelType)
  122. {
  123. importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == returnModelType.ParameterType.Name && x.Key.ParameterType.Namespace == returnModelType.ParameterType.Namespace).Value;
  124. if (importService == null)
  125. {
  126. throw new Exception("Import Service is null");
  127. }
  128. if (importService != ServiceName && !importServiceList.Contains(importService))
  129. {
  130. importServiceList.Add(importService);
  131. }
  132. }
  133. else if (returnModelType is ListModelType listModelType)
  134. {
  135. var subReturnType = listModelType.GenericArgumentModelType;
  136. if (subReturnType is ComplexModelType)
  137. {
  138. importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == subReturnType.ParameterType.Name && x.Key.ParameterType.Namespace == subReturnType.ParameterType.Namespace).Value;
  139. if (importService == null)
  140. {
  141. throw new Exception("Import Service is null");
  142. }
  143. if (importService != ServiceName && !importServiceList.Contains(importService))
  144. {
  145. importServiceList.Add(importService);
  146. }
  147. }
  148. }
  149. else if (returnModelType is ArrayModelType arrayModelType)
  150. {
  151. var subReturnType = arrayModelType.Child;
  152. if (subReturnType is ComplexModelType)
  153. {
  154. importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == subReturnType.ParameterType.Name && x.Key.ParameterType.Namespace == subReturnType.ParameterType.Namespace).Value;
  155. if (importService == null)
  156. {
  157. throw new Exception("Import Service is null");
  158. }
  159. if (importService != ServiceName && !importServiceList.Contains(importService))
  160. {
  161. importServiceList.Add(importService);
  162. }
  163. }
  164. }
  165. else if (returnModelType is DictionaryModelType dictionaryModelType)
  166. {
  167. foreach (var subReturnType in dictionaryModelType.GenericArgumentModelTypeList)
  168. {
  169. if (subReturnType is ComplexModelType)
  170. {
  171. importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == subReturnType.ParameterType.Name && x.Key.ParameterType.Namespace == subReturnType.ParameterType.Namespace).Value;
  172. if (importService == null)
  173. {
  174. throw new Exception("Import Service is null");
  175. }
  176. if (importService != ServiceName && !importServiceList.Contains(importService))
  177. {
  178. importServiceList.Add(importService);
  179. }
  180. }
  181. }
  182. }
  183. }
  184. var complexReturnTypeNameList = GetUserDefinedComplexReturnTypeList();
  185. foreach (var complexReturnTypeName in complexReturnTypeNameList)
  186. {
  187. string importService;
  188. if (complexReturnTypeName.Contains("<"))
  189. {
  190. var returnTypeName = complexReturnTypeName.Substring(0, complexReturnTypeName.IndexOf("<"));
  191. importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name[0..^2] == returnTypeName).Value;
  192. }
  193. else
  194. {
  195. importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == complexReturnTypeName).Value;
  196. }
  197. if (importService != null && importService != ServiceName && !importServiceList.Contains(importService))
  198. {
  199. importServiceList.Add(importService);
  200. }
  201. }
  202. foreach (var importService in importServiceList)
  203. {
  204. serviceDartString.AppendLine($"import '{LetterConverterHelper.FirstCharToLower(importService[0..^7])}.m.dart';");
  205. }
  206. if (importServiceList.Count > 0)
  207. {
  208. serviceDartString.AppendLine();
  209. }
  210. serviceDartString.AppendLine();
  211. serviceDartString.AppendLine($"class {ServiceName} extends JsonRpcClientBase {{");
  212. serviceDartString.AppendLine($"\t{ServiceName}(");
  213. serviceDartString.AppendLine("\t\tString host, {");
  214. serviceDartString.AppendLine($"\t\tString serviceName = \"I{ServiceName}\",");
  215. serviceDartString.AppendLine("\t\tMap<String, String>? headers,");
  216. serviceDartString.AppendLine("\t\tint? timeout,");
  217. serviceDartString.AppendLine("\t}) : super(");
  218. serviceDartString.AppendLine("\t\t\t\t\t\thost,");
  219. serviceDartString.AppendLine("\t\t\t\t\t\tserviceName,");
  220. serviceDartString.AppendLine("\t\t\t\t\t\theaders: headers,");
  221. serviceDartString.AppendLine("\t\t\t\t\t\ttimeout: timeout,");
  222. if (complexReturnTypeNameList.Count() == 0)
  223. {
  224. serviceDartString.AppendLine("\t\t\t\t);");
  225. }
  226. else
  227. {
  228. serviceDartString.AppendLine("\t\t\t\t) {");
  229. serviceDartString.AppendLine("\t\t/// 注册响应实体反序列化处理器");
  230. foreach (var complexRetrunTypeName in complexReturnTypeNameList)
  231. {
  232. serviceDartString.AppendLine($"\t\tFJsonConvert.setDecoder((map) => {complexRetrunTypeName}.fromJson(map));");
  233. }
  234. serviceDartString.AppendLine("\t}");
  235. }
  236. serviceDartString.AppendLine();
  237. foreach (var methodMap in _methodMapList)
  238. {
  239. GenerateDataCache.Instance.CurrentMethod = methodMap.MethodName;
  240. serviceDartString.AppendLine(methodMap.GetMethodDartString());
  241. }
  242. serviceDartString.AppendLine("}");
  243. return serviceDartString.ToString();
  244. }
  245. public List<string> GetUserDefinedComplexReturnTypeList()
  246. {
  247. _userDefinedComplexReturnTypeList = new List<string>();
  248. foreach (var method in _methodMapList)
  249. {
  250. GenerateDataCache.Instance.CurrentMethod = method.MethodName;
  251. var veturnParameterModelType = method.ReturnParameterModelType;
  252. AddUserDefinedComplexReturnType(veturnParameterModelType);
  253. }
  254. return _userDefinedComplexReturnTypeList;
  255. }
  256. private void AddUserDefinedComplexReturnType(ModelType modelType)
  257. {
  258. if (modelType is UserDefinedModelType)
  259. {
  260. if (!_userDefinedComplexReturnTypeList.Contains(modelType.GetFlutterTypeName()))
  261. {
  262. _userDefinedComplexReturnTypeList.Add(modelType.GetFlutterTypeName());
  263. }
  264. }
  265. else if (modelType is UserDefinedDerivedModelType)
  266. {
  267. if (!_userDefinedComplexReturnTypeList.Contains(modelType.GetFlutterTypeName()))
  268. {
  269. _userDefinedComplexReturnTypeList.Add(modelType.GetFlutterTypeName());
  270. }
  271. }
  272. else if (modelType is UserDefinedGenericModelType userDefinedGenericComplexModelType)
  273. {
  274. if (!_userDefinedComplexReturnTypeList.Contains(userDefinedGenericComplexModelType.GetFlutterTypeName()))
  275. {
  276. _userDefinedComplexReturnTypeList.Add(userDefinedGenericComplexModelType.GetFlutterTypeName());
  277. }
  278. var argumentModelType = userDefinedGenericComplexModelType.GenericArgumentModelType;
  279. AddUserDefinedComplexReturnType(argumentModelType);
  280. }
  281. else if (modelType is ListModelType listModelType)
  282. {
  283. var argumentModelType = listModelType.GenericArgumentModelType;
  284. AddUserDefinedComplexReturnType(argumentModelType);
  285. }
  286. else if (modelType is DictionaryModelType dictionaryModelType)
  287. {
  288. foreach (var argumentModelType in dictionaryModelType.GenericArgumentModelTypeList)
  289. {
  290. AddUserDefinedComplexReturnType(argumentModelType);
  291. }
  292. }
  293. }
  294. }
  295. }