teeworlds 2.0.3 → 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 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 test = require('teeworlds');
7
- let client = new test.Client("127.0.0.1", 8303, "test");
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/client.ts CHANGED
@@ -7,7 +7,9 @@ import { spawn } from 'child_process';
7
7
  import MsgUnpacker from './MsgUnpacker';
8
8
  import MsgPacker from './MsgPacker';
9
9
  import { Snapshot } from './snapshot';
10
+ import Huffman from "./huffman";
10
11
 
12
+ const huff = new Huffman();
11
13
  const SnapUnpacker = new Snapshot();
12
14
 
13
15
  enum NETMSGTYPE {
@@ -83,24 +85,6 @@ interface chunk {
83
85
  function toHexStream(buff: Buffer): string {
84
86
  return buff.toJSON().data.map(a => ('0' + (a & 0xff).toString(16)).slice(-2)).join("");
85
87
  }
86
- async function decompress(buff: Buffer): Promise<Buffer> {
87
- return new Promise((resolve) => {
88
- // get hex stream
89
- var hexStream = toHexStream(buff)
90
- var ls = spawn('python', [__dirname + '\\huffman.py', hexStream, "-decompress"])
91
- ls.stdout.on('data', (data) => {
92
- resolve(Buffer.from(eval(data.toString()))); // convert stdout array to actual array, then convert the array to Buffer & return it
93
- });
94
- setTimeout(() => {
95
- ls.stdin.end();
96
- ls.stdout.destroy();
97
- ls.stderr.destroy();
98
-
99
- resolve(Buffer.from([]));
100
- }, 750)
101
-
102
- })
103
- }
104
88
  interface _packet {
105
89
  twprotocol: { flags: number, ack: number, chunkAmount: number, size: number },
106
90
  chunks: chunk[]
@@ -156,7 +140,7 @@ declare interface Client {
156
140
  receivedSnaps: number; /* wait for 2 ss before seeing self as connected */
157
141
  lastMsg: string;
158
142
  _port: number;
159
- socket: net.Socket;
143
+ socket: net.Socket | undefined;
160
144
  TKEN: Buffer;
161
145
  time: number;
162
146
 
@@ -207,7 +191,7 @@ class Client extends EventEmitter {
207
191
  return unpacked;
208
192
  packet = packet.slice(3)
209
193
  if (unpacked.twprotocol.flags & 128) {
210
- packet = await decompress(packet)
194
+ packet = huff.decompress(packet)
211
195
  if (packet.length == 1 && packet[0] == -1)
212
196
  return unpacked
213
197
  }
@@ -310,6 +294,7 @@ class Client extends EventEmitter {
310
294
  clearInterval(connectInterval)
311
295
  }, 500)
312
296
  this.time = new Date().getTime() + 2000; // start sending keepalives after 2s
297
+ if (this.socket)
313
298
  this.socket.on("message", async (a) => {
314
299
  var unpacked: _packet = await this.Unpack(a)
315
300
  if (unpacked.twprotocol.flags != 128 && unpacked.twprotocol.ack) {
@@ -485,7 +470,9 @@ class Client extends EventEmitter {
485
470
  return new Promise((resolve) => {
486
471
  this.SendControlMsg(4).then(() => {
487
472
  resolve(true);
488
- this.socket.close();
473
+ if (this.socket)
474
+ this.socket.close();
475
+ this.socket = undefined
489
476
  this.State = -1;
490
477
  })
491
478
  })
package/lib/huffman.ts ADDED
@@ -0,0 +1,228 @@
1
+ let FREQ_TABLE = [
2
+ 1 << 30, 4545, 2657, 431, 1950, 919, 444, 482, 2244, 617, 838, 542, 715, 1814, 304, 240, 754, 212, 647, 186,
3
+ 283, 131, 146, 166, 543, 164, 167, 136, 179, 859, 363, 113, 157, 154, 204, 108, 137, 180, 202, 176,
4
+ 872, 404, 168, 134, 151, 111, 113, 109, 120, 126, 129, 100, 41, 20, 16, 22, 18, 18, 17, 19,
5
+ 16, 37, 13, 21, 362, 166, 99, 78, 95, 88, 81, 70, 83, 284, 91, 187, 77, 68, 52, 68,
6
+ 59, 66, 61, 638, 71, 157, 50, 46, 69, 43, 11, 24, 13, 19, 10, 12, 12, 20, 14, 9,
7
+ 20, 20, 10, 10, 15, 15, 12, 12, 7, 19, 15, 14, 13, 18, 35, 19, 17, 14, 8, 5,
8
+ 15, 17, 9, 15, 14, 18, 8, 10, 2173, 134, 157, 68, 188, 60, 170, 60, 194, 62, 175, 71,
9
+ 148, 67, 167, 78, 211, 67, 156, 69, 1674, 90, 174, 53, 147, 89, 181, 51, 174, 63, 163, 80,
10
+ 167, 94, 128, 122, 223, 153, 218, 77, 200, 110, 190, 73, 174, 69, 145, 66, 277, 143, 141, 60,
11
+ 136, 53, 180, 57, 142, 57, 158, 61, 166, 112, 152, 92, 26, 22, 21, 28, 20, 26, 30, 21,
12
+ 32, 27, 20, 17, 23, 21, 30, 22, 22, 21, 27, 25, 17, 27, 23, 18, 39, 26, 15, 21,
13
+ 12, 18, 18, 27, 20, 18, 15, 19, 11, 17, 33, 12, 18, 15, 19, 18, 16, 26, 17, 18,
14
+ 9, 10, 25, 22, 22, 17, 20, 16, 6, 16, 15, 20, 14, 18, 24, 335, 1517],
15
+
16
+ HUFFMAN_EOF_SYMBOL = 256,
17
+ HUFFMAN_MAX_SYMBOLS = HUFFMAN_EOF_SYMBOL + 1,
18
+ HUFFMAN_MAX_NODES = HUFFMAN_MAX_SYMBOLS * 2 - 1,
19
+ HUFFMAN_LUTBITS = 10,
20
+ HUFFMAN_LUTSIZE = 1 << HUFFMAN_LUTBITS,
21
+ HUFFMAN_LUTMASK = HUFFMAN_LUTSIZE - 1;
22
+
23
+ interface HuffmanNode {
24
+ bits: number;
25
+ numbits: number;
26
+ left: number;
27
+ right: number;
28
+ symbol: number;
29
+ }
30
+
31
+
32
+ interface HuffmanConstructNode {
33
+ node_id: number;
34
+ frequency: number;
35
+ }
36
+
37
+ class Huffman {
38
+ nodes: HuffmanNode[];
39
+ decode_lut: number[];
40
+ num_nodes: number;
41
+ start_node_index: number;
42
+
43
+ constructor(frequencies = FREQ_TABLE) {
44
+ this.nodes = new Array<HuffmanNode>(HUFFMAN_MAX_NODES);
45
+ for (let i = 0; i < HUFFMAN_MAX_NODES; i++) {
46
+ this.nodes[i] = {} as HuffmanNode;
47
+ }
48
+ this.decode_lut = new Array<number>(HUFFMAN_LUTSIZE);
49
+ this.num_nodes = 0;
50
+ this.start_node_index = 0;
51
+
52
+ this.construct_tree(frequencies);
53
+
54
+
55
+ for (let i = 0; i < HUFFMAN_LUTSIZE; i++) {
56
+ let bits = i;
57
+ let broke = false;
58
+ let index = this.start_node_index;
59
+ for (let x = 0; x < HUFFMAN_LUTBITS; x++) {
60
+ if (bits & 1)
61
+ index = this.nodes[index].right;
62
+ else
63
+ index = this.nodes[index].left;
64
+ bits >>= 1;
65
+ if (this.nodes[index].numbits) {
66
+ this.decode_lut[i] = index;
67
+ broke = true;
68
+ break;
69
+ }
70
+ }
71
+ if (!broke) {
72
+ this.decode_lut[i] = index;
73
+ }
74
+ }
75
+ }
76
+ set_bits_r(node_index: number, bits: number, depth: number) {
77
+ if (this.nodes[node_index].right != 0xffff)
78
+ this.set_bits_r(this.nodes[node_index].right, bits | (1 << depth), depth + 1)
79
+ if (this.nodes[node_index].left != 0xffff)
80
+ this.set_bits_r(this.nodes[node_index].left, bits, depth + 1)
81
+ if (this.nodes[node_index].numbits) {
82
+ this.nodes[node_index].bits = bits;
83
+ this.nodes[node_index].numbits = depth;
84
+ }
85
+ }
86
+
87
+ bubble_sort(index_list: number[], node_list: HuffmanConstructNode[], size: number) {
88
+ let changed = true;
89
+ while (changed) {
90
+ changed = false;
91
+ for (let i = 0; i < size-1; i++) {
92
+ if (node_list[index_list[i]].frequency < node_list[index_list[i + 1]].frequency) {
93
+ let temp = index_list[i];
94
+ index_list[i] = index_list[i + 1];
95
+ index_list[i + 1] = temp;
96
+ changed = true;
97
+ }
98
+ }
99
+ size--;
100
+ }
101
+ return index_list;
102
+ }
103
+
104
+ construct_tree(frequencies = FREQ_TABLE) {
105
+ let nodes_left_storage: HuffmanConstructNode[] = new Array<HuffmanConstructNode>(HUFFMAN_MAX_SYMBOLS);
106
+ for (let i = 0; i < HUFFMAN_MAX_SYMBOLS; i++) {
107
+ nodes_left_storage[i] = {} as HuffmanConstructNode;
108
+ }
109
+ let nodes_left: number[] = new Array<number>(HUFFMAN_MAX_SYMBOLS);
110
+ let num_nodes_left = HUFFMAN_MAX_SYMBOLS;
111
+
112
+ for (let i = 0; i < HUFFMAN_MAX_SYMBOLS; i++) {
113
+ this.nodes[i].numbits = 0xFFFFFFFF;
114
+ this.nodes[i].symbol = i;
115
+ this.nodes[i].left = 0xFFFF;
116
+ this.nodes[i].right = 0xFFFF;
117
+
118
+ if (i == HUFFMAN_EOF_SYMBOL) {
119
+ nodes_left_storage[i].frequency = 1;
120
+ } else
121
+ nodes_left_storage[i].frequency = frequencies[i];
122
+ nodes_left_storage[i].node_id = i;
123
+ nodes_left[i] = i;
124
+ }
125
+ this.num_nodes = HUFFMAN_MAX_SYMBOLS;
126
+
127
+ while (num_nodes_left > 1) {
128
+ nodes_left = this.bubble_sort(nodes_left, nodes_left_storage, num_nodes_left);
129
+
130
+ this.nodes[this.num_nodes].numbits = 0;
131
+ this.nodes[this.num_nodes].left = nodes_left_storage[nodes_left[num_nodes_left - 1]].node_id
132
+ this.nodes[this.num_nodes].right = nodes_left_storage[nodes_left[num_nodes_left - 2]].node_id
133
+
134
+ nodes_left_storage[nodes_left[num_nodes_left - 2]].node_id = this.num_nodes;
135
+ nodes_left_storage[nodes_left[num_nodes_left - 2]].frequency = nodes_left_storage[nodes_left[num_nodes_left - 1]].frequency
136
+ + nodes_left_storage[nodes_left[num_nodes_left - 2]].frequency
137
+ this.num_nodes++;
138
+ num_nodes_left--;
139
+ }
140
+ this.start_node_index = this.num_nodes-1;
141
+ this.set_bits_r(this.start_node_index, 0, 0);
142
+ }
143
+
144
+ compress(inp_buffer: Buffer, start_index = 0, size: number = 0): Buffer {
145
+ let output = [];
146
+ let bits = 0;
147
+ let bitcount = 0;
148
+
149
+ if (size == 0)
150
+ size = inp_buffer.byteLength;
151
+ inp_buffer = inp_buffer.slice(start_index, start_index + size);
152
+ for (let i = 0; i < size; i++) {
153
+ let x = inp_buffer[i];
154
+ bits |= this.nodes[x].bits << bitcount;
155
+ bitcount += this.nodes[x].numbits;
156
+
157
+ while (bitcount >= 8) {
158
+ output.push(bits & 0xff);
159
+ bits >>= 8;
160
+ bitcount -= 8;
161
+ }
162
+ }
163
+ bits |= this.nodes[HUFFMAN_EOF_SYMBOL].bits << bitcount;
164
+ bitcount += this.nodes[HUFFMAN_EOF_SYMBOL].numbits;
165
+
166
+ while (bitcount >= 8) {
167
+ output.push(bits & 0xff);
168
+ bits >>= 8;
169
+ bitcount -= 8;
170
+ }
171
+ output.push(bits);
172
+ return Buffer.from(output);
173
+ }
174
+ decompress(inp_buffer: Buffer, size = 0): Buffer {
175
+
176
+ let bits = 0;
177
+ let bitcount = 0;
178
+ let eof = this.nodes[HUFFMAN_EOF_SYMBOL];
179
+ let output = [];
180
+
181
+ if (size == 0)
182
+ size = inp_buffer.byteLength;
183
+ inp_buffer = inp_buffer.slice(0, size);
184
+ let src_index = 0;
185
+ while (true) {
186
+ let node_i = -1;
187
+ if (bitcount >= HUFFMAN_LUTBITS)
188
+ node_i = this.decode_lut[bits & HUFFMAN_LUTMASK];
189
+ while (bitcount < 24 && src_index != size) {
190
+ bits |= inp_buffer[src_index] << bitcount;
191
+ bitcount += 8;
192
+ src_index++;
193
+ }
194
+ if (node_i == -1)
195
+ node_i = this.decode_lut[bits & HUFFMAN_LUTMASK];
196
+ if (this.nodes[node_i].numbits) {
197
+ bits >>= this.nodes[node_i].numbits;
198
+ bitcount -= this.nodes[node_i].numbits;
199
+ } else {
200
+ bits >>= HUFFMAN_LUTBITS;
201
+ bitcount -= HUFFMAN_LUTBITS;
202
+
203
+ while (true) {
204
+ if (bits & 1) {
205
+ node_i = this.nodes[node_i].right;
206
+ } else
207
+ node_i = this.nodes[node_i].left;
208
+ bitcount -= 1;
209
+ bits >>= 1;
210
+
211
+ if (this.nodes[node_i].numbits)
212
+ break;
213
+ if (bitcount == 0)
214
+ throw new Error("No more bits, decoding error")
215
+ }
216
+
217
+ }
218
+ if (this.nodes[node_i] == eof)
219
+ break;
220
+ output.push(this.nodes[node_i].symbol);
221
+ }
222
+ return Buffer.from(output);
223
+ }
224
+
225
+
226
+ }
227
+
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "teeworlds",
3
- "version": "2.0.3",
3
+ "version": "2.0.6",
4
4
  "description": "Library for (ingame) teeworlds bots.",
5
5
  "license": "MIT",
6
6
  "main": "index.js",
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;
@@ -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 };