mirror of
				https://e.coding.net/circlecloud/YumCore.git
				synced 2025-11-03 23:06:02 +00:00 
			
		
		
		
	fix: 修复MiaoScriptEngine在MOD服下未找到引擎的问题
Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
		@@ -116,7 +116,7 @@ public class Log {
 | 
			
		||||
     */
 | 
			
		||||
    public static void d(String msg, Object... object) {
 | 
			
		||||
        if (debug) {
 | 
			
		||||
            logger.info(String.format(msg, object));
 | 
			
		||||
            logger.info(String.format("[DEBUG] " + msg, object));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -39,13 +39,13 @@ public class P {
 | 
			
		||||
            Field field = pluginClassLoader.getClass().getDeclaredField("plugin");
 | 
			
		||||
            field.setAccessible(true);
 | 
			
		||||
            instance = (JavaPlugin) field.get(pluginClassLoader);
 | 
			
		||||
            try {
 | 
			
		||||
                getInjectConfigMethod = instance.getClass().getMethod("get" + instance.getName() + "Config");
 | 
			
		||||
            } catch (NoSuchMethodException e) {
 | 
			
		||||
                Log.d("配置方法 get%sConfig 未找到 将返回getConfig 调用结果!", instance.getName());
 | 
			
		||||
            }
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            Log.d(e);
 | 
			
		||||
        }
 | 
			
		||||
        try {
 | 
			
		||||
            getInjectConfigMethod = instance.getClass().getMethod("get" + instance.getName() + "Config");
 | 
			
		||||
        } catch (NoSuchMethodException e) {
 | 
			
		||||
            Log.d("配置方法 get%sConfig 未找到 将返回getConfig 调用结果!", instance.getName());
 | 
			
		||||
            Log.d("P 类初始化失败 %s:%s", e.getClass().getName(), e.getMessage());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -24,7 +24,7 @@ public class MiaoScriptEngine implements ScriptEngine, Invocable {
 | 
			
		||||
    private ScriptEngine engine;
 | 
			
		||||
 | 
			
		||||
    static {
 | 
			
		||||
        manager = new ScriptEngineManager();
 | 
			
		||||
        manager = new ScriptEngineManager(ClassLoader.getSystemClassLoader());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void setBindings(Bindings bindings) {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,6 @@
 | 
			
		||||
package pw.yumc.YumCore.update;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.io.FileNotFoundException;
 | 
			
		||||
import java.io.FileReader;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.net.URL;
 | 
			
		||||
@@ -10,8 +9,6 @@ import java.nio.file.StandardCopyOption;
 | 
			
		||||
 | 
			
		||||
import javax.script.ScriptException;
 | 
			
		||||
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
import pw.yumc.YumCore.bungee.Log;
 | 
			
		||||
import pw.yumc.YumCore.engine.MiaoScriptEngine;
 | 
			
		||||
 | 
			
		||||
@@ -41,9 +38,4 @@ public class HotSwapTask {
 | 
			
		||||
            Log.d("热更新脚本加载失败!", e);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void test() throws FileNotFoundException, ScriptException {
 | 
			
		||||
        engine.eval(new FileReader(new File("src/main/resources/hotswap.js")));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										36
									
								
								src/test/java/pw/yumc/YumCore/update/HotSwapTaskTest.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								src/test/java/pw/yumc/YumCore/update/HotSwapTaskTest.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,36 @@
 | 
			
		||||
package pw.yumc.YumCore.update;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.io.FileNotFoundException;
 | 
			
		||||
import java.io.FileReader;
 | 
			
		||||
 | 
			
		||||
import javax.script.ScriptException;
 | 
			
		||||
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
import pw.yumc.YumCore.engine.MiaoScriptEngine;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Created with IntelliJ IDEA
 | 
			
		||||
 *
 | 
			
		||||
 * @author 喵♂呜
 | 
			
		||||
 * Created on 2017/8/29 20:24.
 | 
			
		||||
 */
 | 
			
		||||
public class HotSwapTaskTest {
 | 
			
		||||
    private MiaoScriptEngine engine;
 | 
			
		||||
 | 
			
		||||
    public HotSwapTaskTest() {
 | 
			
		||||
        this.engine = new MiaoScriptEngine();
 | 
			
		||||
        engine.put("$", this);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void test() throws FileNotFoundException, ScriptException {
 | 
			
		||||
        engine.eval(new FileReader(new File("src/test/resources/hotswap.js")));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testClient() throws FileNotFoundException, ScriptException {
 | 
			
		||||
        engine.eval(new FileReader(new File("src/test/resources/nio-client.js")));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										115
									
								
								src/test/java/pw/yumc/YumCore/update/NIOServer.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										115
									
								
								src/test/java/pw/yumc/YumCore/update/NIOServer.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,115 @@
 | 
			
		||||
package pw.yumc.YumCore.update;
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.net.InetSocketAddress;
 | 
			
		||||
import java.nio.ByteBuffer;
 | 
			
		||||
import java.nio.channels.SelectionKey;
 | 
			
		||||
import java.nio.channels.Selector;
 | 
			
		||||
import java.nio.channels.ServerSocketChannel;
 | 
			
		||||
import java.nio.channels.SocketChannel;
 | 
			
		||||
import java.util.Iterator;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * NIO服务端
 | 
			
		||||
 *
 | 
			
		||||
 * @author 小路
 | 
			
		||||
 */
 | 
			
		||||
public class NIOServer {
 | 
			
		||||
    //通道管理器
 | 
			
		||||
    private Selector selector;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 获得一个ServerSocket通道,并对该通道做一些初始化的工作
 | 
			
		||||
     *
 | 
			
		||||
     * @param port
 | 
			
		||||
     *         绑定的端口号
 | 
			
		||||
     * @throws IOException
 | 
			
		||||
     */
 | 
			
		||||
    public void initServer(int port) throws IOException {
 | 
			
		||||
        // 获得一个ServerSocket通道
 | 
			
		||||
        ServerSocketChannel serverChannel = ServerSocketChannel.open();
 | 
			
		||||
        // 设置通道为非阻塞
 | 
			
		||||
        serverChannel.configureBlocking(false);
 | 
			
		||||
        // 将该通道对应的ServerSocket绑定到port端口
 | 
			
		||||
        serverChannel.socket().bind(new InetSocketAddress(port));
 | 
			
		||||
        // 获得一个通道管理器
 | 
			
		||||
        this.selector = Selector.open();
 | 
			
		||||
        //将通道管理器和该通道绑定,并为该通道注册SelectionKey.OP_ACCEPT事件,注册该事件后,
 | 
			
		||||
        //当该事件到达时,selector.select()会返回,如果该事件没到达selector.select()会一直阻塞。
 | 
			
		||||
        serverChannel.register(selector, SelectionKey.OP_ACCEPT);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 采用轮询的方式监听selector上是否有需要处理的事件,如果有,则进行处理
 | 
			
		||||
     *
 | 
			
		||||
     * @throws IOException
 | 
			
		||||
     */
 | 
			
		||||
    @SuppressWarnings("unchecked")
 | 
			
		||||
    public void listen() throws IOException {
 | 
			
		||||
        System.out.println("服务端启动成功!");
 | 
			
		||||
        // 轮询访问selector
 | 
			
		||||
        while (!Thread.currentThread().isInterrupted()) {
 | 
			
		||||
            //当注册的事件到达时,方法返回;否则,该方法会一直阻塞
 | 
			
		||||
            selector.select();
 | 
			
		||||
            // 获得selector中选中的项的迭代器,选中的项为注册的事件
 | 
			
		||||
            Iterator ite = this.selector.selectedKeys().iterator();
 | 
			
		||||
            while (ite.hasNext()) {
 | 
			
		||||
                SelectionKey key = (SelectionKey) ite.next();
 | 
			
		||||
                // 删除已选的key,以防重复处理
 | 
			
		||||
                ite.remove();
 | 
			
		||||
                // 客户端请求连接事件
 | 
			
		||||
                if (key.isAcceptable()) {
 | 
			
		||||
                    ServerSocketChannel server = (ServerSocketChannel) key
 | 
			
		||||
                            .channel();
 | 
			
		||||
                    // 获得和客户端连接的通道
 | 
			
		||||
                    SocketChannel channel = server.accept();
 | 
			
		||||
                    // 设置成非阻塞
 | 
			
		||||
                    channel.configureBlocking(false);
 | 
			
		||||
 | 
			
		||||
                    //在这里可以给客户端发送信息哦
 | 
			
		||||
                    channel.write(ByteBuffer.wrap("向客户端发送了一条信息".getBytes()));
 | 
			
		||||
                    //在和客户端连接成功之后,为了可以接收到客户端的信息,需要给通道设置读的权限。
 | 
			
		||||
                    channel.register(this.selector, SelectionKey.OP_READ);
 | 
			
		||||
 | 
			
		||||
                    // 获得了可读的事件
 | 
			
		||||
                } else if (key.isReadable()) {
 | 
			
		||||
                    try {
 | 
			
		||||
                        read(key);
 | 
			
		||||
                    } catch (Exception ignored) {
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 处理读取客户端发来的信息 的事件
 | 
			
		||||
     *
 | 
			
		||||
     * @param key
 | 
			
		||||
     * @throws IOException
 | 
			
		||||
     */
 | 
			
		||||
    public void read(SelectionKey key) throws IOException {
 | 
			
		||||
        // 服务器可读取消息:得到事件发生的Socket通道
 | 
			
		||||
        SocketChannel channel = (SocketChannel) key.channel();
 | 
			
		||||
        // 创建读取的缓冲区
 | 
			
		||||
        ByteBuffer buffer = ByteBuffer.allocate(10);
 | 
			
		||||
        channel.read(buffer);
 | 
			
		||||
        byte[] data = buffer.array();
 | 
			
		||||
        String msg = new String(data).trim();
 | 
			
		||||
        System.out.println("服务端收到信息:" + msg);
 | 
			
		||||
        ByteBuffer outBuffer = ByteBuffer.wrap(msg.getBytes());
 | 
			
		||||
        channel.write(outBuffer);// 将消息回送给客户端
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 启动服务端测试
 | 
			
		||||
     *
 | 
			
		||||
     * @throws IOException
 | 
			
		||||
     */
 | 
			
		||||
    public static void main(String[] args) throws IOException {
 | 
			
		||||
        NIOServer server = new NIOServer();
 | 
			
		||||
        server.initServer(8233);
 | 
			
		||||
        server.listen();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										31
									
								
								src/test/resources/nio-client.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								src/test/resources/nio-client.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,31 @@
 | 
			
		||||
var SocketChannel = java.nio.channels.SocketChannel;
 | 
			
		||||
var Selector = java.nio.channels.Selector;
 | 
			
		||||
var SelectionKey = java.nio.channels.SelectionKey;
 | 
			
		||||
var InetSocketAddress = java.net.InetSocketAddress;
 | 
			
		||||
var ByteBuffer = java.nio.ByteBuffer;
 | 
			
		||||
var Iterator = java.util.Iterator;
 | 
			
		||||
 | 
			
		||||
var channel = SocketChannel.open();
 | 
			
		||||
channel.configureBlocking(false);
 | 
			
		||||
var selector = Selector.open();
 | 
			
		||||
channel.connect(new InetSocketAddress('127.0.0.1', 8233));
 | 
			
		||||
channel.register(selector, SelectionKey.OP_CONNECT);
 | 
			
		||||
 | 
			
		||||
while (true) {
 | 
			
		||||
    selector.select();
 | 
			
		||||
    var ite = selector.selectedKeys().iterator();
 | 
			
		||||
    while (ite.hasNext()) {
 | 
			
		||||
        var key = ite.next();
 | 
			
		||||
        ite.remove();
 | 
			
		||||
        if (key.isConnectable()) {
 | 
			
		||||
            var client = key.channel();
 | 
			
		||||
            if (client.isConnectionPending()) {
 | 
			
		||||
                client.finishConnect();
 | 
			
		||||
            }
 | 
			
		||||
            client.configureBlocking(false);
 | 
			
		||||
            client.write(ByteBuffer.wrap("向服务端发送了一条信息".getBytes()));
 | 
			
		||||
            client.register(selector, SelectionKey.OP_READ);
 | 
			
		||||
        } else if (key.isReadable()) {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user