18
									
								
								pom.xml
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								pom.xml
									
									
									
									
									
								
							@@ -2,7 +2,7 @@
 | 
				
			|||||||
    <modelVersion>4.0.0</modelVersion>
 | 
					    <modelVersion>4.0.0</modelVersion>
 | 
				
			||||||
    <groupId>pw.yumc</groupId>
 | 
					    <groupId>pw.yumc</groupId>
 | 
				
			||||||
    <artifactId>MiaoScript</artifactId>
 | 
					    <artifactId>MiaoScript</artifactId>
 | 
				
			||||||
    <version>0.6.6</version>
 | 
					    <version>0.7.0</version>
 | 
				
			||||||
    <developers>
 | 
					    <developers>
 | 
				
			||||||
        <developer>
 | 
					        <developer>
 | 
				
			||||||
            <id>502647092</id>
 | 
					            <id>502647092</id>
 | 
				
			||||||
@@ -53,6 +53,7 @@
 | 
				
			|||||||
    <properties>
 | 
					    <properties>
 | 
				
			||||||
        <env.GIT_COMMIT>DEV</env.GIT_COMMIT>
 | 
					        <env.GIT_COMMIT>DEV</env.GIT_COMMIT>
 | 
				
			||||||
        <update.changes>
 | 
					        <update.changes>
 | 
				
			||||||
 | 
					            §620-05-28 §afeat: 新增 Spring 的支持;
 | 
				
			||||||
            §620-05-02 §afeat: 调整 scope 为 @ccms;
 | 
					            §620-05-02 §afeat: 调整 scope 为 @ccms;
 | 
				
			||||||
            §620-04-10 §afeat: 默认从 classpath 加载内建的js模块;
 | 
					            §620-04-10 §afeat: 默认从 classpath 加载内建的js模块;
 | 
				
			||||||
            §620-04-07 §afeat: 默认初始化 内建 nodejs 模块;
 | 
					            §620-04-07 §afeat: 默认初始化 内建 nodejs 模块;
 | 
				
			||||||
@@ -180,11 +181,13 @@
 | 
				
			|||||||
            <groupId>org.spigotmc</groupId>
 | 
					            <groupId>org.spigotmc</groupId>
 | 
				
			||||||
            <artifactId>spigot-api</artifactId>
 | 
					            <artifactId>spigot-api</artifactId>
 | 
				
			||||||
            <version>1.15.2-R0.1-SNAPSHOT</version>
 | 
					            <version>1.15.2-R0.1-SNAPSHOT</version>
 | 
				
			||||||
 | 
					            <scope>compile</scope>
 | 
				
			||||||
        </dependency>
 | 
					        </dependency>
 | 
				
			||||||
        <dependency>
 | 
					        <dependency>
 | 
				
			||||||
            <groupId>org.spongepowered</groupId>
 | 
					            <groupId>org.spongepowered</groupId>
 | 
				
			||||||
            <artifactId>spongeapi</artifactId>
 | 
					            <artifactId>spongeapi</artifactId>
 | 
				
			||||||
            <version>7.1.0</version>
 | 
					            <version>7.1.0</version>
 | 
				
			||||||
 | 
					            <scope>compile</scope>
 | 
				
			||||||
        </dependency>
 | 
					        </dependency>
 | 
				
			||||||
        <dependency>
 | 
					        <dependency>
 | 
				
			||||||
            <groupId>net.md-5</groupId>
 | 
					            <groupId>net.md-5</groupId>
 | 
				
			||||||
@@ -195,6 +198,19 @@
 | 
				
			|||||||
            <groupId>cn.nukkit</groupId>
 | 
					            <groupId>cn.nukkit</groupId>
 | 
				
			||||||
            <artifactId>nukkit</artifactId>
 | 
					            <artifactId>nukkit</artifactId>
 | 
				
			||||||
            <version>1.0-SNAPSHOT</version>
 | 
					            <version>1.0-SNAPSHOT</version>
 | 
				
			||||||
 | 
					            <scope>compile</scope>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>org.springframework</groupId>
 | 
				
			||||||
 | 
					            <artifactId>spring-websocket</artifactId>
 | 
				
			||||||
 | 
					            <version>5.2.6.RELEASE</version>
 | 
				
			||||||
 | 
					            <scope>compile</scope>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>org.apache.tomcat</groupId>
 | 
				
			||||||
 | 
					            <artifactId>tomcat-websocket</artifactId>
 | 
				
			||||||
 | 
					            <version>9.0.35</version>
 | 
				
			||||||
 | 
					            <scope>compile</scope>
 | 
				
			||||||
        </dependency>
 | 
					        </dependency>
 | 
				
			||||||
    </dependencies>
 | 
					    </dependencies>
 | 
				
			||||||
</project>
 | 
					</project>
 | 
				
			||||||
@@ -21,7 +21,7 @@ public class Base {
 | 
				
			|||||||
        this.instance = instance;
 | 
					        this.instance = instance;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public Class getClass(String name) throws ClassNotFoundException {
 | 
					    public Class<?> getClass(String name) throws ClassNotFoundException {
 | 
				
			||||||
        return Class.forName(name);
 | 
					        return Class.forName(name);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -29,7 +29,7 @@ public class Base {
 | 
				
			|||||||
        return this.instance;
 | 
					        return this.instance;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public Class getProxyClass() {
 | 
					    public Class<?> getProxyClass() {
 | 
				
			||||||
        return ProxyClass.class;
 | 
					        return ProxyClass.class;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										34
									
								
								src/main/java/pw/yumc/MiaoScript/MiaoScriptSpring.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								src/main/java/pw/yumc/MiaoScript/MiaoScriptSpring.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,34 @@
 | 
				
			|||||||
 | 
					package pw.yumc.MiaoScript;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import lombok.SneakyThrows;
 | 
				
			||||||
 | 
					import lombok.extern.slf4j.Slf4j;
 | 
				
			||||||
 | 
					import org.springframework.context.ApplicationContext;
 | 
				
			||||||
 | 
					import org.springframework.context.annotation.Bean;
 | 
				
			||||||
 | 
					import org.springframework.stereotype.Component;
 | 
				
			||||||
 | 
					import org.springframework.web.socket.server.standard.ServerEndpointExporter;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.io.File;
 | 
				
			||||||
 | 
					import javax.annotation.PreDestroy;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Slf4j
 | 
				
			||||||
 | 
					@Component
 | 
				
			||||||
 | 
					public class MiaoScriptSpring {
 | 
				
			||||||
 | 
					    private ScriptEngine engine;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    @SneakyThrows
 | 
				
			||||||
 | 
					    public ScriptEngine buildScriptEngine(ApplicationContext applicationContext) {
 | 
				
			||||||
 | 
					        return new ScriptEngine(new File("MiaoScript").getCanonicalPath(), log, applicationContext);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public ServerEndpointExporter serverEndpointExporter() {
 | 
				
			||||||
 | 
					        return new ServerEndpointExporter();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @PreDestroy
 | 
				
			||||||
 | 
					    public void disableEngine() {
 | 
				
			||||||
 | 
					        engine.disableEngine();
 | 
				
			||||||
 | 
					        engine = null;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1,6 +1,5 @@
 | 
				
			|||||||
package pw.yumc.MiaoScript;
 | 
					package pw.yumc.MiaoScript;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.io.InputStreamReader;
 | 
					 | 
				
			||||||
import java.nio.file.Files;
 | 
					import java.nio.file.Files;
 | 
				
			||||||
import java.nio.file.Path;
 | 
					import java.nio.file.Path;
 | 
				
			||||||
import java.nio.file.Paths;
 | 
					import java.nio.file.Paths;
 | 
				
			||||||
@@ -12,8 +11,7 @@ import lombok.SneakyThrows;
 | 
				
			|||||||
/**
 | 
					/**
 | 
				
			||||||
 * Created with IntelliJ IDEA
 | 
					 * Created with IntelliJ IDEA
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * @author 喵♂呜
 | 
					 * @author 喵♂呜 Created on 2017/10/25 21:01.
 | 
				
			||||||
 * Created on 2017/10/25 21:01.
 | 
					 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
public class ScriptEngine {
 | 
					public class ScriptEngine {
 | 
				
			||||||
    private String root;
 | 
					    private String root;
 | 
				
			||||||
@@ -30,7 +28,8 @@ public class ScriptEngine {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @SneakyThrows
 | 
					    @SneakyThrows
 | 
				
			||||||
    public void enableEngine() {
 | 
					    public synchronized void enableEngine() {
 | 
				
			||||||
 | 
					        if (this.engine == null) {
 | 
				
			||||||
            this.engine = new MiaoScriptEngine(manager, "nashorn");
 | 
					            this.engine = new MiaoScriptEngine(manager, "nashorn");
 | 
				
			||||||
            this.engine.put("base", this.base);
 | 
					            this.engine.put("base", this.base);
 | 
				
			||||||
            this.engine.put("ScriptEngineContextHolder", this);
 | 
					            this.engine.put("ScriptEngineContextHolder", this);
 | 
				
			||||||
@@ -43,12 +42,15 @@ public class ScriptEngine {
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
            engine.invokeFunction("boot", root, logger);
 | 
					            engine.invokeFunction("boot", root, logger);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @SneakyThrows
 | 
					    @SneakyThrows
 | 
				
			||||||
    public void disableEngine() {
 | 
					    public synchronized void disableEngine() {
 | 
				
			||||||
 | 
					        if (this.engine != null) {
 | 
				
			||||||
            this.engine.invokeFunction("engineDisable");
 | 
					            this.engine.invokeFunction("engineDisable");
 | 
				
			||||||
            this.engine = null;
 | 
					            this.engine = null;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public MiaoScriptEngine getEngine() {
 | 
					    public MiaoScriptEngine getEngine() {
 | 
				
			||||||
        return engine;
 | 
					        return engine;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -0,0 +1,70 @@
 | 
				
			|||||||
 | 
					package pw.yumc.MiaoScript.websocket;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import lombok.SneakyThrows;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.springframework.beans.BeansException;
 | 
				
			||||||
 | 
					import org.springframework.context.ApplicationContext;
 | 
				
			||||||
 | 
					import org.springframework.context.ApplicationContextAware;
 | 
				
			||||||
 | 
					import org.springframework.stereotype.Component;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import javax.websocket.*;
 | 
				
			||||||
 | 
					import javax.websocket.server.ServerEndpoint;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Component
 | 
				
			||||||
 | 
					@ServerEndpoint("/ws/")
 | 
				
			||||||
 | 
					public class WebSocketServer implements ApplicationContextAware {
 | 
				
			||||||
 | 
					    private static ApplicationContext context;
 | 
				
			||||||
 | 
					    private WebSocketServerProxy proxy;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private boolean checkProxy(Session session) {
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            if (this.proxy == null) {
 | 
				
			||||||
 | 
					                this.proxy = context.getBean(WebSocketServerProxy.class);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            return true;
 | 
				
			||||||
 | 
					        } catch (Exception ex) {
 | 
				
			||||||
 | 
					            try {
 | 
				
			||||||
 | 
					                session.close();
 | 
				
			||||||
 | 
					            } catch (Exception ignore) {
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            return false;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @OnOpen
 | 
				
			||||||
 | 
					    @SneakyThrows
 | 
				
			||||||
 | 
					    public void onOpen(Session session, EndpointConfig config) {
 | 
				
			||||||
 | 
					        if (this.checkProxy(session)) {
 | 
				
			||||||
 | 
					            this.proxy.onOpen(session, config);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @OnMessage
 | 
				
			||||||
 | 
					    @SneakyThrows
 | 
				
			||||||
 | 
					    public void onMessage(Session session, String message) {
 | 
				
			||||||
 | 
					        if (this.checkProxy(session)) {
 | 
				
			||||||
 | 
					            this.proxy.onMessage(session, message);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @OnClose
 | 
				
			||||||
 | 
					    @SneakyThrows
 | 
				
			||||||
 | 
					    public void onClose(Session session, CloseReason reason) {
 | 
				
			||||||
 | 
					        if (this.checkProxy(session)) {
 | 
				
			||||||
 | 
					            this.proxy.onClose(session, reason);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @OnError
 | 
				
			||||||
 | 
					    @SneakyThrows
 | 
				
			||||||
 | 
					    public void onError(Session session, Throwable error) {
 | 
				
			||||||
 | 
					        if (this.checkProxy(session)) {
 | 
				
			||||||
 | 
					            this.proxy.onError(session, error);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
 | 
				
			||||||
 | 
					        context = ctx;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -0,0 +1,15 @@
 | 
				
			|||||||
 | 
					package pw.yumc.MiaoScript.websocket;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import javax.websocket.CloseReason;
 | 
				
			||||||
 | 
					import javax.websocket.EndpointConfig;
 | 
				
			||||||
 | 
					import javax.websocket.Session;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public interface WebSocketServerProxy {
 | 
				
			||||||
 | 
					    void onOpen(Session session, EndpointConfig config);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    void onMessage(Session session, String message);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    void onClose(Session session, CloseReason reason);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    void onError(Session session, Throwable error);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -32,11 +32,9 @@ var global = this;
 | 
				
			|||||||
        }, "MiaoScript thread").start()
 | 
					        }, "MiaoScript thread").start()
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    var pluginYml;
 | 
					 | 
				
			||||||
    function checkClassLoader() {
 | 
					    function checkClassLoader() {
 | 
				
			||||||
        var classLoader = java.lang.Thread.currentThread().contextClassLoader;
 | 
					        var classLoader = java.lang.Thread.currentThread().contextClassLoader;
 | 
				
			||||||
        pluginYml = classLoader.getResource("plugin.yml");
 | 
					        if (classLoader.getResource("bios.js") === null) {
 | 
				
			||||||
        if (pluginYml === null) {
 | 
					 | 
				
			||||||
            throw Error("Error class loader: " + classLoader.class.name + " Please contact the author MiaoWoo!");
 | 
					            throw Error("Error class loader: " + classLoader.class.name + " Please contact the author MiaoWoo!");
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            log.info("Class loader compatible: " + classLoader.class.name);
 | 
					            log.info("Class loader compatible: " + classLoader.class.name);
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user