commit 8467e4a85e8b92cd92c16f37924f2500238809ed Author: MiaoWoo Date: Sat Jun 19 10:25:59 2021 +0000 init: Create MiaoNashorn Project... diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8fc2d24 --- /dev/null +++ b/.gitignore @@ -0,0 +1,55 @@ +# Eclipse stuff +/.settings + +# netbeans +/nbproject + +# we use maven! +/build.xml + +# maven +/target +/repo + +# vim +.*.sw[a-p] + +# various other potential build files +/build +/bin +/dist +/manifest.mf + +# Mac filesystem dust +*.DS_Store + +# intellij +*.iml +*.ipr +*.iws +.idea/ + +# Project Stuff +/src/main/resources/Soulbound + +# Atlassian Stuff +/atlassian-ide-plugin.xml + +# Eclipse +.project +.classpath +.factorypath +.settings + +# Visual Studio Code +.vscode + +# NodeJs PHP LOCK File +*.lock + +# PHP Vendor +vendor/ + +# Minecraft Data +/world +**/node_modules/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..d182d4b --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# MiaoNashorn + +> 用于 Java14+ 环境下 自动加载Nashorn引擎 \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..586ac4c --- /dev/null +++ b/pom.xml @@ -0,0 +1,95 @@ + + 4.0.0 + pw.yumc + MiaoNashorn + 0.0.1 + + + 502647092 + MiaoWoo + admin@yumc.pw + http://www.yumc.pw + + + + ${project.artifactId} + + + src/main/resources + + dev-plugins/** + + true + + + + + Jenkins + https://ci.yumc.pw/job/Minecraft/job/${project.artifactId}/ + + + DEV + + + + + UTF-8 + 1.8 + 1.8 + https://repo.yumc.pw + + + + yumc-repo + ${repo.url}/repository/maven-public/ + + + sponge + https://repo.spongepowered.org/maven/ + + + + + yumc-repo + ${repo.url}/repository/maven-public/ + + + + + jtb + YUMC + ${repo.url}/repository/yumcenter/ + + + + + org.projectlombok + lombok + 1.18.18 + + + org.spigotmc + spigot-api + 1.16.2-R0.1-SNAPSHOT + compile + + + org.spongepowered + spongeapi + 7.2.0 + compile + + + net.md-5 + bungeecord-api + 1.16-R0.4-SNAPSHOT + + + cn.nukkit + nukkit + 2.0.0-SNAPSHOT + compile + + + diff --git a/src/main/java/pw/yumc/MiaoNashorn/MiaoNashorn.java b/src/main/java/pw/yumc/MiaoNashorn/MiaoNashorn.java new file mode 100644 index 0000000..9a17980 --- /dev/null +++ b/src/main/java/pw/yumc/MiaoNashorn/MiaoNashorn.java @@ -0,0 +1,103 @@ +package pw.yumc.MiaoNashorn; + +import sun.misc.Unsafe; + +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import java.io.File; +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; + +/** + * 喵式脚本 + * + * @author 喵♂呜 + * @since 2016年8月29日 上午7:50:39 + */ +public class MiaoNashorn { + private static String MavenRepo = "https://maven.aliyun.com/repository/public"; + private static final Object ucp; + private static final MethodHandle addURLMethodHandle; + + static { + try { + ClassLoader loader = Thread.currentThread().getContextClassLoader(); + Field theUnsafe = sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); + theUnsafe.setAccessible(true); + Unsafe unsafe = (Unsafe) theUnsafe.get(null); + Field field = MethodHandles.Lookup.class.getDeclaredField("IMPL_LOOKUP"); + MethodHandles.Lookup lookup = (MethodHandles.Lookup) unsafe.getObject(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field)); + Field ucpField; + try { + ucpField = loader.getClass().getDeclaredField("ucp"); + } catch (NoSuchFieldException e) { + ucpField = loader.getClass().getSuperclass().getDeclaredField("ucp"); + } + long offset = unsafe.objectFieldOffset(ucpField); + ucp = unsafe.getObject(loader, offset); + Method method = ucp.getClass().getDeclaredMethod("addURL", URL.class); + addURLMethodHandle = lookup.unreflect(method); + createEngine(); + } catch (Throwable e) { + throw new RuntimeException(e); + } + } + + public static void log(String format, Object... args) { + System.out.println("[MiaoNashorn] " + String.format(format, args)); + } + + private static void createEngine() throws Throwable { + String extDirs = System.getProperty("java.ext.dirs"); + if (extDirs != null) { + String[] dirs = extDirs.split(File.pathSeparator); + for (String dir : dirs) { + File nashorn = new File(dir, "nashorn.jar"); + if (nashorn.exists()) { + loadJar(nashorn); + System.out.println("扩展目录发现 Nashorn 已加载完成!"); + } + } + } else { + loadLocalNashorn(); + } + } + + private static void loadLocalNashorn() throws Throwable { + File libRootFile = new File("plugins/MiaoNashorn", "libs"); + libRootFile.mkdirs(); + log("从云端加载 Nashorn 请稍候..."); + String libRoot = libRootFile.getCanonicalPath(); + downloadJar(libRoot, "org.openjdk.nashorn", "nashorn-core", "15.2"); + downloadJar(libRoot, "org.ow2.asm", "asm", "9.1"); + downloadJar(libRoot, "org.ow2.asm", "asm-commons", "9.1"); + downloadJar(libRoot, "org.ow2.asm", "asm-tree", "9.1"); + downloadJar(libRoot, "org.ow2.asm", "asm-util", "9.1"); + log("云端 Nashorn 已加载完成!"); + } + + private static void loadJar(File file) throws Throwable { + addURLMethodHandle.invoke(ucp, file.toURI().toURL()); + } + + private static void downloadJar(String engineRoot, String groupId, String artifactId, String version) throws Throwable { + File lib = new File(engineRoot, artifactId + ".jar"); + if (!lib.exists()) { + log("正在下载类库 %s 版本 %s 请稍候...", artifactId, version); + Files.copy(new URL(MavenRepo + + String.format("/%1$s/%2$s/%3$s/%2$s-%3$s.jar", + groupId.replace(".", "/"), + artifactId, + version) + ).openStream(), + lib.toPath(), + StandardCopyOption.REPLACE_EXISTING); + } + loadJar(lib); + } +} diff --git a/src/main/java/pw/yumc/MiaoNashorn/MiaoNashornBukkit.java b/src/main/java/pw/yumc/MiaoNashorn/MiaoNashornBukkit.java new file mode 100644 index 0000000..a847a0b --- /dev/null +++ b/src/main/java/pw/yumc/MiaoNashorn/MiaoNashornBukkit.java @@ -0,0 +1,15 @@ +package pw.yumc.MiaoNashorn; + +import org.bukkit.plugin.java.JavaPlugin; + +/** + * 喵式脚本 + * + * @author 喵♂呜 + * @since 2016年8月29日 上午7:50:39 + */ +public class MiaoNashornBukkit extends JavaPlugin { + public MiaoNashornBukkit() { + new MiaoNashorn(); + } +} diff --git a/src/main/java/pw/yumc/MiaoNashorn/MiaoNashornBungee.java b/src/main/java/pw/yumc/MiaoNashorn/MiaoNashornBungee.java new file mode 100644 index 0000000..01b3b07 --- /dev/null +++ b/src/main/java/pw/yumc/MiaoNashorn/MiaoNashornBungee.java @@ -0,0 +1,15 @@ +package pw.yumc.MiaoNashorn; + +import net.md_5.bungee.api.plugin.Plugin; + +/** + * Created with IntelliJ IDEA + * + * @author MiaoWoo + * Created on 2020/1/14 16:02. + */ +public class MiaoNashornBungee extends Plugin { + public MiaoNashornBungee() { + new MiaoNashorn(); + } +} diff --git a/src/main/java/pw/yumc/MiaoNashorn/MiaoNashornNukkit.java b/src/main/java/pw/yumc/MiaoNashorn/MiaoNashornNukkit.java new file mode 100644 index 0000000..201a630 --- /dev/null +++ b/src/main/java/pw/yumc/MiaoNashorn/MiaoNashornNukkit.java @@ -0,0 +1,12 @@ +package pw.yumc.MiaoNashorn; + +import cn.nukkit.plugin.PluginBase; + +/** + * @author MiaoWoo + */ +public class MiaoNashornNukkit extends PluginBase { + public MiaoNashornNukkit() { + new MiaoNashorn(); + } +} diff --git a/src/main/java/pw/yumc/MiaoNashorn/MiaoNashornSponge.java b/src/main/java/pw/yumc/MiaoNashorn/MiaoNashornSponge.java new file mode 100644 index 0000000..cc552db --- /dev/null +++ b/src/main/java/pw/yumc/MiaoNashorn/MiaoNashornSponge.java @@ -0,0 +1,16 @@ +package pw.yumc.MiaoNashorn; + +import org.spongepowered.api.plugin.Plugin; + +/** + * Created with IntelliJ IDEA + * + * @author 喵♂呜 + * Created on 2017/10/25 20:35. + */ +@Plugin(id = "miaonashorn", name = "MiaoNashorn", version = "1.0", authors = "MiaoWoo") +public class MiaoNashornSponge { + public MiaoNashornSponge() { + new MiaoNashorn(); + } +} diff --git a/src/main/resources/bungee.yml b/src/main/resources/bungee.yml new file mode 100644 index 0000000..00d1d10 --- /dev/null +++ b/src/main/resources/bungee.yml @@ -0,0 +1,5 @@ +name: ${project.artifactId} +description: ${project.description} +main: ${project.groupId}.${project.artifactId}.${project.artifactId}Bungee +version: ${project.version} +author: MiaoWoo \ No newline at end of file diff --git a/src/main/resources/nukkit.yml b/src/main/resources/nukkit.yml new file mode 100644 index 0000000..f596479 --- /dev/null +++ b/src/main/resources/nukkit.yml @@ -0,0 +1,6 @@ +name: ${project.artifactId} +description: ${project.description} +main: ${project.groupId}.${project.artifactId}.${project.artifactId}Nukkit +version: ${project.version} +api: "1.0.0" +author: MiaoWoo diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..15f397e --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,8 @@ +name: ${project.artifactId} +description: ${project.description} +main: ${project.groupId}.${project.artifactId}.${project.artifactId}Bukkit +version: ${project.version} +api-version: 1.13 +author: MiaoWoo +website: ${ciManagement.url} +load: STARTUP