温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Egret之ProtoBuf安装

发布时间:2020-06-18 13:51:29 来源:网络 阅读:4276 作者:Aonaufly 栏目:移动开发

安装ProtoBuf , 网络上的方法五花八门 . 但是很多都不是那么正规 . 自己通过NPM官网 (https://www.npmjs.com/package/@egret/protobuf) , 总结了一套方法.

一 : 先要安装node.js 和 npm . 没有安装的 , 可以度娘,亦可以参考 : https://blog.51cto.com/aonaufly/1954296    Blog:"TypeScript 体验"

Egret之ProtoBuf安装

二:安装Protobuf基础库

npm install protobufjs@6.8.4 -g

npm install @egret/protobuf -g

① : protobufjs@6.8.4

Egret之ProtoBuf安装

② : @egret/protobuf

Egret之ProtoBuf安装

三 : 将ProtoBuf相关库注入到Egret项目之中

①,新建一个Egret(Eui)项目  (ProtobufNpmDemo)  (各种方法可以建立 , 这里不讲了)

②,我们到项目目录里面(important)

Egret之ProtoBuf安装

③,在此资源管理器打开资源管理器

Egret之ProtoBuf安装

④,使用命令 : pb-egret add 注入ProtoBuf

Egret之ProtoBuf安装

我们再看看我们的项目 , 多了个protobuf文件夹

Egret之ProtoBuf安装

四 : 生成protobuf-bundles(实际是将proto文件JS化,有利于OOP思想)

ps : 可以看到目前的bundles文件夹中无任何的资源

①,新建test.proto资源(在protobuf\protofile中)

②,使用pb-egret generate命令

Egret之ProtoBuf安装

我们再看看bundles , 已经有文件了

Egret之ProtoBuf安装

这些都是根据test.proto生成的类

test.proto:

package Test;

message Login{
    required string userName = 1;
    required string password = 2;
    optional int32 sex = 3;
    required bool isFirstLogin = 4;
    repeated string param = 5;
}

生成的protobuf-bundles.d.ts(其一)如下:

type Long = protobuf.Long;

/** Namespace Test. */
declare namespace Test {

    /** Properties of a Login. */
    interface ILogin {

        /** Login userName */
        userName: string;

        /** Login password */
        password: string;

        /** Login sex */
        sex?: (number|null);

        /** Login isFirstLogin */
        isFirstLogin: boolean;

        /** Login param */
        param?: (string[]|null);
    }

    /** Represents a Login. */
    class Login implements ILogin {

        /**
         * Constructs a new Login.
         * @param [properties] Properties to set
         */
        constructor(properties?: Test.ILogin);

        /** Login userName. */
        public userName: string;

        /** Login password. */
        public password: string;

        /** Login sex. */
        public sex: number;

        /** Login isFirstLogin. */
        public isFirstLogin: boolean;

        /** Login param. */
        public param: string[];

        /**
         * Creates a new Login instance using the specified properties.
         * @param [properties] Properties to set
         * @returns Login instance
         */
        public static create(properties?: Test.ILogin): Test.Login;

        /**
         * Encodes the specified Login message. Does not implicitly {@link Test.Login.verify|verify} messages.
         * @param message Login message or plain object to encode
         * @param [writer] Writer to encode to
         * @returns Writer
         */
        public static encode(message: Test.ILogin, writer?: protobuf.Writer): protobuf.Writer;

        /**
         * Encodes the specified Login message, length delimited. Does not implicitly {@link Test.Login.verify|verify} messages.
         * @param message Login message or plain object to encode
         * @param [writer] Writer to encode to
         * @returns Writer
         */
        public static encodeDelimited(message: Test.ILogin, writer?: protobuf.Writer): protobuf.Writer;

        /**
         * Decodes a Login message from the specified reader or buffer.
         * @param reader Reader or buffer to decode from
         * @param [length] Message length if known beforehand
         * @returns Login
         * @throws {Error} If the payload is not a reader or valid buffer
         * @throws {protobuf.util.ProtocolError} If required fields are missing
         */
        public static decode(reader: (protobuf.Reader|Uint8Array), length?: number): Test.Login;

        /**
         * Decodes a Login message from the specified reader or buffer, length delimited.
         * @param reader Reader or buffer to decode from
         * @returns Login
         * @throws {Error} If the payload is not a reader or valid buffer
         * @throws {protobuf.util.ProtocolError} If required fields are missing
         */
        public static decodeDelimited(reader: (protobuf.Reader|Uint8Array)): Test.Login;

        /**
         * Verifies a Login message.
         * @param message Plain object to verify
         * @returns `null` if valid, otherwise the reason why it is not
         */
        public static verify(message: { [k: string]: any }): (string|null);
    }
}

五:运用

①,代码:

    /**
     * 创建场景界面
     * Create scene interface
     */
    protected createGameScene(): void {
        let $login : Test.ILogin = new Test.Login({userName:"Aoanufly",password:"123456", sex:1, isFirstLogin:false, param:["test", "array", "param"]});
        console.log(`loginName : ${$login.userName}`);
    }

②,结果

Egret之ProtoBuf安装


补充 --- 

① , 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


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI