2014-10-17 02:21:06 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2016-08-04 01:38:06 +00:00
|
|
|
// ENiGMA½
|
2016-09-01 04:25:41 +00:00
|
|
|
const Config = require('./config.js').config;
|
|
|
|
const miscUtil = require('./misc_util.js');
|
|
|
|
const ansi = require('./ansi_term.js');
|
|
|
|
const aep = require('./ansi_escape_parser.js');
|
|
|
|
const sauce = require('./sauce.js');
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2016-08-04 01:38:06 +00:00
|
|
|
// deps
|
2016-09-01 04:25:41 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
const paths = require('path');
|
|
|
|
const assert = require('assert');
|
|
|
|
const iconv = require('iconv-lite');
|
|
|
|
const _ = require('lodash');
|
2016-10-06 05:22:59 +00:00
|
|
|
const farmhash = require('farmhash');
|
2015-04-19 08:13:13 +00:00
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
exports.getArt = getArt;
|
|
|
|
exports.getArtFromPath = getArtFromPath;
|
|
|
|
exports.display = display;
|
|
|
|
exports.defaultEncodingFromExtension = defaultEncodingFromExtension;
|
|
|
|
|
|
|
|
// :TODO: Return MCI code information
|
|
|
|
// :TODO: process SAUCE comments
|
|
|
|
// :TODO: return font + font mapped information from SAUCE
|
|
|
|
|
2016-09-01 04:25:41 +00:00
|
|
|
const SUPPORTED_ART_TYPES = {
|
2014-10-17 02:21:06 +00:00
|
|
|
// :TODO: the defualt encoding are really useless if they are all the same ...
|
|
|
|
// perhaps .ansamiga and .ascamiga could be supported as well as overrides via conf
|
|
|
|
'.ans' : { name : 'ANSI', defaultEncoding : 'cp437', eof : 0x1a },
|
|
|
|
'.asc' : { name : 'ASCII', defaultEncoding : 'cp437', eof : 0x1a },
|
|
|
|
'.pcb' : { name : 'PCBoard', defaultEncoding : 'cp437', eof : 0x1a },
|
|
|
|
'.bbs' : { name : 'Wildcat', defaultEncoding : 'cp437', eof : 0x1a },
|
2015-11-01 20:32:52 +00:00
|
|
|
|
|
|
|
'.amiga' : { name : 'Amiga', defaultEncoding : 'amiga', eof : 0x1a },
|
|
|
|
'.txt' : { name : 'Amiga Text', defaultEncoding : 'cp437', eof : 0x1a },
|
2014-10-17 02:21:06 +00:00
|
|
|
// :TODO: extentions for wwiv, renegade, celerity, syncronet, ...
|
|
|
|
// :TODO: extension for atari
|
|
|
|
// :TODO: extension for topaz ansi/ascii.
|
|
|
|
};
|
|
|
|
|
2015-04-17 04:29:53 +00:00
|
|
|
function getFontNameFromSAUCE(sauce) {
|
|
|
|
if(sauce.Character) {
|
|
|
|
return sauce.Character.fontName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
function sliceAtEOF(data, eofMarker) {
|
2016-09-02 05:37:57 +00:00
|
|
|
let eof = data.length;
|
|
|
|
const stopPos = Math.max(data.length - (256), 0); // 256 = 2 * sizeof(SAUCE)
|
2016-09-02 05:57:57 +00:00
|
|
|
|
2016-09-02 05:37:57 +00:00
|
|
|
for(let i = eof - 1; i > stopPos; i--) {
|
|
|
|
if(eofMarker === data[i]) {
|
2014-10-17 02:21:06 +00:00
|
|
|
eof = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return data.slice(0, eof);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getArtFromPath(path, options, cb) {
|
2016-09-01 04:25:41 +00:00
|
|
|
fs.readFile(path, (err, data) => {
|
2014-10-17 02:21:06 +00:00
|
|
|
if(err) {
|
2016-09-01 04:25:41 +00:00
|
|
|
return cb(err);
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Convert from encodedAs -> j
|
|
|
|
//
|
2016-09-01 04:25:41 +00:00
|
|
|
const ext = paths.extname(path).toLowerCase();
|
|
|
|
const encoding = options.encodedAs || defaultEncodingFromExtension(ext);
|
2015-04-17 04:29:53 +00:00
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
// :TODO: how are BOM's currently handled if present? Are they removed? Do we need to?
|
|
|
|
|
|
|
|
function sliceOfData() {
|
|
|
|
if(options.fullFile === true) {
|
|
|
|
return iconv.decode(data, encoding);
|
|
|
|
} else {
|
2016-09-01 04:25:41 +00:00
|
|
|
const eofMarker = defaultEofFromExtension(ext);
|
2014-10-17 02:21:06 +00:00
|
|
|
return iconv.decode(sliceAtEOF(data, eofMarker), encoding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getResult(sauce) {
|
2016-09-01 04:25:41 +00:00
|
|
|
const result = {
|
2014-10-17 02:21:06 +00:00
|
|
|
data : sliceOfData(),
|
|
|
|
fromPath : path,
|
|
|
|
};
|
|
|
|
|
|
|
|
if(sauce) {
|
|
|
|
result.sauce = sauce;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2015-07-29 04:31:28 +00:00
|
|
|
}
|
2014-10-17 02:21:06 +00:00
|
|
|
|
|
|
|
if(options.readSauce === true) {
|
2016-09-01 04:25:41 +00:00
|
|
|
sauce.readSAUCE(data, (err, sauce) => {
|
2014-10-17 02:21:06 +00:00
|
|
|
if(err) {
|
2016-09-01 04:25:41 +00:00
|
|
|
return cb(null, getResult());
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// If a encoding was not provided & we have a mapping from
|
|
|
|
// the information provided by SAUCE, use that.
|
|
|
|
//
|
|
|
|
if(!options.encodedAs) {
|
|
|
|
/*
|
|
|
|
if(sauce.Character && sauce.Character.fontName) {
|
|
|
|
var enc = SAUCE_FONT_TO_ENCODING_HINT[sauce.Character.fontName];
|
|
|
|
if(enc) {
|
|
|
|
encoding = enc;
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-01 04:25:41 +00:00
|
|
|
*/
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|
2016-09-01 04:25:41 +00:00
|
|
|
return cb(null, getResult(sauce));
|
2014-10-17 02:21:06 +00:00
|
|
|
});
|
|
|
|
} else {
|
2016-09-01 04:25:41 +00:00
|
|
|
return cb(null, getResult());
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getArt(name, options, cb) {
|
2016-09-01 04:25:41 +00:00
|
|
|
const ext = paths.extname(name);
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2016-09-01 04:25:41 +00:00
|
|
|
options.basePath = miscUtil.valueWithDefault(options.basePath, Config.paths.art);
|
2014-10-17 02:21:06 +00:00
|
|
|
options.asAnsi = miscUtil.valueWithDefault(options.asAnsi, true);
|
|
|
|
|
|
|
|
// :TODO: make use of asAnsi option and convert from supported -> ansi
|
|
|
|
|
|
|
|
if('' !== ext) {
|
|
|
|
options.types = [ ext.toLowerCase() ];
|
|
|
|
} else {
|
2015-05-14 22:49:19 +00:00
|
|
|
if(_.isUndefined(options.types)) {
|
2014-10-17 02:21:06 +00:00
|
|
|
options.types = Object.keys(SUPPORTED_ART_TYPES);
|
2015-05-14 22:49:19 +00:00
|
|
|
} else if(_.isString(options.types)) {
|
2014-10-17 02:21:06 +00:00
|
|
|
options.types = [ options.types.toLowerCase() ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If an extension is provided, just read the file now
|
|
|
|
if('' !== ext) {
|
2016-09-01 04:25:41 +00:00
|
|
|
const directPath = paths.join(options.basePath, name);
|
|
|
|
return getArtFromPath(directPath, options, cb);
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 04:25:41 +00:00
|
|
|
fs.readdir(options.basePath, (err, files) => {
|
2014-10-17 02:21:06 +00:00
|
|
|
if(err) {
|
2016-09-01 04:25:41 +00:00
|
|
|
return cb(err);
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 04:25:41 +00:00
|
|
|
const filtered = files.filter( file => {
|
2014-10-17 02:21:06 +00:00
|
|
|
//
|
|
|
|
// Ignore anything not allowed in |options.types|
|
|
|
|
//
|
2016-09-01 04:25:41 +00:00
|
|
|
const fext = paths.extname(file);
|
2014-10-17 02:21:06 +00:00
|
|
|
if(options.types.indexOf(fext.toLowerCase()) < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-01 04:25:41 +00:00
|
|
|
const bn = paths.basename(file, fext).toLowerCase();
|
2014-10-17 02:21:06 +00:00
|
|
|
if(options.random) {
|
2016-09-01 04:25:41 +00:00
|
|
|
const suppliedBn = paths.basename(name, fext).toLowerCase();
|
2014-10-17 02:21:06 +00:00
|
|
|
//
|
|
|
|
// Random selection enabled. We'll allow for
|
|
|
|
// basename1.ext, basename2.ext, ...
|
|
|
|
//
|
|
|
|
if(bn.indexOf(suppliedBn) !== 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-01 04:25:41 +00:00
|
|
|
|
|
|
|
const num = bn.substr(suppliedBn.length);
|
2014-10-17 02:21:06 +00:00
|
|
|
if(num.length > 0) {
|
|
|
|
if(isNaN(parseInt(num, 10))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// We've already validated the extension (above). Must be an exact
|
|
|
|
// match to basename here
|
|
|
|
//
|
|
|
|
if(bn != paths.basename(name, fext).toLowerCase()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-09-01 04:25:41 +00:00
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
return true;
|
|
|
|
});
|
2016-08-04 01:38:06 +00:00
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
if(filtered.length > 0) {
|
|
|
|
//
|
|
|
|
// We should now have:
|
|
|
|
// - Exactly (1) item in |filtered| if non-random
|
|
|
|
// - 1:n items in |filtered| to choose from if random
|
|
|
|
//
|
2016-09-01 04:25:41 +00:00
|
|
|
let readPath;
|
2014-10-17 02:21:06 +00:00
|
|
|
if(options.random) {
|
|
|
|
readPath = paths.join(options.basePath, filtered[Math.floor(Math.random() * filtered.length)]);
|
|
|
|
} else {
|
|
|
|
assert(1 === filtered.length);
|
|
|
|
readPath = paths.join(options.basePath, filtered[0]);
|
|
|
|
}
|
|
|
|
|
2016-09-01 04:25:41 +00:00
|
|
|
return getArtFromPath(readPath, options, cb);
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|
2016-09-01 04:25:41 +00:00
|
|
|
|
|
|
|
return cb(new Error(`No matching art for supplied criteria: ${name}`));
|
2014-10-17 02:21:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function defaultEncodingFromExtension(ext) {
|
|
|
|
return SUPPORTED_ART_TYPES[ext.toLowerCase()].defaultEncoding;
|
|
|
|
}
|
|
|
|
|
|
|
|
function defaultEofFromExtension(ext) {
|
|
|
|
return SUPPORTED_ART_TYPES[ext.toLowerCase()].eof;
|
|
|
|
}
|
|
|
|
|
2016-09-01 04:06:28 +00:00
|
|
|
// :TODO: Implement the following
|
|
|
|
// * Pause (disabled | termHeight | keyPress )
|
|
|
|
// * Cancel (disabled | <keys> )
|
|
|
|
// * Resume from pause -> continous (disabled | <keys>)
|
|
|
|
function display(client, art, options, cb) {
|
|
|
|
if(_.isFunction(options) && !cb) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!art || !art.length) {
|
|
|
|
return cb(new Error('Empty art'));
|
|
|
|
}
|
|
|
|
|
|
|
|
options.mciReplaceChar = options.mciReplaceChar || ' ';
|
|
|
|
options.disableMciCache = options.disableMciCache || false;
|
|
|
|
|
|
|
|
if(!_.isBoolean(options.iceColors)) {
|
|
|
|
// try to detect from SAUCE
|
|
|
|
if(_.has(options, 'sauce.ansiFlags') && (options.sauce.ansiFlags & (1 << 0))) {
|
|
|
|
options.iceColors = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const ansiParser = new aep.ANSIEscapeParser({
|
|
|
|
mciReplaceChar : options.mciReplaceChar,
|
|
|
|
termHeight : client.term.termHeight,
|
|
|
|
termWidth : client.term.termWidth,
|
|
|
|
trailingLF : options.trailingLF,
|
|
|
|
});
|
|
|
|
|
|
|
|
let parseComplete = false;
|
|
|
|
let cprListener;
|
|
|
|
let mciMap;
|
|
|
|
const mciCprQueue = [];
|
|
|
|
let artHash;
|
2016-09-02 05:57:57 +00:00
|
|
|
let mciMapFromCache;
|
2016-09-01 04:06:28 +00:00
|
|
|
|
|
|
|
function completed() {
|
|
|
|
if(cprListener) {
|
|
|
|
client.removeListener('cursor position report', cprListener);
|
|
|
|
}
|
|
|
|
|
2016-09-02 05:57:57 +00:00
|
|
|
if(!options.disableMciCache && !mciMapFromCache) {
|
2016-09-01 04:06:28 +00:00
|
|
|
// cache our MCI findings...
|
|
|
|
client.mciCache[artHash] = mciMap;
|
2016-12-31 21:50:29 +00:00
|
|
|
client.log.trace( { artHash : artHash.toString(16), mciMap : mciMap }, 'Added MCI map to cache');
|
2016-09-01 04:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ansiParser.removeAllListeners(); // :TODO: Necessary???
|
|
|
|
|
|
|
|
const extraInfo = {
|
|
|
|
height : ansiParser.row - 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
return cb(null, mciMap, extraInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(!options.disableMciCache) {
|
|
|
|
artHash = farmhash.hash32(art);
|
|
|
|
|
|
|
|
// see if we have a mciMap cached for this art
|
|
|
|
if(client.mciCache) {
|
|
|
|
mciMap = client.mciCache[artHash];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-01 04:25:41 +00:00
|
|
|
if(mciMap) {
|
2016-09-02 05:57:57 +00:00
|
|
|
mciMapFromCache = true;
|
2016-12-31 21:50:29 +00:00
|
|
|
client.log.trace( { artHash : artHash.toString(16), mciMap : mciMap }, 'Loaded MCI map from cache');
|
2016-09-01 04:25:41 +00:00
|
|
|
} else {
|
2016-09-01 04:06:28 +00:00
|
|
|
// no cached MCI info
|
|
|
|
mciMap = {};
|
|
|
|
|
|
|
|
cprListener = function(pos) {
|
|
|
|
if(mciCprQueue.length > 0) {
|
|
|
|
mciMap[mciCprQueue.shift()].position = pos;
|
|
|
|
|
|
|
|
if(parseComplete && 0 === mciCprQueue.length) {
|
|
|
|
return completed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
client.on('cursor position report', cprListener);
|
|
|
|
|
|
|
|
let generatedId = 100;
|
|
|
|
|
|
|
|
ansiParser.on('mci', mciInfo => {
|
|
|
|
// :TODO: ensure generatedId's do not conflict with any existing |id|
|
|
|
|
const id = _.isNumber(mciInfo.id) ? mciInfo.id : generatedId;
|
|
|
|
const mapKey = `${mciInfo.mci}${id}`;
|
|
|
|
const mapEntry = mciMap[mapKey];
|
|
|
|
|
|
|
|
if(mapEntry) {
|
|
|
|
mapEntry.focusSGR = mciInfo.SGR;
|
|
|
|
mapEntry.focusArgs = mciInfo.args;
|
|
|
|
} else {
|
|
|
|
mciMap[mapKey] = {
|
|
|
|
args : mciInfo.args,
|
|
|
|
SGR : mciInfo.SGR,
|
|
|
|
code : mciInfo.mci,
|
|
|
|
id : id,
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!mciInfo.id) {
|
|
|
|
++generatedId;
|
|
|
|
}
|
|
|
|
|
|
|
|
mciCprQueue.push(mapKey);
|
|
|
|
client.term.write(ansi.queryPos(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ansiParser.on('literal', literal => client.term.write(literal, false) );
|
|
|
|
ansiParser.on('control', control => client.term.write(control, false) );
|
|
|
|
|
|
|
|
ansiParser.on('complete', () => {
|
|
|
|
parseComplete = true;
|
|
|
|
|
|
|
|
if(0 === mciCprQueue.length) {
|
|
|
|
return completed();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let ansiFontSeq;
|
|
|
|
if(options.font) {
|
|
|
|
ansiFontSeq = ansi.setSyncTermFontWithAlias(options.font);
|
|
|
|
} else if(options.sauce) {
|
|
|
|
let fontName = getFontNameFromSAUCE(options.sauce);
|
|
|
|
if(fontName) {
|
|
|
|
fontName = ansi.getSyncTERMFontFromAlias(fontName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't set default (CP437) from SAUCE
|
|
|
|
if(fontName && 'cp437' !== fontName) {
|
|
|
|
ansiFontSeq = ansi.setSyncTERMFont(fontName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ansiFontSeq) {
|
|
|
|
client.term.write(ansiFontSeq, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(options.iceColors) {
|
|
|
|
client.term.write(ansi.blinkToBrightIntensity(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
ansiParser.reset(art);
|
|
|
|
ansiParser.parse();
|
|
|
|
}
|