node-gamedig/lib/typeresolver.js

98 lines
2.8 KiB
JavaScript
Raw Normal View History

const Path = require('path'),
2017-08-09 12:32:09 +02:00
fs = require('fs');
2014-10-29 08:02:03 +01:00
const protocolDir = Path.normalize(__dirname+'/../protocols');
const gamesFile = Path.normalize(__dirname+'/../games.txt');
2014-10-29 08:02:03 +01:00
function parseList(str) {
2017-08-09 12:32:09 +02:00
if(!str) return {};
const out = {};
2017-08-09 12:32:09 +02:00
for (const one of str.split(',')) {
const equals = one.indexOf('=');
const key = equals === -1 ? one : one.substr(0,equals);
2017-08-09 12:32:09 +02:00
let value = equals === -1 ? '' : one.substr(equals+1);
2014-10-29 08:02:03 +01:00
2017-08-09 12:32:09 +02:00
if(value === 'true' || value === '') value = true;
else if(value === 'false') value = false;
else if(!isNaN(parseInt(value))) value = parseInt(value);
2014-10-29 08:02:03 +01:00
2017-08-09 12:32:09 +02:00
out[key] = value;
}
return out;
2014-10-29 08:02:03 +01:00
}
function readGames() {
const lines = fs.readFileSync(gamesFile,'utf8').split('\n');
const games = {};
2014-10-29 08:02:03 +01:00
2017-08-09 12:32:09 +02:00
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('|');
games[split[0].trim()] = {
pretty: split[1].trim(),
protocol: split[2].trim(),
options: parseList(split[3]),
params: parseList(split[4])
};
}
return games;
2014-10-29 08:02:03 +01:00
}
const games = readGames();
2014-10-29 08:02:03 +01:00
function createProtocolInstance(type) {
2017-08-09 12:32:09 +02:00
type = Path.basename(type);
2014-10-29 08:02:03 +01:00
2017-08-09 12:32:09 +02:00
const path = protocolDir+'/'+type;
if(!fs.existsSync(path+'.js')) throw Error('Protocol definition file missing: '+type);
const protocol = require(path);
2014-10-29 08:02:03 +01:00
2017-08-09 12:32:09 +02:00
return new protocol();
2014-10-29 08:02:03 +01:00
}
class TypeResolver {
2017-08-09 12:32:09 +02:00
static lookup(type) {
if(!type) throw Error('No game specified');
2014-10-29 08:02:03 +01:00
2017-08-09 12:32:09 +02:00
if(type.substr(0,9) === 'protocol-') {
return createProtocolInstance(type.substr(9));
}
2014-10-29 08:02:03 +01:00
const game = games[type];
2017-08-09 12:32:09 +02:00
if(!game) throw Error('Invalid game: '+type);
2014-10-29 08:02:03 +01:00
const query = createProtocolInstance(game.protocol);
2017-08-09 12:32:09 +02:00
query.pretty = game.pretty;
for(const key of Object.keys(game.options)) {
query.options[key] = game.options[key];
}
2017-08-09 12:32:09 +02:00
for(const key of Object.keys(game.params)) {
query[key] = game.params[key];
}
2014-10-29 08:02:03 +01:00
2017-08-09 12:32:09 +02:00
return query;
}
static printReadme() {
let out = '';
for(const key of Object.keys(games)) {
const game = games[key];
if (!game.pretty) {
continue;
}
2017-08-09 12:32:09 +02:00
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;