protected override void GenerateByEachFile(FileDescriptorProto protofile, CodeGeneratorResponse response) { bool genericClient; protofile.Options.CustomOptions.TryGetBool(DotBPEOptions.DISABLE_GENERIC_SERVICES_CLIENT, out genericClient); if (genericClient) { return; } if (protofile.Service == null || protofile.Service.Count <= 0) { return; } //生成文件头 StringBuilder sb = new StringBuilder(); sb.AppendLine("// Generated by the protocol buffer compiler. DO NOT EDIT!"); sb.AppendLine($"// source: {protofile.Name}"); //还可以生成注释 sb.AppendLine("#region Designer generated code"); sb.AppendLine(""); sb.AppendLine("using System; "); sb.AppendLine("using System.Threading.Tasks; "); sb.AppendLine("using DotBPE.Rpc; "); sb.AppendLine("using DotBPE.Protocol.Amp; "); sb.AppendLine("using DotBPE.Rpc.Exceptions; "); sb.AppendLine("using Google.Protobuf; "); sb.AppendLine(""); string ns = Utils.GetFileNamespace(protofile); sb.AppendLine("namespace " + ns + " {"); //生成代码 foreach (ServiceDescriptorProto t in protofile.Service) { t.Options.CustomOptions.TryGetBool(DotBPEOptions.DISABLE_GENERIC_SERVICES_CLIENT, out genericClient); if (genericClient) { continue; } sb.AppendLine(""); sb.AppendLine("//start for class " + t.Name + "Client"); GenerateServiceForClient(t, sb); sb.AppendLine("//end for class " + t.Name + "Client"); } sb.AppendLine("}"); sb.AppendLine("#endregion"); //生成文件 var nfile = new CodeGeneratorResponse.Types.File { Name = Utils.GetFileName(protofile.Name) + "Client.cs", Content = sb.ToString() }; response.File.Add(nfile); }
public void Process(CodeGeneratorRequest request, CodeGeneratorResponse response) { if (request.ProtoFile.Count == 0) { return; } string ns = ""; bool genericEnable = false; foreach (var protofile in request.ProtoFile) { protofile.Options.CustomOptions.TryGetBool(DotBPEOptions.GENERIC_OBJECTFACTORY, out genericEnable); if (genericEnable) { ns = Utils.GetFileNamespace(protofile); if (string.IsNullOrEmpty(ns)) { response.Error = protofile.Name + " don't set namespace"; return; } break; } } if (!genericEnable) { return; } StringBuilder sb = new StringBuilder(); sb.AppendLine("// Generated by the protocol buffer compiler. DO NOT EDIT!"); sb.AppendLine($"// source: ProtobufObjectfactory"); //还可以生成注释 sb.AppendLine("#region Designer generated code"); sb.AppendLine(""); sb.AppendLine("using System; "); sb.AppendLine("using System.Collections.Generic; "); sb.AppendLine("using System.Text; "); sb.AppendLine("using Google.Protobuf; "); sb.AppendLine("using Google.Protobuf.Reflection; "); sb.AppendLine(""); sb.AppendLine("namespace " + ns + " {"); //生成代码 sb.AppendLine("public class ProtobufObjectFactory {"); sb.AppendLine("public static IMessage GetRequestTemplate(int serviceId,int messageId)"); sb.AppendLine("{"); foreach (var protofile in request.ProtoFile) { try{ GenerateByEachFile(protofile, response, sb, 1); } catch (Exception ex) { response.Error += "file:" + protofile.Name + ":" + ex.Message + ex.StackTrace; } } sb.AppendLine("return null;"); sb.AppendLine("}"); sb.AppendLine("public static IMessage GetResponseTemplate(int serviceId,int messageId)"); sb.AppendLine("{"); foreach (var protofile in request.ProtoFile) { try{ GenerateByEachFile(protofile, response, sb, 2); } catch (Exception ex) { response.Error += "file:" + protofile.Name + ":" + ex.Message + ex.StackTrace; } } sb.AppendLine("return null;"); sb.AppendLine("}"); sb.AppendLine("}"); sb.AppendLine("}"); sb.AppendLine("#endregion"); //生成文件 var nfile = new CodeGeneratorResponse.Types.File { Name = "ProtobufObjectfactory.cs", Content = sb.ToString() }; response.File.Add(nfile); }
public void Process(CodeGeneratorRequest request, CodeGeneratorResponse response) { if (request.ProtoFile.Count == 0) { return; } string ns = ""; bool genericEnable = false; foreach (var protofile in request.ProtoFile) { protofile.Options.CustomOptions.TryGetBool(DotBPEOptions.GENERIC_HTTPAPIROUTES, out genericEnable); if (genericEnable) { ns = Utils.GetFileNamespace(protofile); if (string.IsNullOrEmpty(ns)) { response.Error = protofile.Name + " don't set namespace"; return; } break; } } if (!genericEnable) { return; } StringBuilder sb = new StringBuilder(); sb.AppendLine("// Generated by the protocol buffer compiler. DO NOT EDIT!"); sb.AppendLine($"// source: ProtobufObjectfactory"); //还可以生成注释 sb.AppendLine("#region Designer generated code"); sb.AppendLine(""); sb.AppendLine("using DotBPE.Protobuf;"); sb.AppendLine("using System.Collections.Generic;"); sb.AppendLine(""); sb.AppendLine("namespace " + ns + " {"); //生成代码 sb.AppendLine(" public static class HttpApiOptions {"); sb.AppendLine(" public static List<HttpApiOption> GetList()"); sb.AppendLine(" {"); sb.AppendLine(" var list = new List<HttpApiOption>();"); foreach (var protofile in request.ProtoFile) { try { ProcessByEachFile(protofile, sb); } catch (Exception ex) { response.Error += "file:" + protofile.Name + ":" + ex.Message + ex.StackTrace; } } sb.AppendLine(" return list;"); sb.AppendLine(" }"); sb.AppendLine(" }"); sb.AppendLine("}"); sb.AppendLine("#endregion"); //生成文件 var nfile = new CodeGeneratorResponse.Types.File { Name = "HttpApiOptions.cs", Content = sb.ToString() }; response.File.Add(nfile); }