node-gamedig/protocols/gamespy3.js

169 lines
4.6 KiB
JavaScript
Raw Normal View History

var async = require('async');
2013-07-10 12:02:48 +02:00
module.exports = require('./core').extend({
init: function() {
this._super();
this.sessionId = 1;
this.encoding = 'latin1';
this.byteorder = 'be';
this.noChallenge = false;
this.useOnlySingleSplit = false;
2013-07-10 12:02:48 +02:00
},
run: function(state) {
2013-07-10 12:02:48 +02:00
var self = this;
var challenge,packets;
async.series([
function(c) {
if(self.noChallenge) return c();
self.sendPacket(9,false,false,false,function(buffer) {
var reader = self.reader(buffer);
challenge = parseInt(reader.string());
c();
});
},
function(c) {
self.sendPacket(0,challenge,new Buffer([0xff,0xff,0xff,0x01]),true,function(b) {
packets = b;
c();
});
},
function(c) {
// 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
var data = {};
for(var iPacket = 0; iPacket < packets.length; iPacket++) {
var packet = packets[iPacket];
var reader = self.reader(packet);
if(self.debug) {
console.log("+++"+packet.toString('hex'));
console.log(":::"+packet.toString('ascii'));
}
2013-07-10 12:02:48 +02:00
if(iPacket == 0) {
while(!reader.done()) {
var key = reader.string();
if(!key) break;
var value = reader.string();
// reread the next line if we hit the weird ut3 bug
if(value == 'p1073741829') value = reader.string();
state.raw[key] = value;
}
}
var firstMode = true;
2013-07-10 12:02:48 +02:00
while(!reader.done()) {
var mode = reader.string();
if(mode.charCodeAt(0) <= 2) mode = mode.substring(1);
if(!mode) continue;
var offset = 0;
if(iPacket != 0 && firstMode) offset = reader.uint(1);
reader.skip(1);
firstMode = false;
while(!reader.done()) {
var item = reader.string();
if(!item) break;
if(
mode == 'player_'
|| mode == 'score_'
|| mode == 'ping_'
|| mode == 'team_'
|| mode == 'deaths_'
|| mode == 'pid_'
) {
if(state.players.length <= offset)
state.players.push({});
}
if(mode == 'player_') state.players[offset].name = item;
if(mode == 'score_') state.players[offset].score = parseInt(item);
if(mode == 'ping_') state.players[offset].ping = parseInt(item);
if(mode == 'team_') state.players[offset].team = parseInt(item);
if(mode == 'deaths_') state.players[offset].deaths = parseInt(item);
if(mode == 'pid_') state.players[offset].pid = item;
offset++;
2013-07-10 12:02:48 +02:00
}
}
}
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);
2013-07-10 12:02:48 +02:00
self.finish(state);
}
]);
2013-07-10 12:02:48 +02:00
},
sendPacket: function(type,challenge,payload,assemble,c) {
var self = this;
var challengeLength = (this.noChallenge || challenge === false) ? 0 : 4;
2013-07-10 12:02:48 +02:00
var payloadLength = payload ? payload.length : 0;
var b = new Buffer(7 + challengeLength + payloadLength);
b.writeUInt8(0xFE, 0);
b.writeUInt8(0xFD, 1);
b.writeUInt8(type, 2);
b.writeUInt32BE(this.sessionId, 3);
2013-07-10 18:41:49 +02:00
if(challengeLength) b.writeInt32BE(challenge, 7);
2013-07-10 12:02:48 +02:00
if(payloadLength) payload.copy(b, 7+challengeLength);
var numPackets = 0;
var packets = {};
this.udpSend(b,function(buffer) {
var reader = self.reader(buffer);
var iType = reader.uint(1);
2013-07-10 12:02:48 +02:00
if(iType != type) return;
var iSessionId = reader.uint(4);
2013-07-10 12:02:48 +02:00
if(iSessionId != self.sessionId) return;
if(!assemble) {
c(reader.rest());
return true;
}
if(self.useOnlySingleSplit) {
// has split headers, but they are worthless and only one packet is used
reader.skip(11);
c([reader.rest()]);
2013-07-10 12:02:48 +02:00
return true;
}
reader.skip(9); // filler data -- usually set to 'splitnum\0'
var id = reader.uint(1);
2013-07-10 12:02:48 +02:00
var last = (id & 0x80);
id = id & 0x7f;
if(last) numPackets = id+1;
reader.skip(1); // "another 'packet number' byte, but isn't understood."
2013-07-10 12:02:48 +02:00
packets[id] = reader.rest();
if(self.debug) {
console.log("Received packet #"+id);
if(last) console.log("(last)");
}
2013-07-10 12:02:48 +02:00
if(!numPackets || Object.keys(packets).length != numPackets) return;
// assemble the parts
var list = [];
for(var i = 0; i < numPackets; i++) {
if(!(i in packets)) {
2014-02-02 12:58:36 +01:00
self.fatal('Missing packet #'+i);
2013-07-10 12:02:48 +02:00
return true;
}
list.push(packets[i]);
2013-07-10 12:02:48 +02:00
}
c(list);
2013-07-10 12:02:48 +02:00
return true;
});
}
});