2022-05-14 10:44:24 +00:00
|
|
|
package pw.yumc.MiaoScript.api;
|
2017-10-26 13:01:24 +00:00
|
|
|
|
2020-07-08 03:11:18 +00:00
|
|
|
import lombok.SneakyThrows;
|
2022-05-14 10:44:24 +00:00
|
|
|
import pw.yumc.MiaoScript.engine.MiaoScriptEngine;
|
2020-07-08 03:11:18 +00:00
|
|
|
|
2017-10-30 03:17:42 +00:00
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
2017-11-02 18:04:22 +00:00
|
|
|
import java.nio.file.Paths;
|
2017-10-30 03:17:42 +00:00
|
|
|
|
2017-10-26 13:01:24 +00:00
|
|
|
/**
|
|
|
|
* Created with IntelliJ IDEA
|
|
|
|
*
|
2020-05-28 09:05:47 +00:00
|
|
|
* @author 喵♂呜 Created on 2017/10/25 21:01.
|
2017-10-26 13:01:24 +00:00
|
|
|
*/
|
|
|
|
public class ScriptEngine {
|
2020-12-07 05:49:27 +00:00
|
|
|
private final ClassLoader loader;
|
2020-11-11 09:27:43 +00:00
|
|
|
private final Object logger;
|
2020-12-07 05:49:27 +00:00
|
|
|
private final String root;
|
2020-11-11 09:27:43 +00:00
|
|
|
private final Base base;
|
2017-10-26 13:01:24 +00:00
|
|
|
private MiaoScriptEngine engine;
|
2021-03-26 07:54:26 +00:00
|
|
|
private Object future;
|
2017-10-26 13:01:24 +00:00
|
|
|
|
2022-06-01 09:52:55 +00:00
|
|
|
ScriptEngine(String root, Object logger, Object instance) {
|
2020-12-07 05:49:27 +00:00
|
|
|
this.loader = Thread.currentThread().getContextClassLoader();
|
2017-10-26 13:01:24 +00:00
|
|
|
this.root = root;
|
|
|
|
this.logger = logger;
|
2020-01-17 01:17:13 +00:00
|
|
|
this.base = new Base(instance);
|
2022-05-14 10:44:24 +00:00
|
|
|
MiaoScriptAPI.setRoot(root);
|
2022-04-01 10:47:28 +00:00
|
|
|
MiaoScriptAPI.setEngine(this);
|
2017-10-26 13:01:24 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 06:03:16 +00:00
|
|
|
public void createEngine() {
|
2020-11-11 09:27:43 +00:00
|
|
|
synchronized (logger) {
|
|
|
|
if (this.engine == null) {
|
2022-06-01 09:52:55 +00:00
|
|
|
this.engine = new MiaoScriptEngine(root);
|
2020-11-11 09:27:43 +00:00
|
|
|
this.engine.put("base", this.base);
|
|
|
|
this.engine.put("ScriptEngineContextHolder", this);
|
|
|
|
}
|
2017-10-26 13:01:24 +00:00
|
|
|
}
|
2020-07-08 03:11:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@SneakyThrows
|
2021-03-26 07:54:26 +00:00
|
|
|
public void loadEngine() {
|
2020-12-07 05:49:27 +00:00
|
|
|
ClassLoader originLoader = Thread.currentThread().getContextClassLoader();
|
|
|
|
Thread.currentThread().setContextClassLoader(this.loader);
|
2020-12-17 08:17:16 +00:00
|
|
|
createEngine();
|
2020-07-08 03:11:18 +00:00
|
|
|
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')");
|
|
|
|
}
|
2021-03-26 07:54:26 +00:00
|
|
|
future = engine.invokeFunction("boot", root, logger);
|
2020-12-07 05:49:27 +00:00
|
|
|
Thread.currentThread().setContextClassLoader(originLoader);
|
2017-10-26 13:01:24 +00:00
|
|
|
}
|
|
|
|
|
2021-03-26 07:54:26 +00:00
|
|
|
@SneakyThrows
|
|
|
|
public void enableEngine() {
|
2021-06-22 06:03:16 +00:00
|
|
|
if (this.engine != null) {
|
|
|
|
engine.invokeFunction("enable", future);
|
|
|
|
}
|
2021-03-26 07:54:26 +00:00
|
|
|
}
|
|
|
|
|
2017-11-02 17:36:32 +00:00
|
|
|
@SneakyThrows
|
2020-11-11 09:27:43 +00:00
|
|
|
public void disableEngine() {
|
|
|
|
synchronized (logger) {
|
|
|
|
if (this.engine != null) {
|
2021-06-22 06:03:16 +00:00
|
|
|
this.engine.invokeFunction("disable");
|
2020-11-11 09:27:43 +00:00
|
|
|
this.engine = null;
|
|
|
|
}
|
2020-05-28 09:05:47 +00:00
|
|
|
}
|
2017-10-26 13:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public MiaoScriptEngine getEngine() {
|
|
|
|
return engine;
|
|
|
|
}
|
|
|
|
}
|