teeworlds 2.0.7 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/lib/MsgUnpacker.ts +32 -16
- package/lib/client.ts +25 -24
- package/lib/snapshot.ts +44 -95
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@ https://www.npmjs.com/package/teeworlds
|
|
|
7
7
|
# Usage
|
|
8
8
|
Example file:
|
|
9
9
|
```const teeworlds = require('teeworlds');
|
|
10
|
-
let client = new teeworlds.Client("127.0.0.1", 8303, "
|
|
10
|
+
let client = new teeworlds.Client("127.0.0.1", 8303, "nameless tee");
|
|
11
11
|
|
|
12
12
|
client.connect();
|
|
13
13
|
|
package/lib/MsgUnpacker.ts
CHANGED
|
@@ -1,44 +1,60 @@
|
|
|
1
1
|
var decoder = new TextDecoder('utf-8');
|
|
2
|
-
function unpackInt(pSrc: number[]): {result: number, remaining: number[]} {
|
|
2
|
+
export function unpackInt(pSrc: number[]): {result: number, remaining: number[]} {
|
|
3
3
|
var result = 0;
|
|
4
4
|
var len = 1;
|
|
5
5
|
|
|
6
6
|
var iter = pSrc[Symbol.iterator]()
|
|
7
7
|
var src: any = iter.next()
|
|
8
|
-
// if (src.done)
|
|
9
|
-
// console.warn("Unexpected end", src)
|
|
10
8
|
src = src.value
|
|
11
9
|
var sign = ((src >> 6) & 1)
|
|
12
10
|
|
|
13
11
|
result |= (src & 0b0011_1111)
|
|
14
12
|
for (let i = 0; i < 4; i++) {
|
|
15
13
|
|
|
16
|
-
if ((src & 0b1000_0000)
|
|
14
|
+
if ((src & 0b1000_0000) === 0)
|
|
17
15
|
break;
|
|
18
16
|
src = iter.next()
|
|
19
17
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
// console.warn("Unexpected end", src);
|
|
23
|
-
src = src.value
|
|
24
|
-
len += 1;
|
|
25
|
-
if (i == 3 && (src & 0b1111_0000) != 0)
|
|
26
|
-
console.warn("NonZeroIntPadding")
|
|
18
|
+
src = src.value;
|
|
19
|
+
len++;
|
|
27
20
|
result |= ((src & 0b0111_1111)) << (6+7*i)
|
|
28
21
|
|
|
29
22
|
}
|
|
30
|
-
if (len > 1 && src == 0b0000_0000) {
|
|
31
|
-
console.warn("OverlongIntEncoding")
|
|
32
|
-
}
|
|
33
23
|
result ^= -sign;
|
|
34
24
|
|
|
35
25
|
return {result, remaining: Array.from(iter)};
|
|
36
26
|
}
|
|
37
|
-
function unpackString(pSrc: number[]): {result: string, remaining: number[]} {
|
|
27
|
+
export function unpackString(pSrc: number[]): {result: string, remaining: number[]} {
|
|
38
28
|
var result = pSrc.slice(0, pSrc.indexOf(0))
|
|
39
29
|
pSrc = pSrc.slice(pSrc.indexOf(0), pSrc.length)
|
|
40
30
|
return {result: decoder.decode(new Uint8Array(result)), remaining: pSrc}
|
|
41
31
|
}
|
|
42
32
|
|
|
33
|
+
export class MsgUnpacker {
|
|
34
|
+
remaining: number[];
|
|
35
|
+
constructor(pSrc: number[]) {
|
|
36
|
+
this.remaining = pSrc;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
unpackInt(_unpacked = false): number {
|
|
40
|
+
let unpacked;
|
|
41
|
+
if (!_unpacked) {
|
|
42
|
+
unpacked = unpackInt(this.remaining);
|
|
43
|
+
this.remaining = unpacked.remaining;
|
|
44
|
+
} else {
|
|
45
|
+
unpacked = {result: this.remaining[0]};
|
|
46
|
+
this.remaining = this.remaining.slice(1);
|
|
47
|
+
|
|
48
|
+
}
|
|
49
|
+
return unpacked.result;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
unpackString(): string {
|
|
53
|
+
let unpacked = unpackString(this.remaining);
|
|
54
|
+
this.remaining = unpacked.remaining;
|
|
55
|
+
return unpacked.result;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
43
59
|
|
|
44
|
-
export = {unpackInt, unpackString}
|
|
60
|
+
// export = {MsgUnpacker, unpackInt, unpackString}
|
package/lib/client.ts
CHANGED
|
@@ -4,7 +4,10 @@ import net from 'dgram';
|
|
|
4
4
|
import fs from 'fs';
|
|
5
5
|
import { EventEmitter } from 'stream';
|
|
6
6
|
import { spawn } from 'child_process';
|
|
7
|
-
|
|
7
|
+
|
|
8
|
+
import { unpackInt, unpackString, MsgUnpacker } from "./MsgUnpacker";
|
|
9
|
+
|
|
10
|
+
|
|
8
11
|
import MsgPacker from './MsgPacker';
|
|
9
12
|
import { Snapshot } from './snapshot';
|
|
10
13
|
import Huffman from "./huffman";
|
|
@@ -312,11 +315,11 @@ class Client extends EventEmitter {
|
|
|
312
315
|
chat.forEach(a => {
|
|
313
316
|
if (a.msg == "SV_CHAT") {
|
|
314
317
|
var unpacked: iMessage = {} as iMessage;
|
|
315
|
-
unpacked.team =
|
|
316
|
-
var remaining: number[] =
|
|
317
|
-
unpacked.client_id =
|
|
318
|
-
remaining =
|
|
319
|
-
unpacked.message =
|
|
318
|
+
unpacked.team = unpackInt(a.raw.toJSON().data).result;
|
|
319
|
+
var remaining: number[] = unpackInt(a.raw.toJSON().data).remaining;
|
|
320
|
+
unpacked.client_id = unpackInt(remaining).result;
|
|
321
|
+
remaining = unpackInt(remaining).remaining;
|
|
322
|
+
unpacked.message = unpackString(remaining).result;
|
|
320
323
|
if (unpacked.client_id != -1)
|
|
321
324
|
unpacked.author = { ClientInfo: this.client_infos[unpacked.client_id], PlayerInfo: this.player_infos[unpacked.client_id] }
|
|
322
325
|
// console.log(unpacked)
|
|
@@ -329,13 +332,11 @@ class Client extends EventEmitter {
|
|
|
329
332
|
chat.forEach(a => {
|
|
330
333
|
if (a.msg == "SV_KILL_MSG") {
|
|
331
334
|
var unpacked: iKillMsg = {} as iKillMsg;
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
unpacked.victim_id =
|
|
335
|
-
|
|
336
|
-
unpacked.
|
|
337
|
-
remaining = MsgUnpacker.unpackInt(remaining).remaining;
|
|
338
|
-
unpacked.special_mode = MsgUnpacker.unpackInt(remaining).result;
|
|
335
|
+
let unpacker = new MsgUnpacker(a.raw.toJSON().data);
|
|
336
|
+
unpacked.killer_id = unpacker.unpackInt();
|
|
337
|
+
unpacked.victim_id = unpacker.unpackInt();
|
|
338
|
+
unpacked.weapon = unpacker.unpackInt();
|
|
339
|
+
unpacked.special_mode = unpacker.unpackInt();
|
|
339
340
|
if (unpacked.victim_id != -1)
|
|
340
341
|
unpacked.victim = { ClientInfo: this.client_infos[unpacked.victim_id], PlayerInfo: this.player_infos[unpacked.victim_id] }
|
|
341
342
|
if (unpacked.killer_id != -1)
|
|
@@ -369,7 +370,7 @@ class Client extends EventEmitter {
|
|
|
369
370
|
} else if (a.toJSON().data[3] == 0x4) {
|
|
370
371
|
// disconnected
|
|
371
372
|
this.State = 0;
|
|
372
|
-
let reason: string = (
|
|
373
|
+
let reason: string = (unpackString(a.toJSON().data.slice(4)).result);
|
|
373
374
|
this.State = -1;
|
|
374
375
|
this.emit("disconnect", reason);
|
|
375
376
|
}
|
|
@@ -417,19 +418,19 @@ class Client extends EventEmitter {
|
|
|
417
418
|
let part = 0;
|
|
418
419
|
let num_parts = 1;
|
|
419
420
|
chunks.forEach(chunk => {
|
|
420
|
-
let AckGameTick = (
|
|
421
|
-
chunk.raw = Buffer.from(
|
|
422
|
-
let DeltaTick =
|
|
421
|
+
let AckGameTick = (unpackInt(chunk.raw.toJSON().data).result);
|
|
422
|
+
chunk.raw = Buffer.from(unpackInt(chunk?.raw?.toJSON().data).remaining);
|
|
423
|
+
let DeltaTick = unpackInt(chunk?.raw?.toJSON().data).result
|
|
423
424
|
if (chunk.msg == "SNAP") {
|
|
424
|
-
chunk.raw = Buffer.from(
|
|
425
|
-
num_parts = (
|
|
426
|
-
chunk.raw = Buffer.from(
|
|
427
|
-
part = (
|
|
425
|
+
chunk.raw = Buffer.from(unpackInt(chunk?.raw?.toJSON().data).remaining); // delta tick
|
|
426
|
+
num_parts = (unpackInt(chunk?.raw?.toJSON().data).result)
|
|
427
|
+
chunk.raw = Buffer.from(unpackInt(chunk?.raw?.toJSON().data).remaining); // num parts
|
|
428
|
+
part = (unpackInt(chunk?.raw?.toJSON().data).result)
|
|
428
429
|
}
|
|
429
|
-
chunk.raw = Buffer.from(
|
|
430
|
+
chunk.raw = Buffer.from(unpackInt(chunk?.raw?.toJSON().data).remaining); // part
|
|
430
431
|
if (chunk.msg != "SNAP_EMPTY")
|
|
431
|
-
chunk.raw = Buffer.from(
|
|
432
|
-
chunk.raw = Buffer.from(
|
|
432
|
+
chunk.raw = Buffer.from(unpackInt(chunk?.raw?.toJSON().data).remaining); // crc
|
|
433
|
+
chunk.raw = Buffer.from(unpackInt(chunk?.raw?.toJSON().data).remaining); // crc
|
|
433
434
|
if (part == 0 || this.snaps.length > 30) {
|
|
434
435
|
this.snaps = [];
|
|
435
436
|
}
|
package/lib/snapshot.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
import { MsgUnpacker } from "./MsgUnpacker";
|
|
2
2
|
var decoder = new TextDecoder('utf-8');
|
|
3
3
|
|
|
4
4
|
const itemAppendix: {"type_id": number, "size": number, "name": string}[] = [
|
|
@@ -287,11 +287,11 @@ class Snapshot {
|
|
|
287
287
|
|
|
288
288
|
return _item;
|
|
289
289
|
}
|
|
290
|
-
unpackSnapshot(snap: number[], lost = 0) {
|
|
291
|
-
// var size =
|
|
292
|
-
|
|
293
|
-
snap = MsgUnpacker.unpackInt(snap).remaining;
|
|
290
|
+
unpackSnapshot(snap: number[], lost = 0) {
|
|
291
|
+
// var size = unpackInt(snap).result;
|
|
292
|
+
let unpacker = new MsgUnpacker(snap);
|
|
294
293
|
|
|
294
|
+
// snap = unpackInt(snap).remaining; // size
|
|
295
295
|
|
|
296
296
|
/* key = (((type_id) << 16) | (id))
|
|
297
297
|
* key_to_id = ((key) & 0xffff)
|
|
@@ -300,106 +300,55 @@ class Snapshot {
|
|
|
300
300
|
* https://github.com/heinrich5991/libtw2/blob/master/doc/snapshot.md
|
|
301
301
|
*/
|
|
302
302
|
|
|
303
|
-
// snap =
|
|
304
|
-
// console.log(
|
|
305
|
-
// snap =
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
303
|
+
// snap = unpackInt(snap).remaining;
|
|
304
|
+
// console.log(unpackInt(snap).result, "tick?") // key?
|
|
305
|
+
// snap = unpackInt(snap).remaining;
|
|
306
|
+
let num_removed_items = unpacker.unpackInt();
|
|
307
|
+
let num_item_deltas = unpacker.unpackInt();
|
|
308
|
+
unpacker.unpackInt(); // _zero padding
|
|
309
|
+
/*snapshot_delta:
|
|
310
|
+
[ 4] num_removed_items
|
|
311
|
+
[ 4] num_item_deltas
|
|
312
|
+
[ 4] _zero
|
|
313
|
+
[*4] removed_item_keys
|
|
314
|
+
[ ] item_deltas
|
|
315
|
+
*/
|
|
316
|
+
|
|
317
|
+
for (let i = 0; i < num_removed_items; i++) {
|
|
318
|
+
unpacker.unpackInt(); // removed_item_keys
|
|
319
|
+
}
|
|
320
|
+
/*item_delta:
|
|
321
|
+
[ 4] type_id
|
|
322
|
+
[ 4] id
|
|
323
|
+
[ 4] _size
|
|
324
|
+
[*4] data_delta*/
|
|
325
|
+
let items: {'items': {'data': number[], 'parsed': Item, 'type_id': number, 'id': number, 'key': number}[]/*, 'client_infos': client_info[], 'player_infos': player_info[]*/, lost: number} = {items: [],/* client_infos: client_infos, player_infos: player_infos,*/ lost: 0};
|
|
326
|
+
|
|
327
|
+
for (let i = 0; i < num_item_deltas; i++) {
|
|
328
|
+
let type_id = unpacker.unpackInt();
|
|
329
|
+
let id = unpacker.unpackInt();
|
|
321
330
|
const key = (((type_id) << 16) | (id))
|
|
322
331
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
if (itemAppendix[type_id] && type_id > 0) {
|
|
326
|
-
// console.log("_size is not set")
|
|
327
|
-
// type_id is in itemAppendix -> _size is not set!
|
|
332
|
+
let _size;
|
|
333
|
+
if (type_id > 0 && type_id < itemAppendix.length) {
|
|
328
334
|
_size = itemAppendix[type_id].size;
|
|
329
|
-
} else
|
|
330
|
-
|
|
331
|
-
// _size is set.
|
|
332
|
-
snap = MsgUnpacker.unpackInt(snap).remaining;
|
|
333
|
-
_size = (MsgUnpacker.unpackInt(snap).result);
|
|
334
|
-
}
|
|
335
|
-
// console.log(_size, "size!")
|
|
336
|
-
|
|
337
|
-
var data: number[] = []
|
|
338
|
-
for (let i = 0; i < _size; i++) {
|
|
339
|
-
if (snap.length == 0) {
|
|
340
|
-
items.lost++;
|
|
341
|
-
}
|
|
342
|
-
if (snap[0]) {
|
|
343
|
-
snap = MsgUnpacker.unpackInt(snap).remaining;
|
|
344
|
-
data.push(MsgUnpacker.unpackInt(snap).result);
|
|
345
|
-
} else
|
|
346
|
-
break;
|
|
335
|
+
} else
|
|
336
|
+
_size = unpacker.unpackInt();
|
|
347
337
|
|
|
338
|
+
let data = [];
|
|
339
|
+
for (let j = 0; j < _size; j++) {
|
|
340
|
+
if (unpacker.remaining.length > 0)
|
|
341
|
+
data.push(unpacker.unpackInt());
|
|
348
342
|
}
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
// console.log("DdnetCharacter???")
|
|
352
|
-
var Ddnet_Character: DdnetCharacter = {
|
|
353
|
-
flags: data[0],
|
|
354
|
-
freeze_end: data[1],
|
|
355
|
-
jumps: data[2],
|
|
356
|
-
tele_checkpoint: data[3],
|
|
357
|
-
strong_weak_id: data[4]
|
|
358
|
-
// score: (!players[id] == undefined || typeof players[id].score == 'undefined') ? -1 : players[id].score
|
|
359
|
-
}
|
|
360
|
-
// console.log(Ddnet_Character)
|
|
361
|
-
if (Ddnet_Character.freeze_end > 0 || Ddnet_Character.freeze_end == -1) // freezed or deepfreezed
|
|
362
|
-
|
|
363
|
-
console.log(Ddnet_Character)
|
|
364
|
-
} // else
|
|
365
|
-
// console.log("lolol uuid??", _size, type_id, id, data)
|
|
366
|
-
}
|
|
367
|
-
if (type_id == 11) {
|
|
368
|
-
// obj_client_info!
|
|
369
|
-
var client_info: ClientInfo = {
|
|
370
|
-
name: this.IntsToStr(data.slice(0, 4)),
|
|
371
|
-
clan: this.IntsToStr(data.slice(4, 7)),
|
|
372
|
-
country: Number(data.slice(7, 8)),
|
|
373
|
-
skin: this.IntsToStr(data.slice(8, 14)),
|
|
374
|
-
use_custom_color: Number(data.slice(14, 15)),
|
|
375
|
-
color_body: Number(data.slice(15, 16)),
|
|
376
|
-
color_feet: Number(data.slice(16, 17)),
|
|
377
|
-
// score: (!players[id] == undefined || typeof players[id].score == 'undefined') ? -1 : players[id].score
|
|
378
|
-
}
|
|
379
|
-
client_infos[id] = client_info;
|
|
380
|
-
// console.log(client_info.name, client_info.clan, client_info.skin)
|
|
381
|
-
} else if (type_id == 10) {
|
|
382
|
-
var player_info: PlayerInfo = {
|
|
383
|
-
local: Number(data.slice(0, 1)),
|
|
384
|
-
client_id: Number(data.slice(1, 2)),
|
|
385
|
-
team: Number(data.slice(2, 3)),
|
|
386
|
-
score: Number(data.slice(3, 4)),
|
|
387
|
-
latency: Number(data.slice(4, 5))
|
|
388
|
-
}
|
|
389
|
-
player_infos[player_info.client_id] = player_info;
|
|
390
|
-
// players[id].score = player_info.score
|
|
391
|
-
// console.log(player_info, client_infos[player_info.client_id], data)
|
|
392
|
-
}
|
|
393
|
-
// if (type_id == 10 || type_id == 11)
|
|
394
|
-
// console.log(this.parseItem(data, type_id), itemAppendix[type_id].name, type_id)
|
|
395
|
-
var parsed = this.parseItem(data, type_id)
|
|
343
|
+
// console.log(type_id, id, _size, data);
|
|
344
|
+
let parsed = this.parseItem(data, type_id)
|
|
396
345
|
|
|
397
346
|
// console.log(data)
|
|
398
347
|
// console.log('')
|
|
399
348
|
items.items.push({data, parsed, type_id, id, key})
|
|
400
349
|
}
|
|
401
|
-
|
|
402
|
-
|
|
350
|
+
|
|
351
|
+
|
|
403
352
|
return items;
|
|
404
353
|
}}
|
|
405
354
|
// module.exports = MsgPacker;
|