2015-07-20 03:49:48 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var Config = require('./config.js').config;
|
|
|
|
var Log = require('./logger.js').log;
|
|
|
|
|
|
|
|
var paths = require('path');
|
|
|
|
var fs = require('fs');
|
|
|
|
var Gaze = require('gaze').Gaze;
|
2015-09-10 03:31:04 +00:00
|
|
|
var events = require('events');
|
|
|
|
var util = require('util');
|
2015-07-20 03:49:48 +00:00
|
|
|
var assert = require('assert');
|
2015-09-09 04:08:45 +00:00
|
|
|
var hjson = require('hjson');
|
2015-09-27 21:35:24 +00:00
|
|
|
var _ = require('lodash');
|
2015-07-20 03:49:48 +00:00
|
|
|
|
2015-09-10 03:31:04 +00:00
|
|
|
function ConfigCache() {
|
|
|
|
events.EventEmitter.call(this);
|
2015-07-20 03:49:48 +00:00
|
|
|
|
|
|
|
var self = this;
|
2015-09-10 03:31:04 +00:00
|
|
|
this.cache = {}; // filePath -> HJSON
|
2015-07-20 03:49:48 +00:00
|
|
|
this.gaze = new Gaze();
|
|
|
|
|
2015-09-18 05:17:15 +00:00
|
|
|
this.reCacheConfigFromFile = function(filePath, cb) {
|
2015-07-20 03:49:48 +00:00
|
|
|
fs.readFile(filePath, { encoding : 'utf-8' }, function fileRead(err, data) {
|
|
|
|
try {
|
2015-09-09 04:08:45 +00:00
|
|
|
self.cache[filePath] = hjson.parse(data);
|
2015-07-20 03:49:48 +00:00
|
|
|
cb(null, self.cache[filePath]);
|
|
|
|
} catch(e) {
|
2015-09-09 04:08:45 +00:00
|
|
|
console.log(e)
|
2015-07-20 03:49:48 +00:00
|
|
|
cb(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
this.gaze.on('error', function gazeErr(err) {
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
this.gaze.on('changed', function fileChanged(filePath) {
|
|
|
|
assert(filePath in self.cache);
|
|
|
|
|
2015-09-18 05:17:15 +00:00
|
|
|
Log.info( { filePath : filePath }, 'Configuration file changed; recaching');
|
2015-07-20 03:49:48 +00:00
|
|
|
|
2015-09-18 05:17:15 +00:00
|
|
|
self.reCacheConfigFromFile(filePath, function reCached(err) {
|
2015-07-20 03:49:48 +00:00
|
|
|
if(err) {
|
2015-09-18 05:17:15 +00:00
|
|
|
Log.error( { error : err, filePath : filePath } , 'Error recaching HJSON config');
|
2015-09-10 03:31:04 +00:00
|
|
|
} else {
|
|
|
|
self.emit('recached', filePath);
|
2015-07-20 03:49:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2015-09-10 03:31:04 +00:00
|
|
|
|
2015-07-20 03:49:48 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 03:31:04 +00:00
|
|
|
util.inherits(ConfigCache, events.EventEmitter);
|
|
|
|
|
2015-09-27 21:35:24 +00:00
|
|
|
ConfigCache.prototype.getConfigWithOptions = function(options, cb) {
|
|
|
|
assert(_.isString(options.filePath));
|
|
|
|
|
2015-07-20 03:49:48 +00:00
|
|
|
var self = this;
|
2015-09-27 21:35:24 +00:00
|
|
|
var isCached = (options.filePath in this.cache);
|
2015-07-20 03:49:48 +00:00
|
|
|
|
2015-09-27 21:35:24 +00:00
|
|
|
if(options.forceReCache || !isCached) {
|
|
|
|
this.reCacheConfigFromFile(options.filePath, function fileCached(err, config) {
|
|
|
|
if(!err && !isCached) {
|
|
|
|
self.gaze.add(options.filePath);
|
2015-07-20 03:49:48 +00:00
|
|
|
}
|
2015-09-18 05:17:15 +00:00
|
|
|
cb(err, config, true);
|
2015-07-20 03:49:48 +00:00
|
|
|
});
|
2015-09-27 21:35:24 +00:00
|
|
|
} else {
|
|
|
|
cb(null, this.cache[options.filePath], false);
|
2015-07-20 03:49:48 +00:00
|
|
|
}
|
2015-07-24 04:23:44 +00:00
|
|
|
};
|
2015-09-10 03:31:04 +00:00
|
|
|
|
2015-09-27 21:35:24 +00:00
|
|
|
|
|
|
|
ConfigCache.prototype.getConfig = function(filePath, cb) {
|
|
|
|
this.getConfigWithOptions( { filePath : filePath }, cb);
|
|
|
|
};
|
|
|
|
|
2015-09-10 03:31:04 +00:00
|
|
|
ConfigCache.prototype.getModConfig = function(fileName, cb) {
|
|
|
|
this.getConfig(paths.join(Config.paths.mods, fileName), cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = exports = new ConfigCache();
|