Commit e342261e authored by liujiahua123123's avatar liujiahua123123

MN

parent 24ef0c1d
...@@ -4,22 +4,30 @@ import lombok.Getter; ...@@ -4,22 +4,30 @@ import lombok.Getter;
import net.mamoe.mirai.event.MiraiEventManager; import net.mamoe.mirai.event.MiraiEventManager;
import net.mamoe.mirai.event.events.server.ServerDisableEvent; import net.mamoe.mirai.event.events.server.ServerDisableEvent;
import net.mamoe.mirai.event.events.server.ServerEnableEvent; import net.mamoe.mirai.event.events.server.ServerEnableEvent;
import net.mamoe.mirai.network.Network; import net.mamoe.mirai.network.MiraiNetwork;
import net.mamoe.mirai.task.MiraiTaskManager; import net.mamoe.mirai.task.MiraiTaskManager;
import net.mamoe.mirai.utils.LoggerTextFormat; import net.mamoe.mirai.utils.LoggerTextFormat;
import net.mamoe.mirai.utils.MiraiLogger; import net.mamoe.mirai.utils.MiraiLogger;
import net.mamoe.mirai.utils.config.MiraiConfig;
import net.mamoe.mirai.utils.config.MiraiMapSection;
import java.awt.*; import java.awt.*;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.HashMap;
import java.util.Scanner;
public class MiraiServer { public class MiraiServer {
@Getter @Getter
private static MiraiServer instance; private static MiraiServer instance;
@Getter //mirai version //mirai version
private final static String miraiVersion = "1.0.0"; private final static String MIRAI_VERSION = "1.0.0";
//qq version
private final static String QQ_VERSION = "4.9.0";
@Getter //is running under UNIX @Getter //is running under UNIX
private boolean unix; private boolean unix;
...@@ -34,6 +42,8 @@ public class MiraiServer { ...@@ -34,6 +42,8 @@ public class MiraiServer {
@Getter @Getter
MiraiLogger logger; MiraiLogger logger;
MiraiConfig setting;
protected MiraiServer(){ protected MiraiServer(){
instance = this; instance = this;
...@@ -63,28 +73,61 @@ public class MiraiServer { ...@@ -63,28 +73,61 @@ public class MiraiServer {
this.eventManager = MiraiEventManager.getInstance(); this.eventManager = MiraiEventManager.getInstance();
this.taskManager = MiraiTaskManager.getInstance(); this.taskManager = MiraiTaskManager.getInstance();
this.getLogger().log(LoggerTextFormat.SKY_BLUE + "About to run Mirai (" + MiraiServer.getMiraiVersion() + ") under " + (isUnix()?"unix":"windows") ); this.getLogger().log(LoggerTextFormat.SKY_BLUE + "About to run Mirai (" + MiraiServer.MIRAI_VERSION + ") under " + (isUnix()?"unix":"windows") );
this.getLogger().log("Loading data under " + LoggerTextFormat.GREEN + this.parentFolder); this.getLogger().log("Loading data under " + LoggerTextFormat.GREEN + this.parentFolder);
File setting = new File(this.parentFolder + "/Mirai.ini");
/*
try { if(!setting.exists()){
Network.start(Network.getAvailablePort()); this.initSetting(setting);
} catch (InterruptedException | IOException e) { }else {
e.printStackTrace(); this.setting = new MiraiConfig(setting);
this.shutdown(); }
int port = this.setting.getMapSection("network").getInt("port");
MiraiNetwork.start(port);
Thread.yield();
if(MiraiNetwork.getLastError()!=null){
this.getLogger().log(LoggerTextFormat.RED + "an error occurred when staring network layer");
this.shutdown();
} }
*/ this.getLogger().log(LoggerTextFormat.SKY_BLUE + "Listening on port " + port);
}
public void initSetting(File setting){
this.getLogger().log(LoggerTextFormat.SKY_BLUE + "Thanks for using Mirai");
this.getLogger().log(LoggerTextFormat.SKY_BLUE + "initializing Settings");
try {
if(setting.createNewFile()){
this.getLogger().log(LoggerTextFormat.SKY_BLUE + "Mirai Config Created");
}
} catch (IOException e) {
e.printStackTrace();
}
this.setting = new MiraiConfig(setting);
MiraiMapSection network = this.setting.getMapSection("network");
network.put("port",19139);
MiraiMapSection qqs = this.setting.getMapSection("qq");
Scanner scanner = new Scanner(System.in);
this.getLogger().log(LoggerTextFormat.SKY_BLUE + "input one " + LoggerTextFormat.RED + " QQ number " + LoggerTextFormat.SKY_BLUE +"for default robot");
this.getLogger().log(LoggerTextFormat.SKY_BLUE + "输入用于默认机器人的QQ号");
long qqNumber = scanner.nextLong();
this.getLogger().log(LoggerTextFormat.SKY_BLUE + "input the password for that QQ account");
this.getLogger().log(LoggerTextFormat.SKY_BLUE + "输入该QQ号对应密码");
String qqPassword = scanner.next();
this.getLogger().log(LoggerTextFormat.SKY_BLUE + "initialized; changing can be made in config file: " + setting.toString());
qqs.put(String.valueOf(qqNumber),qqPassword);
this.setting.save();
} }
private void onEnable(){ private void onEnable(){
this.eventManager.boardcastEvent(new ServerEnableEvent()); this.eventManager.boardcastEvent(new ServerEnableEvent());
this.enabled = true; this.enabled = true;
this.getLogger().log(LoggerTextFormat.GREEN + "Server enabled; Welcome to Mirai");
this.getLogger().log( "Mirai Version=" + MiraiServer.MIRAI_VERSION + " QQ Version=" + MiraiServer.QQ_VERSION);
} }
} }
package net.mamoe.mirai.network; package net.mamoe.mirai.network;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.bytes.ByteArrayDecoder;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import lombok.Getter;
import net.mamoe.mirai.MiraiServer;
import net.mamoe.mirai.event.events.server.ServerDisableEvent;
import java.io.IOException;
import java.net.ServerSocket;
public class MiraiNetwork { public class MiraiNetwork {
private static ServerBootstrap server;
private static Thread thread;
@Getter
private static volatile Throwable lastError = null;
public static void start(int port){
thread = new Thread(() -> {
if (server != null) {
throw new RuntimeException("there is already a ServerBootstrap instance");
}
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
server = new ServerBootstrap();
server.group(bossGroup, workerGroup);
server.channel(NioServerSocketChannel.class);
//b.option(ChannelOption.SO_BACKLOG, 100);
//b.handler(new LoggingHandler(LogLevel.INFO));
server.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("bytesDecoder", new ByteArrayDecoder());
pipeline.addLast("bytesEncoder", new ByteArrayEncoder());
pipeline.addLast("handler", new NetworkPacketHandler());
}
});
server.bind(port).sync().channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
lastError = e;
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
});
thread.start();
MiraiServer.getInstance().getEventManager().onEvent(ServerDisableEvent.class).setHandler(a -> {
thread.interrupt();
});
}
public static int getAvailablePort() throws IOException {
ServerSocket serverSocket = new ServerSocket(0); //读取空闲的可用端口
int port = serverSocket.getLocalPort();
serverSocket.close();
return port;
}
} }
package net.mamoe.mirai.network;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.bytes.ByteArrayDecoder;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import java.io.IOException;
import java.net.ServerSocket;
/**
* JPRE 网络层启动器.
* 本类用于启动网络服务器. 包接受器请参考 {@link NetworkPacketHandler}
* (插件请不要使用本类, 用了也会因端口占用而抛出异常)
*
* @author Him188 @ JPRE Project
*/
public final class Network {
private static ServerBootstrap server;
/**
* 启动网络服务器. 会阻塞线程直到关闭网络服务器.
*
* @param port 端口号
* @throws RuntimeException 服务器已经启动时抛出
*/
public static void start(int port) throws InterruptedException {
if (server != null) {
throw new RuntimeException("there is already a ServerBootstrap instance");
}
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
server = new ServerBootstrap();
server.group(bossGroup, workerGroup);
server.channel(NioServerSocketChannel.class);
//b.option(ChannelOption.SO_BACKLOG, 100);
//b.handler(new LoggingHandler(LogLevel.INFO));
server.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("bytesDecoder", new ByteArrayDecoder());
pipeline.addLast("bytesEncoder", new ByteArrayEncoder());
pipeline.addLast("handler", new NetworkPacketHandler());
}
});
server.bind(port).sync().channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
public static int getAvailablePort() throws IOException {
ServerSocket serverSocket = new ServerSocket(0); //读取空闲的可用端口
int port = serverSocket.getLocalPort();
serverSocket.close();
return port;
}
}
...@@ -7,6 +7,7 @@ import org.ini4j.Profile; ...@@ -7,6 +7,7 @@ import org.ini4j.Profile;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Vector; import java.util.Vector;
...@@ -25,7 +26,6 @@ public class MiraiConfig { ...@@ -25,7 +26,6 @@ public class MiraiConfig {
private Ini ini; private Ini ini;
private volatile Map<String, MiraiConfigSection> cacheSection = new ConcurrentHashMap<>(); private volatile Map<String, MiraiConfigSection> cacheSection = new ConcurrentHashMap<>();
private volatile List<String> needSaving = new Vector<>();
public MiraiConfig(File file){ public MiraiConfig(File file){
if(!file.getName().contains(".")){ if(!file.getName().contains(".")){
...@@ -45,14 +45,15 @@ public class MiraiConfig { ...@@ -45,14 +45,15 @@ public class MiraiConfig {
public void setSection(String key, MiraiConfigSection section){ public void setSection(String key, MiraiConfigSection section){
cacheSection.put(key, section); cacheSection.put(key, section);
needSaving.add(key);
} }
public MiraiMapSection getMapSection(String key){ public MiraiMapSection getMapSection(String key){
if(!cacheSection.containsKey(key)) { if(!cacheSection.containsKey(key)) {
MiraiMapSection section = new MiraiMapSection(); MiraiMapSection section = new MiraiMapSection();
section.putAll(ini.get(key)); if(ini.containsKey(key)){
section.putAll(ini.get(key));
}
cacheSection.put(key, section); cacheSection.put(key, section);
} }
return (MiraiMapSection) cacheSection.get(key); return (MiraiMapSection) cacheSection.get(key);
...@@ -61,7 +62,9 @@ public class MiraiConfig { ...@@ -61,7 +62,9 @@ public class MiraiConfig {
public MiraiListSection getListSection(String key){ public MiraiListSection getListSection(String key){
if(!cacheSection.containsKey(key)) { if(!cacheSection.containsKey(key)) {
MiraiListSection section = new MiraiListSection(); MiraiListSection section = new MiraiListSection();
section.addAll(ini.get(key).values()); if(ini.containsKey(key)){
section.addAll(ini.get(key).values());
}
cacheSection.put(key, section); cacheSection.put(key, section);
} }
return (MiraiListSection) cacheSection.get(key); return (MiraiListSection) cacheSection.get(key);
...@@ -69,19 +72,20 @@ public class MiraiConfig { ...@@ -69,19 +72,20 @@ public class MiraiConfig {
public synchronized void save(){ public synchronized void save(){
needSaving.forEach(a -> { cacheSection.forEach((k,a) -> {
cacheSection.get(a).saveAsSection(ini.get(a)); if(!ini.containsKey(k)) {
ini.put(k,"",new HashMap<>());
}
a.saveAsSection(ini.get(k));
}); });
try { try {
ini.store(file); ini.store(file);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
needSaving.clear();
} }
public void clearCache(){ public void clearCache(){
needSaving.clear();
cacheSection.clear(); cacheSection.clear();
} }
} }
......
...@@ -41,7 +41,7 @@ public class MiraiMapSection extends ConcurrentHashMap<String, Object> implement ...@@ -41,7 +41,7 @@ public class MiraiMapSection extends ConcurrentHashMap<String, Object> implement
} }
public int getInt(String key, int defaultValue) { public int getInt(String key, int defaultValue) {
return this.get(key, defaultValue); return Integer.parseInt(String.valueOf(this.get(key, defaultValue)));
} }
public double getDouble(String key) { public double getDouble(String key) {
...@@ -49,7 +49,7 @@ public class MiraiMapSection extends ConcurrentHashMap<String, Object> implement ...@@ -49,7 +49,7 @@ public class MiraiMapSection extends ConcurrentHashMap<String, Object> implement
} }
public double getDouble(String key, double defaultValue) { public double getDouble(String key, double defaultValue) {
return this.get(key, defaultValue); return Double.parseDouble(String.valueOf(this.get(key, defaultValue)));
} }
public float getFloat(String key) { public float getFloat(String key) {
...@@ -57,7 +57,7 @@ public class MiraiMapSection extends ConcurrentHashMap<String, Object> implement ...@@ -57,7 +57,7 @@ public class MiraiMapSection extends ConcurrentHashMap<String, Object> implement
} }
public float getFloat(String key, float defaultValue) { public float getFloat(String key, float defaultValue) {
return this.get(key, defaultValue); return Float.parseFloat(String.valueOf(this.get(key, defaultValue)));
} }
public String getString(String key) { public String getString(String key) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment