Egret之ProtoBuf安装
安装ProtoBuf , 网络上的方法五花八门 . 但是很多都不是那么正规 . 自己通过NPM官网 (https://www.npmjs.com/package/@egret/protobuf), 总结了一套方法.
一 : 先要安装node.js 和 npm . 没有安装的 , 可以度娘,亦可以参考 :https://blog.51cto.com/aonaufly/1954296 Blog:"TypeScript 体验"
二:安装Protobuf基础库
npm install protobufjs@6.8.4 -g
npm install @egret/protobuf -g
① : protobufjs@6.8.4
② :@egret/protobuf
三 : 将ProtoBuf相关库注入到Egret项目之中
①,新建一个Egret(Eui)项目 (ProtobufNpmDemo) (各种方法可以建立 , 这里不讲了)
②,我们到项目目录里面(important)
③,在此资源管理器打开资源管理器
④,使用命令 :pb-egret add 注入ProtoBuf
我们再看看我们的项目 , 多了个protobuf文件夹
四 : 生成protobuf-bundles(实际是将proto文件JS化,有利于OOP思想)
ps : 可以看到目前的bundles文件夹中无任何的资源
①,新建test.proto资源(在protobuf\protofile中)
②,使用pb-egret generate命令
我们再看看bundles , 已经有文件了
这些都是根据test.proto生成的类
test.proto:
packageTest;messageLogin{requiredstringuserName=1;requiredstringpassword=2;optionalint32sex=3;requiredboolisFirstLogin=4;repeatedstringparam=5;}
生成的protobuf-bundles.d.ts(其一)如下:
typeLong=protobuf.Long;/**NamespaceTest.*/declarenamespaceTest{/**PropertiesofaLogin.*/interfaceILogin{/**LoginuserName*/userName:string;/**Loginpassword*/password:string;/**Loginsex*/sex?:(number|null);/**LoginisFirstLogin*/isFirstLogin:boolean;/**Loginparam*/param?:(string[]|null);}/**RepresentsaLogin.*/classLoginimplementsILogin{/***ConstructsanewLogin.*@param[properties]Propertiestoset*/constructor(properties?:Test.ILogin);/**LoginuserName.*/publicuserName:string;/**Loginpassword.*/publicpassword:string;/**Loginsex.*/publicsex:number;/**LoginisFirstLogin.*/publicisFirstLogin:boolean;/**Loginparam.*/publicparam:string[];/***CreatesanewLogininstanceusingthespecifiedproperties.*@param[properties]Propertiestoset*@returnsLogininstance*/publicstaticcreate(properties?:Test.ILogin):Test.Login;/***EncodesthespecifiedLoginmessage.Doesnotimplicitly{@linkTest.Login.verify|verify}messages.*@parammessageLoginmessageorplainobjecttoencode*@param[writer]Writertoencodeto*@returnsWriter*/publicstaticencode(message:Test.ILogin,writer?:protobuf.Writer):protobuf.Writer;/***EncodesthespecifiedLoginmessage,lengthdelimited.Doesnotimplicitly{@linkTest.Login.verify|verify}messages.*@parammessageLoginmessageorplainobjecttoencode*@param[writer]Writertoencodeto*@returnsWriter*/publicstaticencodeDelimited(message:Test.ILogin,writer?:protobuf.Writer):protobuf.Writer;/***DecodesaLoginmessagefromthespecifiedreaderorbuffer.*@paramreaderReaderorbuffertodecodefrom*@param[length]Messagelengthifknownbeforehand*@returnsLogin*@throws{Error}Ifthepayloadisnotareaderorvalidbuffer*@throws{protobuf.util.ProtocolError}Ifrequiredfieldsaremissing*/publicstaticdecode(reader:(protobuf.Reader|Uint8Array),length?:number):Test.Login;/***DecodesaLoginmessagefromthespecifiedreaderorbuffer,lengthdelimited.*@paramreaderReaderorbuffertodecodefrom*@returnsLogin*@throws{Error}Ifthepayloadisnotareaderorvalidbuffer*@throws{protobuf.util.ProtocolError}Ifrequiredfieldsaremissing*/publicstaticdecodeDelimited(reader:(protobuf.Reader|Uint8Array)):Test.Login;/***VerifiesaLoginmessage.*@parammessagePlainobjecttoverify*@returns`null`ifvalid,otherwisethereasonwhyitisnot*/publicstaticverify(message:{[k:string]:any}):(string|null);}}
五:运用
①,代码:
/***创建场景界面*Createsceneinterface*/protectedcreateGameScene():void{let$login:Test.ILogin=newTest.Login({userName:"Aoanufly",password:"123456",sex:1,isFirstLogin:false,param:["test","array","param"]});console.log(`loginName:${$login.userName}`);}
②,结果
补充 ---
① , Egret官方ProtoBuf:
https://www.cnblogs.com/gamedaybyday/p/9219946.html
②:利用命令将template.proto生成JS
pbjs -t static-module -w commonjs -o template.js template.proto
pbts -o template.d.ts template.js
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。