using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace FlutterCodeGenerator { internal class ServiceMap { public List MethodMapList; public static List TemporaryList { get; set; } private List _usedComplexModelTypeList; private List _userDefinedComplexReturnTypeList; private string _serviceName; public ServiceMap(Type type) { _usedComplexModelTypeList = new List(); TemporaryList = new List(); MethodMapList = new List(); var methodsList = type.GetMethods(); _serviceName = type.Name[1..]; foreach (var method in methodsList) { var methodMap = new MethodMap(method); MethodMapList.Add(methodMap); } _usedComplexModelTypeList.AddRange(TemporaryList); TemporaryList.Clear(); } public string GetServiceModelDartString() { var dartString = new StringBuilder(); var importServiceList = new List(); foreach (var modelType in _usedComplexModelTypeList) { if (!CodeGenerator.AlreadyGeneratedList.Any(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace)) { CodeGenerator.AlreadyGeneratedList.Add(modelType, _serviceName); dartString.AppendLine(modelType.GetDartString()); } else { var importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value; if (importService == null) { throw new Exception("Import Service is null"); } if (importService != _serviceName && !importServiceList.Contains(importService)) { importServiceList.Add(importService); } } } if (string.IsNullOrWhiteSpace(dartString.ToString())) { return null; } var serviceModelDartString = new StringBuilder(); foreach (var importService in importServiceList) { serviceModelDartString.AppendLine($"import '{LetterConverterHelper.FirstCharToLower(importService[0..^7])}.m.dart';"); } if (importServiceList.Count > 0) { serviceModelDartString.AppendLine(); } var dartStringInfo = dartString.ToString(); if (dartStringInfo.Contains("JsonRpcUtils")) { serviceModelDartString.AppendLine(CodeGenerator.StringUtils); serviceModelDartString.AppendLine(); } if (dartStringInfo.Contains("FJsonConvert")) { serviceModelDartString.AppendLine(CodeGenerator.StringJsonConvert); serviceModelDartString.AppendLine(); } serviceModelDartString.AppendLine(dartStringInfo); return serviceModelDartString.ToString(); } public string GetServiceDartString() { var serviceDartString = new StringBuilder(); var importServiceList = new List(); foreach (var methodMap in MethodMapList) { string importService = null; foreach (var modelType in methodMap.ParameterModeTypes) { if (modelType is ComplexModelType) { importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value; if (importService == null) { throw new Exception("Import Service is null"); } if (importService != _serviceName && !importServiceList.Contains(importService)) { importServiceList.Add(importService); } } } var returnModelType = methodMap.ReturnParameterModelType; if (returnModelType is ComplexModelType) { importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == returnModelType.ParameterType.Name && x.Key.ParameterType.Namespace == returnModelType.ParameterType.Namespace).Value; if (importService == null) { throw new Exception("Import Service is null"); } if (importService != _serviceName && !importServiceList.Contains(importService)) { importServiceList.Add(importService); } } else if (returnModelType is ListModelType listModelType) { var subReturnType = listModelType.GenericArgumentModelType; if (subReturnType is ComplexModelType) { importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == subReturnType.ParameterType.Name && x.Key.ParameterType.Namespace == subReturnType.ParameterType.Namespace).Value; if (importService == null) { throw new Exception("Import Service is null"); } if (importService != _serviceName && !importServiceList.Contains(importService)) { importServiceList.Add(importService); } } } else if (returnModelType is ArrayModelType arrayModelType) { var subReturnType = arrayModelType.Child; if (subReturnType is ComplexModelType) { importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == subReturnType.ParameterType.Name && x.Key.ParameterType.Namespace == subReturnType.ParameterType.Namespace).Value; if (importService == null) { throw new Exception("Import Service is null"); } if (importService != _serviceName && !importServiceList.Contains(importService)) { importServiceList.Add(importService); } } } else if (returnModelType is DictionaryModelType dictionaryModelType) { foreach (var subReturnType in dictionaryModelType.GenericArgumentModelTypeList) { if (subReturnType is ComplexModelType) { importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == subReturnType.ParameterType.Name && x.Key.ParameterType.Namespace == subReturnType.ParameterType.Namespace).Value; if (importService == null) { throw new Exception("Import Service is null"); } if (importService != _serviceName && !importServiceList.Contains(importService)) { importServiceList.Add(importService); } } } } } var complexReturnTypeNameList = GetUserDefinedComplexReturnTypeList(); foreach (var complexReturnTypeName in complexReturnTypeNameList) { string importService; if (complexReturnTypeName.Contains("<")) { var returnTypeName = complexReturnTypeName.Substring(0, complexReturnTypeName.IndexOf("<")); importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name[0..^2] == returnTypeName).Value; } else { importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == complexReturnTypeName).Value; } if (importService == null) { throw new Exception("Import Service is null"); } if (importService != _serviceName && !importServiceList.Contains(importService)) { importServiceList.Add(importService); } } foreach (var importService in importServiceList) { serviceDartString.AppendLine($"import '{LetterConverterHelper.FirstCharToLower(importService[0..^7])}.m.dart';"); } if (importServiceList.Count > 0) { serviceDartString.AppendLine(); } serviceDartString.AppendLine(); serviceDartString.AppendLine($"class {_serviceName} extends JsonRpcClientBase {{"); serviceDartString.AppendLine($"\t{_serviceName}("); serviceDartString.AppendLine("\t\tString host, {"); serviceDartString.AppendLine($"\t\tString serviceName = \"I{_serviceName}\","); serviceDartString.AppendLine("\t\tMap? headers,"); serviceDartString.AppendLine("\t\tint? timeout,"); serviceDartString.AppendLine("\t}) : super("); serviceDartString.AppendLine("\t\t\t\t\t\thost,"); serviceDartString.AppendLine("\t\t\t\t\t\tserviceName,"); serviceDartString.AppendLine("\t\t\t\t\t\theaders: headers,"); serviceDartString.AppendLine("\t\t\t\t\t\ttimeout: timeout,"); if (complexReturnTypeNameList.Count() == 0) { serviceDartString.AppendLine("\t\t\t\t);"); } else { serviceDartString.AppendLine("\t\t\t\t) {"); serviceDartString.AppendLine("\t\t/// 注册响应实体反序列化处理器"); foreach (var complexRetrunTypeName in complexReturnTypeNameList) { serviceDartString.AppendLine($"\t\tFJsonConvert.setDecoder((map) => {complexRetrunTypeName}.fromJson(map));"); } serviceDartString.AppendLine("\t}"); } serviceDartString.AppendLine(); foreach (var methodMap in MethodMapList) { serviceDartString.AppendLine(methodMap.GetMethodDartString()); } serviceDartString.AppendLine("}"); return serviceDartString.ToString(); } public List GetUserDefinedComplexReturnTypeList() { _userDefinedComplexReturnTypeList = new List(); foreach (var method in MethodMapList) { var veturnParameterModelType = method.ReturnParameterModelType; AddUserDefinedComplexReturnType(veturnParameterModelType); } return _userDefinedComplexReturnTypeList; } private void AddUserDefinedComplexReturnType(ModelType modelType) { if (modelType is UserDefinedModeType) { if (!_userDefinedComplexReturnTypeList.Contains(modelType.GetFlutterTypeName())) { _userDefinedComplexReturnTypeList.Add(modelType.GetFlutterTypeName()); } } else if (modelType is UserDefinedDerivedModelType) { if (!_userDefinedComplexReturnTypeList.Contains(modelType.GetFlutterTypeName())) { _userDefinedComplexReturnTypeList.Add(modelType.GetFlutterTypeName()); } } else if (modelType is UserDefinedGenericModelType userDefinedGenericComplexModelType) { if (!_userDefinedComplexReturnTypeList.Contains(userDefinedGenericComplexModelType.GetFlutterTypeName())) { _userDefinedComplexReturnTypeList.Add(userDefinedGenericComplexModelType.GetFlutterTypeName()); } var argumentModelType = userDefinedGenericComplexModelType.GenericArgumentModelType; AddUserDefinedComplexReturnType(argumentModelType); } else if (modelType is ListModelType listModelType) { var argumentModelType = listModelType.GenericArgumentModelType; AddUserDefinedComplexReturnType(argumentModelType); } else if (modelType is DictionaryModelType dictionaryModelType) { foreach (var argumentModelType in dictionaryModelType.GenericArgumentModelTypeList) { AddUserDefinedComplexReturnType(argumentModelType); } } } } }