2021-03-26 07:54:26 +00:00
|
|
|
'use strict'
|
2017-11-16 12:28:18 +00:00
|
|
|
var global = this;
|
2017-09-14 12:41:34 +00:00
|
|
|
/**
|
2019-09-23 01:02:34 +00:00
|
|
|
* Init MiaoScriptEngine Runtime
|
2017-09-14 12:41:34 +00:00
|
|
|
*/
|
2022-02-16 06:36:22 +00:00
|
|
|
/*global base ScriptEngineContextHolder*/
|
2020-05-02 16:31:59 +00:00
|
|
|
(function () {
|
2021-06-22 06:03:16 +00:00
|
|
|
var Files = Java.type('java.nio.file.Files')
|
|
|
|
var Paths = Java.type('java.nio.file.Paths')
|
|
|
|
var System = Java.type('java.lang.System')
|
|
|
|
var Thread = Java.type('java.lang.Thread')
|
|
|
|
var FutureTask = Java.type('java.util.concurrent.FutureTask')
|
|
|
|
|
2020-05-02 16:31:59 +00:00
|
|
|
global.boot = function (root, logger) {
|
2021-06-22 06:03:16 +00:00
|
|
|
global.scope = System.getenv("MS_NODE_CORE_SCOPE") || "@ccms"
|
|
|
|
global.logger = logger
|
2019-08-29 02:08:29 +00:00
|
|
|
// Development Env Detect
|
2021-03-26 07:54:26 +00:00
|
|
|
global.root = root || "src/main/resources"
|
2022-05-14 10:44:24 +00:00
|
|
|
readEnvironment()
|
2021-11-04 03:29:37 +00:00
|
|
|
if (!global.debug) {
|
|
|
|
checkUpgrade()
|
|
|
|
}
|
2021-06-22 06:03:16 +00:00
|
|
|
return bootEngineThread(checkClassLoader())
|
|
|
|
}
|
|
|
|
|
|
|
|
function bootEngineThread(loader) {
|
|
|
|
logger.info("ScriptEngine: " + ScriptEngineContextHolder.getEngine().getEngine().class.name)
|
|
|
|
var future = new FutureTask(function () {
|
|
|
|
Thread.currentThread().contextClassLoader = loader
|
2022-05-21 07:53:03 +00:00
|
|
|
var faster = load(System.getenv("MS_NODE_CORE_POLYFILL") || 'classpath:core/polyfill.js')(root, logger)
|
|
|
|
return faster.default ? require(System.getenv("MS_NODE_CORE_MODULE") || (global.scope + '/core')).default : false
|
2021-06-22 06:03:16 +00:00
|
|
|
})
|
|
|
|
// Async Loading MiaoScript Engine
|
|
|
|
new Thread(future, "MiaoScript thread").start()
|
|
|
|
return future
|
|
|
|
}
|
|
|
|
|
|
|
|
global.enable = function (future) {
|
|
|
|
if (!future.isDone()) {
|
|
|
|
logger.info("Waiting MiaoScript booted...")
|
|
|
|
}
|
2022-04-19 09:20:43 +00:00
|
|
|
// await polyfill loading
|
2022-05-21 07:53:03 +00:00
|
|
|
// faster load core
|
|
|
|
var core = future.get()
|
2021-06-22 06:03:16 +00:00
|
|
|
logger.info("MiaoScript booted starting...")
|
2022-05-21 07:53:03 +00:00
|
|
|
var disable = core ? core.enable() : require(System.getenv("MS_NODE_CORE_MODULE") || (global.scope + '/core')).default
|
|
|
|
global.engineDisableImpl = disable || function () {
|
2021-06-22 06:03:16 +00:00
|
|
|
logger.info('Error: abnormal Initialization MiaoScript Engine. Skip disable step...')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
global.disable = function () {
|
|
|
|
global.engineDisableImpl && global.engineDisableImpl()
|
|
|
|
}
|
|
|
|
|
2022-05-14 10:44:24 +00:00
|
|
|
function readEnvironment() {
|
2020-04-22 01:23:21 +00:00
|
|
|
if (__FILE__.indexOf('!') === -1) {
|
2022-05-14 10:44:24 +00:00
|
|
|
logger.info('loading custom BIOS file ' + __FILE__)
|
2021-03-26 07:54:26 +00:00
|
|
|
global.debug = true
|
2017-11-15 10:40:17 +00:00
|
|
|
}
|
2021-06-22 06:03:16 +00:00
|
|
|
if (Files.exists(Paths.get(root, "debug"))) {
|
2022-05-14 10:44:24 +00:00
|
|
|
logger.info('running in debug mode...')
|
2021-03-26 07:54:26 +00:00
|
|
|
global.debug = true
|
2020-01-31 18:41:00 +00:00
|
|
|
}
|
2021-06-22 06:03:16 +00:00
|
|
|
if (Files.exists(Paths.get(root, "level"))) {
|
2022-05-21 07:53:03 +00:00
|
|
|
global.ScriptEngineLoggerLevel = base.read(Paths.get(root, "level"))
|
|
|
|
logger.info('found level set ScriptEngineLoggerLevel to ' + global.ScriptEngineLoggerLevel + '.')
|
|
|
|
}
|
|
|
|
if (Files.exists(Paths.get(root, "channel"))) {
|
|
|
|
global.ScriptEngineChannel = base.read(Paths.get(root, "channel"))
|
|
|
|
logger.info('found channel set ScriptEngineChannel to ' + global.ScriptEngineChannel + '.')
|
2017-11-30 05:27:20 +00:00
|
|
|
}
|
2021-06-22 06:03:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkUpgrade() {
|
|
|
|
if (Files.exists(Paths.get(root, "upgrade"))) {
|
2022-05-14 10:44:24 +00:00
|
|
|
logger.info('found upgrade file starting upgrade...')
|
2021-06-22 06:03:16 +00:00
|
|
|
base.move(Paths.get(root, "node_modules"), Paths.get(root, "old_node_modules"))
|
|
|
|
base.delete(Paths.get(root, "upgrade"))
|
2020-08-27 09:30:59 +00:00
|
|
|
}
|
2021-06-22 06:03:16 +00:00
|
|
|
new Thread(function () {
|
2020-09-21 08:06:37 +00:00
|
|
|
try {
|
2021-06-22 06:03:16 +00:00
|
|
|
base.delete(Paths.get(root, "old_node_modules"))
|
2020-09-21 08:06:37 +00:00
|
|
|
} catch (ex) {
|
|
|
|
}
|
|
|
|
}, "MiaoScript node_modules clean thread").start()
|
2021-03-26 07:54:26 +00:00
|
|
|
}
|
2017-10-26 13:01:24 +00:00
|
|
|
|
2017-11-15 13:00:07 +00:00
|
|
|
function checkClassLoader() {
|
2021-06-22 06:03:16 +00:00
|
|
|
// Check Class Loader, Sometimes Server will can't found plugin.yml file
|
|
|
|
var classLoader = Thread.currentThread().contextClassLoader
|
2020-05-28 09:05:47 +00:00
|
|
|
if (classLoader.getResource("bios.js") === null) {
|
2021-03-26 07:54:26 +00:00
|
|
|
throw Error("Error class loader: " + classLoader.class.name + " Please contact the author MiaoWoo!")
|
2019-02-23 16:00:03 +00:00
|
|
|
} else {
|
2021-06-22 06:03:16 +00:00
|
|
|
logger.info("Class loader compatible: " + classLoader.class.name)
|
2019-03-25 09:13:54 +00:00
|
|
|
if (classLoader.parent) {
|
2021-06-22 06:03:16 +00:00
|
|
|
logger.info("Parent class loader: " + classLoader.parent.class.name)
|
2019-03-25 09:13:54 +00:00
|
|
|
}
|
2017-11-15 10:40:17 +00:00
|
|
|
}
|
2021-03-26 07:54:26 +00:00
|
|
|
return classLoader
|
2017-11-15 10:40:17 +00:00
|
|
|
}
|
2021-03-26 07:54:26 +00:00
|
|
|
})()
|