ServiceMap.cs 15 KB

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