From a9003025ee96908836a45c6712e9099830303ecb Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Fri, 1 Apr 2022 18:47:28 +0800 Subject: [PATCH] feat: support mjs.json js compile & add API Signed-off-by: MiaoWoo --- pom.xml | 2 +- src/main/java/pw/yumc/MiaoScript/Base.java | 7 +- .../pw/yumc/MiaoScript/MiaoScriptEngine.java | 36 +++++++--- .../pw/yumc/MiaoScript/MiaoScriptSponge.java | 3 +- .../java/pw/yumc/MiaoScript/ScriptEngine.java | 2 + .../pw/yumc/MiaoScript/api/MiaoScriptAPI.java | 17 +++++ .../MiaoScript/api/bukkit/ScriptEvent.java | 68 +++++++++++++++++++ src/main/resources/core/require.js | 2 +- src/main/resources/plugin.yml | 4 ++ 9 files changed, 124 insertions(+), 17 deletions(-) create mode 100644 src/main/java/pw/yumc/MiaoScript/api/MiaoScriptAPI.java create mode 100644 src/main/java/pw/yumc/MiaoScript/api/bukkit/ScriptEvent.java diff --git a/pom.xml b/pom.xml index eaef3b3..8e5c96a 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 pw.yumc MiaoScript - 0.18.0 + 0.19.5 502647092 diff --git a/src/main/java/pw/yumc/MiaoScript/Base.java b/src/main/java/pw/yumc/MiaoScript/Base.java index 4eb8f70..1fa10c7 100644 --- a/src/main/java/pw/yumc/MiaoScript/Base.java +++ b/src/main/java/pw/yumc/MiaoScript/Base.java @@ -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 { diff --git a/src/main/java/pw/yumc/MiaoScript/MiaoScriptEngine.java b/src/main/java/pw/yumc/MiaoScript/MiaoScriptEngine.java index 18e0e5c..561a4f8 100644 --- a/src/main/java/pw/yumc/MiaoScript/MiaoScriptEngine.java +++ b/src/main/java/pw/yumc/MiaoScript/MiaoScriptEngine.java @@ -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 diff --git a/src/main/java/pw/yumc/MiaoScript/MiaoScriptSponge.java b/src/main/java/pw/yumc/MiaoScript/MiaoScriptSponge.java index fe96798..372f1b5 100644 --- a/src/main/java/pw/yumc/MiaoScript/MiaoScriptSponge.java +++ b/src/main/java/pw/yumc/MiaoScript/MiaoScriptSponge.java @@ -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 diff --git a/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java b/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java index eec0989..d6bf28e 100644 --- a/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java +++ b/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java @@ -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() { diff --git a/src/main/java/pw/yumc/MiaoScript/api/MiaoScriptAPI.java b/src/main/java/pw/yumc/MiaoScript/api/MiaoScriptAPI.java new file mode 100644 index 0000000..cafce44 --- /dev/null +++ b/src/main/java/pw/yumc/MiaoScript/api/MiaoScriptAPI.java @@ -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(); + } +} diff --git a/src/main/java/pw/yumc/MiaoScript/api/bukkit/ScriptEvent.java b/src/main/java/pw/yumc/MiaoScript/api/bukkit/ScriptEvent.java new file mode 100644 index 0000000..84ee323 --- /dev/null +++ b/src/main/java/pw/yumc/MiaoScript/api/bukkit/ScriptEvent.java @@ -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; + } +} diff --git a/src/main/resources/core/require.js b/src/main/resources/core/require.js index d86ab39..52f81f7 100644 --- a/src/main/resources/core/require.js +++ b/src/main/resources/core/require.js @@ -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) diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 3f3aee1..4d9ca3b 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -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