[MMO 컨텐츠 구현] 2-1. 서버 연동: Google Protocol Buffer (Protobuf)

scarleter99·2023년 10월 29일

Google Protocol Buffer (Protobuf)

  • Protocol Buffer는 구글이 개발한 언어 중립적이고 확장 가능한 데이터 직렬화 형식이다.
  • .proto 파일을 정의하기만 하면 각 언어로 컴파일이 가능하다.
  • 여러 언어를 복합적으로 사용하는 프로젝트에서 유용하다.
  1. Protobuf3 컴파일러를 다운로드한다.
  2. 패킷 정보가 정의된 .proto 파일을 생성한다.
  3. .proto 파일을 컴파일하여 패킷이 정의된 .cs 파일을 생성한다.
  4. NuGet 패키지 관리에서 Protobuf 라이브러리를 Server와 PacketGenerator 프로젝트에 추가한다.
  5. Server에 설치된 Protobuf 라이브러리를 Unity 프로젝트로 복사한다.

.proto 파일

  • .cs파일에 패킷 class로 생성할 패킷 정보를 정의한다.

📄 Protocol.proto

syntax = "proto3";

package Protocol;
option csharp_namespace = "Google.Protobuf.Protocol";

enum MsgId {
  S_ENTER_GAME = 0;
  S_LEAVE_GAME = 1;
  S_SPAWN = 2;
  S_DESPAWN = 3;
  C_MOVE = 4;
  S_MOVE = 5;
  C_SKILL = 6;
  S_SKILL = 7;
  S_CHANGE_HP = 8;
  S_DIE = 9;
}

enum CreatureState {
  IDLE = 0;
  MOVING = 1;
  SKILL = 2;
  DEAD = 3;
}

enum MoveDir {
  UP = 0;
  DOWN = 1;
  LEFT = 2;
  RIGHT = 3;
}

enum GameObjectType {
  NONE = 0;
  PLAYER = 1;
  MONSTER = 2;
  PROJECTILE = 3;
}

enum SkillType {
  SKILL_NONE = 0;
  SKILL_AUTO = 1;
  SKILL_PROJECTILE = 2;
}

message S_EnterGame {
  ObjectInfo player = 1;
}

message S_LeaveGame {
}

message S_Spawn {
  repeated ObjectInfo objects = 1;
}

message S_Despawn {
  repeated int32 objectIds = 1;
}

message C_Move {
  PositionInfo posInfo = 1;
}

message S_Move {
  int32 objectId = 1;
  PositionInfo posInfo = 2;
}

message C_Skill {
  SkillInfo info = 1;
}

message S_Skill {
  int32 objectId = 1;
  SkillInfo info = 2;
}

message S_ChangeHp {
  int32 objectId = 1;
  int32 hp = 2;
}

message S_Die {
  int32 objectId = 1;
  int32 attackerId = 2;
}

message ObjectInfo {
  int32 objectId = 1;
  string name = 2;
  PositionInfo posInfo = 3;
  StatInfo statInfo = 4;
}

message PositionInfo {
  CreatureState state = 1;
  MoveDir moveDir = 2;
  int32 posX = 3;
  int32 posY = 4;
}

message StatInfo {
  int32 level = 1;
  int32 hp = 2;
  int32 maxHp = 3;
  int32 attack = 4;
  float speed = 5;
  int32 totalExp = 6;
}

message SkillInfo {
  int32 skillId = 1;
}

.bat 파일

protoc.exe -I=./ --csharp_out=./ ./Protocol.proto 
IF ERRORLEVEL 1 PAUSE

START ../../../Server/PacketGenerator/bin/PacketGenerator.exe ./Protocol.proto
XCOPY /Y Protocol.cs "../../../Client/Assets/Scripts/Packet"
XCOPY /Y Protocol.cs "../../../Server/Server/Packet"
XCOPY /Y ClientPacketManager.cs "../../../Client/Assets/Scripts/Packet"
XCOPY /Y ServerPacketManager.cs "../../../Server/Server/Packet"

0개의 댓글