node-gamedig/protocols/gamespy3.js

210 lines
7.7 KiB
JavaScript
Raw Normal View History

const async = require('async');
2014-10-29 08:02:03 +01:00
class Gamespy3 extends require('./core') {
2017-08-09 12:32:09 +02:00
constructor() {
super();
this.sessionId = 1;
this.encoding = 'latin1';
this.byteorder = 'be';
this.noChallenge = false;
this.useOnlySingleSplit = false;
this.isJc2mp = false;
}
run(state) {
let challenge,packets;
async.series([
(c) => {
if(this.noChallenge) return c();
this.sendPacket(9,false,false,false,(buffer) => {
const reader = this.reader(buffer);
challenge = parseInt(reader.string());
c();
});
},
(c) => {
let requestPayload;
2017-08-09 12:32:09 +02:00
if(this.isJc2mp) {
// they completely alter the protocol. because why not.
requestPayload = Buffer.from([0xff,0xff,0xff,0x02]);
} else {
requestPayload = Buffer.from([0xff,0xff,0xff,0x01]);
}
this.sendPacket(0,challenge,requestPayload,true,(b) => {
packets = b;
c();
});
},
(c) => {
2017-08-09 12:32:09 +02:00
// iterate over the received packets
// the first packet will start off with k/v pairs, followed with data fields
// the following packets will only have data fields
2017-08-09 12:32:09 +02:00
state.raw.playerTeamInfo = {};
2014-10-29 08:02:03 +01:00
2017-08-09 12:32:09 +02:00
for(let iPacket = 0; iPacket < packets.length; iPacket++) {
const packet = packets[iPacket];
const reader = this.reader(packet);
2014-10-29 08:02:03 +01:00
2017-08-09 12:32:09 +02:00
if(this.debug) {
console.log("+++"+packet.toString('hex'));
console.log(":::"+packet.toString('ascii'));
}
2014-10-29 08:02:03 +01:00
2017-08-09 12:32:09 +02:00
// Parse raw server key/values
2017-08-09 12:32:09 +02:00
if(iPacket === 0) {
while(!reader.done()) {
const key = reader.string();
if(!key) break;
let value = reader.string();
2017-08-09 12:32:09 +02:00
// reread the next line if we hit the weird ut3 bug
if(value === 'p1073741829') value = reader.string();
2014-10-29 08:02:03 +01:00
2017-08-09 12:32:09 +02:00
state.raw[key] = value;
}
}
2014-10-29 08:02:03 +01:00
2017-08-09 12:32:09 +02:00
// Parse player, team, item array state
2014-10-29 08:02:03 +01:00
2017-08-09 12:32:09 +02:00
if(this.isJc2mp) {
state.raw.numPlayers2 = reader.uint(2);
while(!reader.done()) {
const player = {};
2017-08-09 12:32:09 +02:00
player.name = reader.string();
player.steamid = reader.string();
player.ping = reader.uint(2);
state.players.push(player);
}
} else {
let firstMode = true;
while(!reader.done()) {
let mode = reader.string();
2017-08-09 12:32:09 +02:00
if(mode.charCodeAt(0) <= 2) mode = mode.substring(1);
if(!mode) continue;
let offset = 0;
if(iPacket !== 0 && firstMode) offset = reader.uint(1);
reader.skip(1);
firstMode = false;
const modeSplit = mode.split('_');
const modeName = modeSplit[0];
const modeType = modeSplit.length > 1 ? modeSplit[1] : 'no_';
2017-08-09 12:32:09 +02:00
if(!(modeType in state.raw.playerTeamInfo)) {
state.raw.playerTeamInfo[modeType] = [];
}
const store = state.raw.playerTeamInfo[modeType];
2017-08-09 12:32:09 +02:00
while(!reader.done()) {
const item = reader.string();
2017-08-09 12:32:09 +02:00
if(!item) break;
2017-08-09 12:32:09 +02:00
while(store.length <= offset) { store.push({}); }
store[offset][modeName] = item;
offset++;
}
}
}
}
2014-10-29 08:02:03 +01:00
2017-08-09 12:32:09 +02:00
c();
},
(c) => {
2017-08-09 12:32:09 +02:00
// Turn all that raw state into something useful
if('hostname' in state.raw) state.name = state.raw.hostname;
else if('servername' in state.raw) state.name = state.raw.servername;
if('mapname' in state.raw) state.map = state.raw.mapname;
if(state.raw.password === '1') state.password = true;
if('maxplayers' in state.raw) state.maxplayers = parseInt(state.raw.maxplayers);
if('' in state.raw.playerTeamInfo) {
for (const playerInfo of state.raw.playerTeamInfo['']) {
const player = {};
for(const from of Object.keys(playerInfo)) {
let key = from;
let value = playerInfo[from];
2017-08-09 12:32:09 +02:00
if(key === 'player') key = 'name';
if(key === 'score' || key === 'ping' || key === 'team' || key === 'deaths' || key === 'pid') value = parseInt(value);
player[key] = value;
}
state.players.push(player);
}
}
this.finish(state);
}
]);
}
sendPacket(type,challenge,payload,assemble,c) {
const challengeLength = (this.noChallenge || challenge === false) ? 0 : 4;
const payloadLength = payload ? payload.length : 0;
2014-10-29 08:02:03 +01:00
const b = Buffer.alloc(7 + challengeLength + payloadLength);
2017-08-09 12:32:09 +02:00
b.writeUInt8(0xFE, 0);
b.writeUInt8(0xFD, 1);
b.writeUInt8(type, 2);
b.writeUInt32BE(this.sessionId, 3);
if(challengeLength) b.writeInt32BE(challenge, 7);
if(payloadLength) payload.copy(b, 7+challengeLength);
let numPackets = 0;
const packets = {};
2017-08-09 12:32:09 +02:00
this.udpSend(b,(buffer) => {
const reader = this.reader(buffer);
const iType = reader.uint(1);
2017-08-09 12:32:09 +02:00
if(iType !== type) return;
const iSessionId = reader.uint(4);
2017-08-09 12:32:09 +02:00
if(iSessionId !== this.sessionId) return;
if(!assemble) {
c(reader.rest());
return true;
}
if(this.useOnlySingleSplit) {
// has split headers, but they are worthless and only one packet is used
reader.skip(11);
c([reader.rest()]);
return true;
}
reader.skip(9); // filler data -- usually set to 'splitnum\0'
let id = reader.uint(1);
const last = (id & 0x80);
2017-08-09 12:32:09 +02:00
id = id & 0x7f;
if(last) numPackets = id+1;
2017-08-09 12:32:09 +02:00
reader.skip(1); // "another 'packet number' byte, but isn't understood."
2014-10-29 08:02:03 +01:00
2017-08-09 12:32:09 +02:00
packets[id] = reader.rest();
if(this.debug) {
console.log("Received packet #"+id);
if(last) console.log("(last)");
}
2014-10-29 08:02:03 +01:00
2017-08-09 12:32:09 +02:00
if(!numPackets || Object.keys(packets).length !== numPackets) return;
2017-08-09 12:32:09 +02:00
// assemble the parts
const list = [];
2017-08-09 12:32:09 +02:00
for(let i = 0; i < numPackets; i++) {
if(!(i in packets)) {
this.fatal('Missing packet #'+i);
return true;
}
list.push(packets[i]);
}
c(list);
return true;
});
}
}
module.exports = Gamespy3;