c#、java、C++下编译protobuf文件

倾然丶 夕夏残阳落幕 提交于 2020-12-28 18:47:16

1)java

protoc.exe --java_out=./ game.proto 

使用

package org.example.pb;

public class Main {
    public static void main(String[] args) {
        BatteryData.EditProfileReq.Builder builder = BatteryData.EditProfileReq.newBuilder();
        builder.setUface(1);
    }
}

根据option java_package = "com.yjgis.test"; 和option java_outer_classname = "BatteryData"; 决定生成的文件名字和路径下。

而且即使这2行有,但是如果生成的是其它语言的文件,那么这2行对其它语言的生成也是没有影响的。

pom.xml

 <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>3.5.1</version>
        </dependency>

2)cpp

protoc.exe --cpp_out=./ game.proto 

生成game.pb.h  game.pb.cc这2个文件

3)game.proto

syntax = "proto2";

// 编译时生成的路径的名字,将会生成在com/yjgis/test这样的3级文件夹下。 这行对生成c++类型的无影响
option java_package = "org.example.pb"; 

// 生成的java的类的名字。 这行对生成c++类型的无影响
option java_outer_classname = "BatteryData";

enum Stype {
	INVALIDI_STYPE = 0;
	Auth = 1;
	System = 2;
	Logic = 3;
}
 
enum Cmd {
	INVALID_CMD = 0;
	
	eGuestLoginReq = 1;
	eGuestLoginRes = 2;

	eRelogin = 3;

	eUserLostConn = 4;

	eEditProfileReq = 5;
	eEditProfileRes = 6;

	eAccountUpgradeReq = 7;
	eAccountUpgradeRes = 8;

	eUnameLoginReq = 9;
	eUnameLoginRes = 10;

	eLoginOutReq = 11;
	eLoginOutRes = 12;

	eGetUgameInfoReq = 13;
	eGetUgameInfoRes = 14;

	eRecvLoginBonuesReq = 15;
	eRecvLoginBonuesRes = 16;

	eGetWorldRankUchipReq = 17;
	eGetWorldRankUchipRes = 18;

	eGetSysMsgReq = 19;
	eGetSysMsgRes = 20;

	eLoginLogicReq = 21;
	eLoginLogicRes = 22;

	eEnterZoneReq = 23;
	eEnterZoneRes = 24;

	eEnterMatch = 25;
	eUserArrived = 26;

	eExitMatchReq = 27;
	eExitMatchRes = 28;
	eUserExitMatch = 29;
	
	eGameStart = 30;

	eUdpTest = 31;
	eLogicFrame = 32;
	eNextFrameOpts = 33;
}

message GuestLoginReq {
	required string guest_key = 1;
}

message UnameLoginReq {
	required string uname = 1;
	required string upwd = 2;
}

message UserCenterInfo {
	required string unick = 1;
	required int32 uface = 2;
	required int32 usex = 3;
	required int32 uvip = 4;
	required int32 uid = 5;
}

message GuestLoginRes {
	required int32 status = 1;
	optional UserCenterInfo uinfo = 2;
}

message UnameLoginRes {
	required int32 status = 1;
	optional UserCenterInfo uinfo = 2;
}

message EditProfileReq {
	required string unick = 1;
	required int32 uface = 2;
	required int32 usex = 3;
}

message EditProfileRes {
	required int32 status = 1;
}

message AccountUpgradeReq {
	required string uname = 1;
	required string upwd_md5 = 2;
}

message AccountUpgradeRes {
	required int32 status = 1;
}

message LoginOutRes {
	required int32 status = 1;
}

message UserGameInfo {
    required int32 uchip = 1;
	required int32 uexp = 2;
	required int32 uvip = 3;
	required int32 uchip2 = 4;
	required int32 uchip3 = 5;
	required int32 udata1 = 6;
	required int32 udata2 = 7;
	required int32 udata3 = 8;

	required int32 bonues_status = 9;
	required int32 bonues = 10;
	required int32 days = 11;
}

message GetUgameInfoRes {
	required int32 status = 1;
	optional UserGameInfo uinfo = 2;
}

message RecvLoginBonuesRes {
	required int32 status = 1;
}

message WorldChipRankInfo {
	required string unick = 1;
	required int32 uface = 2;
	required int32 usex = 3;
	required int32 uvip = 4;
	required int32 uchip = 5;
}

message GetWorldRankUchipRes {
	required int32 status = 1;
	repeated WorldChipRankInfo rank_info = 2;
}

message GetSysMsgReq {
	required int32 ver_num = 1;
}

message GetSysMsgRes {
	required int32 status = 1;
	optional int32 ver_num = 2;
	repeated string sys_msgs = 3;
}

message LoginLogicRes {
	required int32 status = 1;
}

message EnterZoneReq {
	required int32 zid = 1;
}

message EnterZoneRes {
	required int32 status = 1;
}

message EnterMatch {
	required int32 zid = 1;
	required int32 matchid = 2;
	required int32 seatid = 3;
	required int32 side = 4;
}

message UserArrived {
	required string unick = 1;
	required int32 uface = 2;
	required int32 usex = 3;
	required int32 seatid = 4;
	required int32 side = 5;
}

message ExitMatchRes {
	required int32 status = 1;
}

message UserExitMatch {
	required int32 seatid = 1;
}

message PlayerMatchInfo{
	required int32 heroid = 1;
	required int32 seatid = 2;
	required int32 side = 3;
}

message GameStart {
	repeated PlayerMatchInfo players_match_info = 1;
}

message UdpTest{
	required string content = 1;
}

message LoginLogicReq{
	required string udp_ip = 1;
	required int32 udp_port = 2;
}

message OptionEvent{
	required int32 seatid = 1;
	required int32 opt_type = 2;
	required int32 x = 3;
	required int32 y = 4;
}

message FrameOpts{
	required int32 frameid = 1;
	repeated OptionEvent opts = 2;
}

message LogicFrame {
	required int32 frameid = 1;
	repeated FrameOpts unsync_frames = 2;
}

message NextFrameOpts{
	required int32 frameid = 1;
	required int32 zid = 2;
	required int32 matchid = 3;
	required int32 seatid = 4;
	repeated OptionEvent opts = 5;
}

4)编译用的程序就是一个单独的protoc.exe

 

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!