mirror of
https://github.com/tribufu/node-gamedig
synced 2026-06-01 09:42:41 +00:00
crlf -> ln conversion
This commit is contained in:
parent
50d3ef20a0
commit
f1467c5a04
94 changed files with 11388 additions and 11388 deletions
148
lib/Class.js
148
lib/Class.js
|
|
@ -1,74 +1,74 @@
|
|||
/* based on Simple JavaScript Inheritance
|
||||
* By John Resig http://ejohn.org/
|
||||
* MIT Licensed.
|
||||
*/
|
||||
|
||||
var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
|
||||
|
||||
// The base Class implementation (does nothing)
|
||||
var Class = function(){};
|
||||
|
||||
// Create a new Class that inherits from this class
|
||||
Class.extend = function() {
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
var name = 'Class';
|
||||
var parent = this;
|
||||
var prop = {};
|
||||
if(typeof args[0] == 'string') name = args.shift();
|
||||
if(args.length >= 2) parent = args.shift();
|
||||
prop = args.shift();
|
||||
|
||||
// Copy prototype from the parent object
|
||||
var prototype = {};
|
||||
for(var name in parent.prototype) {
|
||||
prototype[name] = parent.prototype[name];
|
||||
}
|
||||
|
||||
// Copy the properties over onto the new prototype
|
||||
for(var name in prop) {
|
||||
if(typeof prop[name] == "function" && fnTest.test(prop[name])) {
|
||||
// this is a function that references _super, so we have to wrap it
|
||||
// and provide it with its super function
|
||||
prototype[name] = (function(name, fn){
|
||||
return function() {
|
||||
var tmp = this._super;
|
||||
|
||||
// Add a new ._super() method that is the same method
|
||||
// but on the super-class
|
||||
if(typeof parent.prototype[name] == 'undefined') {
|
||||
if(name == 'init') this._super = parent.prototype.constructor;
|
||||
else this._super = function() { throw new Error('Called _super in method without a parent'); }
|
||||
} else this._super = parent.prototype[name];
|
||||
|
||||
// The method only need to be bound temporarily, so we
|
||||
// remove it when we're done executing
|
||||
var ret = fn.apply(this, arguments);
|
||||
this._super = tmp;
|
||||
|
||||
return ret;
|
||||
};
|
||||
})(name, prop[name]);
|
||||
} else {
|
||||
prototype[name] = prop[name];
|
||||
}
|
||||
}
|
||||
|
||||
// The dummy class constructor
|
||||
function Class() {
|
||||
// All construction is actually done in the init method
|
||||
if(this.init) this.init.apply(this, arguments);
|
||||
}
|
||||
|
||||
// Populate our constructed prototype object
|
||||
Class.prototype = prototype;
|
||||
|
||||
// Enforce the constructor to be what we expect
|
||||
Class.prototype.constructor = Class;
|
||||
|
||||
// And make this class extendable
|
||||
Class.extend = arguments.callee;
|
||||
|
||||
return Class;
|
||||
};
|
||||
|
||||
module.exports = Class;
|
||||
/* based on Simple JavaScript Inheritance
|
||||
* By John Resig http://ejohn.org/
|
||||
* MIT Licensed.
|
||||
*/
|
||||
|
||||
var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
|
||||
|
||||
// The base Class implementation (does nothing)
|
||||
var Class = function(){};
|
||||
|
||||
// Create a new Class that inherits from this class
|
||||
Class.extend = function() {
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
var name = 'Class';
|
||||
var parent = this;
|
||||
var prop = {};
|
||||
if(typeof args[0] == 'string') name = args.shift();
|
||||
if(args.length >= 2) parent = args.shift();
|
||||
prop = args.shift();
|
||||
|
||||
// Copy prototype from the parent object
|
||||
var prototype = {};
|
||||
for(var name in parent.prototype) {
|
||||
prototype[name] = parent.prototype[name];
|
||||
}
|
||||
|
||||
// Copy the properties over onto the new prototype
|
||||
for(var name in prop) {
|
||||
if(typeof prop[name] == "function" && fnTest.test(prop[name])) {
|
||||
// this is a function that references _super, so we have to wrap it
|
||||
// and provide it with its super function
|
||||
prototype[name] = (function(name, fn){
|
||||
return function() {
|
||||
var tmp = this._super;
|
||||
|
||||
// Add a new ._super() method that is the same method
|
||||
// but on the super-class
|
||||
if(typeof parent.prototype[name] == 'undefined') {
|
||||
if(name == 'init') this._super = parent.prototype.constructor;
|
||||
else this._super = function() { throw new Error('Called _super in method without a parent'); }
|
||||
} else this._super = parent.prototype[name];
|
||||
|
||||
// The method only need to be bound temporarily, so we
|
||||
// remove it when we're done executing
|
||||
var ret = fn.apply(this, arguments);
|
||||
this._super = tmp;
|
||||
|
||||
return ret;
|
||||
};
|
||||
})(name, prop[name]);
|
||||
} else {
|
||||
prototype[name] = prop[name];
|
||||
}
|
||||
}
|
||||
|
||||
// The dummy class constructor
|
||||
function Class() {
|
||||
// All construction is actually done in the init method
|
||||
if(this.init) this.init.apply(this, arguments);
|
||||
}
|
||||
|
||||
// Populate our constructed prototype object
|
||||
Class.prototype = prototype;
|
||||
|
||||
// Enforce the constructor to be what we expect
|
||||
Class.prototype.constructor = Class;
|
||||
|
||||
// And make this class extendable
|
||||
Class.extend = arguments.callee;
|
||||
|
||||
return Class;
|
||||
};
|
||||
|
||||
module.exports = Class;
|
||||
|
|
|
|||
160
lib/index.js
160
lib/index.js
|
|
@ -1,80 +1,80 @@
|
|||
var dgram = require('dgram'),
|
||||
EventEmitter = require('events').EventEmitter,
|
||||
util = require('util'),
|
||||
dns = require('dns'),
|
||||
TypeResolver = require('./typeresolver');
|
||||
|
||||
var activeQueries = [];
|
||||
|
||||
var udpSocket = dgram.createSocket('udp4');
|
||||
udpSocket.unref();
|
||||
udpSocket.bind(21943);
|
||||
udpSocket.on('message', function(buffer, rinfo) {
|
||||
if(Gamedig.debug) console.log(rinfo.address+':'+rinfo.port+" <--UDP "+buffer.toString('hex'));
|
||||
for(var i = 0; i < activeQueries.length; i++) {
|
||||
var query = activeQueries[i];
|
||||
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', function(e) {
|
||||
if(Gamedig.debug) console.log("UDP ERROR: "+e);
|
||||
});
|
||||
|
||||
Gamedig = {
|
||||
|
||||
query: function(options,callback) {
|
||||
if(callback) options.callback = callback;
|
||||
|
||||
var query;
|
||||
try {
|
||||
query = TypeResolver.lookup(options.type);
|
||||
} catch(e) {
|
||||
process.nextTick(function() {
|
||||
callback({error:e.message});
|
||||
});
|
||||
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(var i in options) query.options[i] = options[i];
|
||||
|
||||
activeQueries.push(query);
|
||||
|
||||
query.on('finished',function(state) {
|
||||
var i = activeQueries.indexOf(query);
|
||||
if(i >= 0) activeQueries.splice(i, 1);
|
||||
});
|
||||
|
||||
process.nextTick(function() {
|
||||
query.start();
|
||||
});
|
||||
|
||||
return query;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
module.exports = Gamedig;
|
||||
var dgram = require('dgram'),
|
||||
EventEmitter = require('events').EventEmitter,
|
||||
util = require('util'),
|
||||
dns = require('dns'),
|
||||
TypeResolver = require('./typeresolver');
|
||||
|
||||
var activeQueries = [];
|
||||
|
||||
var udpSocket = dgram.createSocket('udp4');
|
||||
udpSocket.unref();
|
||||
udpSocket.bind(21943);
|
||||
udpSocket.on('message', function(buffer, rinfo) {
|
||||
if(Gamedig.debug) console.log(rinfo.address+':'+rinfo.port+" <--UDP "+buffer.toString('hex'));
|
||||
for(var i = 0; i < activeQueries.length; i++) {
|
||||
var query = activeQueries[i];
|
||||
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', function(e) {
|
||||
if(Gamedig.debug) console.log("UDP ERROR: "+e);
|
||||
});
|
||||
|
||||
Gamedig = {
|
||||
|
||||
query: function(options,callback) {
|
||||
if(callback) options.callback = callback;
|
||||
|
||||
var query;
|
||||
try {
|
||||
query = TypeResolver.lookup(options.type);
|
||||
} catch(e) {
|
||||
process.nextTick(function() {
|
||||
callback({error:e.message});
|
||||
});
|
||||
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(var i in options) query.options[i] = options[i];
|
||||
|
||||
activeQueries.push(query);
|
||||
|
||||
query.on('finished',function(state) {
|
||||
var i = activeQueries.indexOf(query);
|
||||
if(i >= 0) activeQueries.splice(i, 1);
|
||||
});
|
||||
|
||||
process.nextTick(function() {
|
||||
query.start();
|
||||
});
|
||||
|
||||
return query;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
module.exports = Gamedig;
|
||||
|
|
|
|||
248
lib/reader.js
248
lib/reader.js
|
|
@ -1,124 +1,124 @@
|
|||
var Iconv = require('iconv-lite'),
|
||||
Long = require('long');
|
||||
|
||||
function readUInt64BE(buffer,offset) {
|
||||
var high = buffer.readUInt32BE(offset);
|
||||
var low = buffer.readUInt32BE(offset+4);
|
||||
return new Long(low,high,true);
|
||||
}
|
||||
function readUInt64LE(buffer,offset) {
|
||||
var low = buffer.readUInt32LE(offset);
|
||||
var high = buffer.readUInt32LE(offset+4);
|
||||
return new Long(low,high,true);
|
||||
}
|
||||
|
||||
function Reader(query,buffer) {
|
||||
this.query = query;
|
||||
this.buffer = buffer;
|
||||
this.i = 0;
|
||||
}
|
||||
|
||||
Reader.prototype = {
|
||||
offset: function() { return this.i; },
|
||||
skip: function(i) { this.i += i; },
|
||||
string: function() {
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
var 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];
|
||||
}
|
||||
|
||||
options.encoding = options.encoding || this.query.encoding;
|
||||
if(options.encoding == 'latin1') options.encoding = 'win1252';
|
||||
|
||||
var start = this.i+0;
|
||||
var end = start;
|
||||
if(!('length' in options)) {
|
||||
// terminated by the delimiter
|
||||
var 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;
|
||||
}
|
||||
|
||||
var out = this.buffer.slice(start, end);
|
||||
var enc = options.encoding;
|
||||
if(enc == 'utf8' || enc == 'ucs2' || enc == 'binary') {
|
||||
out = out.toString(enc);
|
||||
} else {
|
||||
out = Iconv.decode(out,enc);
|
||||
}
|
||||
return out;
|
||||
},
|
||||
int: function(bytes) {
|
||||
var 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;
|
||||
},
|
||||
uint: function(bytes) {
|
||||
var 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: function() {
|
||||
var 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;
|
||||
},
|
||||
remaining: function() {
|
||||
return this.buffer.length-this.i;
|
||||
},
|
||||
rest: function() {
|
||||
return this.buffer.slice(this.i);
|
||||
},
|
||||
done: function() {
|
||||
return this.i >= this.buffer.length;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = Reader;
|
||||
var Iconv = require('iconv-lite'),
|
||||
Long = require('long');
|
||||
|
||||
function readUInt64BE(buffer,offset) {
|
||||
var high = buffer.readUInt32BE(offset);
|
||||
var low = buffer.readUInt32BE(offset+4);
|
||||
return new Long(low,high,true);
|
||||
}
|
||||
function readUInt64LE(buffer,offset) {
|
||||
var low = buffer.readUInt32LE(offset);
|
||||
var high = buffer.readUInt32LE(offset+4);
|
||||
return new Long(low,high,true);
|
||||
}
|
||||
|
||||
function Reader(query,buffer) {
|
||||
this.query = query;
|
||||
this.buffer = buffer;
|
||||
this.i = 0;
|
||||
}
|
||||
|
||||
Reader.prototype = {
|
||||
offset: function() { return this.i; },
|
||||
skip: function(i) { this.i += i; },
|
||||
string: function() {
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
var 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];
|
||||
}
|
||||
|
||||
options.encoding = options.encoding || this.query.encoding;
|
||||
if(options.encoding == 'latin1') options.encoding = 'win1252';
|
||||
|
||||
var start = this.i+0;
|
||||
var end = start;
|
||||
if(!('length' in options)) {
|
||||
// terminated by the delimiter
|
||||
var 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;
|
||||
}
|
||||
|
||||
var out = this.buffer.slice(start, end);
|
||||
var enc = options.encoding;
|
||||
if(enc == 'utf8' || enc == 'ucs2' || enc == 'binary') {
|
||||
out = out.toString(enc);
|
||||
} else {
|
||||
out = Iconv.decode(out,enc);
|
||||
}
|
||||
return out;
|
||||
},
|
||||
int: function(bytes) {
|
||||
var 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;
|
||||
},
|
||||
uint: function(bytes) {
|
||||
var 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: function() {
|
||||
var 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;
|
||||
},
|
||||
remaining: function() {
|
||||
return this.buffer.length-this.i;
|
||||
},
|
||||
rest: function() {
|
||||
return this.buffer.slice(this.i);
|
||||
},
|
||||
done: function() {
|
||||
return this.i >= this.buffer.length;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = Reader;
|
||||
|
|
|
|||
|
|
@ -1,91 +1,91 @@
|
|||
var Path = require('path'),
|
||||
fs = require('fs');
|
||||
|
||||
var protocolDir = Path.normalize(__dirname+'/../protocols');
|
||||
var gamesFile = Path.normalize(__dirname+'/../games.txt');
|
||||
|
||||
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;
|
||||
|
||||
var 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;
|
||||
}
|
||||
var games = readGames();
|
||||
|
||||
function createProtocolInstance(type) {
|
||||
type = Path.basename(type);
|
||||
|
||||
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 = {
|
||||
lookup: function(type) {
|
||||
if(!type) throw Error('No game specified');
|
||||
|
||||
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];
|
||||
|
||||
return query;
|
||||
},
|
||||
printReadme: function() {
|
||||
var out = '';
|
||||
for(var key in games) {
|
||||
var 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;
|
||||
}
|
||||
};
|
||||
var Path = require('path'),
|
||||
fs = require('fs');
|
||||
|
||||
var protocolDir = Path.normalize(__dirname+'/../protocols');
|
||||
var gamesFile = Path.normalize(__dirname+'/../games.txt');
|
||||
|
||||
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;
|
||||
|
||||
var 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;
|
||||
}
|
||||
var games = readGames();
|
||||
|
||||
function createProtocolInstance(type) {
|
||||
type = Path.basename(type);
|
||||
|
||||
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 = {
|
||||
lookup: function(type) {
|
||||
if(!type) throw Error('No game specified');
|
||||
|
||||
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];
|
||||
|
||||
return query;
|
||||
},
|
||||
printReadme: function() {
|
||||
var out = '';
|
||||
for(var key in games) {
|
||||
var 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;
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue