voxel / voxel-clientmc

Minecraft client using WebSockets and voxel-engine (voxel.js plugin)
57 stars 13 forks source link

Run mineflayer client bot in a webworker #9

Closed deathcap closed 9 years ago

deathcap commented 9 years ago

GH-8

deathcap commented 9 years ago

current status: successfully parses a few packets, then crashes 'did not read the whole thing', reader returning null on fieldInfo Object {name: "yaw", type: "byte"}:

mf-worker initializing DedicatedWorkerGlobalScope {hex: function, undefined: undefined, Infinity: Infinity, Math: MathConstructor, NaN: NaN…}
blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:25953 mf-worker bot Bot {_events: Object, _maxListeners: undefined, client: Client, username: "user1", isSleeping: false…}
blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30822 onCompressionRequest Object {id: 3, state: "login", threshold: 256}
blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30911 received  3 raw bytes
blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30912 Offset   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
000000   03 80 02                                          ...

blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30814 onLogin Object {id: 2, state: "login", uuid: "c35a9b76-e335-3814-9f71-5003c285d168", username: "webuser-43"}
blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30911 received  49 raw bytes
blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30912 Offset   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
000000   02 24 63 33 35 61 39 62 37 36 2D 65 33 33 35 2D   .$c35a9b76-e335-
000010   33 38 31 34 2D 39 66 37 31 2D 35 30 30 33 63 32   3814-9f71-5003c2
000020   38 35 64 31 36 38 0A 77 65 62 75 73 65 72 2D 34   85d168.webuser-4
000030   33                                                3

blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:25956 Spawn position: {"x":0,"y":0,"z":0}
blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:40131 DID NOT PARSE THE WHOLE THING!
blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30911 received  3 raw bytes
blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30912 Offset   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
000000   09 00 00                                          ...

blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:40131 DID NOT PARSE THE WHOLE THING!
blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30911 received  17 raw bytes
blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30912 Offset   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
000000   17 08 4D 43 7C 42 72 61 6E 64 76 61 6E 69 6C 6C   ..MC|Brandvanill
000010   61                                                a

blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:40131 DID NOT PARSE THE WHOLE THING!
blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30911 received  11 raw bytes
blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30912 Offset   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
000000   15 05 65 6E 5F 55 53 00 00 01 FF                  ..en_US...ÿ

blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30911 received  18 raw bytes
blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30912 Offset   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
000000   01 00 02 E1 5D 00 00 01 14 07 64 65 66 61 75 6C   ...á].....defaul
000010   74 00                                             t.

blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30911 received  17 raw bytes
blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30912 Offset   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
000000   3F 08 4D 43 7C 42 72 61 6E 64 06 53 70 69 67 6F   ?.MC|Brand.Spigo
000010   74                                                t

blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30911 received  2 raw bytes
blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30912 Offset   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
000000   41 01                                             A.

blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:25956 Spawn position: {"x":-236,"y":64,"z":256}
blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30911 received  9 raw bytes
blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30912 Offset   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
000000   05 FF FF C5 01 00 00 01 00                        .ÿÿÅ.....

blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30911 received  10 raw bytes
blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30912 Offset   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
000000   39 00 3D 4C CC CD 3D CC CC CD                     9.=LÌÍ=ÌÌÍ

blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30911 received  2 raw bytes
blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30912 Offset   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
000000   09 00                                             ..

blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30911 received  2 raw bytes
blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30912 Offset   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
000000   37 00                                             7.

blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30911 received  79 raw bytes
blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30912 Offset   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
000000   02 4C 7B 22 65 78 74 72 61 22 3A 5B 7B 22 63 6F   .L{"extra":[{"co
000010   6C 6F 72 22 3A 22 79 65 6C 6C 6F 77 22 2C 22 74   lor":"yellow","t
000020   65 78 74 22 3A 22 77 65 62 75 73 65 72 2D 34 33   ext":"webuser-43
000030   20 6A 6F 69 6E 65 64 20 74 68 65 20 67 61 6D 65    joined the game
000040   22 7D 5D 2C 22 74 65 78 74 22 3A 22 22 7D 01      "}],"text":""}.

blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30911 received  34 raw bytes
blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30912 Offset   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
000000   38 00 01 C3 5A 9B 76 E3 35 38 14 9F 71 50 03 C2   8..ÃZ.vã58..qP.Â
000010   85 D1 68 0A 77 65 62 75 73 65 72 2D 34 33 00 00   .Ñh.webuser-43..
000020   00 00                                             ..

blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30911 received  34 raw bytes
blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:30912 Offset   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
000000   38 00 01 C3 5A 9B 76 E3 35 38 14 9F 71 50 03 C2   8..ÃZ.vã58..qP.Â
000010   85 D1 68 0A 77 65 62 75 73 65 72 2D 34 33 00 00   .Ñh.webuser-43..
000020   00 00                                             ..

blob:http%3A//localhost%3A9966/874028bb-840c-480c-9955-fa82c28f9bbf:40131
DID NOT PARSE THE WHOLE THING!

0x16 http://wiki.vg/Protocol#Entity_Look buffer '03(length) 00 16 00' - expected varint entityid, angle(byte) yaw, angle(byte) pitch, boolean onground. truncated? maybe from missing 'connect' event?

deathcap commented 9 years ago

The parse error occurs because the client is attempting to reparse the packet data it just tried to send to the server (0x16 is client status serverbound, but entity look clientbound), similar problem saw earlier with sending the client username. Must not be using workerstream/websocket-stream properly. Use https://github.com/dominictarr/event-stream#duplex-writestream-readstream to connect read and write streams?

deathcap commented 9 years ago

This change requires some explanation. Now using a duplex stream to join the writer and reader streams, to pass to mineflayer as the 'socket' it reads/writes packet data from/to. The read stream is constructed from the workerstream ParentStream, piped through tobuffer-stream to convert typed arrays to Buffer objects, and then through a custom through stream to handle non-packet data 'commands' from the main thread. The write stream sends packet data back using an ArrayBuffer transferrable using postMessage, also used to send bot actions back to the main thread, whether it is either written to the websocket stream or passed to a handler method. Long story short, the chunk iteration bottleneck (and other mineflayer operations) now takes place asynchronously in the webworker where it no longer blocks the UI.

chunk added in 351.7040000297129 blob:http%3A//localhost%3A9966/1dd964e2-8ada-454f-a4ad-1ccbcd163618:25926 Chunk load (-240,0,240) clientmc.js:266 showChunk -8|7|7 chunk added in 336.9890000903979 blob:http%3A//localhost%3A9966/1dd964e2-8ada-454f-a4ad-1ccbcd163618:25926 Chunk load (-240,0,256) blob:http%3A//localhost%3A9966/1dd964e2-8ada-454f-a4ad-1ccbcd163618:25975 Created new chunk -8|7|8

screen shot 2015-03-26 at 12 32 50 am

screen shot 2015-03-26 at 12 36 21 am