Windows下使用protobuf
下载windows下的protbuf
直接去protobuf在github下的连接【下载】。
选择windows下64位
下载后解压即可。
当然了github的资源有可能十分卡,这里提供了我上传的资源,下载连接
配置环境变量
计算机 右键 -》 属性 选择 【高级系统设置】
弹出对话框 选择 【环境变量】
弹出对话框后,找到Path 双击
出现以下对话框,点击 【新建】 输入您解压的zip包下的bin路径
通过cmd命令行来验证配置的环境变量是否成功:输入protoc 后显示提示的命令行,意味着配置成功。
使用protobuf生成java类
准备好proto文件GameMsgProtocol.proto,下载链接
输入以下命令
就能够获取到java文件,
如下我自己执行
已经生成的java文件
以下是部分代码演示
```java
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: GameMsgProtocol.proto
package com.lee.hero.story.msg;
public final class GameMsgProtocol {
private GameMsgProtocol() {}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistryLite registry) {
}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistry registry) {
registerAllExtensions(
(com.google.protobuf.ExtensionRegistryLite) registry);
}
/**
* <pre>
* 消息代号
* </pre>
*
* Protobuf enum {@code msg.MsgCode}
*/
public enum MsgCode
implements com.google.protobuf.ProtocolMessageEnum {
/**
* <code>USER_ENTRY_CMD = 0;</code>
*/
USER_ENTRY_CMD(0),
/**
* <code>USER_ENTRY_RESULT = 1;</code>
*/
USER_ENTRY_RESULT(1),
/**
* <code>WHO_ELSE_IS_HERE_CMD = 2;</code>
*/
WHO_ELSE_IS_HERE_CMD(2),
/**
* <code>WHO_ELSE_IS_HERE_RESULT = 3;</code>
*/
WHO_ELSE_IS_HERE_RESULT(3),
/**
* <code>USER_MOVE_TO_CMD = 4;</code>
*/
USER_MOVE_TO_CMD(4),
/**
* <code>USER_MOVE_TO_RESULT = 5;</code>
*/
USER_MOVE_TO_RESULT(5),
/**
* <code>USER_QUIT_RESULT = 6;</code>
*/
USER_QUIT_RESULT(6),
/**
* <code>USER_STOP_CMD = 7;</code>
*/
USER_STOP_CMD(7),
/**
* <code>USER_STOP_RESULT = 8;</code>
*/
USER_STOP_RESULT(8),
/**
* <code>USER_ATTK_CMD = 9;</code>
*/
USER_ATTK_CMD(9),
/**
* <code>USER_ATTK_RESULT = 10;</code>
*/
USER_ATTK_RESULT(10),
/**
* <code>USER_SUBTRACT_HP_RESULT = 11;</code>
*/
USER_SUBTRACT_HP_RESULT(11),
/**
* <code>USER_DIE_RESULT = 12;</code>
*/
USER_DIE_RESULT(12),
UNRECOGNIZED(-1),
;
/**
* <code>USER_ENTRY_CMD = 0;</code>
*/
public static final int USER_ENTRY_CMD_VALUE = 0;
/**
* <code>USER_ENTRY_RESULT = 1;</code>
*/
public static final int USER_ENTRY_RESULT_VALUE = 1;
/**
* <code>WHO_ELSE_IS_HERE_CMD = 2;</code>
*/
public static final int WHO_ELSE_IS_HERE_CMD_VALUE = 2;
/**
* <code>WHO_ELSE_IS_HERE_RESULT = 3;</code>
*/
public static final int WHO_ELSE_IS_HERE_RESULT_VALUE = 3;
/**
* <code>USER_MOVE_TO_CMD = 4;</code>
*/
public static final int USER_MOVE_TO_CMD_VALUE = 4;
/**
* <code>USER_MOVE_TO_RESULT = 5;</code>
*/
public static final int USER_MOVE_TO_RESULT_VALUE = 5;
/**
* <code>USER_QUIT_RESULT = 6;</code>
*/
public static final int USER_QUIT_RESULT_VALUE = 6;
/**
* <code>USER_STOP_CMD = 7;</code>
*/
public static final int USER_STOP_CMD_VALUE = 7;
/**
* <code>USER_STOP_RESULT = 8;</code>
*/
public static final int USER_STOP_RESULT_VALUE = 8;
/**
* <code>USER_ATTK_CMD = 9;</code>
*/
public static final int USER_ATTK_CMD_VALUE = 9;
/**
* <code>USER_ATTK_RESULT = 10;</code>
*/
public static final int USER_ATTK_RESULT_VALUE = 10;
/**
* <code>USER_SUBTRACT_HP_RESULT = 11;</code>
*/
public static final int USER_SUBTRACT_HP_RESULT_VALUE = 11;
/**
* <code>USER_DIE_RESULT = 12;</code>
*/
public static final int USER_DIE_RESULT_VALUE = 12;
public final int getNumber() {
if (this == UNRECOGNIZED) {
throw new java.lang.IllegalArgumentException(
"Can't get the number of an unknown enum value.");
}
return value;
}
/**
* @param value The numeric wire value of the corresponding enum entry.
* @return The enum associated with the given numeric wire value.
* @deprecated Use {@link #forNumber(int)} instead.
*/
@java.lang.Deprecated
public static MsgCode valueOf(int value) {
return forNumber(value);
}
/**
* @param value The numeric wire value of the corresponding enum entry.
* @return The enum associated with the given numeric wire value.
*/
public static MsgCode forNumber(int value) {
switch (value) {
case 0: return USER_ENTRY_CMD;
case 1: return USER_ENTRY_RESULT;
case 2: return WHO_ELSE_IS_HERE_CMD;
case 3: return WHO_ELSE_IS_HERE_RESULT;
case 4: return USER_MOVE_TO_CMD;
case 5: return USER_MOVE_TO_RESULT;
case 6: return USER_QUIT_RESULT;
case 7: return USER_STOP_CMD;
case 8: return USER_STOP_RESULT;
case 9: return USER_ATTK_CMD;
case 10: return USER_ATTK_RESULT;
case 11: return USER_SUBTRACT_HP_RESULT;
case 12: return USER_DIE_RESULT;
default: return null;
}
}
public static com.google.protobuf.Internal.EnumLiteMap<MsgCode>
internalGetValueMap() {
return internalValueMap;
}
private static final com.google.protobuf.Internal.EnumLiteMap<
MsgCode> internalValueMap =
new com.google.protobuf.Internal.EnumLiteMap<MsgCode>() {
public MsgCode findValueByNumber(int number) {
return MsgCode.forNumber(number);
}
};
public final com.google.protobuf.Descriptors.EnumValueDescriptor
getValueDescriptor() {
if (this == UNRECOGNIZED) {
throw new java.lang.IllegalStateException(
"Can't get the descriptor of an unrecognized enum value.");
}
return getDescriptor().getValues().get(ordinal());
}
public final com.google.protobuf.Descriptors.EnumDescriptor
getDescriptorForType() {
return getDescriptor();
}
public static final com.google.protobuf.Descriptors.EnumDescriptor
getDescriptor() {
return com.lee.hero.story.msg.GameMsgProtocol.getDescriptor().getEnumTypes().get(0);
}
private static final MsgCode[] VALUES = values();
public static MsgCode valueOf(
com.google.protobuf.Descriptors.EnumValueDescriptor desc) {
if (desc.getType() != getDescriptor()) {
throw new java.lang.IllegalArgumentException(
"EnumValueDescriptor is not for this type.");
}
if (desc.getIndex() == -1) {
return UNRECOGNIZED;
}
return VALUES[desc.getIndex()];
}
private final int value;
private MsgCode(int value) {
this.value = value;
}
// @@protoc_insertion_point(enum_scope:msg.MsgCode)
}
public interface UserEntryCmdOrBuilder extends
// @@protoc_insertion_point(interface_extends:msg.UserEntryCmd)
com.google.protobuf.MessageOrBuilder {
/**
* <pre>
* 用户 Id
* </pre>
*
* <code>uint32 userId = 1;</code>
* @return The userId.
*/
int getUserId();
/**
* <pre>
* 英雄形象
* </pre>
*
* <code>string heroAvatar = 2;</code>
* @return The heroAvatar.
*/
java.lang.String getHeroAvatar();
/**
* <pre>
* 英雄形象
* </pre>
*
* <code>string heroAvatar = 2;</code>
* @return The bytes for heroAvatar.
*/
com.google.protobuf.ByteString
getHeroAvatarBytes();
}
/**
* <pre>
*
* 用户入场
* /
* 指令
* </pre>
*
* Protobuf type {@code msg.UserEntryCmd}
*/
public static final class UserEntryCmd extends
com.google.protobuf.GeneratedMessageV3 implements
// @@protoc_insertion_point(message_implements:msg.UserEntryCmd)
UserEntryCmdOrBuilder {
private static final long serialVersionUID = 0L;
// Use UserEntryCmd.newBuilder() to construct.
private UserEntryCmd(com.google.protobuf.GeneratedMessageV3.Builder<?> builder) {
super(builder);
}
private UserEntryCmd() {
heroAvatar_ = "";
}
@java.lang.Override
@SuppressWarnings({"unused"})
protected java.lang.Object newInstance(
UnusedPrivateParameter unused) {
return new UserEntryCmd();
}
@java.lang.Override
public final com.google.protobuf.UnknownFieldSet
getUnknownFields() {
return this.unknownFields;
}
private UserEntryCmd(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
this();
if (extensionRegistry == null) {
throw new java.lang.NullPointerException();
}
com.google.protobuf.UnknownFieldSet.Builder unknownFields =
com.google.protobuf.UnknownFieldSet.newBuilder();
try {
boolean done = false;
while (!done) {
int tag = input.readTag();
switch (tag) {
case 0:
done = true;
break;
case 8: {
userId_ = input.readUInt32();
break;
}
case 18: {
java.lang.String s = input.readStringRequireUtf8();
heroAvatar_ = s;
break;
}
default: {
if (!parseUnknownField(
input, unknownFields, extensionRegistry, tag)) {
done = true;
}
break;
}
}
}
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
throw e.setUnfinishedMessage(this);
} catch (java.io.IOException e) {
throw new com.google.protobuf.InvalidProtocolBufferException(
e).setUnfinishedMessage(this);
} finally {
this.unknownFields = unknownFields.build();
makeExtensionsImmutable();
}
}
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return com.lee.hero.story.msg.GameMsgProtocol.internal_static_msg_UserEntryCmd_descriptor;
}
@java.lang.Override
protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internalGetFieldAccessorTable() {
return com.lee.hero.story.msg.GameMsgProtocol.internal_static_msg_UserEntryCmd_fieldAccessorTable
.ensureFieldAccessorsInitialized(
com.lee.hero.story.msg.GameMsgProtocol.UserEntryCmd.class, com.lee.hero.story.msg.GameMsgProtocol.UserEntryCmd.Builder.class);
}
public static final int USERID_FIELD_NUMBER = 1;
private int userId_;
/**
* <pre>
* 用户 Id
* </pre>
*
* <code>uint32 userId = 1;</code>
* @return The userId.
*/
@java.lang.Override
public int getUserId() {
return userId_;
}
public static final int HEROAVATAR_FIELD_NUMBER = 2;
private volatile java.lang.Object heroAvatar_;
/**
* <pre>
* 英雄形象
* </pre>
*
* <code>string heroAvatar = 2;</code>
* @return The heroAvatar.
*/
@java.lang.Override
public java.lang.String getHeroAvatar() {
java.lang.Object ref = heroAvatar_;
if (ref instanceof java.lang.String) {
return (java.lang.String) ref;
} else {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
java.lang.String s = bs.toStringUtf8();
heroAvatar_ = s;
return s;
}
}
/**
* <pre>
* 英雄形象
* </pre>
*
* <code>string heroAvatar = 2;</code>
* @return The bytes for heroAvatar.
*/
@java.lang.Override
public com.google.protobuf.ByteString
getHeroAvatarBytes() {
java.lang.Object ref = heroAvatar_;
if (ref instanceof java.lang.String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
heroAvatar_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
private byte memoizedIsInitialized = -1;
@java.lang.Override
public final boolean isInitialized() {
byte isInitialized = memoizedIsInitialized;
if (isInitialized == 1) return true;
if (isInitialized == 0) return false;
memoizedIsInitialized = 1;
return true;
}
@java.lang.Override
public void writeTo(com.google.protobuf.CodedOutputStream output)
throws java.io.IOException {
if (userId_ != 0) {
output.writeUInt32(1, userId_);
}
if (!getHeroAvatarBytes().isEmpty()) {
com.google.protobuf.GeneratedMessageV3.writeString(output, 2, heroAvatar_);
}
unknownFields.writeTo(output);
}
@java.lang.Override
public int getSerializedSize() {
int size = memoizedSize;
if (size != -1) return size;
size = 0;
if (userId_ != 0) {
size += com.google.protobuf.CodedOutputStream
.computeUInt32Size(1, userId_);
}
if (!getHeroAvatarBytes().isEmpty()) {
size += com.google.protobuf.GeneratedMessageV3.computeStringSize(2, heroAvatar_);
}
size += unknownFields.getSerializedSize();
memoizedSize = size;
return size;
}
@java.lang.Override
public boolean equals(final java.lang.Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof com.lee.hero.story.msg.GameMsgProtocol.UserEntryCmd)) {
return super.equals(obj);
}
com.lee.hero.story.msg.GameMsgProtocol.UserEntryCmd other = (com.lee.hero.story.msg.GameMsgProtocol.UserEntryCmd) obj;
if (getUserId()
!= other.getUserId()) return false;
if (!getHeroAvatar()
.equals(other.getHeroAvatar())) return false;
if (!unknownFields.equals(other.unknownFields)) return false;
return true;
}
......
}