From 68d079e4a32ae525b236f956e8fc88c35eb8e24b Mon Sep 17 00:00:00 2001 From: Shauren Date: Fri, 13 Jan 2023 18:25:52 +0100 Subject: Contrib: Adjust protobuf generator for latest include cleaned up output --- contrib/protoc-bnet/BnetFileGenerator.cpp | 5 +- contrib/protoc-bnet/BnetServiceGenerator.cpp | 160 +++++++++++++-------- contrib/protoc-bnet/BnetServiceGenerator.h | 2 + .../google/protobuf/compiler/cpp/cpp_enum_field.cc | 3 +- .../protobuf/compiler/cpp/cpp_primitive_field.cc | 2 +- 5 files changed, 103 insertions(+), 69 deletions(-) diff --git a/contrib/protoc-bnet/BnetFileGenerator.cpp b/contrib/protoc-bnet/BnetFileGenerator.cpp index 8b27621e106..a14ac971da6 100644 --- a/contrib/protoc-bnet/BnetFileGenerator.cpp +++ b/contrib/protoc-bnet/BnetFileGenerator.cpp @@ -328,8 +328,7 @@ void BnetFileGenerator::GenerateSource(pb::io::Printer* printer) "#define INTERNAL_SUPPRESS_PROTOBUF_FIELD_DEPRECATION\n" "#include \"$basename$.pb.h\"\n" "\n" - "#include \n" // for swap() - "#include \n" // for move() + "#include \n" // for swap() and move() "\n" "#include \n" "#include \n" @@ -354,11 +353,9 @@ void BnetFileGenerator::GenerateSource(pb::io::Printer* printer) "#include \n"); } - printer->Print("#include \"Log.h\"\n"); if (file_->service_count() > 0) { - printer->Print("#include \"Errors.h\"\n"); printer->Print("#include \"BattlenetRpcErrorCodes.h\"\n"); } diff --git a/contrib/protoc-bnet/BnetServiceGenerator.cpp b/contrib/protoc-bnet/BnetServiceGenerator.cpp index 149eee8fd7c..37604a2f3a6 100644 --- a/contrib/protoc-bnet/BnetServiceGenerator.cpp +++ b/contrib/protoc-bnet/BnetServiceGenerator.cpp @@ -42,6 +42,10 @@ void BnetServiceGenerator::GenerateInterface(pb::io::Printer* printer) " public:\n" "\n" " explicit $classname$(bool use_original_hash);\n" + " $classname$($classname$ const&) = delete;\n" + " $classname$($classname$&&) = delete;\n" + " $classname$& operator=($classname$ const&) = delete;\n" + " $classname$& operator=($classname$&&) = delete;\n" " virtual ~$classname$();\n" "\n" "$original_hash$" @@ -79,16 +83,21 @@ void BnetServiceGenerator::GenerateInterface(pb::io::Printer* printer) printer->Print("// server methods --------------------------------------------------\n"); GenerateServerMethodSignatures(printer); + + printer->Outdent(); + + printer->Print( + "\n" + " private:\n"); + + printer->Indent(); + + GenerateServerMethodParserSignatures(printer); } printer->Outdent(); printer->Print(vars_, - "\n" - " private:\n" - " uint32 service_hash_;\n" - "\n" - " GOOGLE_DISALLOW_EVIL_CONSTRUCTORS($classname$);\n" "};\n"); } @@ -135,6 +144,23 @@ void BnetServiceGenerator::GenerateServerMethodSignatures(pb::io::Printer* print } } +void BnetServiceGenerator::GenerateServerMethodParserSignatures(pb::io::Printer* printer) +{ + for (int i = 0; i < descriptor_->method_count(); i++) + { + pb::MethodDescriptor const* method = descriptor_->method(i); + if (!method->options().HasExtension(Battlenet::method_options)) + continue; + + std::map sub_vars; + sub_vars["name"] = method->name(); + sub_vars["input_type"] = pbcpp::ClassName(method->input_type(), true); + sub_vars["output_type"] = pbcpp::ClassName(method->output_type(), true); + + printer->Print(sub_vars, "void ParseAndHandle$name$(uint32 token, uint32 methodId, MessageBuffer& buffer);\n"); + } +} + // =================================================================== void BnetServiceGenerator::GenerateDescriptorInitializer(pb::io::Printer* printer, int index) @@ -151,7 +177,7 @@ void BnetServiceGenerator::GenerateDescriptorInitializer(pb::io::Printer* printe void BnetServiceGenerator::GenerateImplementation(pb::io::Printer* printer) { printer->Print(vars_, - "$classname$::$classname$(bool use_original_hash) : service_hash_(use_original_hash ? OriginalHash::value : NameHash::value) {\n" + "$classname$::$classname$(bool use_original_hash) : ServiceBase(use_original_hash ? OriginalHash::value : NameHash::value) {\n" "}\n" "\n" "$classname$::~$classname$() {\n" @@ -170,13 +196,13 @@ void BnetServiceGenerator::GenerateImplementation(pb::io::Printer* printer) { GenerateServerCallMethod(printer); GenerateServerImplementations(printer); + GenerateServerMethodParserImplementations(printer); } else { printer->Print(vars_, - "void $classname$::CallServerMethod(uint32 token, uint32 methodId, MessageBuffer /*buffer*/) {\n" - " TC_LOG_ERROR(\"service.protobuf\", \"%s Server tried to call server method %u\",\n" - " GetCallerInfo().c_str(), methodId);\n" + "void $classname$::CallServerMethod(uint32 /*token*/, uint32 methodId, MessageBuffer /*buffer*/) {\n" + " LogDisallowedMethod(methodId);\n" "}\n" "\n"); } @@ -203,8 +229,7 @@ void BnetServiceGenerator::GenerateClientMethodImplementations(pb::io::Printer* { printer->Print(sub_vars, "void $classname$::$name$($input_type$ const* request, std::function responseCallback, bool client /*= false*/, bool server /*= false*/) {\n" - " TC_LOG_DEBUG(\"service.protobuf\", \"%s Server called client method $full_name$($input_type_name${ %s })\",\n" - " GetCallerInfo().c_str(), request->ShortDebugString().c_str());\n" + " LogCallClientMethod(\"$full_name$\", \"$input_type_name$\", request);\n" " std::function callback = [responseCallback](MessageBuffer buffer) -> void {\n" " $output_type$ response;\n" " if (response.ParseFromArray(buffer.GetReadPointer(), buffer.GetActiveSize()))\n" @@ -218,8 +243,7 @@ void BnetServiceGenerator::GenerateClientMethodImplementations(pb::io::Printer* { printer->Print(sub_vars, "void $classname$::$name$($input_type$ const* request, bool client /*= false*/, bool server /*= false*/) {\n" - " TC_LOG_DEBUG(\"service.protobuf\", \"%s Server called client method $full_name$($input_type_name${ %s })\",\n" - " GetCallerInfo().c_str(), request->ShortDebugString().c_str());\n" + " LogCallClientMethod(\"$full_name$\", \"$input_type_name$\", request);\n" " SendRequest(service_hash_, $method_id$ | (client ? 0x40000000 : 0) | (server ? 0x80000000 : 0), request);\n" "}\n" "\n"); @@ -250,55 +274,14 @@ void BnetServiceGenerator::GenerateServerCallMethod(pb::io::Printer* printer) sub_vars["output_type_name"] = method->output_type()->full_name(); printer->Print(sub_vars, - " case $method_id$: {\n" - " $input_type$ request;\n" - " if (!request.ParseFromArray(buffer.GetReadPointer(), buffer.GetActiveSize())) {\n" - " TC_LOG_DEBUG(\"service.protobuf\", \"%s Failed to parse request for $full_name$ server method call.\", GetCallerInfo().c_str());\n" - " SendResponse(service_hash_, methodId, token, ERROR_RPC_MALFORMED_REQUEST);\n" - " return;\n" - " }\n" - ); - - if (method->output_type()->name() != "NO_RESPONSE") - { - printer->Print(sub_vars, - " TC_LOG_DEBUG(\"service.protobuf\", \"%s Client called server method $full_name$($input_type_name${ %s }).\",\n" - " GetCallerInfo().c_str(), request.ShortDebugString().c_str());\n" - " std::function continuation = [token, methodId](ServiceBase* service, uint32 status, ::google::protobuf::Message const* response)\n" - " {\n" - " ASSERT(response->GetDescriptor() == $output_type$::descriptor());\n" - " $classname$* self = static_cast<$classname$*>(service);\n" - " TC_LOG_DEBUG(\"service.protobuf\", \"%s Client called server method $full_name$() returned $output_type_name${ %s } status %u.\",\n" - " self->GetCallerInfo().c_str(), response->ShortDebugString().c_str(), status);\n" - " if (!status)\n" - " self->SendResponse(self->service_hash_, methodId, token, response);\n" - " else\n" - " self->SendResponse(self->service_hash_, methodId, token, status);\n" - " };\n" - " $output_type$ response;\n" - " uint32 status = Handle$name$(&request, &response, continuation);\n" - " if (continuation)\n" - " continuation(this, status, &response);\n" - ); - } - else - { - printer->Print(sub_vars, - " uint32 status = Handle$name$(&request);\n" - " TC_LOG_DEBUG(\"service.protobuf\", \"%s Client called server method $full_name$($input_type_name${ %s }) status %u.\",\n" - " GetCallerInfo().c_str(), request.ShortDebugString().c_str(), status);\n" - " if (status)\n" - " SendResponse(service_hash_, methodId, token, status);\n"); - } - - printer->Print(sub_vars, - " break;\n" - " }\n"); + " case $method_id$:\n" + " ParseAndHandle$name$(token, methodId, buffer);\n" + " break;\n"); } printer->Print(vars_, " default:\n" - " TC_LOG_ERROR(\"service.protobuf\", \"Bad method id %u.\", methodId);\n" + " LogInvalidMethod(methodId);\n" " SendResponse(service_hash_, methodId, token, ERROR_RPC_INVALID_METHOD);\n" " break;\n" " }\n" @@ -323,9 +306,8 @@ void BnetServiceGenerator::GenerateServerImplementations(pb::io::Printer* printe if (method->output_type()->name() != "NO_RESPONSE") { - printer->Print(sub_vars, "uint32 $classname$::Handle$name$($input_type$ const* request, $output_type$* response, std::function& continuation) {\n" - " TC_LOG_ERROR(\"service.protobuf\", \"%s Client tried to call not implemented method $full_name$({ %s })\",\n" - " GetCallerInfo().c_str(), request->ShortDebugString().c_str());\n" + printer->Print(sub_vars, "uint32 $classname$::Handle$name$($input_type$ const* request, $output_type$* /*response*/, std::function& /*continuation*/) {\n" + " LogUnimplementedServerMethod(\"$full_name$\", request);\n" " return ERROR_RPC_NOT_IMPLEMENTED;\n" "}\n" "\n"); @@ -333,8 +315,7 @@ void BnetServiceGenerator::GenerateServerImplementations(pb::io::Printer* printe else { printer->Print(sub_vars, "uint32 $classname$::Handle$name$($input_type$ const* request) {\n" - " TC_LOG_ERROR(\"service.protobuf\", \"%s Client tried to call not implemented method $full_name$({ %s })\",\n" - " GetCallerInfo().c_str(), request->ShortDebugString().c_str());\n" + " LogUnimplementedServerMethod(\"$full_name$\", request);\n" " return ERROR_RPC_NOT_IMPLEMENTED;\n" "}\n" "\n"); @@ -342,6 +323,59 @@ void BnetServiceGenerator::GenerateServerImplementations(pb::io::Printer* printe } } +void BnetServiceGenerator::GenerateServerMethodParserImplementations(pb::io::Printer* printer) +{ + for (int i = 0; i < descriptor_->method_count(); i++) + { + pb::MethodDescriptor const* method = descriptor_->method(i); + if (!method->options().HasExtension(Battlenet::method_options)) + continue; + + std::map sub_vars; + sub_vars["classname"] = vars_["classname"]; + sub_vars["name"] = method->name(); + sub_vars["full_name"] = descriptor_->name() + "." + method->name(); + sub_vars["input_type"] = pbcpp::ClassName(method->input_type(), true); + sub_vars["output_type"] = pbcpp::ClassName(method->output_type(), true); + sub_vars["input_type_name"] = method->input_type()->full_name(); + sub_vars["output_type_name"] = method->output_type()->full_name(); + + printer->Print(sub_vars, + "void $classname$::ParseAndHandle$name$(uint32 token, uint32 methodId, MessageBuffer& buffer) {\n" + " $input_type$ request;\n" + " if (!request.ParseFromArray(buffer.GetReadPointer(), buffer.GetActiveSize())) {\n" + " LogFailedParsingRequest(\"$full_name$\");\n" + " SendResponse(service_hash_, methodId, token, ERROR_RPC_MALFORMED_REQUEST);\n" + " return;\n" + " }\n" + ); + + if (method->output_type()->name() != "NO_RESPONSE") + { + printer->Print(sub_vars, + " LogCallServerMethod(\"$full_name$\", \"$input_type_name$\", &request);\n" + " std::function continuation = CreateServerContinuation(token, methodId, \"$full_name$\", $output_type$::descriptor());\n" + " $output_type$ response;\n" + " uint32 status = Handle$name$(&request, &response, continuation);\n" + " if (continuation)\n" + " continuation(this, status, &response);\n" + ); + } + else + { + printer->Print(sub_vars, + " uint32 status = Handle$name$(&request);\n" + " LogCallServerMethod(\"$full_name$\", \"$input_type_name$\", &request);\n" + " if (status)\n" + " SendResponse(service_hash_, methodId, token, status);\n"); + } + + printer->Print(sub_vars, + "}\n" + "\n"); + } +} + std::uint32_t BnetServiceGenerator::HashServiceName(std::string const& name) { std::uint32_t hash = 0x811C9DC5; diff --git a/contrib/protoc-bnet/BnetServiceGenerator.h b/contrib/protoc-bnet/BnetServiceGenerator.h index 2debe8d5436..ea50bbd92e0 100644 --- a/contrib/protoc-bnet/BnetServiceGenerator.h +++ b/contrib/protoc-bnet/BnetServiceGenerator.h @@ -66,6 +66,7 @@ private: // Prints signatures for all methods in the void GenerateClientMethodSignatures(pb::io::Printer* printer); void GenerateServerMethodSignatures(pb::io::Printer* printer); + void GenerateServerMethodParserSignatures(pb::io::Printer* printer); // Source file stuff. @@ -74,6 +75,7 @@ private: // Generate the CallMethod() method of the service. void GenerateServerCallMethod(pb::io::Printer* printer); void GenerateServerImplementations(pb::io::Printer* printer); + void GenerateServerMethodParserImplementations(pb::io::Printer* printer); std::uint32_t HashServiceName(std::string const& name); diff --git a/contrib/protoc-bnet/google/protobuf/compiler/cpp/cpp_enum_field.cc b/contrib/protoc-bnet/google/protobuf/compiler/cpp/cpp_enum_field.cc index ea15fd74744..de39a9fc54f 100644 --- a/contrib/protoc-bnet/google/protobuf/compiler/cpp/cpp_enum_field.cc +++ b/contrib/protoc-bnet/google/protobuf/compiler/cpp/cpp_enum_field.cc @@ -285,7 +285,8 @@ GenerateSwappingCode(io::Printer* printer) const { void RepeatedEnumFieldGenerator:: GenerateConstructorCode(io::Printer* printer) const { - if (descriptor_->options().packed()) { + if (descriptor_->options().packed() + && HasGeneratedMethods(descriptor_->file())) { printer->Print(variables_, "_$name$_cached_byte_size_ = 0;\n"); } } diff --git a/contrib/protoc-bnet/google/protobuf/compiler/cpp/cpp_primitive_field.cc b/contrib/protoc-bnet/google/protobuf/compiler/cpp/cpp_primitive_field.cc index 244a7f178d9..3e2cf0a50ce 100644 --- a/contrib/protoc-bnet/google/protobuf/compiler/cpp/cpp_primitive_field.cc +++ b/contrib/protoc-bnet/google/protobuf/compiler/cpp/cpp_primitive_field.cc @@ -335,7 +335,7 @@ GenerateSwappingCode(io::Printer* printer) const { void RepeatedPrimitiveFieldGenerator:: GenerateConstructorCode(io::Printer* printer) const { - if (descriptor_->options().packed()) { + if (descriptor_->options().packed() && HasGeneratedMethods(descriptor_->file())) { printer->Print(variables_, "_$name$_cached_byte_size_ = 0;\n"); } } -- cgit v1.2.3