2014-10-27 04:06:41 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2015-05-13 05:04:22 +00:00
|
|
|
var Config = require('./config.js').config;
|
|
|
|
var art = require('./art.js');
|
|
|
|
var miscUtil = require('./misc_util.js');
|
|
|
|
var Log = require('./logger.js').log;
|
|
|
|
|
|
|
|
var fs = require('fs');
|
|
|
|
var paths = require('path');
|
|
|
|
var async = require('async');
|
|
|
|
var _ = require('lodash');
|
|
|
|
var assert = require('assert');
|
|
|
|
var stripJsonComments = require('strip-json-comments');
|
|
|
|
|
|
|
|
exports.loadTheme = loadTheme;
|
2014-10-27 04:06:41 +00:00
|
|
|
exports.getThemeArt = getThemeArt;
|
2014-10-30 04:23:44 +00:00
|
|
|
exports.getRandomTheme = getRandomTheme;
|
2014-10-29 11:30:20 +00:00
|
|
|
exports.initAvailableThemes = initAvailableThemes;
|
2014-11-05 06:50:42 +00:00
|
|
|
exports.displayThemeArt = displayThemeArt;
|
2014-10-27 04:06:41 +00:00
|
|
|
|
2015-05-13 05:04:22 +00:00
|
|
|
function loadTheme(themeID, cb) {
|
|
|
|
var path = paths.join(Config.paths.themes, themeID, 'theme.json');
|
2014-10-27 04:06:41 +00:00
|
|
|
|
2015-05-13 05:04:22 +00:00
|
|
|
fs.readFile(path, { encoding : 'utf8' }, function onData(err, data) {
|
2014-10-27 04:06:41 +00:00
|
|
|
if(err) {
|
|
|
|
cb(err);
|
|
|
|
} else {
|
|
|
|
try {
|
2015-05-13 05:04:22 +00:00
|
|
|
var theme = JSON.parse(stripJsonComments(data));
|
|
|
|
|
|
|
|
if(!_.isObject(theme.info)) {
|
|
|
|
cb(new Error('Invalid theme JSON'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!_.isObject(theme.helpers)); // we create this on the fly!
|
2015-04-17 04:29:53 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Create some handy helpers
|
|
|
|
//
|
2015-05-13 05:04:22 +00:00
|
|
|
theme.helpers = {
|
|
|
|
getPasswordChar : function() {
|
|
|
|
var pwChar = Config.defaults.passwordChar;
|
2015-05-14 04:21:55 +00:00
|
|
|
if(_.has(theme, 'customization.defaults.general')) {
|
|
|
|
var themePasswordChar = theme.customization.defaults.general.passwordChar;
|
2015-05-13 05:04:22 +00:00
|
|
|
if(_.isString(themePasswordChar)) {
|
|
|
|
pwChar = themePasswordChar.substr(0, 1);
|
|
|
|
} else if(_.isNumber(themePasswordChar)) {
|
|
|
|
pwChar = String.fromCharCode(themePasswordChar);
|
|
|
|
}
|
2015-04-17 04:29:53 +00:00
|
|
|
}
|
2015-05-13 05:04:22 +00:00
|
|
|
return pwChar;
|
2015-04-17 04:29:53 +00:00
|
|
|
}
|
2015-05-13 05:04:22 +00:00
|
|
|
}
|
2015-04-17 04:29:53 +00:00
|
|
|
|
2015-05-13 05:04:22 +00:00
|
|
|
cb(null, theme);
|
2014-10-27 04:06:41 +00:00
|
|
|
} catch(e) {
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-29 11:30:20 +00:00
|
|
|
var availableThemes = {};
|
2014-10-30 04:23:44 +00:00
|
|
|
|
2014-10-29 11:30:20 +00:00
|
|
|
function initAvailableThemes(cb) {
|
2014-10-30 04:23:44 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getDir(callback) {
|
2014-10-31 04:59:21 +00:00
|
|
|
fs.readdir(Config.paths.themes, function onReadDir(err, files) {
|
2014-10-30 04:23:44 +00:00
|
|
|
callback(err, files);
|
|
|
|
});
|
|
|
|
},
|
2014-10-31 04:59:21 +00:00
|
|
|
function filterFiles(files, callback) {
|
2014-10-30 04:23:44 +00:00
|
|
|
var filtered = files.filter(function onFilter(file) {
|
2014-10-29 11:30:20 +00:00
|
|
|
return fs.statSync(paths.join(Config.paths.themes, file)).isDirectory();
|
2014-10-30 04:23:44 +00:00
|
|
|
});
|
|
|
|
callback(null, filtered);
|
|
|
|
},
|
|
|
|
function populateAvailable(filtered, callback) {
|
|
|
|
filtered.forEach(function onTheme(themeId) {
|
2015-05-13 05:04:22 +00:00
|
|
|
loadTheme(themeId, function themeLoaded(err, theme) {
|
2014-10-30 04:23:44 +00:00
|
|
|
if(!err) {
|
2015-05-13 05:04:22 +00:00
|
|
|
availableThemes[themeId] = theme;
|
|
|
|
Log.debug( { info : theme.info }, 'Theme loaded');
|
2014-10-30 04:23:44 +00:00
|
|
|
}
|
|
|
|
});
|
2015-05-13 05:04:22 +00:00
|
|
|
|
2014-10-30 04:23:44 +00:00
|
|
|
});
|
2014-10-31 04:59:21 +00:00
|
|
|
callback(null);
|
2014-10-30 04:23:44 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
function onComplete(err) {
|
|
|
|
if(err) {
|
|
|
|
cb(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-29 11:30:20 +00:00
|
|
|
cb(null, availableThemes.length);
|
2014-10-30 04:23:44 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-04-21 05:24:15 +00:00
|
|
|
function getRandomTheme() {
|
|
|
|
if(Object.getOwnPropertyNames(availableThemes).length > 0) {
|
|
|
|
var themeIds = Object.keys(availableThemes);
|
|
|
|
return themeIds[Math.floor(Math.random() * themeIds.length)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-27 04:06:41 +00:00
|
|
|
function getThemeArt(name, themeID, options, cb) {
|
|
|
|
// allow options to be optional
|
2015-04-16 04:46:45 +00:00
|
|
|
if(_.isUndefined(cb)) {
|
2014-10-27 04:06:41 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// set/override some options
|
2015-06-09 04:41:57 +00:00
|
|
|
|
|
|
|
// :TODO: replace asAnsi stuff with something like retrieveAs = 'ansi' | 'pipe' | ...
|
|
|
|
// :TODO: Some of these options should only be set if not provided!
|
2014-10-27 04:06:41 +00:00
|
|
|
options.asAnsi = true;
|
2015-04-19 08:13:13 +00:00
|
|
|
options.readSauce = true; // encoding/fonts/etc.
|
2014-10-27 04:06:41 +00:00
|
|
|
options.random = miscUtil.valueWithDefault(options.random, true);
|
2014-10-29 11:30:20 +00:00
|
|
|
options.basePath = paths.join(Config.paths.themes, themeID);
|
2014-10-27 04:06:41 +00:00
|
|
|
|
2014-11-10 04:24:09 +00:00
|
|
|
art.getArt(name, options, function onThemeArt(err, artInfo) {
|
2014-10-27 04:06:41 +00:00
|
|
|
if(err) {
|
2014-10-29 11:30:20 +00:00
|
|
|
// try fallback of art directory
|
2014-10-27 04:06:41 +00:00
|
|
|
options.basePath = Config.paths.art;
|
2014-11-10 04:24:09 +00:00
|
|
|
art.getArt(name, options, function onFallbackArt(err, artInfo) {
|
2014-10-27 04:06:41 +00:00
|
|
|
if(err) {
|
|
|
|
cb(err);
|
|
|
|
} else {
|
2014-11-10 04:24:09 +00:00
|
|
|
cb(null, artInfo);
|
2014-10-27 04:06:41 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2014-11-10 04:24:09 +00:00
|
|
|
cb(null, artInfo);
|
2014-10-27 04:06:41 +00:00
|
|
|
}
|
|
|
|
});
|
2014-11-05 06:50:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 04:29:53 +00:00
|
|
|
function displayThemeArt(options, cb) {
|
|
|
|
assert(_.isObject(options));
|
|
|
|
assert(_.isObject(options.client));
|
|
|
|
assert(_.isString(options.name));
|
|
|
|
|
2015-04-21 04:50:58 +00:00
|
|
|
getThemeArt(options.name, options.client.user.properties.theme_id, function themeArt(err, artInfo) {
|
2014-11-05 06:50:42 +00:00
|
|
|
if(err) {
|
|
|
|
cb(err);
|
|
|
|
} else {
|
2015-04-17 04:29:53 +00:00
|
|
|
var dispOptions = {
|
|
|
|
art : artInfo.data,
|
|
|
|
sauce : artInfo.sauce,
|
|
|
|
client : options.client,
|
|
|
|
font : options.font,
|
|
|
|
};
|
|
|
|
|
2015-06-26 04:34:33 +00:00
|
|
|
art.display(dispOptions, function displayed(err, mciMap, extraInfo) {
|
|
|
|
cb(err, { mciMap : mciMap, artInfo : artInfo, extraInfo : extraInfo } );
|
2014-11-05 06:50:42 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2015-07-06 01:05:55 +00:00
|
|
|
}
|