2017-08-09 11:05:55 +02:00
|
|
|
const async = require('async');
|
2014-10-29 08:02:03 +01:00
|
|
|
|
2017-08-09 11:05:55 +02: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();
|
|
|
|
});
|
|
|
|
},
|
2017-08-09 11:05:55 +02:00
|
|
|
(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();
|
|
|
|
});
|
|
|
|
},
|
2017-08-09 11:05:55 +02:00
|
|
|
(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
|
2015-01-17 13:51:02 +01:00
|
|
|
|
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];
|
2017-08-09 11:05:55 +02:00
|
|
|
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
|
2015-01-17 13:51:02 +01:00
|
|
|
|
2017-08-09 12:32:09 +02:00
|
|
|
if(iPacket === 0) {
|
|
|
|
while(!reader.done()) {
|
|
|
|
const key = reader.string();
|
|
|
|
if(!key) break;
|
2017-08-09 11:05:55 +02:00
|
|
|
let value = reader.string();
|
2015-01-17 13:51:02 +01:00
|
|
|
|
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()) {
|
2017-08-09 11:05:55 +02:00
|
|
|
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()) {
|
2017-08-09 11:05:55 +02:00
|
|
|
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;
|
2015-01-17 13:51:02 +01:00
|
|
|
|
2017-08-09 11:05:55 +02:00
|
|
|
const modeSplit = mode.split('_');
|
|
|
|
const modeName = modeSplit[0];
|
|
|
|
const modeType = modeSplit.length > 1 ? modeSplit[1] : 'no_';
|
2015-01-17 13:51:02 +01:00
|
|
|
|
2017-08-09 12:32:09 +02:00
|
|
|
if(!(modeType in state.raw.playerTeamInfo)) {
|
|
|
|
state.raw.playerTeamInfo[modeType] = [];
|
|
|
|
}
|
2017-08-09 11:05:55 +02:00
|
|
|
const store = state.raw.playerTeamInfo[modeType];
|
2015-01-17 13:51:02 +01:00
|
|
|
|
2017-08-09 12:32:09 +02:00
|
|
|
while(!reader.done()) {
|
2017-08-09 11:05:55 +02:00
|
|
|
const item = reader.string();
|
2017-08-09 12:32:09 +02:00
|
|
|
if(!item) break;
|
2015-01-17 13:51:02 +01:00
|
|
|
|
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();
|
|
|
|
},
|
2015-01-17 13:51:02 +01:00
|
|
|
|
2017-08-09 11:05:55 +02:00
|
|
|
(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;
|
2017-08-09 11:05:55 +02:00
|
|
|
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;
|
2017-08-09 11:05:55 +02:00
|
|
|
const payloadLength = payload ? payload.length : 0;
|
2014-10-29 08:02:03 +01:00
|
|
|
|
2017-08-09 11:05:55 +02: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;
|
2017-08-09 11:05:55 +02:00
|
|
|
const packets = {};
|
2017-08-09 12:32:09 +02:00
|
|
|
this.udpSend(b,(buffer) => {
|
2017-08-09 11:05:55 +02:00
|
|
|
const reader = this.reader(buffer);
|
|
|
|
const iType = reader.uint(1);
|
2017-08-09 12:32:09 +02:00
|
|
|
if(iType !== type) return;
|
2017-08-09 11:05:55 +02:00
|
|
|
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'
|
2017-08-09 11:05:55 +02:00
|
|
|
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;
|
2015-01-17 13:51:02 +01:00
|
|
|
|
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;
|
2015-01-17 13:51:02 +01:00
|
|
|
|
2017-08-09 12:32:09 +02:00
|
|
|
// assemble the parts
|
2017-08-09 11:05:55 +02:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
2017-08-09 11:05:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Gamespy3;
|