feat: support mjs.json js compile & add API

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2022-04-01 18:47:28 +08:00
parent 91a87ab20e
commit a9003025ee
9 changed files with 124 additions and 17 deletions

View File

@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>pw.yumc</groupId>
<artifactId>MiaoScript</artifactId>
<version>0.18.0</version>
<version>0.19.5</version>
<developers>
<developer>
<id>502647092</id>

View File

@ -1,5 +1,7 @@
package pw.yumc.MiaoScript;
import pw.yumc.MiaoScript.api.MiaoScriptAPI;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
@ -13,15 +15,14 @@ import java.nio.file.Path;
* Created on 2017/10/9 12:40.
*/
public class Base {
public static final String VERSION = "0.18.0";
private Object instance;
public Base(Object instance) {
Base(Object instance) {
this.instance = instance;
}
public String getVersion() {
return Base.VERSION;
return MiaoScriptAPI.VERSION;
}
public Class<?> getClass(String name) throws ClassNotFoundException {

View File

@ -60,6 +60,13 @@ public class MiaoScriptEngine implements ScriptEngine, Invocable {
}
public MiaoScriptEngine(ScriptEngineManager engineManager, final String engineType, String engineRoot) {
// JDK11 Polyfill 存在类效验问题 直接用OpenJDK的Nashorn
if (System.getProperty("java.version").startsWith("11.") && engineRoot != null) {
this.loadNetworkNashorn(engineRoot);
if (engine == null)
throw new UnsupportedOperationException("当前环境 JDK11 不支持 Nashorn 脚本类型!");
return;
}
try {
engine = engineManager.getEngineByName(engineType);
} catch (final NullPointerException ignored) {
@ -67,22 +74,26 @@ public class MiaoScriptEngine implements ScriptEngine, Invocable {
if (engine == null) {
val extDirs = System.getProperty("java.ext.dirs");
if (extDirs != null) {
val dirs = extDirs.split(File.pathSeparator);
for (String dir : dirs) {
File nashorn = new File(dir, "nashorn.jar");
if (nashorn.exists()) {
this.loadJar(nashorn);
this.createEngineByName(engineType);
}
}
this.loadLocalNashorn(extDirs, engineType);
} else if (engineRoot != null) {
this.loadLocalNashorn(engineRoot);
this.loadNetworkNashorn(engineRoot);
}
}
if (engine == null)
throw new UnsupportedOperationException("当前环境不支持 " + engineType + " 脚本类型!");
}
private void loadLocalNashorn(String extDirs, String engineType) {
val dirs = extDirs.split(File.pathSeparator);
for (String dir : dirs) {
File nashorn = new File(dir, "nashorn.jar");
if (nashorn.exists()) {
this.loadJar(nashorn);
this.createEngineByName(engineType);
}
}
}
private void initReflect() {
try {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
@ -107,7 +118,7 @@ public class MiaoScriptEngine implements ScriptEngine, Invocable {
}
@SneakyThrows
private void loadLocalNashorn(String engineRoot) {
private void loadNetworkNashorn(String engineRoot) {
initReflect();
File libRootFile = new File(engineRoot, "lib");
libRootFile.mkdirs();
@ -117,7 +128,10 @@ public class MiaoScriptEngine implements ScriptEngine, Invocable {
downloadJar(libRoot, "org.ow2.asm", "asm-commons", "9.2");
downloadJar(libRoot, "org.ow2.asm", "asm-tree", "9.2");
downloadJar(libRoot, "org.ow2.asm", "asm-util", "9.2");
this.createEngineByName("nashorn");
Class<?> NashornScriptEngineFactory = Class.forName("org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory");
Method getScriptEngine = NashornScriptEngineFactory.getMethod("getScriptEngine");
Object factory = NashornScriptEngineFactory.newInstance();
engine = (ScriptEngine) getScriptEngine.invoke(factory);
}
@SneakyThrows

View File

@ -10,6 +10,7 @@ import org.spongepowered.api.event.game.state.GameStartedServerEvent;
import org.spongepowered.api.event.game.state.GameStartingServerEvent;
import org.spongepowered.api.event.game.state.GameStoppingServerEvent;
import org.spongepowered.api.plugin.Plugin;
import pw.yumc.MiaoScript.api.MiaoScriptAPI;
import java.io.File;
@ -19,7 +20,7 @@ import java.io.File;
* @author
* Created on 2017/10/25 20:35.
*/
@Plugin(id = "miaoscript", name = "MiaoScript", version = Base.VERSION, authors = "MiaoWoo")
@Plugin(id = "miaoscript", name = "MiaoScript", version = MiaoScriptAPI.VERSION, authors = "MiaoWoo")
public class MiaoScriptSponge {
private ScriptEngine engine;
@Inject

View File

@ -1,6 +1,7 @@
package pw.yumc.MiaoScript;
import lombok.SneakyThrows;
import pw.yumc.MiaoScript.api.MiaoScriptAPI;
import java.nio.file.Files;
import java.nio.file.Path;
@ -24,6 +25,7 @@ public class ScriptEngine {
this.root = root;
this.logger = logger;
this.base = new Base(instance);
MiaoScriptAPI.setEngine(this);
}
public void createEngine() {

View File

@ -0,0 +1,17 @@
package pw.yumc.MiaoScript.api;
import pw.yumc.MiaoScript.MiaoScriptEngine;
import pw.yumc.MiaoScript.ScriptEngine;
public class MiaoScriptAPI {
public static final String VERSION = "0.19.5";
private static ScriptEngine scriptEngine;
public static void setEngine(ScriptEngine scriptEngine) {
MiaoScriptAPI.scriptEngine = scriptEngine;
}
public static MiaoScriptEngine getEngine() {
return MiaoScriptAPI.scriptEngine.getEngine();
}
}

View File

@ -0,0 +1,68 @@
package pw.yumc.MiaoScript.api.bukkit;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import javax.script.Bindings;
public class ScriptEvent extends Event implements Cancellable {
private final String plugin;
private final String event;
private final Bindings data;
private boolean cancelled = false;
public ScriptEvent(String plugin, String event, Bindings data) {
this.plugin = plugin;
this.event = event;
this.data = data;
}
/**
* Which Plugin Call Event
*
* @return PluginName
*/
public String getPlugin() {
return plugin;
}
/**
* Plugin Event Name
*
* @return EventName
*/
public String getEvent() {
return event;
}
/**
* Plugin Event Data
*
* @return EventData
*/
public Bindings getData() {
return data;
}
@Override
public boolean isCancelled() {
return this.cancelled;
}
@Override
public void setCancelled(boolean b) {
this.cancelled = b;
}
private static final HandlerList handlerList = new HandlerList();
public static HandlerList getHandlerList() {
return handlerList;
}
@Override
public HandlerList getHandlers() {
return handlerList;
}
}

View File

@ -241,7 +241,7 @@
}
cacheModules[id] = module
var cfile = _canonical(file)
if (cfile.endsWith('.js')) {
if (cfile.endsWith('.js') || cfile.endsWith('.mjs.json')) {
compileJs(module, file, __assign(optional, { id: id }))
} else if (cfile.endsWith('.json')) {
compileJson(module, file)

View File

@ -6,9 +6,13 @@ api-version: 1.13
author: MiaoWoo
website: ${ciManagement.url}
softdepend:
- OriginAttribute
- AttributeSystem
- ItemLoreOrigin
- PlaceholderAPI
- AttributePlus
- PlayerPoints
- SX-Attribute
- CrazyCrates
- ProtocolLib
- DragonCore