teeworlds 2.0.4 → 2.0.7

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/lib/client.js DELETED
@@ -1,536 +0,0 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- extendStatics(d, b);
11
- function __() { this.constructor = d; }
12
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
- };
14
- })();
15
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
16
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
17
- return new (P || (P = Promise))(function (resolve, reject) {
18
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
19
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
20
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
21
- step((generator = generator.apply(thisArg, _arguments || [])).next());
22
- });
23
- };
24
- var __generator = (this && this.__generator) || function (thisArg, body) {
25
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
26
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
27
- function verb(n) { return function (v) { return step([n, v]); }; }
28
- function step(op) {
29
- if (f) throw new TypeError("Generator is already executing.");
30
- while (_) try {
31
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
32
- if (y = 0, t) op = [op[0] & 2, t.value];
33
- switch (op[0]) {
34
- case 0: case 1: t = op; break;
35
- case 4: _.label++; return { value: op[1], done: false };
36
- case 5: _.label++; y = op[1]; op = [0]; continue;
37
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
38
- default:
39
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
40
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
41
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
42
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
43
- if (t[2]) _.ops.pop();
44
- _.trys.pop(); continue;
45
- }
46
- op = body.call(thisArg, _);
47
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
48
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
49
- }
50
- };
51
- var __importDefault = (this && this.__importDefault) || function (mod) {
52
- return (mod && mod.__esModule) ? mod : { "default": mod };
53
- };
54
- var crypto_1 = require("crypto");
55
- var dgram_1 = __importDefault(require("dgram"));
56
- var stream_1 = require("stream");
57
- var child_process_1 = require("child_process");
58
- var MsgUnpacker_1 = __importDefault(require("./MsgUnpacker"));
59
- var MsgPacker_1 = __importDefault(require("./MsgPacker"));
60
- var snapshot_1 = require("./snapshot");
61
- var SnapUnpacker = new snapshot_1.Snapshot();
62
- var NETMSGTYPE;
63
- (function (NETMSGTYPE) {
64
- NETMSGTYPE[NETMSGTYPE["EX"] = 0] = "EX";
65
- NETMSGTYPE[NETMSGTYPE["SV_MOTD"] = 1] = "SV_MOTD";
66
- NETMSGTYPE[NETMSGTYPE["SV_BROADCAST"] = 2] = "SV_BROADCAST";
67
- NETMSGTYPE[NETMSGTYPE["SV_CHAT"] = 3] = "SV_CHAT";
68
- NETMSGTYPE[NETMSGTYPE["SV_KILLMSG"] = 4] = "SV_KILLMSG";
69
- NETMSGTYPE[NETMSGTYPE["SV_SOUNDGLOBAL"] = 5] = "SV_SOUNDGLOBAL";
70
- NETMSGTYPE[NETMSGTYPE["SV_TUNEPARAMS"] = 6] = "SV_TUNEPARAMS";
71
- NETMSGTYPE[NETMSGTYPE["SV_EXTRAPROJECTILE"] = 7] = "SV_EXTRAPROJECTILE";
72
- NETMSGTYPE[NETMSGTYPE["SV_READYTOENTER"] = 8] = "SV_READYTOENTER";
73
- NETMSGTYPE[NETMSGTYPE["SV_WEAPONPICKUP"] = 9] = "SV_WEAPONPICKUP";
74
- NETMSGTYPE[NETMSGTYPE["SV_EMOTICON"] = 10] = "SV_EMOTICON";
75
- NETMSGTYPE[NETMSGTYPE["SV_VOTECLEAROPTIONS"] = 11] = "SV_VOTECLEAROPTIONS";
76
- NETMSGTYPE[NETMSGTYPE["SV_VOTEOPTIONLISTADD"] = 12] = "SV_VOTEOPTIONLISTADD";
77
- NETMSGTYPE[NETMSGTYPE["SV_VOTEOPTIONADD"] = 13] = "SV_VOTEOPTIONADD";
78
- NETMSGTYPE[NETMSGTYPE["SV_VOTEOPTIONREMOVE"] = 14] = "SV_VOTEOPTIONREMOVE";
79
- NETMSGTYPE[NETMSGTYPE["SV_VOTESET"] = 15] = "SV_VOTESET";
80
- NETMSGTYPE[NETMSGTYPE["SV_VOTESTATUS"] = 16] = "SV_VOTESTATUS";
81
- NETMSGTYPE[NETMSGTYPE["CL_SAY"] = 17] = "CL_SAY";
82
- NETMSGTYPE[NETMSGTYPE["CL_SETTEAM"] = 18] = "CL_SETTEAM";
83
- NETMSGTYPE[NETMSGTYPE["CL_SETSPECTATORMODE"] = 19] = "CL_SETSPECTATORMODE";
84
- NETMSGTYPE[NETMSGTYPE["CL_STARTINFO"] = 20] = "CL_STARTINFO";
85
- NETMSGTYPE[NETMSGTYPE["CL_CHANGEINFO"] = 21] = "CL_CHANGEINFO";
86
- NETMSGTYPE[NETMSGTYPE["CL_KILL"] = 22] = "CL_KILL";
87
- NETMSGTYPE[NETMSGTYPE["CL_EMOTICON"] = 23] = "CL_EMOTICON";
88
- NETMSGTYPE[NETMSGTYPE["CL_VOTE"] = 24] = "CL_VOTE";
89
- NETMSGTYPE[NETMSGTYPE["CL_CALLVOTE"] = 25] = "CL_CALLVOTE";
90
- NETMSGTYPE[NETMSGTYPE["CL_ISDDNETLEGACY"] = 26] = "CL_ISDDNETLEGACY";
91
- NETMSGTYPE[NETMSGTYPE["SV_DDRACETIMELEGACY"] = 27] = "SV_DDRACETIMELEGACY";
92
- NETMSGTYPE[NETMSGTYPE["SV_RECORDLEGACY"] = 28] = "SV_RECORDLEGACY";
93
- NETMSGTYPE[NETMSGTYPE["UNUSED"] = 29] = "UNUSED";
94
- NETMSGTYPE[NETMSGTYPE["SV_TEAMSSTATELEGACY"] = 30] = "SV_TEAMSSTATELEGACY";
95
- NETMSGTYPE[NETMSGTYPE["CL_SHOWOTHERSLEGACY"] = 31] = "CL_SHOWOTHERSLEGACY";
96
- NETMSGTYPE[NETMSGTYPE["NUM"] = 32] = "NUM";
97
- })(NETMSGTYPE || (NETMSGTYPE = {}));
98
- var items;
99
- (function (items) {
100
- items[items["OBJ_EX"] = 0] = "OBJ_EX";
101
- items[items["OBJ_PLAYER_INPUT"] = 1] = "OBJ_PLAYER_INPUT";
102
- items[items["OBJ_PROJECTILE"] = 2] = "OBJ_PROJECTILE";
103
- items[items["OBJ_LASER"] = 3] = "OBJ_LASER";
104
- items[items["OBJ_PICKUP"] = 4] = "OBJ_PICKUP";
105
- items[items["OBJ_FLAG"] = 5] = "OBJ_FLAG";
106
- items[items["OBJ_GAME_INFO"] = 6] = "OBJ_GAME_INFO";
107
- items[items["OBJ_GAME_DATA"] = 7] = "OBJ_GAME_DATA";
108
- items[items["OBJ_CHARACTER_CORE"] = 8] = "OBJ_CHARACTER_CORE";
109
- items[items["OBJ_CHARACTER"] = 9] = "OBJ_CHARACTER";
110
- items[items["OBJ_PLAYER_INFO"] = 10] = "OBJ_PLAYER_INFO";
111
- items[items["OBJ_CLIENT_INFO"] = 11] = "OBJ_CLIENT_INFO";
112
- items[items["OBJ_SPECTATOR_INFO"] = 12] = "OBJ_SPECTATOR_INFO";
113
- items[items["EVENT_COMMON"] = 13] = "EVENT_COMMON";
114
- items[items["EVENT_EXPLOSION"] = 14] = "EVENT_EXPLOSION";
115
- items[items["EVENT_SPAWN"] = 15] = "EVENT_SPAWN";
116
- items[items["EVENT_HAMMERHIT"] = 16] = "EVENT_HAMMERHIT";
117
- items[items["EVENT_DEATH"] = 17] = "EVENT_DEATH";
118
- items[items["EVENT_SOUND_GLOBAL"] = 18] = "EVENT_SOUND_GLOBAL";
119
- items[items["EVENT_SOUND_WORLD"] = 19] = "EVENT_SOUND_WORLD";
120
- items[items["EVENT_DAMAGE_INDICATOR"] = 20] = "EVENT_DAMAGE_INDICATOR";
121
- })(items || (items = {}));
122
- function toHexStream(buff) {
123
- return buff.toJSON().data.map(function (a) { return ('0' + (a & 0xff).toString(16)).slice(-2); }).join("");
124
- }
125
- function decompress(buff) {
126
- return __awaiter(this, void 0, void 0, function () {
127
- return __generator(this, function (_a) {
128
- return [2 /*return*/, new Promise(function (resolve) {
129
- // get hex stream
130
- var hexStream = toHexStream(buff);
131
- var ls = child_process_1.spawn('python', [__dirname + '\\huffman.py', hexStream, "-decompress"]);
132
- ls.stdout.on('data', function (data) {
133
- resolve(Buffer.from(eval(data.toString()))); // convert stdout array to actual array, then convert the array to Buffer & return it
134
- });
135
- setTimeout(function () {
136
- ls.stdin.end();
137
- ls.stdout.destroy();
138
- ls.stderr.destroy();
139
- resolve(Buffer.from([]));
140
- }, 750);
141
- })];
142
- });
143
- });
144
- }
145
- var messageTypes = [
146
- ["none, starts at 1", "SV_MOTD", "SV_BROADCAST", "SV_CHAT", "SV_KILL_MSG", "SV_SOUND_GLOBAL", "SV_TUNE_PARAMS", "SV_EXTRA_PROJECTILE", "SV_READY_TO_ENTER", "SV_WEAPON_PICKUP", "SV_EMOTICON", "SV_VOTE_CLEAR_OPTIONS", "SV_VOTE_OPTION_LIST_ADD", "SV_VOTE_OPTION_ADD", "SV_VOTE_OPTION_REMOVE", "SV_VOTE_SET", "SV_VOTE_STATUS", "CL_SAY", "CL_SET_TEAM", "CL_SET_SPECTATOR_MODE", "CL_START_INFO", "CL_CHANGE_INFO", "CL_KILL", "CL_EMOTICON", "CL_VOTE", "CL_CALL_VOTE", "CL_IS_DDNET", "SV_DDRACE_TIME", "SV_RECORD", "UNUSED", "SV_TEAMS_STATE", "CL_SHOW_OTHERS_LEGACY"],
147
- ["none, starts at 1", "INFO", "MAP_CHANGE", "MAP_DATA", "CON_READY", "SNAP", "SNAP_EMPTY", "SNAP_SINGLE", "INPUT_TIMING", "RCON_AUTH_STATUS", "RCON_LINE", "READY", "ENTER_GAME", "INPUT", "RCON_CMD", "RCON_AUTH", "REQUEST_MAP_DATA", "PING", "PING_REPLY", "RCON_CMD_ADD", "RCON_CMD_REMOVE"]
148
- ];
149
- var messageUUIDs = {
150
- "WHAT_IS": Buffer.from([0x24, 0x5e, 0x50, 0x97, 0x9f, 0xe0, 0x39, 0xd6, 0xbf, 0x7d, 0x9a, 0x29, 0xe1, 0x69, 0x1e, 0x4c]),
151
- "IT_IS": Buffer.from([0x69, 0x54, 0x84, 0x7e, 0x2e, 0x87, 0x36, 0x03, 0xb5, 0x62, 0x36, 0xda, 0x29, 0xed, 0x1a, 0xca]),
152
- "I_DONT_KNOW": Buffer.from([0x41, 0x69, 0x11, 0xb5, 0x79, 0x73, 0x33, 0xbf, 0x8d, 0x52, 0x7b, 0xf0, 0x1e, 0x51, 0x9c, 0xf0]),
153
- "RCON_TYPE": Buffer.from([0x12, 0x81, 0x0e, 0x1f, 0xa1, 0xdb, 0x33, 0x78, 0xb4, 0xfb, 0x16, 0x4e, 0xd6, 0x50, 0x59, 0x26]),
154
- "MAP_DETAILS": Buffer.from([0xf9, 0x11, 0x7b, 0x3c, 0x80, 0x39, 0x34, 0x16, 0x9f, 0xc0, 0xae, 0xf2, 0xbc, 0xb7, 0x5c, 0x03]),
155
- "CLIENT_VERSION": Buffer.from([0x8c, 0x00, 0x13, 0x04, 0x84, 0x61, 0x3e, 0x47, 0x87, 0x87, 0xf6, 0x72, 0xb3, 0x83, 0x5b, 0xd4]),
156
- "CAPABILITIES": Buffer.from([0xf6, 0x21, 0xa5, 0xa1, 0xf5, 0x85, 0x37, 0x75, 0x8e, 0x73, 0x41, 0xbe, 0xee, 0x79, 0xf2, 0xb2]),
157
- };
158
- function arrStartsWith(arr, arrStart, start) {
159
- if (start === void 0) { start = 0; }
160
- arr.splice(0, start);
161
- for (var i = 0; i < arrStart.length; i++) {
162
- if (arr[i] == arrStart[i])
163
- continue;
164
- else
165
- return false;
166
- }
167
- return true;
168
- }
169
- var Client = /** @class */ (function (_super) {
170
- __extends(Client, _super);
171
- function Client(ip, port, nickname) {
172
- var _this = _super.call(this) || this;
173
- _this.host = ip;
174
- _this.port = port;
175
- _this.name = nickname;
176
- _this.snaps = [];
177
- _this.client_infos = [];
178
- _this.player_infos = [];
179
- _this.State = 0; // 0 = offline; 1 = STATE_CONNECTING = 1, STATE_LOADING = 2, STATE_ONLINE = 3
180
- _this.ack = 0; // ack of messages the client has received
181
- _this.clientAck = 0; // ack of messages the client has sent
182
- _this.receivedSnaps = 0; /* wait for 2 snaps before seeing self as connected */
183
- _this.lastMsg = "";
184
- _this._port = Math.floor(Math.random() * 65535);
185
- _this.socket = dgram_1.default.createSocket("udp4");
186
- _this.socket.bind();
187
- _this.TKEN = Buffer.from([255, 255, 255, 255]);
188
- _this.time = new Date().getTime() + 2000; // time (used for keepalives, start to send keepalives after 2 seconds)
189
- _this.State = 0;
190
- return _this;
191
- }
192
- Client.prototype.Unpack = function (packet) {
193
- return __awaiter(this, void 0, void 0, function () {
194
- var unpacked, i, chunk;
195
- return __generator(this, function (_a) {
196
- switch (_a.label) {
197
- case 0:
198
- unpacked = { twprotocol: { flags: packet[0], ack: packet[1], chunkAmount: packet[2], size: packet.byteLength - 3 }, chunks: [] };
199
- if (packet.indexOf(Buffer.from([0xff, 0xff, 0xff, 0xff])) == 0 && !(unpacked.twprotocol.flags & 8) || unpacked.twprotocol.flags == 255) // flags == 255 is connectionless (used for sending usernames)
200
- return [2 /*return*/, unpacked];
201
- packet = packet.slice(3);
202
- if (!(unpacked.twprotocol.flags & 128)) return [3 /*break*/, 2];
203
- return [4 /*yield*/, decompress(packet)];
204
- case 1:
205
- packet = _a.sent();
206
- if (packet.length == 1 && packet[0] == -1)
207
- return [2 /*return*/, unpacked];
208
- _a.label = 2;
209
- case 2:
210
- // return unpacked;
211
- for (i = 0; i < unpacked.twprotocol.chunkAmount; i++) {
212
- chunk = {};
213
- chunk.bytes = ((packet[0] & 0x3f) << 4) | (packet[1] & ((1 << 4) - 1)); // idk what this shit is but it works
214
- chunk.flags = (packet[0] >> 6) & 3;
215
- chunk.sequence = -1;
216
- if (chunk.flags & 1) {
217
- chunk.seq = ((packet[1] & 0xf0) << 2) | packet[2];
218
- packet = packet.slice(3); // remove flags & size
219
- }
220
- else
221
- packet = packet.slice(2);
222
- chunk.type = packet[0] & 1 ? "sys" : "game"; // & 1 = binary, ****_***1. e.g 0001_0111 sys, 0001_0110 game
223
- chunk.msgid = (packet[0] - (packet[0] & 1)) / 2;
224
- chunk.msg = messageTypes[packet[0] & 1][chunk.msgid];
225
- chunk.raw = packet.slice(1, chunk.bytes);
226
- Object.values(messageUUIDs).forEach(function (a, i) {
227
- if (a.compare(packet.slice(0, 16)) == 0) {
228
- chunk.extended_msgid = a;
229
- // chunk.type = 'sys';
230
- chunk.msg = Object.keys(messageUUIDs)[i];
231
- }
232
- });
233
- packet = packet.slice(chunk.bytes); // +1 cuz it adds an extra \x00 for easier parsing i guess
234
- unpacked.chunks.push(chunk);
235
- }
236
- return [2 /*return*/, unpacked];
237
- }
238
- });
239
- });
240
- };
241
- Client.prototype.SendControlMsg = function (msg, ExtraMsg) {
242
- var _this = this;
243
- if (ExtraMsg === void 0) { ExtraMsg = ""; }
244
- return new Promise(function (resolve, reject) {
245
- if (_this.socket) {
246
- var latestBuf = Buffer.from([0x10 + (((16 << 4) & 0xf0) | ((_this.ack >> 8) & 0xf)), _this.ack & 0xff, 0x00, msg]);
247
- latestBuf = Buffer.concat([latestBuf, Buffer.from(ExtraMsg), _this.TKEN]); // move header (latestBuf), optional extraMsg & TKEN into 1 buffer
248
- _this.socket.send(latestBuf, 0, latestBuf.length, _this.port, _this.host, function (err, bytes) {
249
- resolve(bytes);
250
- });
251
- }
252
- setTimeout(function () { resolve("failed, rip"); }, 2000);
253
- /* after 2 seconds it was probably not able to send,
254
- so when sending a quit message the user doesnt
255
- stay stuck not being able to ctrl + c
256
- */
257
- });
258
- };
259
- Client.prototype.SendMsgEx = function (Msg, Flags) {
260
- if (this.State == -1)
261
- throw new Error("Client is not connected");
262
- if (!this.socket)
263
- return;
264
- var header = [];
265
- header[0] = ((Flags & 3) << 6) | ((Msg.size >> 4) & 0x3f);
266
- header[1] = (Msg.size & 0xf);
267
- if (Flags & 1) {
268
- this.clientAck = (this.clientAck + 1) % (1 << 10);
269
- header[1] |= (this.clientAck >> 2) & 0xf0;
270
- header[2] = this.clientAck & 0xff;
271
- }
272
- var latestBuf = Buffer.from([0x0 + (((16 << 4) & 0xf0) | ((this.ack >> 8) & 0xf)), this.ack & 0xff, 0x1, header[0], header[1], this.clientAck]);
273
- var latestBuf = Buffer.concat([latestBuf, Msg.buffer, this.TKEN]);
274
- this.socket.send(latestBuf, 0, latestBuf.length, this.port, this.host);
275
- };
276
- Client.prototype.SendMsgExWithChunks = function (Msgs, Flags) {
277
- var _this = this;
278
- if (this.State == -1)
279
- throw new Error("Client is not connected");
280
- if (!this.socket)
281
- return;
282
- var header = [];
283
- Msgs.forEach(function (Msg, index) {
284
- header[index] = new Array(2);
285
- header[index][0] = ((Flags & 3) << 6) | ((Msg.size >> 4) & 0x3f);
286
- header[index][1] = (Msg.size & 0xf);
287
- if (Flags & 1) {
288
- _this.clientAck = (_this.clientAck + 1) % (1 << 10);
289
- header[index][1] |= (_this.clientAck >> 2) & 0xf0;
290
- header[index][2] = _this.clientAck & 0xff;
291
- }
292
- });
293
- var packetHeader = Buffer.from([0x0 + (((16 << 4) & 0xf0) | ((this.ack >> 8) & 0xf)), this.ack & 0xff, Msgs.length]);
294
- var chunks = Buffer.from([]);
295
- Msgs.forEach(function (Msg, index) {
296
- chunks = Buffer.concat([chunks, Buffer.from(header[index]), Msg.buffer]);
297
- });
298
- var packet = Buffer.concat([(packetHeader), chunks, this.TKEN]);
299
- this.socket.send(packet, 0, packet.length, this.port, this.host);
300
- };
301
- Client.prototype.connect = function () {
302
- var _this = this;
303
- this.SendControlMsg(1, "TKEN");
304
- var connectInterval = setInterval(function () {
305
- if (_this.State == 0)
306
- _this.SendControlMsg(1, "TKEN");
307
- else
308
- clearInterval(connectInterval);
309
- }, 500);
310
- this.time = new Date().getTime() + 2000; // start sending keepalives after 2s
311
- if (this.socket)
312
- this.socket.on("message", function (a) { return __awaiter(_this, void 0, void 0, function () {
313
- var unpacked, chunkMessages, chat, chat, info, client_version, randomUuid, reason, Msg, Msg, info, info, chunks, part_1, num_parts_1;
314
- var _this = this;
315
- return __generator(this, function (_a) {
316
- switch (_a.label) {
317
- case 0: return [4 /*yield*/, this.Unpack(a)];
318
- case 1:
319
- unpacked = _a.sent();
320
- if (unpacked.twprotocol.flags != 128 && unpacked.twprotocol.ack) {
321
- unpacked.chunks.forEach(function (a) {
322
- if (a.msg && !a.msg.startsWith("SNAP")) {
323
- if (a.seq != undefined && a.seq != -1)
324
- _this.ack = a.seq;
325
- }
326
- });
327
- }
328
- chunkMessages = unpacked.chunks.map(function (a) { return a.msg; });
329
- if (chunkMessages.includes("SV_CHAT")) {
330
- chat = unpacked.chunks.filter(function (a) { return a.msg == "SV_CHAT"; });
331
- chat.forEach(function (a) {
332
- if (a.msg == "SV_CHAT") {
333
- var unpacked = {};
334
- unpacked.team = MsgUnpacker_1.default.unpackInt(a.raw.toJSON().data).result;
335
- var remaining = MsgUnpacker_1.default.unpackInt(a.raw.toJSON().data).remaining;
336
- unpacked.client_id = MsgUnpacker_1.default.unpackInt(remaining).result;
337
- remaining = MsgUnpacker_1.default.unpackInt(remaining).remaining;
338
- unpacked.message = MsgUnpacker_1.default.unpackString(remaining).result;
339
- if (unpacked.client_id != -1)
340
- unpacked.author = { ClientInfo: _this.client_infos[unpacked.client_id], PlayerInfo: _this.player_infos[unpacked.client_id] };
341
- // console.log(unpacked)
342
- _this.emit("message", unpacked);
343
- }
344
- });
345
- }
346
- if (chunkMessages.includes("SV_KILL_MSG")) {
347
- chat = unpacked.chunks.filter(function (a) { return a.msg == "SV_KILL_MSG"; });
348
- chat.forEach(function (a) {
349
- if (a.msg == "SV_KILL_MSG") {
350
- var unpacked = {};
351
- unpacked.killer_id = MsgUnpacker_1.default.unpackInt(a.raw.toJSON().data).result;
352
- var remaining = MsgUnpacker_1.default.unpackInt(a.raw.toJSON().data).remaining;
353
- unpacked.victim_id = MsgUnpacker_1.default.unpackInt(remaining).result;
354
- remaining = MsgUnpacker_1.default.unpackInt(remaining).remaining;
355
- unpacked.weapon = MsgUnpacker_1.default.unpackInt(remaining).result;
356
- remaining = MsgUnpacker_1.default.unpackInt(remaining).remaining;
357
- unpacked.special_mode = MsgUnpacker_1.default.unpackInt(remaining).result;
358
- if (unpacked.victim_id != -1)
359
- unpacked.victim = { ClientInfo: _this.client_infos[unpacked.victim_id], PlayerInfo: _this.player_infos[unpacked.victim_id] };
360
- if (unpacked.killer_id != -1)
361
- unpacked.killer = { ClientInfo: _this.client_infos[unpacked.killer_id], PlayerInfo: _this.player_infos[unpacked.killer_id] };
362
- // console.log(unpacked)
363
- _this.emit("kill", unpacked);
364
- }
365
- });
366
- }
367
- if (a.toJSON().data[0] == 0x10) {
368
- if (a.toString().includes("TKEN") || arrStartsWith(a.toJSON().data, [0x10, 0x0, 0x0, 0x0])) {
369
- clearInterval(connectInterval);
370
- this.TKEN = Buffer.from(a.toJSON().data.slice(a.toJSON().data.length - 4, a.toJSON().data.length));
371
- this.SendControlMsg(3);
372
- this.State = 2; // loading state
373
- info = new MsgPacker_1.default(1, true);
374
- info.AddString("0.6 626fce9a778df4d4");
375
- info.AddString(""); // password
376
- client_version = new MsgPacker_1.default(0, true);
377
- client_version.AddBuffer(Buffer.from("8c00130484613e478787f672b3835bd4", 'hex'));
378
- randomUuid = new Uint8Array(16);
379
- crypto_1.randomBytes(16).copy(randomUuid);
380
- client_version.AddBuffer(Buffer.from(randomUuid));
381
- client_version.AddInt(15091);
382
- client_version.AddString("DDNet 15.9.1");
383
- this.SendMsgExWithChunks([client_version, info], 1);
384
- }
385
- else if (a.toJSON().data[3] == 0x4) {
386
- // disconnected
387
- this.State = 0;
388
- reason = (MsgUnpacker_1.default.unpackString(a.toJSON().data.slice(4)).result);
389
- this.State = -1;
390
- this.emit("disconnect", reason);
391
- }
392
- }
393
- if (unpacked.chunks[0] && chunkMessages.includes("SV_READY_TO_ENTER")) {
394
- Msg = new MsgPacker_1.default(15, true);
395
- this.SendMsgEx(Msg, 1);
396
- }
397
- else if ((unpacked.chunks[0] && chunkMessages.includes("CAPABILITIES") || unpacked.chunks[0] && chunkMessages.includes("MAP_CHANGE"))) {
398
- Msg = new MsgPacker_1.default(14, true);
399
- this.SendMsgEx(Msg, 1);
400
- }
401
- else if ((unpacked.chunks[0] && chunkMessages.includes("CON_READY") || unpacked.chunks[0] && chunkMessages.includes("SV_MOTD"))) {
402
- info = new MsgPacker_1.default(20, false);
403
- info.AddString(this.name); /* name */
404
- info.AddString(""); /* clan */
405
- info.AddInt(-1); /* country */
406
- info.AddString("greyfox"); /* skin */
407
- info.AddInt(1); /* use custom color */
408
- info.AddInt(10346103); /* color body */
409
- info.AddInt(65535); /* color feet */
410
- this.SendMsgEx(info, 1);
411
- }
412
- else if (unpacked.chunks[0] && chunkMessages.includes("SV_READY_TO_ENTER")) {
413
- if (this.State != 3) {
414
- this.emit('connected');
415
- }
416
- this.State = 3;
417
- }
418
- else if (unpacked.chunks[0] && chunkMessages.includes("PING")) {
419
- info = new MsgPacker_1.default(23, true);
420
- this.SendMsgEx(info, 1);
421
- }
422
- if (chunkMessages.includes("SNAP") || chunkMessages.includes("SNAP_EMPTY") || chunkMessages.includes("SNAP_SINGLE")) {
423
- this.receivedSnaps++; /* wait for 2 ss before seeing self as connected */
424
- if (this.receivedSnaps >= 2) {
425
- if (this.State != 3)
426
- this.emit('connected');
427
- this.State = 3;
428
- }
429
- chunks = unpacked.chunks.filter(function (a) { return a.msg == "SNAP" || a.msg == "SNAP_SINGLE" || a.msg == "SNAP_EMPTY"; });
430
- if (chunks.length > 0) {
431
- part_1 = 0;
432
- num_parts_1 = 1;
433
- chunks.forEach(function (chunk) {
434
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
435
- var AckGameTick = (MsgUnpacker_1.default.unpackInt(chunk.raw.toJSON().data).result);
436
- chunk.raw = Buffer.from(MsgUnpacker_1.default.unpackInt((_a = chunk === null || chunk === void 0 ? void 0 : chunk.raw) === null || _a === void 0 ? void 0 : _a.toJSON().data).remaining);
437
- var DeltaTick = MsgUnpacker_1.default.unpackInt((_b = chunk === null || chunk === void 0 ? void 0 : chunk.raw) === null || _b === void 0 ? void 0 : _b.toJSON().data).result;
438
- if (chunk.msg == "SNAP") {
439
- chunk.raw = Buffer.from(MsgUnpacker_1.default.unpackInt((_c = chunk === null || chunk === void 0 ? void 0 : chunk.raw) === null || _c === void 0 ? void 0 : _c.toJSON().data).remaining); // delta tick
440
- num_parts_1 = (MsgUnpacker_1.default.unpackInt((_d = chunk === null || chunk === void 0 ? void 0 : chunk.raw) === null || _d === void 0 ? void 0 : _d.toJSON().data).result);
441
- chunk.raw = Buffer.from(MsgUnpacker_1.default.unpackInt((_e = chunk === null || chunk === void 0 ? void 0 : chunk.raw) === null || _e === void 0 ? void 0 : _e.toJSON().data).remaining); // num parts
442
- part_1 = (MsgUnpacker_1.default.unpackInt((_f = chunk === null || chunk === void 0 ? void 0 : chunk.raw) === null || _f === void 0 ? void 0 : _f.toJSON().data).result);
443
- }
444
- chunk.raw = Buffer.from(MsgUnpacker_1.default.unpackInt((_g = chunk === null || chunk === void 0 ? void 0 : chunk.raw) === null || _g === void 0 ? void 0 : _g.toJSON().data).remaining); // part
445
- if (chunk.msg != "SNAP_EMPTY")
446
- chunk.raw = Buffer.from(MsgUnpacker_1.default.unpackInt((_h = chunk === null || chunk === void 0 ? void 0 : chunk.raw) === null || _h === void 0 ? void 0 : _h.toJSON().data).remaining); // crc
447
- chunk.raw = Buffer.from(MsgUnpacker_1.default.unpackInt((_j = chunk === null || chunk === void 0 ? void 0 : chunk.raw) === null || _j === void 0 ? void 0 : _j.toJSON().data).remaining); // crc
448
- if (part_1 == 0 || _this.snaps.length > 30) {
449
- _this.snaps = [];
450
- }
451
- _this.snaps.push(chunk.raw);
452
- if ((num_parts_1 - 1) == part_1 && _this.snaps.length == num_parts_1) {
453
- var mergedSnaps = Buffer.concat(_this.snaps);
454
- var snapUnpacked = SnapUnpacker.unpackSnapshot(mergedSnaps.toJSON().data, 1);
455
- snapUnpacked.items.forEach(function (a, i) {
456
- if (a.type_id == items.OBJ_CLIENT_INFO) {
457
- _this.client_infos[a.id] = a.parsed;
458
- // console.log(a.parsed, i)
459
- // console.log(this.client_infos[a.id])
460
- }
461
- else if (a.type_id == items.OBJ_PLAYER_INFO) {
462
- _this.player_infos[i] = a.parsed;
463
- }
464
- else if (a.type_id == items.OBJ_EX || a.type_id > 0x4000) {
465
- if (a.data.length == 5 && (a.parsed.freeze_end > 0 || a.parsed.freeze_end == -1)) {
466
- // var packer = new MsgPacker(22, false)
467
- // this.SendMsgEx(packer, 1)
468
- }
469
- }
470
- });
471
- }
472
- });
473
- }
474
- }
475
- if (new Date().getTime() - this.time >= 1000) {
476
- this.time = new Date().getTime();
477
- this.SendControlMsg(0);
478
- }
479
- return [2 /*return*/];
480
- }
481
- });
482
- }); });
483
- };
484
- Client.prototype.Disconnect = function () {
485
- var _this = this;
486
- return new Promise(function (resolve) {
487
- _this.SendControlMsg(4).then(function () {
488
- resolve(true);
489
- if (_this.socket)
490
- _this.socket.close();
491
- _this.socket = undefined;
492
- _this.State = -1;
493
- });
494
- });
495
- };
496
- Client.prototype.Say = function (message, team) {
497
- if (team === void 0) { team = false; }
498
- var packer = new MsgPacker_1.default(NETMSGTYPE.CL_SAY, false);
499
- packer.AddInt(team ? 1 : 0); // team
500
- packer.AddString(message);
501
- this.SendMsgEx(packer, 1);
502
- };
503
- Client.prototype.Vote = function (vote) {
504
- var packer = new MsgPacker_1.default(NETMSGTYPE.CL_VOTE, false);
505
- packer.AddInt(vote ? 1 : 0);
506
- this.SendMsgEx(packer, 1);
507
- };
508
- Client.prototype.ChangePlayerInfo = function (playerInfo) {
509
- var packer = new MsgPacker_1.default(NETMSGTYPE.CL_CHANGEINFO, false);
510
- packer.AddString(playerInfo.name); //m_pName);
511
- packer.AddString(playerInfo.clan); //m_pClan);
512
- packer.AddInt(playerInfo.country); //m_Country);
513
- packer.AddString(playerInfo.skin); //m_pSkin);
514
- packer.AddInt(playerInfo.use_custom_color ? 1 : 0); //m_UseCustomColor);
515
- packer.AddInt(playerInfo.color_body); //m_ColorBody);
516
- packer.AddInt(playerInfo.color_feet); //m_ColorFeet);
517
- this.SendMsgEx(packer, 1);
518
- };
519
- Client.prototype.Kill = function () {
520
- var packer = new MsgPacker_1.default(NETMSGTYPE.CL_KILL, false);
521
- this.SendMsgEx(packer, 1);
522
- };
523
- Client.prototype.ChangeTeam = function (team) {
524
- var packer = new MsgPacker_1.default(NETMSGTYPE.CL_SETTEAM, false);
525
- packer.AddInt(team);
526
- this.SendMsgEx(packer, 1);
527
- };
528
- Client.prototype.Emote = function (emote) {
529
- var packer = new MsgPacker_1.default(NETMSGTYPE.CL_EMOTICON, false);
530
- packer.AddInt(emote);
531
- this.SendMsgEx(packer, 1);
532
- };
533
- return Client;
534
- }(stream_1.EventEmitter));
535
- module.exports = Client;
536
- // module.exports = Client;