ServiceMap.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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("jsonDecode"))
  89. {
  90. serviceModelDartString.AppendLine(CommonParameters.StringDartConvert);
  91. serviceModelDartString.AppendLine();
  92. }
  93. if (dartStringInfo.Contains("FJsonConvert"))
  94. {
  95. serviceModelDartString.AppendLine(CommonParameters.StringJsonConvert);
  96. serviceModelDartString.AppendLine();
  97. }
  98. serviceModelDartString.AppendLine(dartStringInfo);
  99. return serviceModelDartString.ToString();
  100. }
  101. public string GetServiceDartString()
  102. {
  103. var serviceDartString = new StringBuilder();
  104. var importServiceList = new List<string>();
  105. var alreadyGeneratedList = GenerateDataCache.Instance.AlreadyGeneratedList;
  106. foreach (var methodMap in _methodMapList)
  107. {
  108. GenerateDataCache.Instance.CurrentMethod = methodMap.MethodName;
  109. string importService = null;
  110. foreach (var modelType in methodMap.ParameterModelTypes)
  111. {
  112. if (modelType is ComplexModelType)
  113. {
  114. importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
  115. if (importService == null)
  116. {
  117. throw new Exception("Import Service is null");
  118. }
  119. if (importService != ServiceName && !importServiceList.Contains(importService))
  120. {
  121. importServiceList.Add(importService);
  122. }
  123. }
  124. }
  125. var returnModelType = methodMap.ReturnParameterModelType;
  126. if (returnModelType is ComplexModelType)
  127. {
  128. importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == returnModelType.ParameterType.Name && x.Key.ParameterType.Namespace == returnModelType.ParameterType.Namespace).Value;
  129. if (importService == null)
  130. {
  131. throw new Exception("Import Service is null");
  132. }
  133. if (importService != ServiceName && !importServiceList.Contains(importService))
  134. {
  135. importServiceList.Add(importService);
  136. }
  137. }
  138. else if (returnModelType is ListModelType listModelType)
  139. {
  140. var subReturnType = listModelType.GenericArgumentModelType;
  141. if (subReturnType is ComplexModelType)
  142. {
  143. importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == subReturnType.ParameterType.Name && x.Key.ParameterType.Namespace == subReturnType.ParameterType.Namespace).Value;
  144. if (importService == null)
  145. {
  146. throw new Exception("Import Service is null");
  147. }
  148. if (importService != ServiceName && !importServiceList.Contains(importService))
  149. {
  150. importServiceList.Add(importService);
  151. }
  152. }
  153. }
  154. else if (returnModelType is ArrayModelType arrayModelType)
  155. {
  156. var subReturnType = arrayModelType.Child;
  157. if (subReturnType is ComplexModelType)
  158. {
  159. importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == subReturnType.ParameterType.Name && x.Key.ParameterType.Namespace == subReturnType.ParameterType.Namespace).Value;
  160. if (importService == null)
  161. {
  162. throw new Exception("Import Service is null");
  163. }
  164. if (importService != ServiceName && !importServiceList.Contains(importService))
  165. {
  166. importServiceList.Add(importService);
  167. }
  168. }
  169. }
  170. else if (returnModelType is DictionaryModelType dictionaryModelType)
  171. {
  172. foreach (var subReturnType in dictionaryModelType.GenericArgumentModelTypeList)
  173. {
  174. if (subReturnType is ComplexModelType)
  175. {
  176. importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == subReturnType.ParameterType.Name && x.Key.ParameterType.Namespace == subReturnType.ParameterType.Namespace).Value;
  177. if (importService == null)
  178. {
  179. throw new Exception("Import Service is null");
  180. }
  181. if (importService != ServiceName && !importServiceList.Contains(importService))
  182. {
  183. importServiceList.Add(importService);
  184. }
  185. }
  186. }
  187. }
  188. }
  189. var complexReturnTypeNameList = GetUserDefinedComplexReturnTypeList();
  190. foreach (var complexReturnTypeName in complexReturnTypeNameList)
  191. {
  192. string importService;
  193. if (complexReturnTypeName.Contains("<"))
  194. {
  195. var returnTypeName = complexReturnTypeName.Substring(0, complexReturnTypeName.IndexOf("<"));
  196. importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name[0..^2] == returnTypeName).Value;
  197. }
  198. else
  199. {
  200. importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == complexReturnTypeName).Value;
  201. }
  202. if (importService != null && importService != ServiceName && !importServiceList.Contains(importService))
  203. {
  204. importServiceList.Add(importService);
  205. }
  206. }
  207. foreach (var importService in importServiceList)
  208. {
  209. serviceDartString.AppendLine($"import '{LetterConverterHelper.FirstCharToLower(importService[0..^7])}.m.dart';");
  210. }
  211. if (importServiceList.Count > 0)
  212. {
  213. serviceDartString.AppendLine();
  214. }
  215. serviceDartString.AppendLine();
  216. serviceDartString.AppendLine($"class {ServiceName} extends JsonRpcClientBase {{");
  217. serviceDartString.AppendLine($"\t{ServiceName}(");
  218. serviceDartString.AppendLine("\t\tString host, {");
  219. serviceDartString.AppendLine($"\t\tString serviceName = \"I{ServiceName}\",");
  220. serviceDartString.AppendLine("\t\tMap<String, String>? headers,");
  221. serviceDartString.AppendLine("\t\tint? timeout,");
  222. serviceDartString.AppendLine("\t}) : super(");
  223. serviceDartString.AppendLine("\t\t\t\t\t\thost,");
  224. serviceDartString.AppendLine("\t\t\t\t\t\tserviceName,");
  225. serviceDartString.AppendLine("\t\t\t\t\t\theaders: headers,");
  226. serviceDartString.AppendLine("\t\t\t\t\t\ttimeout: timeout,");
  227. if (complexReturnTypeNameList.Count() == 0)
  228. {
  229. serviceDartString.AppendLine("\t\t\t\t);");
  230. }
  231. else
  232. {
  233. serviceDartString.AppendLine("\t\t\t\t) {");
  234. serviceDartString.AppendLine("\t\t/// 注册响应实体反序列化处理器");
  235. foreach (var complexRetrunTypeName in complexReturnTypeNameList)
  236. {
  237. serviceDartString.AppendLine($"\t\tFJsonConvert.setDecoder((map) => {complexRetrunTypeName}.fromJson(map));");
  238. }
  239. serviceDartString.AppendLine("\t}");
  240. }
  241. serviceDartString.AppendLine();
  242. foreach (var methodMap in _methodMapList)
  243. {
  244. GenerateDataCache.Instance.CurrentMethod = methodMap.MethodName;
  245. serviceDartString.AppendLine(methodMap.GetMethodDartString());
  246. }
  247. serviceDartString.AppendLine("}");
  248. return serviceDartString.ToString();
  249. }
  250. public List<string> GetUserDefinedComplexReturnTypeList()
  251. {
  252. _userDefinedComplexReturnTypeList = new List<string>();
  253. foreach (var method in _methodMapList)
  254. {
  255. GenerateDataCache.Instance.CurrentMethod = method.MethodName;
  256. var veturnParameterModelType = method.ReturnParameterModelType;
  257. AddUserDefinedComplexReturnType(veturnParameterModelType);
  258. }
  259. return _userDefinedComplexReturnTypeList;
  260. }
  261. private void AddUserDefinedComplexReturnType(ModelType modelType)
  262. {
  263. if (modelType is UserDefinedModelType)
  264. {
  265. if (!_userDefinedComplexReturnTypeList.Contains(modelType.GetFlutterTypeName()))
  266. {
  267. _userDefinedComplexReturnTypeList.Add(modelType.GetFlutterTypeName());
  268. }
  269. }
  270. else if (modelType is UserDefinedDerivedModelType)
  271. {
  272. if (!_userDefinedComplexReturnTypeList.Contains(modelType.GetFlutterTypeName()))
  273. {
  274. _userDefinedComplexReturnTypeList.Add(modelType.GetFlutterTypeName());
  275. }
  276. }
  277. else if (modelType is UserDefinedGenericModelType userDefinedGenericComplexModelType)
  278. {
  279. if (!_userDefinedComplexReturnTypeList.Contains(userDefinedGenericComplexModelType.GetFlutterTypeName()))
  280. {
  281. _userDefinedComplexReturnTypeList.Add(userDefinedGenericComplexModelType.GetFlutterTypeName());
  282. }
  283. var argumentModelType = userDefinedGenericComplexModelType.GenericArgumentModelType;
  284. AddUserDefinedComplexReturnType(argumentModelType);
  285. }
  286. else if (modelType is ListModelType listModelType)
  287. {
  288. var argumentModelType = listModelType.GenericArgumentModelType;
  289. AddUserDefinedComplexReturnType(argumentModelType);
  290. }
  291. else if (modelType is DictionaryModelType dictionaryModelType)
  292. {
  293. foreach (var argumentModelType in dictionaryModelType.GenericArgumentModelTypeList)
  294. {
  295. AddUserDefinedComplexReturnType(argumentModelType);
  296. }
  297. }
  298. }
  299. }
  300. }