当前位置:网站首页>Protobuf use
Protobuf use
2022-04-23 07:50:00 【[struggling]】
Protobuf Use
Protobuf The concept of not doing is here , Those who read this article should know , If you don't understand, you can check it yourself .
There are three steps
First step : Download configuration Protobuf Environmental Science
Protobuf github Download link
There are various versions of package, Remember to choose release, Find the right version according to your needs
What I use here is Protocol Buffers v3.0.0 Version 3.0.0 link
Drop down to find Assets The label expands as follows
I was in Windows Operation on the system , So I choose to download protoc-3.0.0-win32.zip Find after unzipping bin There's a... Under the folder protoc.exe, In environment variable path add to protoc.exe Location path , Then open the cmd Command line cd To protoc.exe In the directory , Then input protoc --version Can correctly display protobuf Version on OK 了
The second step : Use Protobuf Generate scripts for the corresponding language
Create a folder , Create a new... In the folder xxx.proto file , Edit as follows apc.proto
syntax = "proto3";
message ABC{
string name = 1;
string message_name = 2;
};
first line syntax = “proto3”; Specify that you are using proto3 grammar
message ABC Specify a class , The name of the class is ABC
*.proto You can also refer to each other , newly build ddd.proto Edit as follows
syntax = "proto3";
import "apc.proto";
message Npc
{
string damage = 1;
int32 range = 2;
ABC abc = 3;
};
import “apc.proto”; significance : stay ddd.proto Import abc.proto
Can be in ddd.proto Use in ABC The class
Start generating corresponding language Such as C++、C#、Java
Generate C++, Command line input
protoc.exe -I=F:\Protobuf\proto --cpp_out=F:\Protobuf\Out F:\Protobuf\proto\apc.proto
-I=F:\Protobuf\proto Appoint *.proto File path : Be careful :-I yes Horizontal bar and Capital I
–cpp_out=F:\Protobuf\Out Specifies that the exported is C++ file , Export path is F:\Protobuf\Out
Last F:\Protobuf\proto\apc.proto Indicates which one needs to be exported .proto file
The export result is a .h And a .cc file apc.pb.cc apc.pb.h
Generate C# , Command line input
protoc.exe -I=F:\Protobuf\proto --csharp_out=F:\Protobuf\Out F:\Protobuf\proto\apc.proto
The export result is a .cs file Apc.cs
Generate Java, Command line input
protoc.exe -I F:\Protobuf\proto --java_out=F:\Protobuf\Out F:\Protobuf\proto\apc.proto
The export result is a .java file Apc.java
Three times , The final generated file is as follows
The third step : How to use the generated in the project C++、Java、 and C#
I mainly use C# Language , Need to be in Assets Find below C# Corresponding Package : protobuf-csharp-3.0.0.zip perhaps protobuf-csharp-3.0.0.tar.gz, These two are the same , I downloaded it protobuf-csharp-3.0.0.zip
download protobuf-csharp-3.0.0.zip After decompression , Directory as follows
Find the file by Directory protobuf-3.0.0\csharp\src\Google.Protobuf.sln
And then use Visual Studio open Google.Protobuf.sln
, I use it VS2017
The following options Google.protobuf -> Right mouse button -> Generate
After successful generation, it will be in the following path :protobuf-3.0.0\csharp\src\Google.Protobuf\bin\Release\net45
Three files Google.Protobuf.dll ,Google.Protobuf.pdb , Google.Protobuf.xml
Put these three files into your own project , I was in Unity Use in , Need to put in Plugins In the folder
At the same time, we need to Generated in step 2 C# file Apc.cs
When it comes to Unity Just one script directory in the ,
C# The code is called as follows
void Start()
{
// Instantiate objects
ABC aBC = new ABC();
aBC.Name = "123";
aBC.MessageName = "ABC";
// Serialize the object to byte Array
byte[] byteData = Serialize(aBC);
// Deserialization , take byte Array Convert back to Class object
ABC ddd = Deserialize<ABC>(byteData);
Debug.LogError(ddd.Name + " " + ddd.MessageName);
}
// serialize : take protobuf Class object to byte Array
public static byte[] Serialize(IMessage msg)
{
using (MemoryStream ms = new MemoryStream())
{
CodedOutputStream outPut = new CodedOutputStream(ms);
outPut.WriteMessage(msg);
outPut.Flush();
byte[] byteData = ms.ToArray();
return byteData;
}
}
// Deserialization : take byte Array Convert to Corresponding protobuf Class object
public static T Deserialize<T>(byte[] byteData) where T : IMessage, new()
{
CodedInputStream stream = new CodedInputStream(byteData);
T msg = new T();
stream.ReadMessage(msg);
return msg;
}
Be careful : The third step is to download and compile the corresponding... According to your own language Package and Use .proto Generate corresponding language Script
版权声明
本文为[[struggling]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230627004677.html
边栏推荐
- 简单理解==和equals,String为什么可以不用new
- MySQL in window10 version does not work after setting remote access permission
- Xamarin版的C# SVG路径解析器
- SQL user-defined scalar value function that looks up relevant column values n times forward or backward according to a specified table name, column name and column value
- 系统与软件安全研究(二)
- Shapley Explanation Networks
- js之节点操作,为什么要学习节点操作
- Apache Hudi 如何加速传统的批处理模式?
- js案例之求最大值,反转数组,冒泡排序
- Online Safe Trajectory Generation For Quadrotors Using Fast Marching Method and Bernstein Basis Poly
猜你喜欢
向量到一个平面的投影向量
使用flask时代码无报错自动结束,无法保持连接,访问不了url。
Online Safe Trajectory Generation For Quadrotors Using Fast Marching Method and Bernstein Basis Poly
FUEL: Fast UAV Exploration using Incremental Frontier Structure and Hierarchical Planning
庄懂的TA笔记(七)<Lambert+Phong+Shadow+3EvColor+AO>
Apache Hudi 如何加速传统的批处理模式?
MySQL8. 0 installation / uninstallation tutorial [window10 version]
H5 local storage data sessionstorage, localstorage
平面定义-平面方程
SQL sorts string numbers
随机推荐
Event system (II) multicast events
二叉树的深度
Nodejs (I) event driven programming
SQL user-defined scalar value function that looks up relevant column values n times forward or backward according to a specified table name, column name and column value
C# 多个矩形围成的多边形标注位置的问题
庄懂的TA笔记(七)<Lambert+Phong+Shadow+3EvColor+AO>
C reads the registry
颜色转换公式大全及转换表格(31种)
Daily question | fear dominated by reverse linked list
系统与软件安全研究(一)
Install and configure Taobao image NPM (cnpm)
命令行参数传递库argparse的使用
将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小)
踩坑日记:Unable to process Jar entry [module-info.class]
Protobuf 使用
Scrapy modifies the time in the statistics at the end of the crawler as the current system time
根据某一指定的表名、列名及列值来向前或向后N条查相关列值的SQL自定义标量值函数
Moment. Format of format method function in JS
取得所有点列表中的最大值GetMaxPoint
Preliminary configuration of OpenGL super Dictionary (freeglut, glew, gltools, GLUT)