天天看點

Egret之ProtoBuf安裝

安裝ProtoBuf , 網絡上的方法五花八門 . 但是很多都不是那麼正規 . 自己通過NPM官網 (https://www.npmjs.com/package/@egret/protobuf) , 總結了一套方法.

一 : 先要安裝node.js 和 npm . 沒有安裝的 , 可以度娘,亦可以參考 : http://blog.51cto.com/aonaufly/1954296    Blog:"TypeScript 體驗"

Egret之ProtoBuf安裝

二:安裝Protobuf基礎庫

npm install [email protected] -g

npm install @egret/protobuf -g

① : [email protected]

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