Pb killer

Let me start by asking PBkiller to introduce herself

Pbkiller: I am a professional and trained protobuf problem killer, I can easily solve protobuf problems in Cocos Creaotr development for you, and I can make you feel free to torture protbuf problems that have been plaguing you.

A subject,

My pbKiller’s greatest skill is the ability to quickly arrest Proto files and bring them to their knees, whether in a browser, phone, or desktop environment.

1. Call

All I have to do is whisper my name and I’ll be at your side, and I’ll be very gentle with my master.

let pbkiller = require('pbkiller');
Copy the code

2. Make the Proto file kneel in front of the owner

I am cruel to proto files, but please put all proto files in assets/resources/ PB directory in advance, call preload, I will be there immediately, you use the loadAll() command, I will complete the proto files. Let them all kneel before you.

// All proto objects are concentrated on the returned pb object pbkiller.preload(() => {let pb = pbkiller.loadAll(); 
});
Copy the code

3. Make the JSON format kneel too

If the owner likes the PROto file in JSON format, please kindly use the PBJS tool attached with ProtobufJS to convert the proto file into JSON format and store it in assets/ Resources/PB directory. I will make it kneel down as well.

// All proto objects are concentrated on the returned PB objectlet pb = pbkiller.loadAll('json'); 
Copy the code

4. Arrest the proto file in the specified file name

If the host only needs to arrest one or more proto files, loadFromFile can be used.


let pb = pbkiller.loadFromFile('xxx.proto');

let pb = pbkiller.loadFromFile(['xxx.proto'.'yyy.proto']);

let pb = pbkiller.loadFromFile(['xxx.json'.'yyy.json']);

Copy the code

###5. Arrest proto files in different regions

Master, my default capture path is Assets/Resources /pb, you can modify the path by using the following properties.

// Change the path pbkiller.root = using the root attribute'my-pb-path'; // Assets /resources/my-pb-path pbkiller.loadall ();Copy the code

Second, the torture

Although we had killer in our name, we were happy not to kill the enemy at once, but to torture it, because it might have tortured my master before, so we couldn’t let it die too easily, hahaha.

1. Grab a proto and wrestle with the owner (instantiation)

Master, I’ve got Proto for you, so you can use the new keyword to wrestle Proto.

// Capture all proto files and specify all objects in the path of the grace.proto. MSG package to be compiledlet pb = pbkiller.loadAll('proto'.'grace.proto.msg');

letplayer = new pb.Player(); // Scuff player.name ='ShawnZhang'; // Kick it againCopy the code

2. Spanking and crying (serialization and deserialization)

Master, give Proto a slap and use toArrayBuffer, if you’re feeling a little tired you can use toBuffer and they’ll do exactly the same thing.

letplayer = new pb.Player(); / / to scuffleletdata = player.toBuffer(); // Another slapCopy the code

We typed player as data, now type it back and let it cry, but only quietly. Decode.

// Master allows you to cry quietly, come backlet player = pb.Player.decode(data);
Copy the code

Third, infighting

Me: “PBkiller didn’t expect you to talk so much, let me introduce you, talk so much. To treat Proto so cruel, and master master, especially disgusting, very disgusting “.

Pbkiller: “This year, you think the killer is so easy to be, the killer should also learn to self-marketing, pay attention to user experience, you used to introduce the way out”.

Me: “You dare to say I out, careful I kill you.”

Pbkiller: “You’re the boss, you call the shots, I see how many people like the way you talk.” pbkiller has a look of disdain on his face.

Iv. Formal explanation

The PBKiller plugin library can help you simplify the use of ProtobufJS in Cocos Creator and is compatible with all platforms and Creator versions!

1. Install

The PBkiller plug-in has two installation modes:

  • Install-src: source mode, which imports the protobufJS source code into the project, independent of any external files. This mode is recommended for users unfamiliar with NPM.
  • Install – lite:Simplify the model, this mode needs to be installed in advanceprotobufjs npmModule, installation command: NPM install protobufjs@5

After the installation is complete, the following files are imported:

  1. Protobufjs source code (simplified mode without this directory)
  2. Pbkiller source code, and FS/PATH camouflage module
  3. Simple test scenarios and code
  4. Test the proto file

Here is the import file and directory structure:

Pbkiller ├ ─ ─ protobuf protobufjs source │ ├ ─ ─ bytebuffer. Js │ ├ ─ ─ long. Js │ └ ─ ─ protobufjs. Js ├ ─ ─ the SRC pbkiller source │ ├ ─ ─ the fs. Js │ ├─ ├─ ├─ pbkiller └─test├ ─ ─test-pbkiller. Fire ├ ─test└─ proto, proto, proto, proto, proto └─ ActionCode.proto ├─ ChatMsg. Proto ├─ player.json ├.protoCopy the code

2. Quick use

The import module

let pbkiller = require('pbkiller');
Copy the code

Load all proto files in the resources/pb directory

// Load all proto filesletpb = pbkiller.loadAll(); // instantiate the Player object in protolet player = new pb.grace.proto.msg.Player();
Copy the code

The specified file formats: [proto | json] for proto by default

// Note that json files are generated from PBJS provided by Protobufjslet pb = pbkiller.loadAll('json');
Copy the code

Specifies the path to the compiled object

let pb = pbkiller.loadAll('proto'.'grace.proto.msg');
cc.log(new pb.Player());
Copy the code

3. Pay special attention

Proto and.json** files can be used when loading proTo. Pbkiller supports mixing of the two, but it is important to note that if there are dependencies between proTo files, make sure that the dependencies are in the same file format.