2019-01-12 11:43:36 +01:00
|
|
|
const Path = require('path'),
|
|
|
|
fs = require('fs');
|
|
|
|
|
|
|
|
class GameResolver {
|
|
|
|
constructor() {
|
2019-02-07 07:37:33 +01:00
|
|
|
const loaded = this._readGames();
|
|
|
|
this.gamesByKey = loaded.gamesByKey;
|
|
|
|
this.games = loaded.games;
|
2019-01-12 11:43:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lookup(type) {
|
|
|
|
if(!type) throw Error('No game specified');
|
|
|
|
|
|
|
|
if(type.substr(0,9) === 'protocol-') {
|
|
|
|
return {
|
|
|
|
protocol: type.substr(9)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-07 07:37:33 +01:00
|
|
|
const game = this.gamesByKey.get(type);
|
2019-01-12 11:43:36 +01:00
|
|
|
if(!game) throw Error('Invalid game: '+type);
|
|
|
|
return game.options;
|
|
|
|
}
|
|
|
|
|
|
|
|
printReadme() {
|
|
|
|
let out = '';
|
2021-02-25 09:15:53 +01:00
|
|
|
out += '| GameDig Type ID | Name | See Also\n';
|
2019-02-06 13:23:34 +01:00
|
|
|
out += '|---|---|---\n';
|
2019-02-07 07:37:33 +01:00
|
|
|
|
|
|
|
const sorted = this.games
|
|
|
|
.filter(game => game.pretty)
|
|
|
|
.sort((a,b) => {
|
|
|
|
return a.pretty.localeCompare(b.pretty);
|
|
|
|
});
|
|
|
|
for(const game of sorted) {
|
|
|
|
let keysOut = game.keys.map(key => '`'+key+'`').join('<br>');
|
|
|
|
out += "| " + keysOut.padEnd(10, " ") + " "
|
2019-02-06 13:23:34 +01:00
|
|
|
+ "| " + game.pretty;
|
2021-02-25 09:15:53 +01:00
|
|
|
let notes = [];
|
|
|
|
if(game.extra.doc_notes) {
|
|
|
|
notes.push("[Notes](#" + game.extra.doc_notes + ")");
|
|
|
|
}
|
|
|
|
if(game.options.protocol === 'valve') {
|
|
|
|
notes.push('[Valve Protocol](#valve)');
|
|
|
|
}
|
|
|
|
if(notes.length) {
|
|
|
|
out += " | " + notes.join(', ');
|
|
|
|
}
|
2019-01-12 11:43:36 +01:00
|
|
|
out += "\n";
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
_readGames() {
|
|
|
|
const gamesFile = Path.normalize(__dirname+'/../games.txt');
|
|
|
|
const lines = fs.readFileSync(gamesFile,'utf8').split('\n');
|
2019-02-07 07:37:33 +01:00
|
|
|
|
|
|
|
const gamesByKey = new Map();
|
|
|
|
const games = [];
|
2019-01-12 11:43:36 +01: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('|');
|
2019-02-07 07:37:33 +01:00
|
|
|
const keys = split[0].trim().split(',');
|
|
|
|
const name = split[1].trim();
|
2019-01-12 11:43:36 +01:00
|
|
|
const options = this._parseList(split[3]);
|
|
|
|
options.protocol = split[2].trim();
|
2019-02-07 07:37:33 +01:00
|
|
|
const extra = this._parseList(split[4]);
|
2019-01-12 11:43:36 +01:00
|
|
|
|
2019-02-07 07:37:33 +01:00
|
|
|
const game = {
|
|
|
|
keys: keys,
|
|
|
|
pretty: name,
|
2019-01-12 11:43:36 +01:00
|
|
|
options: options,
|
2019-02-07 07:37:33 +01:00
|
|
|
extra: extra
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const key of keys) {
|
|
|
|
gamesByKey.set(key, game);
|
|
|
|
}
|
|
|
|
games.push(game);
|
2019-01-12 11:43:36 +01:00
|
|
|
}
|
2019-02-07 07:37:33 +01:00
|
|
|
return { gamesByKey, games };
|
2019-01-12 11:43:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_parseList(str) {
|
|
|
|
if(!str) return {};
|
|
|
|
const out = {};
|
|
|
|
for (const one of str.split(',')) {
|
|
|
|
const equals = one.indexOf('=');
|
|
|
|
const key = equals === -1 ? one : one.substr(0,equals);
|
2019-02-20 04:15:34 +01:00
|
|
|
|
|
|
|
/** @type {string|number|boolean} */
|
2019-01-12 11:43:36 +01:00
|
|
|
let value = equals === -1 ? '' : one.substr(equals+1);
|
|
|
|
|
|
|
|
if(value === 'true' || value === '') value = true;
|
|
|
|
else if(value === 'false') value = false;
|
|
|
|
else if(!isNaN(parseInt(value))) value = parseInt(value);
|
|
|
|
|
|
|
|
out[key] = value;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = GameResolver;
|