2015-04-05 07:15:04 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2016-06-26 04:36:40 +00:00
|
|
|
// ENiGMA½
|
2018-06-21 01:57:06 +00:00
|
|
|
const Config = require('./config.js').get;
|
2016-08-04 03:46:38 +00:00
|
|
|
const StatLog = require('./stat_log.js');
|
2015-04-21 04:50:58 +00:00
|
|
|
|
2016-06-26 04:36:40 +00:00
|
|
|
// deps
|
|
|
|
const _ = require('lodash');
|
|
|
|
const assert = require('assert');
|
2015-04-19 08:13:13 +00:00
|
|
|
|
2015-06-07 01:34:55 +00:00
|
|
|
exports.parseAsset = parseAsset;
|
2015-09-09 04:08:45 +00:00
|
|
|
exports.getAssetWithShorthand = getAssetWithShorthand;
|
2015-06-07 01:34:55 +00:00
|
|
|
exports.getArtAsset = getArtAsset;
|
2015-08-08 21:52:47 +00:00
|
|
|
exports.getModuleAsset = getModuleAsset;
|
2015-06-07 01:34:55 +00:00
|
|
|
exports.resolveConfigAsset = resolveConfigAsset;
|
2016-08-04 03:46:38 +00:00
|
|
|
exports.resolveSystemStatAsset = resolveSystemStatAsset;
|
2015-06-07 01:34:55 +00:00
|
|
|
exports.getViewPropertyAsset = getViewPropertyAsset;
|
2015-04-05 07:15:04 +00:00
|
|
|
|
2016-06-20 03:09:45 +00:00
|
|
|
const ALL_ASSETS = [
|
2018-06-22 05:15:04 +00:00
|
|
|
'art',
|
|
|
|
'menu',
|
|
|
|
'method',
|
|
|
|
'userModule',
|
|
|
|
'systemMethod',
|
|
|
|
'systemModule',
|
|
|
|
'prompt',
|
|
|
|
'config',
|
|
|
|
'sysStat',
|
2015-04-05 07:15:04 +00:00
|
|
|
];
|
|
|
|
|
2016-06-26 04:36:40 +00:00
|
|
|
const ASSET_RE = new RegExp('\\@(' + ALL_ASSETS.join('|') + ')\\:([\\w\\d\\.]*)(?:\\/([\\w\\d\\_]+))*');
|
2015-04-05 07:15:04 +00:00
|
|
|
|
2018-01-15 19:22:11 +00:00
|
|
|
function parseAsset(s) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const m = ASSET_RE.exec(s);
|
2015-04-05 07:15:04 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(m) {
|
|
|
|
let result = { type : m[1] };
|
2015-04-05 07:15:04 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(m[3]) {
|
|
|
|
result.location = m[2];
|
|
|
|
result.asset = m[3];
|
|
|
|
} else {
|
|
|
|
result.asset = m[2];
|
|
|
|
}
|
2015-04-05 07:15:04 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return result;
|
|
|
|
}
|
2015-04-19 08:13:13 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 04:08:45 +00:00
|
|
|
function getAssetWithShorthand(spec, defaultType) {
|
2018-06-22 05:15:04 +00:00
|
|
|
if(!_.isString(spec)) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-09-09 04:08:45 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if('@' === spec[0]) {
|
|
|
|
const asset = parseAsset(spec);
|
|
|
|
assert(_.isString(asset.type));
|
2015-09-09 04:08:45 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return asset;
|
|
|
|
}
|
2017-12-01 00:15:18 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return {
|
|
|
|
type : defaultType,
|
|
|
|
asset : spec,
|
|
|
|
};
|
2015-09-09 04:08:45 +00:00
|
|
|
}
|
|
|
|
|
2016-06-26 04:36:40 +00:00
|
|
|
function getArtAsset(spec) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const asset = getAssetWithShorthand(spec, 'art');
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(!asset) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-04-19 08:13:13 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
assert( ['art', 'method' ].indexOf(asset.type) > -1);
|
|
|
|
return asset;
|
2015-04-21 04:50:58 +00:00
|
|
|
}
|
2015-04-27 02:46:16 +00:00
|
|
|
|
2016-06-26 04:36:40 +00:00
|
|
|
function getModuleAsset(spec) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const asset = getAssetWithShorthand(spec, 'systemModule');
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(!asset) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-08-08 21:52:47 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
assert( ['userModule', 'systemModule' ].includes(asset.type) );
|
2017-12-01 00:15:18 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return asset;
|
2015-08-08 21:52:47 +00:00
|
|
|
}
|
|
|
|
|
2016-06-26 04:36:40 +00:00
|
|
|
function resolveConfigAsset(spec) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const asset = parseAsset(spec);
|
|
|
|
if(asset) {
|
|
|
|
assert('config' === asset.type);
|
|
|
|
|
|
|
|
const path = asset.asset.split('.');
|
|
|
|
let conf = Config();
|
|
|
|
for(let i = 0; i < path.length; ++i) {
|
|
|
|
if(_.isUndefined(conf[path[i]])) {
|
|
|
|
return spec;
|
|
|
|
}
|
|
|
|
conf = conf[path[i]];
|
|
|
|
}
|
|
|
|
return conf;
|
|
|
|
} else {
|
|
|
|
return spec;
|
|
|
|
}
|
2016-08-04 03:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function resolveSystemStatAsset(spec) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const asset = parseAsset(spec);
|
|
|
|
if(!asset) {
|
|
|
|
return spec;
|
|
|
|
}
|
2016-08-04 03:46:38 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
assert('sysStat' === asset.type);
|
2016-08-04 03:46:38 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return StatLog.getSystemStat(asset.asset) || spec;
|
2015-04-27 02:46:16 +00:00
|
|
|
}
|
2015-06-07 01:34:55 +00:00
|
|
|
|
|
|
|
function getViewPropertyAsset(src) {
|
2018-06-22 05:15:04 +00:00
|
|
|
if(!_.isString(src) || '@' !== src.charAt(0)) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-06-07 01:34:55 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return parseAsset(src);
|
2016-06-26 04:36:40 +00:00
|
|
|
}
|