mirror of
https://github.com/tribufu/node-gamedig
synced 2026-05-18 09:35:50 +00:00
Super epic commit 3
Move everything around Add another 50 or so games *** 'port' option should now be CONNECT port, not query port *** add reference for many missing games
This commit is contained in:
parent
8488cdcca9
commit
bc6b5c9225
64 changed files with 2071 additions and 430 deletions
|
|
@ -1,51 +1,85 @@
|
|||
var Path = require('path'),
|
||||
fs = require('fs');
|
||||
|
||||
var gamesDir = Path.normalize(__dirname+'/../games');
|
||||
var protocolDir = Path.normalize(__dirname+'/../protocols');
|
||||
var gamesFile = Path.normalize(__dirname+'/../games.txt');
|
||||
|
||||
function readAliases() {
|
||||
var lines = fs.readFileSync(gamesDir+'/aliases.txt','utf8').split('\n');
|
||||
var aliases = {};
|
||||
function parseList(str) {
|
||||
if(!str) return {};
|
||||
var split = str.split(',');
|
||||
var out = {};
|
||||
split.forEach(function(one) {
|
||||
var equals = one.indexOf('=');
|
||||
var key = equals == -1 ? one : one.substr(0,equals);
|
||||
var 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);
|
||||
|
||||
out[key] = value;
|
||||
});
|
||||
return out;
|
||||
}
|
||||
function readGames() {
|
||||
var lines = fs.readFileSync(gamesFile,'utf8').split('\n');
|
||||
var games = {};
|
||||
|
||||
lines.forEach(function(line) {
|
||||
// strip comments
|
||||
var comment = line.indexOf('#');
|
||||
if(comment != -1) line = line.substr(0,comment);
|
||||
line = line.trim();
|
||||
if(!line) return;
|
||||
if(line.charAt(0) == '#') return;
|
||||
|
||||
var split = line.split('|');
|
||||
|
||||
aliases[split[0].trim()] = {
|
||||
games[split[0].trim()] = {
|
||||
pretty: split[1].trim(),
|
||||
protocol: split[2].trim(),
|
||||
port: split[3] ? parseInt(split[3]) : 0
|
||||
options: parseList(split[3]),
|
||||
params: parseList(split[4])
|
||||
};
|
||||
});
|
||||
return aliases;
|
||||
return games;
|
||||
}
|
||||
var aliases = readAliases();
|
||||
var games = readGames();
|
||||
|
||||
function createQueryInstance(type) {
|
||||
function createProtocolInstance(type) {
|
||||
type = Path.basename(type);
|
||||
|
||||
var path = gamesDir+'/'+type;
|
||||
if(type.substr(0,9) == 'protocol-') {
|
||||
path = gamesDir+'/protocols/'+type.substr(9);
|
||||
}
|
||||
|
||||
if(!fs.existsSync(path+'.js')) return false;
|
||||
var path = protocolDir+'/'+type;
|
||||
if(!fs.existsSync(path+'.js')) throw Error('Protocol definition file missing: '+type);
|
||||
var protocol = require(path);
|
||||
|
||||
return new protocol();
|
||||
}
|
||||
|
||||
module.exports = function(type) {
|
||||
var alias = aliases[type];
|
||||
module.exports = {
|
||||
lookup: function(type) {
|
||||
if(type.substr(0,9) == 'protocol-') {
|
||||
return createProtocolInstance(type.substr(9));
|
||||
}
|
||||
|
||||
var game = games[type];
|
||||
if(!game) throw Error('Invalid game: '+type);
|
||||
|
||||
var query = createProtocolInstance(game.protocol);
|
||||
query.pretty = game.pretty;
|
||||
for(var key in game.options)
|
||||
query.options[key] = game.options[key];
|
||||
for(var key in game.params)
|
||||
query[key] = game.params[key];
|
||||
|
||||
if(alias) {
|
||||
var query = createQueryInstance('protocol-'+alias.protocol);
|
||||
if(!query) return false;
|
||||
query.pretty = alias.pretty;
|
||||
if(alias.port) query.options.port = alias.port;
|
||||
return query;
|
||||
},
|
||||
printReadme: function() {
|
||||
for(var key in games) {
|
||||
var game = games[key];
|
||||
var out = "* "+game.pretty+" ("+key+")";
|
||||
if(game.options.port_query_offset || game.options.port_query)
|
||||
out += " [[Separate Query Port](#separate-query-port)]";
|
||||
console.log(out);
|
||||
}
|
||||
}
|
||||
return createQueryInstance(type);
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue