teeworlds 2.0.5 → 2.0.6
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 +5 -2
- package/lib/huffman.ts +1 -115
- package/lib/snapshot.ts +6 -2
- package/package.json +1 -1
- package/lib/MsgPacker.js +0 -53
- package/lib/MsgUnpacker.js +0 -37
- package/lib/client.js +0 -511
- package/lib/huffman.js +0 -236
- package/lib/snapshot.js +0 -397
package/README.md
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
# tw-chatonly
|
|
2
2
|
Library to connect a bot to a Teeworlds server.
|
|
3
3
|
|
|
4
|
+
https://www.npmjs.com/package/teeworlds
|
|
5
|
+
|
|
6
|
+
|
|
4
7
|
# Usage
|
|
5
8
|
Example file:
|
|
6
|
-
```const
|
|
7
|
-
let client = new
|
|
9
|
+
```const teeworlds = require('teeworlds');
|
|
10
|
+
let client = new teeworlds.Client("127.0.0.1", 8303, "teest");
|
|
8
11
|
|
|
9
12
|
client.connect();
|
|
10
13
|
|
package/lib/huffman.ts
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
// from Ryozuki, found on ddnet discord ( 11.10.2018 )
|
|
2
|
-
// from typing import List
|
|
3
|
-
// from sys import argv
|
|
4
1
|
let FREQ_TABLE = [
|
|
5
2
|
1 << 30, 4545, 2657, 431, 1950, 919, 444, 482, 2244, 617, 838, 542, 715, 1814, 304, 240, 754, 212, 647, 186,
|
|
6
3
|
283, 131, 146, 166, 543, 164, 167, 136, 179, 859, 363, 113, 157, 154, 204, 108, 137, 180, 202, 176,
|
|
@@ -31,44 +28,12 @@ interface HuffmanNode {
|
|
|
31
28
|
symbol: number;
|
|
32
29
|
}
|
|
33
30
|
|
|
34
|
-
// class HuffmanNode {
|
|
35
|
-
// bits: number;
|
|
36
|
-
// numbits: number;
|
|
37
|
-
// left: number;
|
|
38
|
-
// right: number;
|
|
39
|
-
// symbol: number;
|
|
40
|
-
// constructor(/*bits: number, numbits: number, left: number, right: number, symbol: number*/) {
|
|
41
|
-
// this.bits = -1;
|
|
42
|
-
// this.numbits = -1;
|
|
43
|
-
// this.left = -1;
|
|
44
|
-
// this.right = -1;
|
|
45
|
-
// this.symbol = -1;
|
|
46
|
-
// }
|
|
47
|
-
|
|
48
|
-
// equal(node: HuffmanNode) {
|
|
49
|
-
// return this.bits == node.bits && this.numbits == node.numbits && this.left == node.left && this.right == node.right && this.symbol == node.symbol;
|
|
50
|
-
// }
|
|
51
|
-
|
|
52
|
-
// }
|
|
53
31
|
|
|
54
32
|
interface HuffmanConstructNode {
|
|
55
33
|
node_id: number;
|
|
56
34
|
frequency: number;
|
|
57
35
|
}
|
|
58
36
|
|
|
59
|
-
// class HuffmanConstructNode {
|
|
60
|
-
// node_id: number;
|
|
61
|
-
// frequency: number;
|
|
62
|
-
// constructor(node_id: number, frequency: number) {
|
|
63
|
-
// this.node_id = node_id;
|
|
64
|
-
// this.frequency = frequency;
|
|
65
|
-
// }
|
|
66
|
-
// }
|
|
67
|
-
// class HuffmanConstructNode:
|
|
68
|
-
// def __init__(self):
|
|
69
|
-
// self.node_id: int = None
|
|
70
|
-
// self.frequency: int = None
|
|
71
|
-
|
|
72
37
|
class Huffman {
|
|
73
38
|
nodes: HuffmanNode[];
|
|
74
39
|
decode_lut: number[];
|
|
@@ -76,7 +41,6 @@ class Huffman {
|
|
|
76
41
|
start_node_index: number;
|
|
77
42
|
|
|
78
43
|
constructor(frequencies = FREQ_TABLE) {
|
|
79
|
-
// this.frequencies = frequencies;
|
|
80
44
|
this.nodes = new Array<HuffmanNode>(HUFFMAN_MAX_NODES);
|
|
81
45
|
for (let i = 0; i < HUFFMAN_MAX_NODES; i++) {
|
|
82
46
|
this.nodes[i] = {} as HuffmanNode;
|
|
@@ -87,7 +51,6 @@ class Huffman {
|
|
|
87
51
|
|
|
88
52
|
this.construct_tree(frequencies);
|
|
89
53
|
|
|
90
|
-
let xx = 0;
|
|
91
54
|
|
|
92
55
|
for (let i = 0; i < HUFFMAN_LUTSIZE; i++) {
|
|
93
56
|
let bits = i;
|
|
@@ -207,35 +170,6 @@ class Huffman {
|
|
|
207
170
|
}
|
|
208
171
|
output.push(bits);
|
|
209
172
|
return Buffer.from(output);
|
|
210
|
-
|
|
211
|
-
/* output = bytearray()
|
|
212
|
-
bits = 0
|
|
213
|
-
bitcount = 0
|
|
214
|
-
|
|
215
|
-
if size is None:
|
|
216
|
-
size = len(inp_buffer)
|
|
217
|
-
|
|
218
|
-
for x in inp_buffer[start_index:size:]:
|
|
219
|
-
bits |= self.nodes[x].bits << bitcount
|
|
220
|
-
bitcount += self.nodes[x].numbits
|
|
221
|
-
|
|
222
|
-
while bitcount >= 8:
|
|
223
|
-
output.append(bits & 0xff)
|
|
224
|
-
bits >>= 8
|
|
225
|
-
bitcount -= 8
|
|
226
|
-
|
|
227
|
-
bits |= self.nodes[HUFFMAN_EOF_SYMBOL].bits << bitcount
|
|
228
|
-
bitcount += self.nodes[HUFFMAN_EOF_SYMBOL].numbits
|
|
229
|
-
|
|
230
|
-
while bitcount >= 8:
|
|
231
|
-
output.append(bits & 0xff)
|
|
232
|
-
bits >>= 8
|
|
233
|
-
bitcount -= 8
|
|
234
|
-
|
|
235
|
-
# write out last bits
|
|
236
|
-
output.append(bits)
|
|
237
|
-
return output
|
|
238
|
-
*/
|
|
239
173
|
}
|
|
240
174
|
decompress(inp_buffer: Buffer, size = 0): Buffer {
|
|
241
175
|
|
|
@@ -259,7 +193,6 @@ class Huffman {
|
|
|
259
193
|
}
|
|
260
194
|
if (node_i == -1)
|
|
261
195
|
node_i = this.decode_lut[bits & HUFFMAN_LUTMASK];
|
|
262
|
-
// console.log(node_i, bits & HUFFMAN_LUTMASK, this.decode_lut)
|
|
263
196
|
if (this.nodes[node_i].numbits) {
|
|
264
197
|
bits >>= this.nodes[node_i].numbits;
|
|
265
198
|
bitcount -= this.nodes[node_i].numbits;
|
|
@@ -289,54 +222,7 @@ class Huffman {
|
|
|
289
222
|
return Buffer.from(output);
|
|
290
223
|
}
|
|
291
224
|
|
|
292
|
-
|
|
293
|
-
bitcount = 0
|
|
294
|
-
eof = self.nodes[HUFFMAN_EOF_SYMBOL]
|
|
295
|
-
output = bytearray()
|
|
296
|
-
|
|
297
|
-
if size is None:
|
|
298
|
-
size = len(inp_buffer)
|
|
299
|
-
|
|
300
|
-
src_index = 0
|
|
301
|
-
while True:
|
|
302
|
-
node_i = None
|
|
303
|
-
if bitcount >= HUFFMAN_LUTBITS:
|
|
304
|
-
node_i = self.decode_lut[bits & HUFFMAN_LUTMASK]
|
|
305
|
-
|
|
306
|
-
while bitcount < 24 and src_index != size:
|
|
307
|
-
bits |= inp_buffer[src_index] << bitcount
|
|
308
|
-
src_index += 1
|
|
309
|
-
bitcount += 8
|
|
310
|
-
|
|
311
|
-
if node_i is None:
|
|
312
|
-
node_i = self.decode_lut[bits & HUFFMAN_LUTMASK]
|
|
313
|
-
if self.nodes[node_i].numbits:
|
|
314
|
-
bits >>= self.nodes[node_i].numbits
|
|
315
|
-
bitcount -= self.nodes[node_i].numbits
|
|
316
|
-
else:
|
|
317
|
-
bits >>= HUFFMAN_LUTBITS
|
|
318
|
-
bitcount -= HUFFMAN_LUTBITS
|
|
319
|
-
|
|
320
|
-
while True:
|
|
321
|
-
if bits & 1:
|
|
322
|
-
node_i = self.nodes[node_i].right
|
|
323
|
-
else:
|
|
324
|
-
node_i = self.nodes[node_i].left
|
|
325
|
-
|
|
326
|
-
bitcount -= 1
|
|
327
|
-
bits >>= 1
|
|
328
|
-
|
|
329
|
-
if self.nodes[node_i].numbits:
|
|
330
|
-
break
|
|
331
|
-
|
|
332
|
-
if bitcount === 0:
|
|
333
|
-
raise ValueError("No more bits, decoding error")
|
|
334
|
-
|
|
335
|
-
if self.nodes[node_i] === eof:
|
|
336
|
-
break
|
|
337
|
-
output.append(self.nodes[node_i].symbol)
|
|
338
|
-
|
|
339
|
-
return output */
|
|
225
|
+
|
|
340
226
|
}
|
|
341
227
|
|
|
342
228
|
export = Huffman;
|
package/lib/snapshot.ts
CHANGED
|
@@ -336,11 +336,15 @@ class Snapshot {
|
|
|
336
336
|
|
|
337
337
|
var data: number[] = []
|
|
338
338
|
for (let i = 0; i < _size; i++) {
|
|
339
|
-
snap = MsgUnpacker.unpackInt(snap).remaining;
|
|
340
|
-
data.push(MsgUnpacker.unpackInt(snap).result);
|
|
341
339
|
if (snap.length == 0) {
|
|
342
340
|
items.lost++;
|
|
343
341
|
}
|
|
342
|
+
if (snap[0]) {
|
|
343
|
+
snap = MsgUnpacker.unpackInt(snap).remaining;
|
|
344
|
+
data.push(MsgUnpacker.unpackInt(snap).result);
|
|
345
|
+
} else
|
|
346
|
+
break;
|
|
347
|
+
|
|
344
348
|
}
|
|
345
349
|
if (type_id > 0x4000 || type_id == 0) {
|
|
346
350
|
if (_size == 5 && id == 0) {
|
package/package.json
CHANGED
package/lib/MsgPacker.js
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var MsgPacker = /** @class */ (function () {
|
|
3
|
-
function MsgPacker(msg, sys) {
|
|
4
|
-
this.result = Buffer.from([2 * msg + (sys ? 1 : 0)]); // booleans turn into int automatically.
|
|
5
|
-
this.sys = sys;
|
|
6
|
-
}
|
|
7
|
-
MsgPacker.prototype.AddString = function (str) {
|
|
8
|
-
this.result = Buffer.concat([this.result, Buffer.from(str), Buffer.from([0x00])]);
|
|
9
|
-
};
|
|
10
|
-
MsgPacker.prototype.AddBuffer = function (buffer) {
|
|
11
|
-
this.result = Buffer.concat([this.result, buffer]);
|
|
12
|
-
};
|
|
13
|
-
MsgPacker.prototype.AddInt = function (i) {
|
|
14
|
-
var result = [];
|
|
15
|
-
var pDst = (i >> 25) & 0x40;
|
|
16
|
-
var i = i ^ (i >> 31);
|
|
17
|
-
pDst |= i & 0x3f;
|
|
18
|
-
i >>= 6;
|
|
19
|
-
if (i) {
|
|
20
|
-
pDst |= 0x80;
|
|
21
|
-
result.push(pDst);
|
|
22
|
-
while (true) {
|
|
23
|
-
pDst++;
|
|
24
|
-
pDst = i & (0x7f);
|
|
25
|
-
i >>= 7;
|
|
26
|
-
pDst |= (Number(i != 0)) << 7;
|
|
27
|
-
result.push(pDst);
|
|
28
|
-
if (!i)
|
|
29
|
-
break;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
else
|
|
33
|
-
result.push(pDst);
|
|
34
|
-
// ... i'll just stop trying to understand.
|
|
35
|
-
this.result = Buffer.concat([this.result, Buffer.from(result)]);
|
|
36
|
-
};
|
|
37
|
-
Object.defineProperty(MsgPacker.prototype, "size", {
|
|
38
|
-
get: function () {
|
|
39
|
-
return this.result.byteLength;
|
|
40
|
-
},
|
|
41
|
-
enumerable: false,
|
|
42
|
-
configurable: true
|
|
43
|
-
});
|
|
44
|
-
Object.defineProperty(MsgPacker.prototype, "buffer", {
|
|
45
|
-
get: function () {
|
|
46
|
-
return this.result;
|
|
47
|
-
},
|
|
48
|
-
enumerable: false,
|
|
49
|
-
configurable: true
|
|
50
|
-
});
|
|
51
|
-
return MsgPacker;
|
|
52
|
-
}());
|
|
53
|
-
module.exports = MsgPacker;
|
package/lib/MsgUnpacker.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var decoder = new TextDecoder('utf-8');
|
|
3
|
-
function unpackInt(pSrc) {
|
|
4
|
-
var result = 0;
|
|
5
|
-
var len = 1;
|
|
6
|
-
var iter = pSrc[Symbol.iterator]();
|
|
7
|
-
var src = iter.next();
|
|
8
|
-
// if (src.done)
|
|
9
|
-
// console.warn("Unexpected end", src)
|
|
10
|
-
src = src.value;
|
|
11
|
-
var sign = ((src >> 6) & 1);
|
|
12
|
-
result |= (src & 63);
|
|
13
|
-
for (var i = 0; i < 4; i++) {
|
|
14
|
-
if ((src & 128) == 0)
|
|
15
|
-
break;
|
|
16
|
-
src = iter.next();
|
|
17
|
-
// console.log(src & 0b1000_0000)
|
|
18
|
-
// if (src.done)
|
|
19
|
-
// console.warn("Unexpected end", src);
|
|
20
|
-
src = src.value;
|
|
21
|
-
len += 1;
|
|
22
|
-
if (i == 3 && (src & 240) != 0)
|
|
23
|
-
console.warn("NonZeroIntPadding");
|
|
24
|
-
result |= ((src & 127)) << (6 + 7 * i);
|
|
25
|
-
}
|
|
26
|
-
if (len > 1 && src == 0) {
|
|
27
|
-
console.warn("OverlongIntEncoding");
|
|
28
|
-
}
|
|
29
|
-
result ^= -sign;
|
|
30
|
-
return { result: result, remaining: Array.from(iter) };
|
|
31
|
-
}
|
|
32
|
-
function unpackString(pSrc) {
|
|
33
|
-
var result = pSrc.slice(0, pSrc.indexOf(0));
|
|
34
|
-
pSrc = pSrc.slice(pSrc.indexOf(0), pSrc.length);
|
|
35
|
-
return { result: decoder.decode(new Uint8Array(result)), remaining: pSrc };
|
|
36
|
-
}
|
|
37
|
-
module.exports = { unpackInt: unpackInt, unpackString: unpackString };
|