mirror of
https://github.com/tribufu/node-gamedig
synced 2026-05-18 09:35:50 +00:00
Convert tabs to spaces
This commit is contained in:
parent
0dd25bfcda
commit
3674d384d0
39 changed files with 2414 additions and 2414 deletions
|
|
@ -1,209 +1,209 @@
|
|||
const async = require('async');
|
||||
|
||||
class Gamespy3 extends require('./core') {
|
||||
constructor() {
|
||||
super();
|
||||
this.sessionId = 1;
|
||||
this.encoding = 'latin1';
|
||||
this.byteorder = 'be';
|
||||
this.noChallenge = false;
|
||||
this.useOnlySingleSplit = false;
|
||||
this.isJc2mp = false;
|
||||
}
|
||||
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;
|
||||
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();
|
||||
});
|
||||
},
|
||||
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;
|
||||
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]);
|
||||
}
|
||||
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();
|
||||
});
|
||||
},
|
||||
this.sendPacket(0,challenge,requestPayload,true,(b) => {
|
||||
packets = b;
|
||||
c();
|
||||
});
|
||||
},
|
||||
(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
|
||||
// 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
|
||||
|
||||
state.raw.playerTeamInfo = {};
|
||||
state.raw.playerTeamInfo = {};
|
||||
|
||||
for(let iPacket = 0; iPacket < packets.length; iPacket++) {
|
||||
const packet = packets[iPacket];
|
||||
for(let iPacket = 0; iPacket < packets.length; iPacket++) {
|
||||
const packet = packets[iPacket];
|
||||
const reader = this.reader(packet);
|
||||
|
||||
if(this.debug) {
|
||||
console.log("+++"+packet.toString('hex'));
|
||||
console.log(":::"+packet.toString('ascii'));
|
||||
}
|
||||
if(this.debug) {
|
||||
console.log("+++"+packet.toString('hex'));
|
||||
console.log(":::"+packet.toString('ascii'));
|
||||
}
|
||||
|
||||
// Parse raw server key/values
|
||||
// Parse raw server key/values
|
||||
|
||||
if(iPacket === 0) {
|
||||
while(!reader.done()) {
|
||||
const key = reader.string();
|
||||
if(!key) break;
|
||||
if(iPacket === 0) {
|
||||
while(!reader.done()) {
|
||||
const key = reader.string();
|
||||
if(!key) break;
|
||||
let value = reader.string();
|
||||
|
||||
// reread the next line if we hit the weird ut3 bug
|
||||
if(value === 'p1073741829') value = reader.string();
|
||||
// reread the next line if we hit the weird ut3 bug
|
||||
if(value === 'p1073741829') value = reader.string();
|
||||
|
||||
state.raw[key] = value;
|
||||
}
|
||||
}
|
||||
state.raw[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
// Parse player, team, item array state
|
||||
// Parse player, team, item array state
|
||||
|
||||
if(this.isJc2mp) {
|
||||
state.raw.numPlayers2 = reader.uint(2);
|
||||
while(!reader.done()) {
|
||||
if(this.isJc2mp) {
|
||||
state.raw.numPlayers2 = reader.uint(2);
|
||||
while(!reader.done()) {
|
||||
const player = {};
|
||||
player.name = reader.string();
|
||||
player.steamid = reader.string();
|
||||
player.ping = reader.uint(2);
|
||||
state.players.push(player);
|
||||
}
|
||||
} else {
|
||||
let firstMode = true;
|
||||
while(!reader.done()) {
|
||||
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();
|
||||
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;
|
||||
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_';
|
||||
|
||||
if(!(modeType in state.raw.playerTeamInfo)) {
|
||||
state.raw.playerTeamInfo[modeType] = [];
|
||||
}
|
||||
if(!(modeType in state.raw.playerTeamInfo)) {
|
||||
state.raw.playerTeamInfo[modeType] = [];
|
||||
}
|
||||
const store = state.raw.playerTeamInfo[modeType];
|
||||
|
||||
while(!reader.done()) {
|
||||
while(!reader.done()) {
|
||||
const item = reader.string();
|
||||
if(!item) break;
|
||||
if(!item) break;
|
||||
|
||||
while(store.length <= offset) { store.push({}); }
|
||||
store[offset][modeName] = item;
|
||||
offset++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
while(store.length <= offset) { store.push({}); }
|
||||
store[offset][modeName] = item;
|
||||
offset++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
c();
|
||||
},
|
||||
c();
|
||||
},
|
||||
|
||||
(c) => {
|
||||
// Turn all that raw state into something useful
|
||||
// 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('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;
|
||||
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];
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
]);
|
||||
}
|
||||
this.finish(state);
|
||||
}
|
||||
]);
|
||||
}
|
||||
|
||||
sendPacket(type,challenge,payload,assemble,c) {
|
||||
const challengeLength = (this.noChallenge || challenge === false) ? 0 : 4;
|
||||
sendPacket(type,challenge,payload,assemble,c) {
|
||||
const challengeLength = (this.noChallenge || challenge === false) ? 0 : 4;
|
||||
const payloadLength = payload ? payload.length : 0;
|
||||
|
||||
const b = Buffer.alloc(7 + challengeLength + payloadLength);
|
||||
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);
|
||||
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;
|
||||
let numPackets = 0;
|
||||
const packets = {};
|
||||
this.udpSend(b,(buffer) => {
|
||||
this.udpSend(b,(buffer) => {
|
||||
const reader = this.reader(buffer);
|
||||
const iType = reader.uint(1);
|
||||
if(iType !== type) return;
|
||||
if(iType !== type) return;
|
||||
const iSessionId = reader.uint(4);
|
||||
if(iSessionId !== this.sessionId) return;
|
||||
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;
|
||||
}
|
||||
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'
|
||||
reader.skip(9); // filler data -- usually set to 'splitnum\0'
|
||||
let id = reader.uint(1);
|
||||
const last = (id & 0x80);
|
||||
id = id & 0x7f;
|
||||
if(last) numPackets = id+1;
|
||||
id = id & 0x7f;
|
||||
if(last) numPackets = id+1;
|
||||
|
||||
reader.skip(1); // "another 'packet number' byte, but isn't understood."
|
||||
reader.skip(1); // "another 'packet number' byte, but isn't understood."
|
||||
|
||||
packets[id] = reader.rest();
|
||||
if(this.debug) {
|
||||
console.log("Received packet #"+id);
|
||||
if(last) console.log("(last)");
|
||||
}
|
||||
packets[id] = reader.rest();
|
||||
if(this.debug) {
|
||||
console.log("Received packet #"+id);
|
||||
if(last) console.log("(last)");
|
||||
}
|
||||
|
||||
if(!numPackets || Object.keys(packets).length !== numPackets) return;
|
||||
if(!numPackets || Object.keys(packets).length !== numPackets) return;
|
||||
|
||||
// assemble the parts
|
||||
// assemble the parts
|
||||
const list = [];
|
||||
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;
|
||||
});
|
||||
}
|
||||
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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue