Added support for cs2d (2.0.5)

This commit is contained in:
mmorrison 2019-01-13 23:54:36 -06:00
parent 04bd6d9514
commit 5aaff8e1e0
6 changed files with 90 additions and 4 deletions

View file

@ -278,7 +278,7 @@ class Core extends EventEmitter {
/**
* @param {Buffer|string} buffer
* @param {function(Buffer):T} onPacket
* @param {function(Buffer):T=} onPacket
* @param {(function():T)=} onTimeout
* @returns Promise<T>
* @template T
@ -297,6 +297,10 @@ class Core extends EventEmitter {
const socket = this.udpSocket;
socket.send(buffer, address, port);
if (!onPacket && !onTimeout) {
return null;
}
let socketCallback;
let timeout;
try {

74
protocols/cs2d.js Normal file
View file

@ -0,0 +1,74 @@
const Core = require('./core');
class Cs2d extends Core {
async run(state) {
{
const reader = await this.sendQuery(
Buffer.from('\x01\x00\x03\x10\x21\xFB\x01\x75\x00', 'binary'),
Buffer.from('\x01\x00\xfb\x01', 'binary')
);
const flags = reader.uint(1);
state.raw.flags = flags;
state.password = this.readFlag(flags, 0);
state.raw.registeredOnly = this.readFlag(flags, 1);
state.raw.fogOfWar = this.readFlag(flags, 2);
state.raw.friendlyFire = this.readFlag(flags, 3);
state.raw.botsEnabled = this.readFlag(flags, 5);
state.raw.luaScripts = this.readFlag(flags, 6);
state.name = this.readString(reader);
state.map = this.readString(reader);
state.raw.numplayers = reader.uint(1);
state.maxplayers = reader.uint(1);
state.raw.gamemode = reader.uint(1);
if (state.raw.botsEnabled) {
state.raw.numbots = reader.uint(1);
} else {
state.raw.numbots = 0;
}
}
{
const reader = await this.sendQuery(
Buffer.from('\x01\x00\xFB\x05', 'binary'),
Buffer.from('\x01\x00\xFB\x05', 'binary')
);
state.raw.numplayers2 = reader.uint(1);
while(!reader.done()) {
const player = {};
player.id = reader.uint(1);
player.name = this.readString(reader);
player.team = reader.uint(1);
player.score = reader.uint(4);
player.deaths = reader.uint(4);
if (state.bots.length < state.raw.numbots) {
state.bots.push(player);
} else {
state.players.push(player);
}
}
}
}
async sendQuery(request, expectedHeader) {
// Send multiple copies of the request packet, because cs2d likes to just ignore them randomly
await this.udpSend(request);
await this.udpSend(request);
return await this.udpSend(request, (buffer) => {
const reader = this.reader(buffer);
const header = reader.part(4);
if (!header.equals(expectedHeader)) return;
return reader;
});
}
readFlag(flags, offset) {
return !!(flags & (1 << offset));
}
readString(reader) {
const length = reader.uint(1);
return reader.string({length:length});
}
}
module.exports = Cs2d;