@ -3,7 +3,7 @@
|
||||
* MiaoScript脚本插件加载类
|
||||
*/
|
||||
/*global Java, base, module, exports, require, __FILE__*/
|
||||
var zip = require("core/zip");
|
||||
// var zip = require("core/zip");
|
||||
var fs = require('core/fs');
|
||||
|
||||
/**
|
||||
@ -56,37 +56,74 @@ function loadJsPlugin(files) {
|
||||
return file.name.endsWith(".js")
|
||||
}).forEach(function (file) {
|
||||
var p = require(file);
|
||||
log.d("插件编译结果: %s", JSON.stringify(p));
|
||||
if (!p.description || !p.description.name) {
|
||||
log.w("文件 %s 不存在 description 描述信息 无法加载插件!");
|
||||
log.w("文件 %s 不存在 description 描述信息 无法加载插件!", file);
|
||||
} else {
|
||||
exports.plugins.push(p);
|
||||
plugins.push(p);
|
||||
plugins[p.description.name] = p;
|
||||
log.i('插件 %s 版本 %s 加载成功!', p.description.name, p.description.version);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function runAndCatch(name, exec) {
|
||||
if (exec) {
|
||||
try {
|
||||
exec();
|
||||
} catch (ex) {
|
||||
log.w('插件 %s 执行 %s 发生错误: %s', name, exec.name, ex.message);
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function checkAndGet(name) {
|
||||
if (!exports.plugins[name]) {
|
||||
throw new Error("插件 " + name + "不存在!");
|
||||
}
|
||||
return exports.plugins[name];
|
||||
}
|
||||
|
||||
var plugins = [];
|
||||
|
||||
exports.$ = undefined;
|
||||
exports.plugins = [];
|
||||
exports.plugins = plugins;
|
||||
exports.init = function (plugin, path) {
|
||||
if (plugin !== null) {
|
||||
// 如果过plugin不等于null 则代表是正式环境
|
||||
exports.$ = plugin;
|
||||
log.i("Init MiaoScript Engine Version: %s", plugin.description.version);
|
||||
require('./event');
|
||||
log.i("初始化 MiaoScript 插件系统 版本: %s", plugin.description.version);
|
||||
}
|
||||
loadPlugins(path);
|
||||
};
|
||||
exports.load = function () {
|
||||
exports.plugins.forEach(function (p) {
|
||||
p.load();
|
||||
})
|
||||
if (arguments.length === 0) {
|
||||
plugins.forEach(function (p) {
|
||||
runAndCatch(p.description.name, p.load);
|
||||
})
|
||||
} else {
|
||||
var p = checkAndGet(arguments[0]);
|
||||
runAndCatch(p.description.name, p.load);
|
||||
}
|
||||
};
|
||||
exports.enable = function () {
|
||||
exports.plugins.forEach(function (p) {
|
||||
p.enable();
|
||||
})
|
||||
if (arguments.length === 0) {
|
||||
plugins.forEach(function (p) {
|
||||
runAndCatch(p.description.name, p.enable);
|
||||
})
|
||||
} else {
|
||||
var p = checkAndGet(arguments[0]);
|
||||
runAndCatch(p.description.name, p.enable);
|
||||
}
|
||||
};
|
||||
exports.disable = function () {
|
||||
exports.plugins.forEach(function (p) {
|
||||
p.disable();
|
||||
})
|
||||
if (arguments.length === 0) {
|
||||
plugins.forEach(function (p) {
|
||||
runAndCatch(p.description.name, p.disable);
|
||||
})
|
||||
} else {
|
||||
var p = checkAndGet(arguments[0]);
|
||||
runAndCatch(p.description.name, p.disable);
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user