2015-04-05 07:15:04 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
|
|
|
const Config = require('./config.js').get;
|
|
|
|
const StatLog = require('./stat_log.js');
|
|
|
|
|
|
|
|
// deps
|
|
|
|
const _ = require('lodash');
|
|
|
|
const assert = require('assert');
|
|
|
|
|
|
|
|
exports.parseAsset = parseAsset;
|
|
|
|
exports.getAssetWithShorthand = getAssetWithShorthand;
|
|
|
|
exports.getArtAsset = getArtAsset;
|
|
|
|
exports.getModuleAsset = getModuleAsset;
|
|
|
|
exports.resolveConfigAsset = resolveConfigAsset;
|
|
|
|
exports.resolveSystemStatAsset = resolveSystemStatAsset;
|
|
|
|
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
|
|
|
];
|
|
|
|
|
2018-07-15 17:49:56 +00:00
|
|
|
const ASSET_RE = new RegExp(
|
|
|
|
'^@(' + ALL_ASSETS.join('|') + ')' +
|
|
|
|
/:(?:([^:]+):)?([A-Za-z0-9_\-.]+)$/.source
|
|
|
|
);
|
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);
|
|
|
|
if(m) {
|
2018-07-15 17:49:56 +00:00
|
|
|
const result = { type : m[1] };
|
2015-04-05 07:15:04 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(m[3]) {
|
2018-07-15 17:49:56 +00:00
|
|
|
result.asset = m[3];
|
|
|
|
if(m[2]) {
|
|
|
|
result.location = m[2];
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2018-07-15 17:49:56 +00:00
|
|
|
result.asset = m[2];
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
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 {
|
2018-06-23 03:26:46 +00:00
|
|
|
type : defaultType,
|
|
|
|
asset : spec,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
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);
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
const path = asset.asset.split('.');
|
|
|
|
let conf = Config();
|
2018-06-22 05:15:04 +00:00
|
|
|
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
|
|
|
}
|