Contrib: Adjust protobuf generator for latest include cleaned up output

This commit is contained in:
Shauren
2023-01-13 18:25:52 +01:00
parent 3f1816d407
commit 68d079e4a3
5 changed files with 103 additions and 69 deletions

View File

@@ -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 <algorithm>\n" // for swap()
"#include <utility>\n" // for move()
"#include <utility>\n" // for swap() and move()
"\n"
"#include <google/protobuf/stubs/common.h>\n"
"#include <google/protobuf/stubs/once.h>\n"
@@ -354,11 +353,9 @@ void BnetFileGenerator::GenerateSource(pb::io::Printer* printer)
"#include <google/protobuf/wire_format.h>\n");
}
printer->Print("#include \"Log.h\"\n");
if (file_->service_count() > 0)
{
printer->Print("#include \"Errors.h\"\n");
printer->Print("#include \"BattlenetRpcErrorCodes.h\"\n");
}

View File

@@ -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<std::string, std::string> 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<void($output_type$ const*)> 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<void(MessageBuffer)> 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<void(ServiceBase*, uint32, ::google::protobuf::Message const*)> 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<void(ServiceBase*, uint32, ::google::protobuf::Message const*)>& 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<void(ServiceBase*, uint32, ::google::protobuf::Message const*)>& /*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<std::string, std::string> 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<void(ServiceBase*, uint32, ::google::protobuf::Message const*)> 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;

View File

@@ -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);

View File

@@ -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");
}
}

View File

@@ -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");
}
}