Convert tabs to spaces

This commit is contained in:
mmorrison 2017-08-09 05:32:09 -05:00
parent 0dd25bfcda
commit 3674d384d0
39 changed files with 2414 additions and 2414 deletions

View file

@ -7,94 +7,94 @@ const udpSocket = dgram.createSocket('udp4');
udpSocket.unref();
udpSocket.bind(21943);
udpSocket.on('message', (buffer, rinfo) => {
if(Gamedig.debug) console.log(rinfo.address+':'+rinfo.port+" <--UDP "+buffer.toString('hex'));
for(const query of activeQueries) {
if(
query.options.address !== rinfo.address
&& query.options.altaddress !== rinfo.address
) continue;
if(query.options.port_query !== rinfo.port) continue;
query._udpResponse(buffer);
break;
}
if(Gamedig.debug) console.log(rinfo.address+':'+rinfo.port+" <--UDP "+buffer.toString('hex'));
for(const query of activeQueries) {
if(
query.options.address !== rinfo.address
&& query.options.altaddress !== rinfo.address
) continue;
if(query.options.port_query !== rinfo.port) continue;
query._udpResponse(buffer);
break;
}
});
udpSocket.on('error', (e) => {
if(Gamedig.debug) console.log("UDP ERROR: "+e);
if(Gamedig.debug) console.log("UDP ERROR: "+e);
});
class Gamedig {
static query(options,callback) {
const promise = new Promise((resolve,reject) => {
static query(options,callback) {
const promise = new Promise((resolve,reject) => {
for (const key of Object.keys(options)) {
if (['port_query', 'port'].includes(key)) {
options[key] = parseInt(options[key]);
}
}
options.callback = (state) => {
if (state.error) reject(state.error);
else resolve(state);
};
let query;
try {
query = TypeResolver.lookup(options.type);
} catch(e) {
process.nextTick(() => {
options.callback({error:e});
});
return;
}
query.debug = Gamedig.debug;
query.udpSocket = udpSocket;
query.type = options.type;
if(!('port' in query.options) && ('port_query' in query.options)) {
if(Gamedig.isCommandLine) {
process.stderr.write(
"Warning! This game is so old, that we don't know"
+" what the server's connection port is. We've guessed that"
+" the query port for "+query.type+" is "+query.options.port_query+"."
+" If you know the connection port for this type of server, please let"
+" us know on the GameDig issue tracker, thanks!\n"
);
}
query.options.port = query.options.port_query;
delete query.options.port_query;
}
// copy over options
for(const key of Object.keys(options)) {
query.options[key] = options[key];
if (['port_query', 'port'].includes(key)) {
options[key] = parseInt(options[key]);
}
}
activeQueries.push(query);
options.callback = (state) => {
if (state.error) reject(state.error);
else resolve(state);
};
query.on('finished',() => {
const i = activeQueries.indexOf(query);
if(i >= 0) activeQueries.splice(i, 1);
});
let query;
try {
query = TypeResolver.lookup(options.type);
} catch(e) {
process.nextTick(() => {
options.callback({error:e});
});
return;
}
query.debug = Gamedig.debug;
query.udpSocket = udpSocket;
query.type = options.type;
process.nextTick(() => {
query.start();
});
});
if(!('port' in query.options) && ('port_query' in query.options)) {
if(Gamedig.isCommandLine) {
process.stderr.write(
"Warning! This game is so old, that we don't know"
+" what the server's connection port is. We've guessed that"
+" the query port for "+query.type+" is "+query.options.port_query+"."
+" If you know the connection port for this type of server, please let"
+" us know on the GameDig issue tracker, thanks!\n"
);
}
query.options.port = query.options.port_query;
delete query.options.port_query;
}
if (callback && callback instanceof Function) {
if(callback.length === 2) {
promise
.then((state) => callback(null,state))
.catch((error) => callback(error));
} else if (callback.length === 1) {
promise
.then((state) => callback(state))
.catch((error) => callback({error:error}));
}
}
// copy over options
for(const key of Object.keys(options)) {
query.options[key] = options[key];
}
return promise;
}
activeQueries.push(query);
query.on('finished',() => {
const i = activeQueries.indexOf(query);
if(i >= 0) activeQueries.splice(i, 1);
});
process.nextTick(() => {
query.start();
});
});
if (callback && callback instanceof Function) {
if(callback.length === 2) {
promise
.then((state) => callback(null,state))
.catch((error) => callback(error));
} else if (callback.length === 1) {
promise
.then((state) => callback(state))
.catch((error) => callback({error:error}));
}
}
return promise;
}
}

View file

@ -1,147 +1,147 @@
const Iconv = require('iconv-lite'),
Long = require('long');
Long = require('long');
function readUInt64BE(buffer,offset) {
const high = buffer.readUInt32BE(offset);
const low = buffer.readUInt32BE(offset+4);
return new Long(low,high,true);
return new Long(low,high,true);
}
function readUInt64LE(buffer,offset) {
const low = buffer.readUInt32LE(offset);
const high = buffer.readUInt32LE(offset+4);
return new Long(low,high,true);
return new Long(low,high,true);
}
class Reader {
constructor(query,buffer) {
this.query = query;
this.buffer = buffer;
this.i = 0;
}
constructor(query,buffer) {
this.query = query;
this.buffer = buffer;
this.i = 0;
}
offset() {
return this.i;
}
offset() {
return this.i;
}
skip(i) {
this.i += i;
}
skip(i) {
this.i += i;
}
string(...args) {
string(...args) {
let options = {};
if(args.length === 0) {
options = {};
} else if(args.length === 1) {
if(typeof args[0] === 'string') options = { delimiter: args[0] };
else if(typeof args[0] === 'number') options = { length: args[0] };
else options = args[0];
}
if(args.length === 0) {
options = {};
} else if(args.length === 1) {
if(typeof args[0] === 'string') options = { delimiter: args[0] };
else if(typeof args[0] === 'number') options = { length: args[0] };
else options = args[0];
}
options.encoding = options.encoding || this.query.encoding;
if(options.encoding === 'latin1') options.encoding = 'win1252';
options.encoding = options.encoding || this.query.encoding;
if(options.encoding === 'latin1') options.encoding = 'win1252';
const start = this.i+0;
let end = start;
if(!('length' in options)) {
// terminated by the delimiter
let delim = options.delimiter || this.query.delimiter;
if(typeof delim === 'string') delim = delim.charCodeAt(0);
while(true) {
if(end >= this.buffer.length) {
end = this.buffer.length;
break;
}
if(this.buffer.readUInt8(end) === delim) break;
end++;
}
this.i = end+1;
} else {
end = start+options.length;
if(end >= this.buffer.length) {
end = this.buffer.length;
}
this.i = end;
}
const start = this.i+0;
let end = start;
if(!('length' in options)) {
// terminated by the delimiter
let delim = options.delimiter || this.query.delimiter;
if(typeof delim === 'string') delim = delim.charCodeAt(0);
while(true) {
if(end >= this.buffer.length) {
end = this.buffer.length;
break;
}
if(this.buffer.readUInt8(end) === delim) break;
end++;
}
this.i = end+1;
} else {
end = start+options.length;
if(end >= this.buffer.length) {
end = this.buffer.length;
}
this.i = end;
}
let out = this.buffer.slice(start, end);
const enc = options.encoding;
if(enc === 'utf8' || enc === 'ucs2' || enc === 'binary') {
out = out.toString(enc);
} else {
out = Iconv.decode(out,enc);
}
return out;
}
let out = this.buffer.slice(start, end);
const enc = options.encoding;
if(enc === 'utf8' || enc === 'ucs2' || enc === 'binary') {
out = out.toString(enc);
} else {
out = Iconv.decode(out,enc);
}
return out;
}
int(bytes) {
let r = 0;
if(this.remaining() >= bytes) {
if(this.query.byteorder === 'be') {
if(bytes === 1) r = this.buffer.readInt8(this.i);
else if(bytes === 2) r = this.buffer.readInt16BE(this.i);
else if(bytes === 4) r = this.buffer.readInt32BE(this.i);
} else {
if(bytes === 1) r = this.buffer.readInt8(this.i);
else if(bytes === 2) r = this.buffer.readInt16LE(this.i);
else if(bytes === 4) r = this.buffer.readInt32LE(this.i);
}
}
this.i += bytes;
return r;
}
int(bytes) {
let r = 0;
if(this.remaining() >= bytes) {
if(this.query.byteorder === 'be') {
if(bytes === 1) r = this.buffer.readInt8(this.i);
else if(bytes === 2) r = this.buffer.readInt16BE(this.i);
else if(bytes === 4) r = this.buffer.readInt32BE(this.i);
} else {
if(bytes === 1) r = this.buffer.readInt8(this.i);
else if(bytes === 2) r = this.buffer.readInt16LE(this.i);
else if(bytes === 4) r = this.buffer.readInt32LE(this.i);
}
}
this.i += bytes;
return r;
}
/** @returns {number} */
uint(bytes) {
let r = 0;
if(this.remaining() >= bytes) {
if(this.query.byteorder === 'be') {
if(bytes === 1) r = this.buffer.readUInt8(this.i);
else if(bytes === 2) r = this.buffer.readUInt16BE(this.i);
else if(bytes === 4) r = this.buffer.readUInt32BE(this.i);
else if(bytes === 8) r = readUInt64BE(this.buffer,this.i).toString();
} else {
if(bytes === 1) r = this.buffer.readUInt8(this.i);
else if(bytes === 2) r = this.buffer.readUInt16LE(this.i);
else if(bytes === 4) r = this.buffer.readUInt32LE(this.i);
else if(bytes === 8) r = readUInt64LE(this.buffer,this.i).toString();
}
}
this.i += bytes;
return r;
}
/** @returns {number} */
uint(bytes) {
let r = 0;
if(this.remaining() >= bytes) {
if(this.query.byteorder === 'be') {
if(bytes === 1) r = this.buffer.readUInt8(this.i);
else if(bytes === 2) r = this.buffer.readUInt16BE(this.i);
else if(bytes === 4) r = this.buffer.readUInt32BE(this.i);
else if(bytes === 8) r = readUInt64BE(this.buffer,this.i).toString();
} else {
if(bytes === 1) r = this.buffer.readUInt8(this.i);
else if(bytes === 2) r = this.buffer.readUInt16LE(this.i);
else if(bytes === 4) r = this.buffer.readUInt32LE(this.i);
else if(bytes === 8) r = readUInt64LE(this.buffer,this.i).toString();
}
}
this.i += bytes;
return r;
}
float() {
let r = 0;
if(this.remaining() >= 4) {
if(this.query.byteorder === 'be') r = this.buffer.readFloatBE(this.i);
else r = this.buffer.readFloatLE(this.i);
}
this.i += 4;
return r;
}
float() {
let r = 0;
if(this.remaining() >= 4) {
if(this.query.byteorder === 'be') r = this.buffer.readFloatBE(this.i);
else r = this.buffer.readFloatLE(this.i);
}
this.i += 4;
return r;
}
part(bytes) {
let r;
if(this.remaining() >= bytes) {
r = this.buffer.slice(this.i,this.i+bytes);
} else {
r = Buffer.from([]);
}
this.i += bytes;
return r;
}
part(bytes) {
let r;
if(this.remaining() >= bytes) {
r = this.buffer.slice(this.i,this.i+bytes);
} else {
r = Buffer.from([]);
}
this.i += bytes;
return r;
}
remaining() {
return this.buffer.length-this.i;
}
remaining() {
return this.buffer.length-this.i;
}
rest() {
return this.buffer.slice(this.i);
}
rest() {
return this.buffer.slice(this.i);
}
done() {
return this.i >= this.buffer.length;
}
done() {
return this.i >= this.buffer.length;
}
}
module.exports = Reader;

View file

@ -1,94 +1,94 @@
const Path = require('path'),
fs = require('fs');
fs = require('fs');
const protocolDir = Path.normalize(__dirname+'/../protocols');
const gamesFile = Path.normalize(__dirname+'/../games.txt');
function parseList(str) {
if(!str) return {};
if(!str) return {};
const out = {};
for (const one of str.split(',')) {
for (const one of str.split(',')) {
const equals = one.indexOf('=');
const key = equals === -1 ? one : one.substr(0,equals);
let value = equals === -1 ? '' : one.substr(equals+1);
let value = equals === -1 ? '' : one.substr(equals+1);
if(value === 'true' || value === '') value = true;
else if(value === 'false') value = false;
else if(!isNaN(value)) value = parseInt(value);
if(value === 'true' || value === '') value = true;
else if(value === 'false') value = false;
else if(!isNaN(value)) value = parseInt(value);
out[key] = value;
}
return out;
out[key] = value;
}
return out;
}
function readGames() {
const lines = fs.readFileSync(gamesFile,'utf8').split('\n');
const games = {};
for (let line of lines) {
// strip comments
const comment = line.indexOf('#');
if(comment !== -1) line = line.substr(0,comment);
line = line.trim();
if(!line) continue;
for (let line of lines) {
// strip comments
const comment = line.indexOf('#');
if(comment !== -1) line = line.substr(0,comment);
line = line.trim();
if(!line) continue;
const split = line.split('|');
const split = line.split('|');
games[split[0].trim()] = {
pretty: split[1].trim(),
protocol: split[2].trim(),
options: parseList(split[3]),
params: parseList(split[4])
};
}
return games;
games[split[0].trim()] = {
pretty: split[1].trim(),
protocol: split[2].trim(),
options: parseList(split[3]),
params: parseList(split[4])
};
}
return games;
}
const games = readGames();
function createProtocolInstance(type) {
type = Path.basename(type);
type = Path.basename(type);
const path = protocolDir+'/'+type;
if(!fs.existsSync(path+'.js')) throw Error('Protocol definition file missing: '+type);
const path = protocolDir+'/'+type;
if(!fs.existsSync(path+'.js')) throw Error('Protocol definition file missing: '+type);
const protocol = require(path);
return new protocol();
return new protocol();
}
class TypeResolver {
static lookup(type) {
if(!type) throw Error('No game specified');
static lookup(type) {
if(!type) throw Error('No game specified');
if(type.substr(0,9) === 'protocol-') {
return createProtocolInstance(type.substr(9));
}
if(type.substr(0,9) === 'protocol-') {
return createProtocolInstance(type.substr(9));
}
const game = games[type];
if(!game) throw Error('Invalid game: '+type);
if(!game) throw Error('Invalid game: '+type);
const query = createProtocolInstance(game.protocol);
query.pretty = game.pretty;
for(const key of Object.keys(game.options)) {
query.pretty = game.pretty;
for(const key of Object.keys(game.options)) {
query.options[key] = game.options[key];
}
for(const key of Object.keys(game.params)) {
for(const key of Object.keys(game.params)) {
query[key] = game.params[key];
}
return query;
}
static printReadme() {
let out = '';
for(const key of Object.keys(games)) {
const game = games[key];
out += "* "+game.pretty+" ("+key+")";
if(game.options.port_query_offset || game.options.port_query)
out += " [[Separate Query Port](#separate-query-port)]";
if(game.params.doc_notes)
out += " [[Additional Notes](#"+game.params.doc_notes+")]";
out += "\n";
}
return out;
}
return query;
}
static printReadme() {
let out = '';
for(const key of Object.keys(games)) {
const game = games[key];
out += "* "+game.pretty+" ("+key+")";
if(game.options.port_query_offset || game.options.port_query)
out += " [[Separate Query Port](#separate-query-port)]";
if(game.params.doc_notes)
out += " [[Additional Notes](#"+game.params.doc_notes+")]";
out += "\n";
}
return out;
}
}
module.exports = TypeResolver;