首页 > 其他分享 >Cocos Creator之MAC平台protobufjs

Cocos Creator之MAC平台protobufjs

时间:2023-11-15 23:31:37浏览次数:34  
标签:Cocos protobuf object param awesomepackage MAC message AwesomeMessage protobufjs

一,环境

MAC

二, 安装brew

1,命令(不必要)

/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

2,安装

2.1,命令

sudo npm install -g [email protected]

2.2,安装位置

/usr/local/lib/node_modules/protobufjs/dist

三,准备一个proto测试文件(test.proto)

package awesomepackage;
// 指定你正在使用 proto3 语法
// 若不设置,protocol buffer编译器会默认为您使用的是 proto2 
syntax = "proto3";
message AwesomeMessage {
    string awesome_field = 1;
}

四,开始生成

1, 进入到proto路径

Cocos Creator之MAC平台protobufjs_.d.ts

2,命令

sudo pbjs -t static-module -w commonjs -o test.js test.proto

     3, 生成的test.js

/*eslint-disable block-scoped-var, no-redeclare, no-control-regex, no-prototype-builtins*/
"use strict";

var $protobuf = require("protobufjs/minimal");

// Common aliases
var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;

// Exported root namespace
var $root = $protobuf.roots["default"] || ($protobuf.roots["default"] = {});

$root.awesomepackage = (function() {

    /**
     * Namespace awesomepackage.
     * @exports awesomepackage
     * @namespace
     */
    var awesomepackage = {};

    awesomepackage.AwesomeMessage = (function() {

        /**
         * Properties of an AwesomeMessage.
         * @typedef awesomepackage.AwesomeMessage$Properties
         * @type {Object}
         * @property {string} [awesomeField] AwesomeMessage awesomeField.
         */

        /**
         * Constructs a new AwesomeMessage.
         * @exports awesomepackage.AwesomeMessage
         * @constructor
         * @param {awesomepackage.AwesomeMessage$Properties=} [properties] Properties to set
         */
        function AwesomeMessage(properties) {
            if (properties)
                for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i)
                    this[keys[i]] = properties[keys[i]];
        }

        /**
         * AwesomeMessage awesomeField.
         * @type {string|undefined}
         */
        AwesomeMessage.prototype.awesomeField = "";

        /**
         * Creates a new AwesomeMessage instance using the specified properties.
         * @param {awesomepackage.AwesomeMessage$Properties=} [properties] Properties to set
         * @returns {awesomepackage.AwesomeMessage} AwesomeMessage instance
         */
        AwesomeMessage.create = function create(properties) {
            return new AwesomeMessage(properties);
        };

        /**
         * Encodes the specified AwesomeMessage message. Does not implicitly {@link awesomepackage.AwesomeMessage.verify|verify} messages.
         * @param {awesomepackage.AwesomeMessage$Properties} message AwesomeMessage message or plain object to encode
         * @param {$protobuf.Writer} [writer] Writer to encode to
         * @returns {$protobuf.Writer} Writer
         */
        AwesomeMessage.encode = function encode(message, writer) {
            if (!writer)
                writer = $Writer.create();
            if (message.awesomeField != null && message.hasOwnProperty("awesomeField"))
                writer.uint32(/* id 1, wireType 2 =*/10).string(message.awesomeField);
            return writer;
        };

        /**
         * Encodes the specified AwesomeMessage message, length delimited. Does not implicitly {@link awesomepackage.AwesomeMessage.verify|verify} messages.
         * @param {awesomepackage.AwesomeMessage$Properties} message AwesomeMessage message or plain object to encode
         * @param {$protobuf.Writer} [writer] Writer to encode to
         * @returns {$protobuf.Writer} Writer
         */
        AwesomeMessage.encodeDelimited = function encodeDelimited(message, writer) {
            return this.encode(message, writer).ldelim();
        };

        /**
         * Decodes an AwesomeMessage message from the specified reader or buffer.
         * @param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from
         * @param {number} [length] Message length if known beforehand
         * @returns {awesomepackage.AwesomeMessage} AwesomeMessage
         * @throws {Error} If the payload is not a reader or valid buffer
         * @throws {$protobuf.util.ProtocolError} If required fields are missing
         */
        AwesomeMessage.decode = function decode(reader, length) {
            if (!(reader instanceof $Reader))
                reader = $Reader.create(reader);
            var end = length === undefined ? reader.len : reader.pos + length, message = new $root.awesomepackage.AwesomeMessage();
            while (reader.pos < end) {
                var tag = reader.uint32();
                switch (tag >>> 3) {
                case 1:
                    message.awesomeField = reader.string();
                    break;
                default:
                    reader.skipType(tag & 7);
                    break;
                }
            }
            return message;
        };

        /**
         * Decodes an AwesomeMessage message from the specified reader or buffer, length delimited.
         * @param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from
         * @returns {awesomepackage.AwesomeMessage} AwesomeMessage
         * @throws {Error} If the payload is not a reader or valid buffer
         * @throws {$protobuf.util.ProtocolError} If required fields are missing
         */
        AwesomeMessage.decodeDelimited = function decodeDelimited(reader) {
            if (!(reader instanceof $Reader))
                reader = $Reader(reader);
            return this.decode(reader, reader.uint32());
        };

        /**
         * Verifies an AwesomeMessage message.
         * @param {Object.<string,*>} message Plain object to verify
         * @returns {?string} `null` if valid, otherwise the reason why it is not
         */
        AwesomeMessage.verify = function verify(message) {
            if (typeof message !== "object" || message === null)
                return "object expected";
            if (message.awesomeField != null)
                if (!$util.isString(message.awesomeField))
                    return "awesomeField: string expected";
            return null;
        };

        /**
         * Creates an AwesomeMessage message from a plain object. Also converts values to their respective internal types.
         * @param {Object.<string,*>} object Plain object
         * @returns {awesomepackage.AwesomeMessage} AwesomeMessage
         */
        AwesomeMessage.fromObject = function fromObject(object) {
            if (object instanceof $root.awesomepackage.AwesomeMessage)
                return object;
            var message = new $root.awesomepackage.AwesomeMessage();
            if (object.awesomeField != null)
                message.awesomeField = String(object.awesomeField);
            return message;
        };

        /**
         * Creates an AwesomeMessage message from a plain object. Also converts values to their respective internal types.
         * This is an alias of {@link awesomepackage.AwesomeMessage.fromObject}.
         * @function
         * @param {Object.<string,*>} object Plain object
         * @returns {awesomepackage.AwesomeMessage} AwesomeMessage
         */
        AwesomeMessage.from = AwesomeMessage.fromObject;

        /**
         * Creates a plain object from an AwesomeMessage message. Also converts values to other types if specified.
         * @param {awesomepackage.AwesomeMessage} message AwesomeMessage
         * @param {$protobuf.ConversionOptions} [options] Conversion options
         * @returns {Object.<string,*>} Plain object
         */
        AwesomeMessage.toObject = function toObject(message, options) {
            if (!options)
                options = {};
            var object = {};
            if (options.defaults)
                object.awesomeField = "";
            if (message.awesomeField != null && message.hasOwnProperty("awesomeField"))
                object.awesomeField = message.awesomeField;
            return object;
        };

        /**
         * Creates a plain object from this AwesomeMessage message. Also converts values to other types if specified.
         * @param {$protobuf.ConversionOptions} [options] Conversion options
         * @returns {Object.<string,*>} Plain object
         */
        AwesomeMessage.prototype.toObject = function toObject(options) {
            return this.constructor.toObject(this, options);
        };

        /**
         * Converts this AwesomeMessage to JSON.
         * @returns {Object.<string,*>} JSON object
         */
        AwesomeMessage.prototype.toJSON = function toJSON() {
            return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
        };

        return AwesomeMessage;
    })();

    return awesomepackage;
})();

module.exports = $root;

3.1, 修改test.js的内容

// var $protobuf = require("protobufjs/minimal");
var $protobuf = protobuf

五, 生成.d.ts文件

1,命令

sudo pbts -o test.d.ts .js

2, test.d.ts的内容

import * as $protobuf from "protobufjs";

/**
 * Namespace awesomepackage.
 * @exports awesomepackage
 * @namespace
 */
export namespace awesomepackage {

    type AwesomeMessage$Properties = {
        awesomeField?: string;
    };

    /**
     * Constructs a new AwesomeMessage.
     * @exports awesomepackage.AwesomeMessage
     * @constructor
     * @param {awesomepackage.AwesomeMessage$Properties=} [properties] Properties to set
     */
    class AwesomeMessage {

        /**
         * Constructs a new AwesomeMessage.
         * @exports awesomepackage.AwesomeMessage
         * @constructor
         * @param {awesomepackage.AwesomeMessage$Properties=} [properties] Properties to set
         */
        constructor(properties?: awesomepackage.AwesomeMessage$Properties);

        /**
         * AwesomeMessage awesomeField.
         * @type {string|undefined}
         */
        public awesomeField?: string;

        /**
         * Creates a new AwesomeMessage instance using the specified properties.
         * @param {awesomepackage.AwesomeMessage$Properties=} [properties] Properties to set
         * @returns {awesomepackage.AwesomeMessage} AwesomeMessage instance
         */
        public static create(properties?: awesomepackage.AwesomeMessage$Properties): awesomepackage.AwesomeMessage;

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

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

        /**
         * Decodes an AwesomeMessage message from the specified reader or buffer.
         * @param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from
         * @param {number} [length] Message length if known beforehand
         * @returns {awesomepackage.AwesomeMessage} AwesomeMessage
         * @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): awesomepackage.AwesomeMessage;

        /**
         * Decodes an AwesomeMessage message from the specified reader or buffer, length delimited.
         * @param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from
         * @returns {awesomepackage.AwesomeMessage} AwesomeMessage
         * @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)): awesomepackage.AwesomeMessage;

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

        /**
         * Creates an AwesomeMessage message from a plain object. Also converts values to their respective internal types.
         * @param {Object.<string,*>} object Plain object
         * @returns {awesomepackage.AwesomeMessage} AwesomeMessage
         */
        public static fromObject(object: { [k: string]: any }): awesomepackage.AwesomeMessage;

        /**
         * Creates an AwesomeMessage message from a plain object. Also converts values to their respective internal types.
         * This is an alias of {@link awesomepackage.AwesomeMessage.fromObject}.
         * @function
         * @param {Object.<string,*>} object Plain object
         * @returns {awesomepackage.AwesomeMessage} AwesomeMessage
         */
        public static from(object: { [k: string]: any }): awesomepackage.AwesomeMessage;

        /**
         * Creates a plain object from an AwesomeMessage message. Also converts values to other types if specified.
         * @param {awesomepackage.AwesomeMessage} message AwesomeMessage
         * @param {$protobuf.ConversionOptions} [options] Conversion options
         * @returns {Object.<string,*>} Plain object
         */
        public static toObject(message: awesomepackage.AwesomeMessage, options?: $protobuf.ConversionOptions): { [k: string]: any };

        /**
         * Creates a plain object from this AwesomeMessage message. Also converts values to other types if specified.
         * @param {$protobuf.ConversionOptions} [options] Conversion options
         * @returns {Object.<string,*>} Plain object
         */
        public toObject(options?: $protobuf.ConversionOptions): { [k: string]: any };

        /**
         * Converts this AwesomeMessage to JSON.
         * @returns {Object.<string,*>} JSON object
         */
        public toJSON(): { [k: string]: any };
    }
}

标签:Cocos,protobuf,object,param,awesomepackage,MAC,message,AwesomeMessage,protobufjs
From: https://blog.51cto.com/aonaufly/8409053

相关文章

  • [Mac软件]Adobe XD(Experience Design) v57.1.12.2一个功能强大的原型设计软件
    AdobeXD是一个直观、强大的UI/UX开发工具,旨在设计、原型设计、用户之间共享材料,以及通过数字技术设计交互。AdobeXD为您提供开发网站、应用程序、语音界面、游戏界面、电子邮件模板等所需的一切。无限制地创建设计各种互动,创建看起来和感觉真实的互动原型。感谢你的时间使用基于......
  • mac上python安装mysqlclient模块方法
    运行pycharm后,打开命令行窗口确保您已经安装了MySQL,并且包括了MySQL的开发文件。您可以通过Homebrew来安装MySQL:brewinstallmysql#安装mysqlclient之前,先安装openssl:brewinstallopenssl#使用以下命令来安装mysqlclient:envLDFLAGS="-I/usr/local/opt/openssl/i......
  • 苹果系统Mac升级后之前的网络软件不可用——Mac系统维护——Mac系统升级后软件报错—
      ========================================== 博士同学最近联系我,说是自己的mac系统升级后之前可以用的网络软件不可用使用了,由于平时工作需要,这个网络软件如果没有是十分不方便的,为此找我急需给出解决方案。作为一个资深穷人虽然使用Linux系统有了10十多年的时间,但是对于mac......
  • golang版本升级(Mac GoLand版)
    本地的go版本是1.18,想升级到1.21,运用GoLand做快速升级第一步  进入GoLand-》Preferences第二步  进入Go-〉GOROOT点击“+”号,选择“Download”第三步选择需要更新的版本 点击“OK”后“Apply”,最新版的go就会下载到你的GOPATH里面了 升级完新版本后,deb......
  • Mac 怎么样将当前登录人设置成超级管理员?
    在macOS中,你可以通过以下步骤将当前登录用户设置为超级管理员:打开“系统偏好设置”:点击屏幕左上角的苹果图标,然后选择“系统偏好设置”。进入“用户与群组”:在系统偏好设置窗口中,找到并点击“用户与群组”选项。解锁设置:如果锁定了设置,你需要点击左下角的锁形图标并输入管理员密......
  • Cocos Creator 3.0基于PBR的物理渲染详解
    CocosCreator3.0出来以后,终于CocosCreator升级成为全新的2D/3D游戏引擎,适合2D和3D游戏开发,而3D游戏对画面效果和渲染管线的定制会有很高的需求,PBR美术工作流作为次世代主流的解决方案,今天我们来详细的分析一下基于CocosCreator的PBR物理渲染。1:计算机是如何显示颜色的讨......
  • Cocos creator屏幕适配怎么做?
    前言cocoscreator屏幕适配是面试中必问的一个题目,今天给大家说说怎么样做屏幕适配。1:屏幕适配的2个核心的配置a:设计分辨率:美术设计资源时候的分辨率,也是我们所有资源的参考分辨率,我们就是基于这个分辨率来适配到其它不同的手机分辨率上的;b:适配策略:固定高度/固定......
  • Mac 复制文件名目录路径
    Mac快速复制文件路径在Mac电脑上,我们经常需要复制文件的路径,其实,Mac系统提供了快速复制文件路径的方法。下面我们来详细介绍。方法一:使用菜单栏首先,打开Finder,然后选择你要复制路径的文件或文件夹,接着按住“Option”键并点击右键,你会看到“复制‘文件名’的路径”选项,点击它即......
  • Mac Maven环境变量配置 zsh: command not found: mvn
    之前配过环境变量,但是后来打开还是报zsh:commandnotfound:mvn需要在运行前先刷下环境变量source~/.bash_profile每次使用前都刷一下比较麻烦,这是因为当Mac上安装了zsh后,.bash_profile文件的配置无法生效最终解决方案:#修改~/.zshrc[jimmy@MacBook-Pro~%vi......
  • Mac Os VS Code 无法升级Cannot update while running on a read-only volume.
    macOSVSCode无法升级,提示“Cannotupdatewhilerunningonaread-onlyvolume“错误提示错误如下:Cannotupdatewhilerunningonaread-onlyvolume.Theapplicationisonaread-onlyvolume.Pleasemovetheapplicationandtryagain.Ifyou’reonmacOSSierr......