teeworlds 2.4.2 → 2.4.4

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.
@@ -18,7 +18,7 @@ function unpackInt(pSrc) {
18
18
  exports.unpackInt = unpackInt;
19
19
  function unpackString(pSrc) {
20
20
  var result = pSrc.slice(0, pSrc.indexOf(0));
21
- pSrc = pSrc.slice(pSrc.indexOf(0), pSrc.length);
21
+ pSrc = pSrc.slice(pSrc.indexOf(0) + 1, pSrc.length);
22
22
  return { result: decoder.decode(new Uint8Array(result)), remaining: pSrc };
23
23
  }
24
24
  exports.unpackString = unpackString;
@@ -26,17 +26,15 @@ var MsgUnpacker = /** @class */ (function () {
26
26
  function MsgUnpacker(pSrc) {
27
27
  this.remaining = pSrc;
28
28
  }
29
- MsgUnpacker.prototype.unpackInt = function (_unpacked) {
30
- if (_unpacked === void 0) { _unpacked = false; }
31
- var unpacked;
32
- if (!_unpacked) {
33
- unpacked = unpackInt(this.remaining);
34
- this.remaining = unpacked.remaining;
35
- }
36
- else {
37
- unpacked = { result: this.remaining[0] };
38
- this.remaining = this.remaining.slice(1);
39
- }
29
+ MsgUnpacker.prototype.unpackInt = function () {
30
+ // let unpacked;
31
+ // if (!_unpacked) {
32
+ var unpacked = unpackInt(this.remaining);
33
+ this.remaining = unpacked.remaining;
34
+ // } else {
35
+ // unpacked = {result: this.remaining[0]};
36
+ // this.remaining = this.remaining.slice(1);
37
+ // }
40
38
  return unpacked.result;
41
39
  };
42
40
  MsgUnpacker.prototype.unpackString = function () {
@@ -1,5 +1,5 @@
1
1
  const decoder = new TextDecoder('utf-8');
2
- export function unpackInt(pSrc: number[]): {result: number, remaining: number[]} {
2
+ export function unpackInt(pSrc: Buffer): {result: number, remaining: Buffer} {
3
3
 
4
4
  var srcIndex = 0;
5
5
  const sign = ((pSrc[srcIndex] >> 6) & 1)
@@ -18,28 +18,28 @@ export function unpackInt(pSrc: number[]): {result: number, remaining: number[]}
18
18
 
19
19
  return {result, remaining: pSrc.slice(srcIndex+1)};
20
20
  }
21
- export function unpackString(pSrc: number[]): {result: string, remaining: number[]} {
21
+ export function unpackString(pSrc: Buffer): {result: string, remaining: Buffer} {
22
22
  var result = pSrc.slice(0, pSrc.indexOf(0))
23
- pSrc = pSrc.slice(pSrc.indexOf(0), pSrc.length)
23
+ pSrc = pSrc.slice(pSrc.indexOf(0) + 1, pSrc.length)
24
24
  return {result: decoder.decode(new Uint8Array(result)), remaining: pSrc}
25
25
  }
26
26
 
27
27
  export class MsgUnpacker {
28
- remaining: number[];
29
- constructor(pSrc: number[]) {
28
+ remaining: Buffer;
29
+ constructor(pSrc: Buffer) {
30
30
  this.remaining = pSrc;
31
31
  }
32
32
 
33
- unpackInt(_unpacked = false): number {
34
- let unpacked;
35
- if (!_unpacked) {
36
- unpacked = unpackInt(this.remaining);
37
- this.remaining = unpacked.remaining;
38
- } else {
39
- unpacked = {result: this.remaining[0]};
40
- this.remaining = this.remaining.slice(1);
33
+ unpackInt(): number {
34
+ // let unpacked;
35
+ // if (!_unpacked) {
36
+ let unpacked = unpackInt(this.remaining);
37
+ this.remaining = unpacked.remaining;
38
+ // } else {
39
+ // unpacked = {result: this.remaining[0]};
40
+ // this.remaining = this.remaining.slice(1);
41
41
 
42
- }
42
+ // }
43
43
  return unpacked.result;
44
44
  }
45
45
 
package/lib/client.js CHANGED
@@ -214,7 +214,7 @@ var Client = /** @class */ (function (_super) {
214
214
  chunk.msg = messageTypes[packet[0] & 1][chunk.msgid];
215
215
  chunk.raw = packet.slice(1, chunk.bytes);
216
216
  Object.values(messageUUIDs).forEach(function (a, i) {
217
- if (a.compare(packet.slice(0, 16)) == 0) {
217
+ if (a.byteLength >= 16 && a.compare(packet.slice(0, 16)) == 0) {
218
218
  chunk.extended_msgid = a;
219
219
  chunk.msg = Object.keys(messageUUIDs)[i];
220
220
  }
@@ -337,7 +337,7 @@ var Client = /** @class */ (function (_super) {
337
337
  chunk.msg = messageTypes[packet[0] & 1][chunk.msgid];
338
338
  chunk.raw = packet.slice(1, chunk.bytes);
339
339
  Object.values(messageUUIDs).forEach(function (a, i) {
340
- if (a.compare(packet.slice(0, 16)) === 0) {
340
+ if (a.byteLength >= 16 && a.compare(packet.slice(0, 16)) === 0) {
341
341
  chunk.extended_msgid = a;
342
342
  chunk.msg = Object.keys(messageUUIDs)[i];
343
343
  }
@@ -400,10 +400,10 @@ var Client = /** @class */ (function (_super) {
400
400
  if (_this.State == 0 || rinfo.address != _this.host || rinfo.port != _this.port)
401
401
  return;
402
402
  clearInterval(connectInterval);
403
- if (packet.toJSON().data[0] == 0x10) {
404
- if (packet.toString().includes("TKEN") || packet.toJSON().data[3] == 0x2) {
403
+ if (packet[0] == 0x10) {
404
+ if (packet.toString().includes("TKEN") || packet[3] == 0x2) {
405
405
  clearInterval(connectInterval);
406
- _this.TKEN = Buffer.from(packet.toJSON().data.slice(packet.toJSON().data.length - 4, packet.toJSON().data.length));
406
+ _this.TKEN = packet.slice(-4);
407
407
  _this.SendControlMsg(3);
408
408
  _this.State = States.STATE_LOADING; // loading state
409
409
  _this.receivedSnaps = 0;
@@ -412,7 +412,7 @@ var Client = /** @class */ (function (_super) {
412
412
  info.AddString(((_b = _this.options) === null || _b === void 0 ? void 0 : _b.password) === undefined ? "" : (_c = _this.options) === null || _c === void 0 ? void 0 : _c.password); // password
413
413
  var client_version = new MsgPacker_1.MsgPacker(0, true, 1);
414
414
  client_version.AddBuffer(Buffer.from("8c00130484613e478787f672b3835bd4", 'hex'));
415
- var randomUuid = Buffer.alloc(16);
415
+ var randomUuid = Buffer.allocUnsafe(16);
416
416
  (0, crypto_1.randomBytes)(16).copy(randomUuid);
417
417
  client_version.AddBuffer(randomUuid);
418
418
  if (((_d = _this.options) === null || _d === void 0 ? void 0 : _d.ddnet_version) !== undefined) {
@@ -425,13 +425,13 @@ var Client = /** @class */ (function (_super) {
425
425
  }
426
426
  _this.SendMsgEx([client_version, info]);
427
427
  }
428
- else if (packet.toJSON().data[3] == 0x4) {
428
+ else if (packet[3] == 0x4) {
429
429
  // disconnected
430
430
  _this.State = States.STATE_OFFLINE;
431
- var reason = ((0, MsgUnpacker_1.unpackString)(packet.toJSON().data.slice(4)).result);
431
+ var reason = ((0, MsgUnpacker_1.unpackString)(packet.slice(4)).result);
432
432
  _this.emit("disconnect", reason);
433
433
  }
434
- if (packet.toJSON().data[3] !== 0x0) { // keepalive
434
+ if (packet[3] !== 0x0) { // keepalive
435
435
  _this.lastRecvTime = new Date().getTime();
436
436
  }
437
437
  }
@@ -461,8 +461,9 @@ var Client = /** @class */ (function (_super) {
461
461
  }
462
462
  });
463
463
  _this.sentChunkQueue.forEach(function (buff, i) {
464
- var chunk = _this.MsgToChunk(buff);
465
- if (chunk.flags & 1) {
464
+ var chunkFlags = (buff[0] >> 6) & 3;
465
+ if (chunkFlags & 1) {
466
+ var chunk = _this.MsgToChunk(buff);
466
467
  if (chunk.seq && chunk.seq >= _this.ack)
467
468
  _this.sentChunkQueue.splice(i, 1);
468
469
  }
@@ -519,7 +520,7 @@ var Client = /** @class */ (function (_super) {
519
520
  if (Math.abs(_this.PredGameTick - _this.AckGameTick) > 10)
520
521
  _this.PredGameTick = _this.AckGameTick + 1;
521
522
  // snapChunks.forEach(chunk => {
522
- var unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw.toJSON().data);
523
+ var unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw);
523
524
  var NumParts = 1;
524
525
  var Part = 0;
525
526
  var GameTick = unpacker.unpackInt();
@@ -527,11 +528,11 @@ var Client = /** @class */ (function (_super) {
527
528
  var PartSize = 0;
528
529
  var Crc = 0;
529
530
  var CompleteSize = 0;
530
- if (chunk.msg == "SNAP") {
531
+ if (chunk.msgid == NETMSG_Sys.NETMSG_SNAP) {
531
532
  NumParts = unpacker.unpackInt();
532
533
  Part = unpacker.unpackInt();
533
534
  }
534
- if (chunk.msg != "SNAP_EMPTY") {
535
+ if (chunk.msgid != NETMSG_Sys.NETMSG_SNAPEMPTY) {
535
536
  Crc = unpacker.unpackInt();
536
537
  PartSize = unpacker.unpackInt();
537
538
  }
@@ -544,12 +545,12 @@ var Client = /** @class */ (function (_super) {
544
545
  _this.currentSnapshotGameTick = GameTick;
545
546
  }
546
547
  // chunk.raw = Buffer.from(unpacker.remaining);
547
- _this.snaps[Part] = Buffer.from(unpacker.remaining);
548
+ _this.snaps[Part] = unpacker.remaining;
548
549
  _this.SnapshotParts |= 1 << Part;
549
550
  if (_this.SnapshotParts == ((1 << NumParts) - 1)) {
550
551
  var mergedSnaps = Buffer.concat(_this.snaps);
551
552
  _this.SnapshotParts = 0;
552
- var snapUnpacked = _this.SnapUnpacker.unpackSnapshot(mergedSnaps.toJSON().data, DeltaTick, GameTick, Crc);
553
+ var snapUnpacked = _this.SnapUnpacker.unpackSnapshot(mergedSnaps, DeltaTick, GameTick, Crc);
553
554
  _this.emit("snapshot");
554
555
  _this.AckGameTick = snapUnpacked.recvTick;
555
556
  if (Math.abs(_this.PredGameTick - _this.AckGameTick) > 10)
@@ -567,7 +568,7 @@ var Client = /** @class */ (function (_super) {
567
568
  _this.VoteList = [];
568
569
  }
569
570
  else if (chunk.msgid == NETMSG_Game.SV_VOTEOPTIONLISTADD) {
570
- var unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw.toJSON().data);
571
+ var unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw);
571
572
  var NumOptions = unpacker.unpackInt();
572
573
  var list = [];
573
574
  for (var i = 0; i < 15; i++) {
@@ -577,18 +578,18 @@ var Client = /** @class */ (function (_super) {
577
578
  (_a = _this.VoteList).push.apply(_a, list);
578
579
  }
579
580
  else if (chunk.msgid == NETMSG_Game.SV_VOTEOPTIONADD) {
580
- var unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw.toJSON().data);
581
+ var unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw);
581
582
  _this.VoteList.push(unpacker.unpackString());
582
583
  }
583
584
  else if (chunk.msgid == NETMSG_Game.SV_VOTEOPTIONREMOVE) {
584
- var unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw.toJSON().data);
585
+ var unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw);
585
586
  var index_1 = _this.VoteList.indexOf(unpacker.unpackString());
586
587
  if (index_1 > -1)
587
588
  _this.VoteList = _this.VoteList.splice(index_1, 1);
588
589
  }
589
590
  // events
590
591
  if (chunk.msgid == NETMSG_Game.SV_EMOTICON) {
591
- var unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw.toJSON().data);
592
+ var unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw);
592
593
  var unpacked_1 = {
593
594
  client_id: unpacker.unpackInt(),
594
595
  emoticon: unpacker.unpackInt()
@@ -602,11 +603,11 @@ var Client = /** @class */ (function (_super) {
602
603
  _this.emit("emote", unpacked_1);
603
604
  }
604
605
  else if (chunk.msgid == NETMSG_Game.SV_BROADCAST) {
605
- var unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw.toJSON().data);
606
+ var unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw);
606
607
  _this.emit("broadcast", unpacker.unpackString());
607
608
  }
608
609
  if (chunk.msgid == NETMSG_Game.SV_CHAT) {
609
- var unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw.toJSON().data);
610
+ var unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw);
610
611
  var unpacked_2 = {
611
612
  team: unpacker.unpackInt(),
612
613
  client_id: unpacker.unpackInt(),
@@ -622,7 +623,7 @@ var Client = /** @class */ (function (_super) {
622
623
  }
623
624
  else if (chunk.msgid == NETMSG_Game.SV_KILLMSG) {
624
625
  var unpacked_3 = {};
625
- var unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw.toJSON().data);
626
+ var unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw);
626
627
  unpacked_3.killer_id = unpacker.unpackInt();
627
628
  unpacked_3.victim_id = unpacker.unpackInt();
628
629
  unpacked_3.weapon = unpacker.unpackInt();
@@ -635,7 +636,7 @@ var Client = /** @class */ (function (_super) {
635
636
  _this.emit("kill", unpacked_3);
636
637
  }
637
638
  else if (chunk.msgid == NETMSG_Game.SV_MOTD) {
638
- var unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw.toJSON().data);
639
+ var unpacker = new MsgUnpacker_1.MsgUnpacker(chunk.raw);
639
640
  var message = unpacker.unpackString();
640
641
  _this.emit("motd", message);
641
642
  }
@@ -662,21 +663,16 @@ var Client = /** @class */ (function (_super) {
662
663
  inputMsg.AddInt(this.AckGameTick);
663
664
  inputMsg.AddInt(this.PredGameTick);
664
665
  inputMsg.AddInt(40);
665
- var input_data = [
666
- input.m_Direction,
667
- input.m_TargetX,
668
- input.m_TargetY,
669
- input.m_Jump,
670
- input.m_Fire,
671
- input.m_Hook,
672
- input.m_PlayerFlags,
673
- input.m_WantedWeapon,
674
- input.m_NextWeapon,
675
- input.m_PrevWeapon
676
- ];
677
- input_data.forEach(function (a) {
678
- inputMsg.AddInt(a);
679
- });
666
+ inputMsg.AddInt(input.m_Direction);
667
+ inputMsg.AddInt(input.m_TargetX);
668
+ inputMsg.AddInt(input.m_TargetY);
669
+ inputMsg.AddInt(input.m_Jump);
670
+ inputMsg.AddInt(input.m_Fire);
671
+ inputMsg.AddInt(input.m_Hook);
672
+ inputMsg.AddInt(input.m_PlayerFlags);
673
+ inputMsg.AddInt(input.m_WantedWeapon);
674
+ inputMsg.AddInt(input.m_NextWeapon);
675
+ inputMsg.AddInt(input.m_PrevWeapon);
680
676
  this.SendMsgEx(inputMsg);
681
677
  };
682
678
  Object.defineProperty(Client.prototype, "input", {
package/lib/client.ts CHANGED
@@ -348,7 +348,7 @@ export class Client extends EventEmitter {
348
348
  chunk.msg = messageTypes[packet[0] & 1][chunk.msgid];
349
349
  chunk.raw = packet.slice(1, chunk.bytes)
350
350
  Object.values(messageUUIDs).forEach((a, i) => {
351
- if (a.compare(packet.slice(0, 16)) == 0) {
351
+ if (a.byteLength >= 16 && a.compare(packet.slice(0, 16)) == 0) {
352
352
  chunk.extended_msgid = a;
353
353
  chunk.msg = Object.keys(messageUUIDs)[i];
354
354
  }
@@ -477,7 +477,7 @@ export class Client extends EventEmitter {
477
477
  chunk.msg = messageTypes[packet[0]&1][chunk.msgid];
478
478
  chunk.raw = packet.slice(1, chunk.bytes)
479
479
  Object.values(messageUUIDs).forEach((a, i) => {
480
- if (a.compare(packet.slice(0, 16)) === 0) {
480
+ if (a.byteLength >= 16 && a.compare(packet.slice(0, 16)) === 0) {
481
481
  chunk.extended_msgid = a;
482
482
  chunk.msg = Object.keys(messageUUIDs)[i];
483
483
  }
@@ -543,10 +543,10 @@ export class Client extends EventEmitter {
543
543
  return;
544
544
  clearInterval(connectInterval)
545
545
 
546
- if (packet.toJSON().data[0] == 0x10) {
547
- if (packet.toString().includes("TKEN") || packet.toJSON().data[3] == 0x2) {
546
+ if (packet[0] == 0x10) {
547
+ if (packet.toString().includes("TKEN") || packet[3] == 0x2) {
548
548
  clearInterval(connectInterval);
549
- this.TKEN = Buffer.from(packet.toJSON().data.slice(packet.toJSON().data.length - 4, packet.toJSON().data.length))
549
+ this.TKEN = packet.slice(-4)
550
550
  this.SendControlMsg(3);
551
551
  this.State = States.STATE_LOADING; // loading state
552
552
  this.receivedSnaps = 0;
@@ -557,7 +557,7 @@ export class Client extends EventEmitter {
557
557
 
558
558
  var client_version = new MsgPacker(0, true, 1);
559
559
  client_version.AddBuffer(Buffer.from("8c00130484613e478787f672b3835bd4", 'hex'));
560
- let randomUuid = Buffer.alloc(16);
560
+ let randomUuid = Buffer.allocUnsafe(16);
561
561
 
562
562
  randomBytes(16).copy(randomUuid);
563
563
 
@@ -571,13 +571,13 @@ export class Client extends EventEmitter {
571
571
  }
572
572
 
573
573
  this.SendMsgEx([client_version, info])
574
- } else if (packet.toJSON().data[3] == 0x4) {
574
+ } else if (packet[3] == 0x4) {
575
575
  // disconnected
576
576
  this.State = States.STATE_OFFLINE;
577
- let reason: string = (unpackString(packet.toJSON().data.slice(4)).result);
577
+ let reason: string = (unpackString(packet.slice(4)).result);
578
578
  this.emit("disconnect", reason);
579
579
  }
580
- if (packet.toJSON().data[3] !== 0x0) { // keepalive
580
+ if (packet[3] !== 0x0) { // keepalive
581
581
  this.lastRecvTime = new Date().getTime();
582
582
  }
583
583
  } else {
@@ -611,8 +611,9 @@ export class Client extends EventEmitter {
611
611
 
612
612
  })
613
613
  this.sentChunkQueue.forEach((buff, i) => {
614
- let chunk = this.MsgToChunk(buff);
615
- if (chunk.flags & 1) {
614
+ let chunkFlags = (buff[0] >> 6) & 3;
615
+ if (chunkFlags & 1) {
616
+ let chunk = this.MsgToChunk(buff);
616
617
  if (chunk.seq && chunk.seq >= this.ack)
617
618
  this.sentChunkQueue.splice(i, 1);
618
619
  }
@@ -672,7 +673,7 @@ export class Client extends EventEmitter {
672
673
  this.PredGameTick = this.AckGameTick + 1;
673
674
 
674
675
  // snapChunks.forEach(chunk => {
675
- let unpacker = new MsgUnpacker(chunk.raw.toJSON().data);
676
+ let unpacker = new MsgUnpacker(chunk.raw);
676
677
 
677
678
  let NumParts = 1;
678
679
  let Part = 0;
@@ -682,12 +683,12 @@ export class Client extends EventEmitter {
682
683
  let Crc = 0;
683
684
  let CompleteSize = 0;
684
685
 
685
- if (chunk.msg == "SNAP") {
686
+ if (chunk.msgid == NETMSG_Sys.NETMSG_SNAP) {
686
687
  NumParts = unpacker.unpackInt();
687
688
  Part = unpacker.unpackInt();
688
689
  }
689
690
 
690
- if (chunk.msg != "SNAP_EMPTY") {
691
+ if (chunk.msgid != NETMSG_Sys.NETMSG_SNAPEMPTY) {
691
692
  Crc = unpacker.unpackInt();
692
693
  PartSize = unpacker.unpackInt();
693
694
  }
@@ -703,7 +704,7 @@ export class Client extends EventEmitter {
703
704
  }
704
705
 
705
706
  // chunk.raw = Buffer.from(unpacker.remaining);
706
- this.snaps[Part] = Buffer.from(unpacker.remaining);
707
+ this.snaps[Part] = unpacker.remaining;
707
708
 
708
709
  this.SnapshotParts |= 1 << Part;
709
710
 
@@ -711,7 +712,7 @@ export class Client extends EventEmitter {
711
712
  let mergedSnaps = Buffer.concat(this.snaps);
712
713
  this.SnapshotParts = 0;
713
714
 
714
- let snapUnpacked = this.SnapUnpacker.unpackSnapshot(mergedSnaps.toJSON().data, DeltaTick, GameTick, Crc);
715
+ let snapUnpacked = this.SnapUnpacker.unpackSnapshot(mergedSnaps, DeltaTick, GameTick, Crc);
715
716
 
716
717
  this.emit("snapshot");
717
718
  this.AckGameTick = snapUnpacked.recvTick;
@@ -736,7 +737,7 @@ export class Client extends EventEmitter {
736
737
  if (chunk.msgid == NETMSG_Game.SV_VOTECLEAROPTIONS) {
737
738
  this.VoteList = [];
738
739
  } else if (chunk.msgid == NETMSG_Game.SV_VOTEOPTIONLISTADD) {
739
- let unpacker = new MsgUnpacker(chunk.raw.toJSON().data)
740
+ let unpacker = new MsgUnpacker(chunk.raw)
740
741
  let NumOptions = unpacker.unpackInt()
741
742
  let list: string[] = [];
742
743
  for (let i = 0; i < 15; i++) {
@@ -746,11 +747,11 @@ export class Client extends EventEmitter {
746
747
 
747
748
  this.VoteList.push(...list);
748
749
  } else if (chunk.msgid == NETMSG_Game.SV_VOTEOPTIONADD) {
749
- let unpacker = new MsgUnpacker(chunk.raw.toJSON().data)
750
+ let unpacker = new MsgUnpacker(chunk.raw)
750
751
 
751
752
  this.VoteList.push(unpacker.unpackString());
752
753
  } else if (chunk.msgid == NETMSG_Game.SV_VOTEOPTIONREMOVE) {
753
- let unpacker = new MsgUnpacker(chunk.raw.toJSON().data)
754
+ let unpacker = new MsgUnpacker(chunk.raw)
754
755
 
755
756
  let index = this.VoteList.indexOf(unpacker.unpackString());
756
757
 
@@ -761,7 +762,7 @@ export class Client extends EventEmitter {
761
762
 
762
763
  // events
763
764
  if (chunk.msgid == NETMSG_Game.SV_EMOTICON) {
764
- let unpacker = new MsgUnpacker(chunk.raw.toJSON().data);
765
+ let unpacker = new MsgUnpacker(chunk.raw);
765
766
  let unpacked = {
766
767
  client_id: unpacker.unpackInt(),
767
768
  emoticon: unpacker.unpackInt()
@@ -778,11 +779,11 @@ export class Client extends EventEmitter {
778
779
 
779
780
 
780
781
  } else if (chunk.msgid == NETMSG_Game.SV_BROADCAST) {
781
- let unpacker = new MsgUnpacker(chunk.raw.toJSON().data);
782
+ let unpacker = new MsgUnpacker(chunk.raw);
782
783
 
783
784
  this.emit("broadcast", unpacker.unpackString());
784
785
  } if (chunk.msgid == NETMSG_Game.SV_CHAT) {
785
- let unpacker = new MsgUnpacker(chunk.raw.toJSON().data);
786
+ let unpacker = new MsgUnpacker(chunk.raw);
786
787
  let unpacked: iMessage = {
787
788
  team: unpacker.unpackInt(),
788
789
  client_id: unpacker.unpackInt(),
@@ -798,7 +799,7 @@ export class Client extends EventEmitter {
798
799
  this.emit("message", unpacked)
799
800
  } else if (chunk.msgid == NETMSG_Game.SV_KILLMSG) {
800
801
  let unpacked: iKillMsg = {} as iKillMsg;
801
- let unpacker = new MsgUnpacker(chunk.raw.toJSON().data);
802
+ let unpacker = new MsgUnpacker(chunk.raw);
802
803
  unpacked.killer_id = unpacker.unpackInt();
803
804
  unpacked.victim_id = unpacker.unpackInt();
804
805
  unpacked.weapon = unpacker.unpackInt();
@@ -811,7 +812,7 @@ export class Client extends EventEmitter {
811
812
  unpacked.killer = { ClientInfo: this.client_info(unpacked.killer_id), PlayerInfo: this.player_info(unpacked.killer_id) }
812
813
  this.emit("kill", unpacked)
813
814
  } else if (chunk.msgid == NETMSG_Game.SV_MOTD) {
814
- let unpacker = new MsgUnpacker(chunk.raw.toJSON().data);
815
+ let unpacker = new MsgUnpacker(chunk.raw);
815
816
  let message = unpacker.unpackString();
816
817
  this.emit("motd", message);
817
818
  }
@@ -843,22 +844,17 @@ export class Client extends EventEmitter {
843
844
  inputMsg.AddInt(this.PredGameTick);
844
845
  inputMsg.AddInt(40);
845
846
 
846
- let input_data = [
847
-
848
- input.m_Direction,
849
- input.m_TargetX,
850
- input.m_TargetY,
851
- input.m_Jump,
852
- input.m_Fire,
853
- input.m_Hook,
854
- input.m_PlayerFlags,
855
- input.m_WantedWeapon,
856
- input.m_NextWeapon,
857
- input.m_PrevWeapon
858
- ]
859
- input_data.forEach(a => {
860
- inputMsg.AddInt(a);
861
- });
847
+ inputMsg.AddInt(input.m_Direction)
848
+ inputMsg.AddInt(input.m_TargetX)
849
+ inputMsg.AddInt(input.m_TargetY)
850
+ inputMsg.AddInt(input.m_Jump)
851
+ inputMsg.AddInt(input.m_Fire)
852
+ inputMsg.AddInt(input.m_Hook)
853
+ inputMsg.AddInt(input.m_PlayerFlags)
854
+ inputMsg.AddInt(input.m_WantedWeapon)
855
+ inputMsg.AddInt(input.m_NextWeapon)
856
+ inputMsg.AddInt(input.m_PrevWeapon)
857
+
862
858
  this.SendMsgEx(inputMsg);
863
859
  }
864
860
  get input() {
package/lib/snapshot.js CHANGED
@@ -1,30 +1,53 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Snapshot = exports.items = exports.itemAppendix = void 0;
3
+ exports.Snapshot = exports.items = void 0;
4
4
  var MsgUnpacker_1 = require("./MsgUnpacker");
5
5
  var decoder = new TextDecoder('utf-8');
6
- exports.itemAppendix = [
7
- { "type_id": 0, "size": 0, "name": "obj_ex" },
8
- { "type_id": 1, "size": 10, "name": "obj_player_input" },
9
- { "type_id": 2, "size": 6, "name": "obj_projectile" },
10
- { "type_id": 3, "size": 5, "name": "obj_laser" },
11
- { "type_id": 4, "size": 4, "name": "obj_pickup" },
12
- { "type_id": 5, "size": 3, "name": "obj_flag" },
13
- { "type_id": 6, "size": 8, "name": "obj_game_info" },
14
- { "type_id": 7, "size": 4, "name": "obj_game_data" },
15
- { "type_id": 8, "size": 15, "name": "obj_character_core" },
16
- { "type_id": 9, "size": 22, "name": "obj_character" },
17
- { "type_id": 10, "size": 5, "name": "obj_player_info" },
18
- { "type_id": 11, "size": 17, "name": "obj_client_info" },
19
- { "type_id": 12, "size": 3, "name": "obj_spectator_info" },
20
- { "type_id": 13, "size": 2, "name": "event_common" },
21
- { "type_id": 14, "size": 2, "name": "event_explosion" },
22
- { "type_id": 15, "size": 2, "name": "event_spawn" },
23
- { "type_id": 16, "size": 2, "name": "event_hammerhit" },
24
- { "type_id": 17, "size": 3, "name": "event_death" },
25
- { "type_id": 18, "size": 3, "name": "event_sound_global" },
26
- { "type_id": 19, "size": 3, "name": "event_sound_world" },
27
- { "type_id": 20, "size": 3, "name": "event_damage_indicator" }
6
+ // export const itemAppendix: {"type_id": number, "size": number, "name": string}[] = [
7
+ // {"type_id": 0, "size": 0, "name": "obj_ex"},
8
+ // {"type_id": 1, "size": 10, "name": "obj_player_input"},
9
+ // {"type_id": 2, "size": 6, "name": "obj_projectile"},
10
+ // {"type_id": 3, "size": 5, "name": "obj_laser"},
11
+ // {"type_id": 4, "size": 4, "name": "obj_pickup"},
12
+ // {"type_id": 5, "size": 3, "name": "obj_flag"},
13
+ // {"type_id": 6, "size": 8, "name": "obj_game_info"},
14
+ // {"type_id": 7, "size": 4, "name": "obj_game_data"},
15
+ // {"type_id": 8, "size": 15, "name": "obj_character_core"},
16
+ // {"type_id": 9, "size": 22, "name": "obj_character"},
17
+ // {"type_id": 10, "size": 5, "name": "obj_player_info"},
18
+ // {"type_id": 11, "size": 17, "name": "obj_client_info"},
19
+ // {"type_id": 12, "size": 3, "name": "obj_spectator_info"},
20
+ // {"type_id": 13, "size": 2, "name": "event_common"},
21
+ // {"type_id": 14, "size": 2, "name": "event_explosion"},
22
+ // {"type_id": 15, "size": 2, "name": "event_spawn"},
23
+ // {"type_id": 16, "size": 2, "name": "event_hammerhit"},
24
+ // {"type_id": 17, "size": 3, "name": "event_death"},
25
+ // {"type_id": 18, "size": 3, "name": "event_sound_global"},
26
+ // {"type_id": 19, "size": 3, "name": "event_sound_world"},
27
+ // {"type_id": 20, "size": 3, "name": "event_damage_indicator"}
28
+ // ]
29
+ var itemAppendix = [
30
+ 0,
31
+ 10,
32
+ 6,
33
+ 5,
34
+ 4,
35
+ 3,
36
+ 8,
37
+ 4,
38
+ 15,
39
+ 22,
40
+ 5,
41
+ 17,
42
+ 3,
43
+ 2,
44
+ 2,
45
+ 2,
46
+ 2,
47
+ 3,
48
+ 3,
49
+ 3,
50
+ 3,
28
51
  ];
29
52
  var items;
30
53
  (function (items) {
@@ -292,23 +315,36 @@ var Snapshot = /** @class */ (function () {
292
315
  }
293
316
  return _item;
294
317
  };
295
- Snapshot.prototype.crc = function (tick) {
318
+ Snapshot.prototype.crc = function () {
296
319
  var checksum = 0;
297
- this.eSnapHolder.forEach(function (snap) {
298
- if (snap.ack == tick)
299
- snap.Snapshot.Data.forEach(function (el) { return checksum += el; });
320
+ // this.eSnapHolder.forEach(snap => {
321
+ // if (snap.ack == tick)
322
+ // snap.Snapshot.Data.forEach(el => checksum += el);
323
+ // })
324
+ this.deltas.forEach(function (snap) {
325
+ // if (snap.ack == tick)
326
+ snap.data.forEach(function (el) { return checksum += el; });
300
327
  });
301
328
  return checksum & 0xffffffff;
302
329
  };
303
330
  Snapshot.prototype.unpackSnapshot = function (snap, deltatick, recvTick, WantedCrc) {
304
331
  var _this = this;
305
332
  var unpacker = new MsgUnpacker_1.MsgUnpacker(snap);
333
+ var deltaSnaps = [];
306
334
  if (deltatick == -1) {
307
335
  this.eSnapHolder = [];
308
336
  this.deltas = [];
309
337
  }
310
338
  else {
311
- this.eSnapHolder = this.eSnapHolder.filter(function (a) { return a.ack >= deltatick; });
339
+ this.eSnapHolder = this.eSnapHolder.filter(function (a) {
340
+ if (a.ack == deltatick)
341
+ deltaSnaps.push(a);
342
+ return a.ack >= deltatick;
343
+ });
344
+ // if (deltatick != -1 && this.eSnapHolder.length == 0) {
345
+ // console.log("no deltatick stored")
346
+ // return {items: [], recvTick: -1}
347
+ // }
312
348
  }
313
349
  if (snap.length == 0) {
314
350
  // empty snap, copy old one into new ack
@@ -318,6 +354,8 @@ var Snapshot = /** @class */ (function () {
318
354
  });
319
355
  return { items: [], recvTick: recvTick };
320
356
  }
357
+ var oldDeltas = this.deltas;
358
+ this.deltas = [];
321
359
  /* key = (((type_id) << 16) | (id))
322
360
  * key_to_id = ((key) & 0xffff)
323
361
  * key_to_type_id = ((key >> 16) & 0xffff)
@@ -335,81 +373,142 @@ var Snapshot = /** @class */ (function () {
335
373
  [ ] item_deltas
336
374
  */
337
375
  var deleted = [];
338
- var _loop_1 = function (i) {
376
+ for (var i = 0; i < num_removed_items; i++) {
339
377
  var deleted_key = unpacker.unpackInt(); // removed_item_keys
340
378
  // let index = this.deltas.map(delta => delta.key).indexOf(deleted_key);
341
- var index = this_1.deltas.findIndex(function (delta) { return delta.key === deleted_key; });
342
- if (index > -1)
343
- this_1.deltas.splice(index, 1);
379
+ // let index = this.deltas.findIndex(delta => delta.key === deleted_key);
380
+ // if (index > -1)
381
+ // this.deltas.splice(index, 1);
382
+ // console.log(deleted_key)
344
383
  deleted.push(deleted_key);
345
- };
346
- var this_1 = this;
347
- for (var i = 0; i < num_removed_items; i++) {
348
- _loop_1(i);
349
384
  }
350
- if (deleted.length)
351
- this.eSnapHolder = this.eSnapHolder.filter(function (a) { return !deleted.includes(a.Snapshot.Key); });
352
385
  /*item_delta:
353
386
  [ 4] type_id
354
387
  [ 4] id
355
388
  [ 4] _size
356
389
  [*4] data_delta*/
357
- var items = { items: [], /* client_infos: client_infos, player_infos: player_infos,*/ lost: 0 };
358
- var deltaSnaps = this.eSnapHolder.filter(function (a) { return a.ack === deltatick; });
390
+ // let items: {'items': {'data': number[], '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};
391
+ // let deltaSnaps = this.eSnapHolder.filter(a => a.ack === deltatick);
359
392
  if (deltaSnaps.length == 0 && deltatick >= 0) {
360
393
  return { items: [], recvTick: -1 };
361
394
  }
362
- var _loop_2 = function (i) {
395
+ var _loop_1 = function (i) {
363
396
  var type_id = unpacker.unpackInt();
364
397
  var id = unpacker.unpackInt();
365
398
  var key = (((type_id) << 16) | (id));
366
399
  var _size = void 0;
367
- if (type_id > 0 && type_id < exports.itemAppendix.length) {
368
- _size = exports.itemAppendix[type_id].size;
400
+ if (type_id > 0 && type_id < itemAppendix.length) {
401
+ _size = itemAppendix[type_id];
369
402
  }
370
403
  else
371
404
  _size = unpacker.unpackInt();
372
405
  var data = [];
373
406
  for (var j = 0; j < _size; j++) {
374
- if (unpacker.remaining.length > 0)
375
- data.push(unpacker.unpackInt());
407
+ // if (unpacker.remaining.length > 0) {
408
+ data[j] = (unpacker.unpackInt());
409
+ // } else console.log(_size, "???")
376
410
  }
411
+ var changed = false;
377
412
  if (deltatick >= 0) {
378
413
  // let index = deltaSnaps.map(delta => delta.Snapshot.Key).indexOf(key)
379
- var index = deltaSnaps.findIndex(function (delta) { return delta.Snapshot.Key === key; });
380
- if (index > -1) {
381
- var out = UndiffItem(deltaSnaps[index].Snapshot.Data, data);
414
+ var delta = deltaSnaps.find(function (delta) { return delta.Snapshot.Key === key; });
415
+ if (delta !== undefined) {
416
+ var out = UndiffItem(delta.Snapshot.Data, data);
382
417
  data = out;
418
+ changed = true;
383
419
  } // else no previous, use new data
384
420
  }
385
- var parsed = this_2.parseItem(data, type_id, id);
386
- this_2.eSnapHolder.push({ Snapshot: { Data: data, Key: key }, ack: recvTick });
387
- items.items.push({ data: data, parsed: parsed, type_id: type_id, id: id, key: key });
421
+ var parsed = void 0;
422
+ if (!changed) {
423
+ var oldDelta = oldDeltas.find(function (delta) { return delta.key == key; });
424
+ if (oldDelta !== undefined && compareArrays(data, oldDelta.data)) {
425
+ parsed = oldDelta.parsed;
426
+ }
427
+ else
428
+ parsed = this_1.parseItem(data, type_id, id);
429
+ }
430
+ else
431
+ parsed = this_1.parseItem(data, type_id, id);
432
+ this_1.eSnapHolder.push({ Snapshot: { Data: data, Key: key }, ack: recvTick });
433
+ // items.items.push({data, type_id, id, key})
434
+ this_1.deltas.push({
435
+ data: data,
436
+ key: key,
437
+ id: id,
438
+ type_id: type_id,
439
+ parsed: parsed
440
+ });
388
441
  };
389
- var this_2 = this;
442
+ var this_1 = this;
390
443
  for (var i = 0; i < num_item_deltas; i++) {
391
- _loop_2(i);
444
+ _loop_1(i);
392
445
  }
393
- var _loop_3 = function (newSnap) {
394
- if (this_3.eSnapHolder.findIndex(function (a) { return a.ack == recvTick && a.Snapshot.Key == newSnap.Snapshot.Key; }) === -1) { // ugly copy new snap to eSnapHolder (if it isnt pushed already)
395
- this_3.eSnapHolder.push({ Snapshot: { Data: newSnap.Snapshot.Data, Key: newSnap.Snapshot.Key }, ack: recvTick });
446
+ var _loop_2 = function (newSnap) {
447
+ if (deleted.includes(newSnap.Snapshot.Key)) {
448
+ return "continue";
396
449
  }
397
- if (deltatick > -1) {
398
- var ____index = this_3.deltas.findIndex(function (delta) { return delta.key == newSnap.Snapshot.Key; });
399
- if (____index > -1) {
400
- this_3.deltas[____index] = { data: newSnap.Snapshot.Data, key: newSnap.Snapshot.Key, id: newSnap.Snapshot.Key & 0xffff, type_id: ((newSnap.Snapshot.Key >> 16) & 0xffff), parsed: this_3.parseItem(newSnap.Snapshot.Data, ((newSnap.Snapshot.Key >> 16) & 0xffff), ((newSnap.Snapshot.Key) & 0xffff)) };
401
- return "continue";
450
+ if (this_2.eSnapHolder.findIndex(function (a) { return a.ack == recvTick && a.Snapshot.Key == newSnap.Snapshot.Key; }) === -1) { // ugly copy new snap to eSnapHolder (if it isnt pushed already)
451
+ this_2.eSnapHolder.push({ Snapshot: { Data: newSnap.Snapshot.Data, Key: newSnap.Snapshot.Key }, ack: recvTick });
452
+ // this.deltas.push({})
453
+ // if (deltatick > -1) {
454
+ // let ____index = this.deltas.findIndex(delta => delta.key == newSnap.Snapshot.Key)
455
+ // if (____index > -1) {
456
+ // this.deltas[____index] = {
457
+ // data: newSnap.Snapshot.Data,
458
+ // key: newSnap.Snapshot.Key,
459
+ // id: newSnap.Snapshot.Key & 0xffff,
460
+ // type_id: ((newSnap.Snapshot.Key >> 16) & 0xffff),
461
+ // parsed: this.parseItem(newSnap.Snapshot.Data, ((newSnap.Snapshot.Key >> 16) & 0xffff), ((newSnap.Snapshot.Key) & 0xffff))
462
+ // };
463
+ // continue;
464
+ // }
465
+ // // }
466
+ var oldDelta = oldDeltas.find(function (delta) { return delta.key == newSnap.Snapshot.Key; });
467
+ if (oldDelta !== undefined && compareArrays(newSnap.Snapshot.Data, oldDelta.data)) {
468
+ this_2.deltas.push(oldDelta);
402
469
  }
403
- } // else
404
- this_3.deltas.push({ data: newSnap.Snapshot.Data, key: newSnap.Snapshot.Key, id: newSnap.Snapshot.Key & 0xffff, type_id: ((newSnap.Snapshot.Key >> 16) & 0xffff), parsed: this_3.parseItem(newSnap.Snapshot.Data, ((newSnap.Snapshot.Key >> 16) & 0xffff), ((newSnap.Snapshot.Key) & 0xffff)) });
470
+ else {
471
+ this_2.deltas.push({
472
+ data: newSnap.Snapshot.Data,
473
+ key: newSnap.Snapshot.Key,
474
+ id: newSnap.Snapshot.Key & 0xffff,
475
+ type_id: ((newSnap.Snapshot.Key >> 16) & 0xffff),
476
+ parsed: this_2.parseItem(newSnap.Snapshot.Data, ((newSnap.Snapshot.Key >> 16) & 0xffff), ((newSnap.Snapshot.Key) & 0xffff))
477
+ });
478
+ }
479
+ }
405
480
  };
406
- var this_3 = this;
481
+ var this_2 = this;
482
+ // if (deleted.length) {
483
+ // let _beforeLength = this.eSnapHolder.length;
484
+ // this.eSnapHolder = this.eSnapHolder.filter(snap => !deleted.includes(snap.Snapshot.Key));
485
+ // let _beforeLength = this.eSnapHolder.length;
486
+ // if ((_beforeLength - this.eSnapHolder.length) !== num_removed_items) {
487
+ // console.log("remove!", (_beforeLength - this.eSnapHolder.length) == num_removed_items, (_beforeLength - this.eSnapHolder.length), num_removed_items, WantedCrc)
488
+ // }
489
+ // }
407
490
  for (var _i = 0, deltaSnaps_1 = deltaSnaps; _i < deltaSnaps_1.length; _i++) {
408
491
  var newSnap = deltaSnaps_1[_i];
409
- _loop_3(newSnap);
492
+ _loop_2(newSnap);
410
493
  }
411
- if (this.crc(recvTick) !== WantedCrc) {
494
+ // if (items.items.length != num_item_deltas)
495
+ // console.log("length", items.items.length, num_item_deltas, items.items.length - num_item_deltas, WantedCrc)
496
+ /* this.deltas = [];
497
+ for (let newSnap of this.eSnapHolder) {
498
+ if (newSnap.ack == recvTick)
499
+ this.deltas.push({
500
+ data: newSnap.Snapshot.Data,
501
+ key: newSnap.Snapshot.Key,
502
+ id: newSnap.Snapshot.Key & 0xffff,
503
+ type_id: ((newSnap.Snapshot.Key >> 16) & 0xffff),
504
+ parsed: this.parseItem(newSnap.Snapshot.Data, ((newSnap.Snapshot.Key >> 16) & 0xffff), ((newSnap.Snapshot.Key) & 0xffff))
505
+ });
506
+ }*/
507
+ var _crc = this.crc();
508
+ if (_crc !== WantedCrc) {
509
+ this.deltas = oldDeltas;
412
510
  this.crc_errors++;
511
+ console.log("crc error", _crc, WantedCrc, this.crc_errors);
413
512
  if (this.crc_errors > 5) {
414
513
  recvTick = -1;
415
514
  this.crc_errors = 0;
@@ -422,11 +521,25 @@ var Snapshot = /** @class */ (function () {
422
521
  }
423
522
  else if (this.crc_errors > 0)
424
523
  this.crc_errors--;
425
- return { items: items, recvTick: recvTick };
524
+ // let filterLength = this.eSnapHolder.filter(a => a.ack == recvTick).length
525
+ // if (this.deltas.length !== filterLength) {
526
+ // this.deltas = this.deltas.filter(a => !deleted.includes(a.key))
527
+ // console.log(this.deltas.length, filterLength, this.deltas.length - filterLength, num_item_deltas )
528
+ // }
529
+ return { items: this.deltas, recvTick: recvTick };
426
530
  };
427
531
  return Snapshot;
428
532
  }());
429
533
  exports.Snapshot = Snapshot;
534
+ function compareArrays(first, second) {
535
+ if (first.length !== second.length)
536
+ return false;
537
+ for (var i = 0; i < first.length; i++) {
538
+ if (first[i] !== second[i])
539
+ return false;
540
+ }
541
+ return true;
542
+ }
430
543
  function UndiffItem(oldItem, newItem) {
431
544
  var out = newItem;
432
545
  // if (JSON.stringify(newItem) === JSON.stringify(oldItem))
@@ -436,6 +549,7 @@ function UndiffItem(oldItem, newItem) {
436
549
  out[i] += a;
437
550
  }
438
551
  else {
552
+ console.log("UNDEFINED UNDEFINED UNDEFINED");
439
553
  out[i] = 0;
440
554
  }
441
555
  });
package/lib/snapshot.ts CHANGED
@@ -1,28 +1,51 @@
1
1
  import { MsgUnpacker } from "./MsgUnpacker";
2
2
  var decoder = new TextDecoder('utf-8');
3
3
 
4
- export const itemAppendix: {"type_id": number, "size": number, "name": string}[] = [
5
- {"type_id": 0, "size": 0, "name": "obj_ex"},
6
- {"type_id": 1, "size": 10, "name": "obj_player_input"},
7
- {"type_id": 2, "size": 6, "name": "obj_projectile"},
8
- {"type_id": 3, "size": 5, "name": "obj_laser"},
9
- {"type_id": 4, "size": 4, "name": "obj_pickup"},
10
- {"type_id": 5, "size": 3, "name": "obj_flag"},
11
- {"type_id": 6, "size": 8, "name": "obj_game_info"},
12
- {"type_id": 7, "size": 4, "name": "obj_game_data"},
13
- {"type_id": 8, "size": 15, "name": "obj_character_core"},
14
- {"type_id": 9, "size": 22, "name": "obj_character"},
15
- {"type_id": 10, "size": 5, "name": "obj_player_info"},
16
- {"type_id": 11, "size": 17, "name": "obj_client_info"},
17
- {"type_id": 12, "size": 3, "name": "obj_spectator_info"},
18
- {"type_id": 13, "size": 2, "name": "event_common"},
19
- {"type_id": 14, "size": 2, "name": "event_explosion"},
20
- {"type_id": 15, "size": 2, "name": "event_spawn"},
21
- {"type_id": 16, "size": 2, "name": "event_hammerhit"},
22
- {"type_id": 17, "size": 3, "name": "event_death"},
23
- {"type_id": 18, "size": 3, "name": "event_sound_global"},
24
- {"type_id": 19, "size": 3, "name": "event_sound_world"},
25
- {"type_id": 20, "size": 3, "name": "event_damage_indicator"}
4
+ // export const itemAppendix: {"type_id": number, "size": number, "name": string}[] = [
5
+ // {"type_id": 0, "size": 0, "name": "obj_ex"},
6
+ // {"type_id": 1, "size": 10, "name": "obj_player_input"},
7
+ // {"type_id": 2, "size": 6, "name": "obj_projectile"},
8
+ // {"type_id": 3, "size": 5, "name": "obj_laser"},
9
+ // {"type_id": 4, "size": 4, "name": "obj_pickup"},
10
+ // {"type_id": 5, "size": 3, "name": "obj_flag"},
11
+ // {"type_id": 6, "size": 8, "name": "obj_game_info"},
12
+ // {"type_id": 7, "size": 4, "name": "obj_game_data"},
13
+ // {"type_id": 8, "size": 15, "name": "obj_character_core"},
14
+ // {"type_id": 9, "size": 22, "name": "obj_character"},
15
+ // {"type_id": 10, "size": 5, "name": "obj_player_info"},
16
+ // {"type_id": 11, "size": 17, "name": "obj_client_info"},
17
+ // {"type_id": 12, "size": 3, "name": "obj_spectator_info"},
18
+ // {"type_id": 13, "size": 2, "name": "event_common"},
19
+ // {"type_id": 14, "size": 2, "name": "event_explosion"},
20
+ // {"type_id": 15, "size": 2, "name": "event_spawn"},
21
+ // {"type_id": 16, "size": 2, "name": "event_hammerhit"},
22
+ // {"type_id": 17, "size": 3, "name": "event_death"},
23
+ // {"type_id": 18, "size": 3, "name": "event_sound_global"},
24
+ // {"type_id": 19, "size": 3, "name": "event_sound_world"},
25
+ // {"type_id": 20, "size": 3, "name": "event_damage_indicator"}
26
+ // ]
27
+ const itemAppendix: number[] = [
28
+ 0,
29
+ 10,
30
+ 6,
31
+ 5,
32
+ 4,
33
+ 3,
34
+ 8,
35
+ 4,
36
+ 15,
37
+ 22,
38
+ 5,
39
+ 17,
40
+ 3,
41
+ 2,
42
+ 2,
43
+ 2,
44
+ 2,
45
+ 3,
46
+ 3,
47
+ 3,
48
+ 3,
26
49
  ]
27
50
 
28
51
  export enum items {
@@ -299,33 +322,47 @@ export class Snapshot {
299
322
  return _item;
300
323
  }
301
324
 
302
- crc(tick: number) {
325
+ crc() {
303
326
  var checksum = 0;
304
- this.eSnapHolder.forEach(snap => {
305
- if (snap.ack == tick)
306
- snap.Snapshot.Data.forEach(el => checksum += el);
327
+ // this.eSnapHolder.forEach(snap => {
328
+ // if (snap.ack == tick)
329
+ // snap.Snapshot.Data.forEach(el => checksum += el);
330
+ // })
331
+ this.deltas.forEach(snap => {
332
+ // if (snap.ack == tick)
333
+ snap.data.forEach(el => checksum += el);
307
334
  })
308
335
 
309
336
  return checksum & 0xffffffff;
310
337
  }
311
-
312
- unpackSnapshot(snap: number[], deltatick: number, recvTick: number, WantedCrc: number) {
338
+ unpackSnapshot(snap: Buffer, deltatick: number, recvTick: number, WantedCrc: number) {
313
339
  let unpacker = new MsgUnpacker(snap);
340
+ let deltaSnaps: eSnap[] = [];
314
341
  if (deltatick == -1) {
315
342
  this.eSnapHolder = [];
316
343
  this.deltas = [];
317
344
  } else {
318
- this.eSnapHolder = this.eSnapHolder.filter(a => a.ack >= deltatick)
345
+ this.eSnapHolder = this.eSnapHolder.filter(a => {
346
+ if (a.ack == deltatick)
347
+ deltaSnaps.push(a);
348
+ return a.ack >= deltatick
349
+ })
350
+ // if (deltatick != -1 && this.eSnapHolder.length == 0) {
351
+ // console.log("no deltatick stored")
352
+ // return {items: [], recvTick: -1}
353
+ // }
319
354
  }
320
355
  if (snap.length == 0) {
321
356
  // empty snap, copy old one into new ack
322
357
  this.eSnapHolder.forEach(snap => {
323
- if (snap.ack == deltatick)
358
+ if (snap.ack == deltatick)
324
359
  this.eSnapHolder.push({Snapshot: snap.Snapshot, ack: recvTick});
325
360
 
326
361
  })
327
362
  return {items: [], recvTick: recvTick};
328
363
  }
364
+ let oldDeltas = this.deltas;
365
+ this.deltas = [];
329
366
  /* key = (((type_id) << 16) | (id))
330
367
  * key_to_id = ((key) & 0xffff)
331
368
  * key_to_type_id = ((key >> 16) & 0xffff)
@@ -348,22 +385,21 @@ export class Snapshot {
348
385
  for (let i = 0; i < num_removed_items; i++) {
349
386
  let deleted_key = unpacker.unpackInt(); // removed_item_keys
350
387
  // let index = this.deltas.map(delta => delta.key).indexOf(deleted_key);
351
- let index = this.deltas.findIndex(delta => delta.key === deleted_key);
352
- if (index > -1)
353
- this.deltas.splice(index, 1);
388
+ // let index = this.deltas.findIndex(delta => delta.key === deleted_key);
389
+ // if (index > -1)
390
+ // this.deltas.splice(index, 1);
391
+ // console.log(deleted_key)
354
392
  deleted.push(deleted_key)
355
393
  }
356
- if (deleted.length)
357
- this.eSnapHolder = this.eSnapHolder.filter(a => !deleted.includes(a.Snapshot.Key));
358
-
359
394
  /*item_delta:
360
395
  [ 4] type_id
361
396
  [ 4] id
362
397
  [ 4] _size
363
398
  [*4] data_delta*/
364
399
 
365
- 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};
366
- let deltaSnaps = this.eSnapHolder.filter(a => a.ack === deltatick);
400
+ // let items: {'items': {'data': number[], '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};
401
+
402
+ // let deltaSnaps = this.eSnapHolder.filter(a => a.ack === deltatick);
367
403
 
368
404
  if (deltaSnaps.length == 0 && deltatick >= 0) {
369
405
  return {items: [], recvTick: -1};
@@ -377,50 +413,121 @@ export class Snapshot {
377
413
 
378
414
  let _size;
379
415
  if (type_id > 0 && type_id < itemAppendix.length) {
380
- _size = itemAppendix[type_id].size;
416
+ _size = itemAppendix[type_id];
381
417
  } else
382
418
  _size = unpacker.unpackInt();
383
419
 
384
420
  let data: number[] = [];
385
421
  for (let j = 0; j < _size; j++) {
386
- if (unpacker.remaining.length > 0)
387
- data.push(unpacker.unpackInt());
422
+ // if (unpacker.remaining.length > 0) {
423
+ data[j] = (unpacker.unpackInt());
424
+ // } else console.log(_size, "???")
388
425
  }
426
+ let changed = false;
389
427
  if (deltatick >= 0) {
390
428
  // let index = deltaSnaps.map(delta => delta.Snapshot.Key).indexOf(key)
391
- let index = deltaSnaps.findIndex(delta => delta.Snapshot.Key === key);
392
- if (index > -1) {
393
-
394
- let out = UndiffItem(deltaSnaps[index].Snapshot.Data, data)
429
+ let delta = deltaSnaps.find(delta => delta.Snapshot.Key === key);
430
+ if (delta !== undefined) {
431
+ let out = UndiffItem(delta.Snapshot.Data, data)
395
432
  data = out;
433
+ changed = true;
396
434
  } // else no previous, use new data
397
435
  }
436
+ let parsed: Item;
437
+ if (!changed) {
438
+ let oldDelta = oldDeltas.find(delta => delta.key == key);
439
+ if (oldDelta !== undefined && compareArrays(data, oldDelta.data)) {
440
+ parsed = oldDelta.parsed;
398
441
 
399
- let parsed = this.parseItem(data, type_id, id)
442
+ } else
443
+ parsed = this.parseItem(data, type_id, id)
444
+
445
+ } else
446
+ parsed = this.parseItem(data, type_id, id)
447
+
400
448
  this.eSnapHolder.push({Snapshot: {Data: data, Key: key}, ack: recvTick});
401
449
 
402
- items.items.push({data, parsed, type_id, id, key})
450
+ // items.items.push({data, type_id, id, key})
451
+
452
+ this.deltas.push({
453
+ data,
454
+ key,
455
+ id,
456
+ type_id,
457
+ parsed
458
+ });
459
+
403
460
 
404
461
 
405
462
 
406
463
  }
464
+ // if (deleted.length) {
465
+ // let _beforeLength = this.eSnapHolder.length;
466
+ // this.eSnapHolder = this.eSnapHolder.filter(snap => !deleted.includes(snap.Snapshot.Key));
467
+ // let _beforeLength = this.eSnapHolder.length;
468
+ // if ((_beforeLength - this.eSnapHolder.length) !== num_removed_items) {
469
+ // console.log("remove!", (_beforeLength - this.eSnapHolder.length) == num_removed_items, (_beforeLength - this.eSnapHolder.length), num_removed_items, WantedCrc)
470
+
471
+ // }
472
+ // }
473
+
407
474
  for (let newSnap of deltaSnaps) {
475
+ if (deleted.includes(newSnap.Snapshot.Key)) {
476
+ // if ()
477
+ // this.deltas = this.deltas.filter(a => !deleted.includes(a.key))
478
+ continue;
479
+ }
408
480
  if (this.eSnapHolder.findIndex(a => a.ack == recvTick && a.Snapshot.Key == newSnap.Snapshot.Key) === -1) { // ugly copy new snap to eSnapHolder (if it isnt pushed already)
409
481
  this.eSnapHolder.push({Snapshot: {Data: newSnap.Snapshot.Data, Key: newSnap.Snapshot.Key}, ack: recvTick});
410
- }
411
- if (deltatick > -1) {
412
- let ____index = this.deltas.findIndex(delta => delta.key == newSnap.Snapshot.Key)
482
+ // this.deltas.push({})
483
+ // if (deltatick > -1) {
484
+ // let ____index = this.deltas.findIndex(delta => delta.key == newSnap.Snapshot.Key)
413
485
 
414
- if (____index > -1) {
415
- this.deltas[____index] = {data: newSnap.Snapshot.Data, key: newSnap.Snapshot.Key, id: newSnap.Snapshot.Key & 0xffff, type_id: ((newSnap.Snapshot.Key >> 16) & 0xffff), parsed: this.parseItem(newSnap.Snapshot.Data, ((newSnap.Snapshot.Key >> 16) & 0xffff), ((newSnap.Snapshot.Key) & 0xffff))};
416
- continue;
417
- }
418
- } // else
419
- this.deltas.push({data: newSnap.Snapshot.Data, key: newSnap.Snapshot.Key, id: newSnap.Snapshot.Key & 0xffff, type_id: ((newSnap.Snapshot.Key >> 16) & 0xffff), parsed: this.parseItem(newSnap.Snapshot.Data, ((newSnap.Snapshot.Key >> 16) & 0xffff), ((newSnap.Snapshot.Key) & 0xffff))});
486
+ // if (____index > -1) {
487
+ // this.deltas[____index] = {
488
+ // data: newSnap.Snapshot.Data,
489
+ // key: newSnap.Snapshot.Key,
490
+ // id: newSnap.Snapshot.Key & 0xffff,
491
+ // type_id: ((newSnap.Snapshot.Key >> 16) & 0xffff),
492
+ // parsed: this.parseItem(newSnap.Snapshot.Data, ((newSnap.Snapshot.Key >> 16) & 0xffff), ((newSnap.Snapshot.Key) & 0xffff))
493
+ // };
494
+ // continue;
495
+ // }
496
+ // // }
497
+ let oldDelta = oldDeltas.find(delta => delta.key == newSnap.Snapshot.Key);
498
+ if (oldDelta !== undefined && compareArrays(newSnap.Snapshot.Data, oldDelta.data)) {
499
+ this.deltas.push(oldDelta);
500
+
501
+ } else {
502
+ this.deltas.push({
503
+ data: newSnap.Snapshot.Data,
504
+ key: newSnap.Snapshot.Key,
505
+ id: newSnap.Snapshot.Key & 0xffff,
506
+ type_id: ((newSnap.Snapshot.Key >> 16) & 0xffff),
507
+ parsed: this.parseItem(newSnap.Snapshot.Data, ((newSnap.Snapshot.Key >> 16) & 0xffff), ((newSnap.Snapshot.Key) & 0xffff))
508
+ });
509
+
510
+ }
511
+ }
420
512
  }
421
-
422
- if (this.crc(recvTick) !== WantedCrc) {
513
+ // if (items.items.length != num_item_deltas)
514
+ // console.log("length", items.items.length, num_item_deltas, items.items.length - num_item_deltas, WantedCrc)
515
+ /* this.deltas = [];
516
+ for (let newSnap of this.eSnapHolder) {
517
+ if (newSnap.ack == recvTick)
518
+ this.deltas.push({
519
+ data: newSnap.Snapshot.Data,
520
+ key: newSnap.Snapshot.Key,
521
+ id: newSnap.Snapshot.Key & 0xffff,
522
+ type_id: ((newSnap.Snapshot.Key >> 16) & 0xffff),
523
+ parsed: this.parseItem(newSnap.Snapshot.Data, ((newSnap.Snapshot.Key >> 16) & 0xffff), ((newSnap.Snapshot.Key) & 0xffff))
524
+ });
525
+ }*/
526
+ let _crc = this.crc();
527
+ if (_crc !== WantedCrc) {
528
+ this.deltas = oldDeltas;
423
529
  this.crc_errors++;
530
+ console.log("crc error", _crc, WantedCrc, this.crc_errors)
424
531
  if (this.crc_errors > 5) {
425
532
  recvTick = -1;
426
533
  this.crc_errors = 0;
@@ -432,9 +539,23 @@ export class Snapshot {
432
539
  }
433
540
  } else if (this.crc_errors > 0)
434
541
  this.crc_errors--;
435
-
436
- return {items, recvTick};
542
+ // let filterLength = this.eSnapHolder.filter(a => a.ack == recvTick).length
543
+ // if (this.deltas.length !== filterLength) {
544
+ // this.deltas = this.deltas.filter(a => !deleted.includes(a.key))
545
+ // console.log(this.deltas.length, filterLength, this.deltas.length - filterLength, num_item_deltas )
546
+ // }
547
+
548
+ return {items: this.deltas, recvTick};
549
+ }
550
+ }
551
+ function compareArrays(first: number[], second: number[]) {
552
+ if (first.length !== second.length)
553
+ return false;
554
+ for (var i = 0; i < first.length; i++) {
555
+ if (first[i] !== second[i])
556
+ return false;
437
557
  }
558
+ return true;
438
559
  }
439
560
 
440
561
  function UndiffItem(oldItem: number[], newItem: number[]): number[] {
@@ -445,6 +566,7 @@ function UndiffItem(oldItem: number[], newItem: number[]): number[] {
445
566
  if (a !== undefined && out[i] !== undefined) {
446
567
  out[i] += a;
447
568
  } else {
569
+ console.log("UNDEFINED UNDEFINED UNDEFINED")
448
570
  out[i] = 0;
449
571
  }
450
572
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "teeworlds",
3
- "version": "2.4.2",
3
+ "version": "2.4.4",
4
4
  "description": "Library for (ingame) teeworlds bots.",
5
5
  "license": "MIT",
6
6
  "main": "index.js",
package/test.js DELETED
@@ -1,32 +0,0 @@
1
- const { Snapshot } = require("./lib/snapshot")
2
-
3
- let test_snap1 = [0,18,0,4,18,1744,1072,2,3,4,17,1840,912,1,0,4,16,880,880,0,0,4,15,1840,848,1,0,4,14,912,848,0,0,4,13,880,848,1,0,4,12,848,848,0,0,4,11,880,816,0,0,4,9,1264,656,0,0,4,8,1104,656,0,0,4,7,912,656,0,0,4,6,1712,624,2,2,4,5,1840,432,1,0,4,3,1840,336,1,0,9,0,292,1584,305,0,128,0,0,0,-1,0,0,1584,304,0,0,0,10,0,10,1,0,0,6,0,0,0,0,0,20,0,0,1,11,0,-287183387,-320474125,-1594563099,-2139062272,-2139062144,-2139062144,-2139062272,-1,-454695199,-169020288,-2139062144,-2139062144,-2139062144,-2139062272,0,65408,65408,10,0,1,0,0,0,0]
4
- let first_crc = 0x5b96263a;
5
- let test_snap2 = [0,1,0,9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,];
6
- let second_crc = 0x5b96263b;
7
- let snapunpacker = new Snapshot();
8
-
9
- let sum = (nums) => {
10
- var sum = 0;
11
- nums.forEach(a => {
12
- // if (a < 0)
13
- sum += a
14
- })
15
- console.log(sum, master_sum)
16
- return sum;
17
- }
18
-
19
- let master_sum = 0;
20
-
21
- // test_snap.forEach(a => master_sum += sum(...a));
22
- // master_sum = sum(...test_snap)
23
- // test_snap.forEach(a => {master_sum += a; console.log(master_sum)})
24
- // console.log(snapunpacker.unpackSnapshot(test_snap).items.items);
25
-
26
- snapunpacker = new Snapshot();
27
- console.log(snapunpacker.unpackSnapshot(test_snap1, -1, 1, first_crc));
28
- console.log(snapunpacker.unpackSnapshot(test_snap2, 1, 2, second_crc));
29
- // console.log(snapunpacker.eSnapHolder)
30
- // snapunpacker.eSnapHolder.forEach(a => console.log(a.Snapshot.Data))
31
- snapunpacker.eSnapHolder.forEach(a => master_sum += sum(a.Snapshot.Data))
32
- console.log(master_sum & 0xffffffff, 0x5b96263a)