63 lines
1.7 KiB
Java
63 lines
1.7 KiB
Java
package pw.yumc.MiaoScript;
|
|
|
|
import lombok.SneakyThrows;
|
|
|
|
import javax.script.ScriptEngineManager;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Paths;
|
|
|
|
/**
|
|
* Created with IntelliJ IDEA
|
|
*
|
|
* @author 喵♂呜 Created on 2017/10/25 21:01.
|
|
*/
|
|
public class ScriptEngine {
|
|
private String root;
|
|
private Object logger;
|
|
private Base base;
|
|
private MiaoScriptEngine engine;
|
|
private ScriptEngineManager manager;
|
|
|
|
public ScriptEngine(String root, Object logger, Object instance) {
|
|
this.root = root;
|
|
this.logger = logger;
|
|
this.base = new Base(instance);
|
|
this.manager = new ScriptEngineManager();
|
|
}
|
|
|
|
public synchronized MiaoScriptEngine createEngine() {
|
|
if (this.engine == null) {
|
|
this.engine = new MiaoScriptEngine(manager, "nashorn");
|
|
this.engine.put("base", this.base);
|
|
this.engine.put("ScriptEngineContextHolder", this);
|
|
}
|
|
return this.engine;
|
|
}
|
|
|
|
@SneakyThrows
|
|
public void enableEngine() {
|
|
createEngine();
|
|
Path bios = Paths.get(root, "bios.js");
|
|
// 如果存在自定义bios就加载自定义的
|
|
if (Files.exists(bios)) {
|
|
this.engine.eval("load('" + bios.toFile().getCanonicalPath() + "')");
|
|
} else {
|
|
this.engine.eval("load('classpath:bios.js')");
|
|
}
|
|
engine.invokeFunction("boot", root, logger);
|
|
}
|
|
|
|
@SneakyThrows
|
|
public synchronized void disableEngine() {
|
|
if (this.engine != null) {
|
|
this.engine.invokeFunction("engineDisable");
|
|
this.engine = null;
|
|
}
|
|
}
|
|
|
|
public MiaoScriptEngine getEngine() {
|
|
return engine;
|
|
}
|
|
}
|