mirror of
https://github.com/mamoe/mirai.git
synced 2025-03-25 06:50:09 +08:00
YAML supported
This commit is contained in:
parent
a23c2e0d6c
commit
83de4b4c53
mirai-core
pom.xml
src/main/java/net/mamoe/mirai
@ -40,6 +40,16 @@
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<version>2.12.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.yaml</groupId>
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
<version>1.18</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.yaml</groupId>
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
<version>1.18</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
@ -8,7 +8,10 @@ import net.mamoe.mirai.network.Robot;
|
||||
import net.mamoe.mirai.task.MiraiTaskManager;
|
||||
import net.mamoe.mirai.utils.LoggerTextFormat;
|
||||
import net.mamoe.mirai.utils.MiraiLogger;
|
||||
import net.mamoe.mirai.utils.config.MiraiConfig;
|
||||
import net.mamoe.mirai.utils.config.MiraiConfigSection;
|
||||
import net.mamoe.mirai.utils.setting.MiraiSetting;
|
||||
import net.mamoe.mirai.utils.setting.MiraiSettingListSection;
|
||||
import net.mamoe.mirai.utils.setting.MiraiSettingMapSection;
|
||||
|
||||
import java.io.File;
|
||||
@ -42,6 +45,8 @@ public class MiraiServer {
|
||||
|
||||
MiraiSetting setting;
|
||||
|
||||
MiraiConfig qqs;
|
||||
|
||||
|
||||
protected MiraiServer(){
|
||||
instance = this;
|
||||
@ -73,13 +78,24 @@ public class MiraiServer {
|
||||
getLogger().info("Loading data under " + LoggerTextFormat.GREEN + this.parentFolder);
|
||||
|
||||
File setting = new File(this.parentFolder + "/Mirai.ini");
|
||||
getLogger().info("Selecting setting from " + LoggerTextFormat.GREEN + setting);
|
||||
|
||||
if(!setting.exists()){
|
||||
this.initSetting(setting);
|
||||
}else {
|
||||
this.setting = new MiraiSetting(setting);
|
||||
}
|
||||
getLogger().info("Success");
|
||||
|
||||
File qqs = new File(this.parentFolder + "/QQ.yml");
|
||||
getLogger().info("Reading QQ accounts from " + LoggerTextFormat.GREEN + qqs);
|
||||
if(!qqs.exists()){
|
||||
this.initQQConfig(qqs);
|
||||
}else {
|
||||
this.qqs = new MiraiConfig(qqs);
|
||||
}
|
||||
if(this.qqs.isEmpty()){
|
||||
this.initQQConfig(qqs);
|
||||
}
|
||||
|
||||
/*
|
||||
MiraiSettingMapSection qqs = this.setting.getMapSection("qq");
|
||||
@ -95,6 +111,8 @@ public class MiraiServer {
|
||||
});
|
||||
*/
|
||||
|
||||
getLogger().info("ready to connect");
|
||||
|
||||
Robot robot = new Robot(1994701021, "xiaoqqq");
|
||||
try {
|
||||
//System.out.println(Protocol.Companion.getSERVER_IP().toString());
|
||||
@ -136,8 +154,28 @@ public class MiraiServer {
|
||||
}
|
||||
this.setting = new MiraiSetting(setting);
|
||||
MiraiSettingMapSection network = this.setting.getMapSection("network");
|
||||
network.set("enable_proxy","not supporting yet");
|
||||
|
||||
MiraiSettingMapSection qqs = this.setting.getMapSection("qq");
|
||||
MiraiSettingListSection proxy = this.setting.getListSection("proxy");
|
||||
proxy.add("1.2.3.4:95");
|
||||
proxy.add("1.2.3.4:100");
|
||||
|
||||
MiraiSettingMapSection worker = this.setting.getMapSection("worker");
|
||||
worker.set("core_task_pool_worker_amount",5);
|
||||
|
||||
MiraiSettingMapSection plugin = this.setting.getMapSection("plugin");
|
||||
plugin.set("debug", false);
|
||||
|
||||
this.setting.save();
|
||||
getLogger().info(LoggerTextFormat.SKY_BLUE + "initialized; changing can be made in setting file: " + setting.toString());
|
||||
}
|
||||
|
||||
private void initQQConfig(File qqConfig){
|
||||
this.qqs = new MiraiConfig(qqConfig);
|
||||
|
||||
MiraiConfigSection<Object> section = new MiraiConfigSection<>();
|
||||
|
||||
System.out.println("/");
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
getLogger().info(LoggerTextFormat.SKY_BLUE + "input one " + LoggerTextFormat.RED + " QQ number " + LoggerTextFormat.SKY_BLUE + "for default robot");
|
||||
getLogger().info(LoggerTextFormat.SKY_BLUE + "输入用于默认机器人的QQ号");
|
||||
@ -145,9 +183,13 @@ public class MiraiServer {
|
||||
getLogger().info(LoggerTextFormat.SKY_BLUE + "input the password for that QQ account");
|
||||
getLogger().info(LoggerTextFormat.SKY_BLUE + "输入该QQ号对应密码");
|
||||
String qqPassword = scanner.next();
|
||||
getLogger().info(LoggerTextFormat.SKY_BLUE + "initialized; changing can be made in config file: " + setting.toString());
|
||||
qqs.put(String.valueOf(qqNumber),qqPassword);
|
||||
this.setting.save();
|
||||
|
||||
section.put("password",qqPassword);
|
||||
section.put("owner","default");
|
||||
|
||||
this.qqs.put(String.valueOf(qqNumber),section);
|
||||
this.qqs.save();
|
||||
getLogger().info(LoggerTextFormat.SKY_BLUE + "QQ account initialized; changing can be made in Config file: " + qqConfig.toString());
|
||||
}
|
||||
|
||||
private void onEnable(){
|
||||
|
@ -34,4 +34,5 @@ object MiraiLogger {
|
||||
|
||||
}
|
||||
|
||||
|
||||
fun log(any: Any?) = MiraiLogger.info(any)
|
73
mirai-core/src/main/java/net/mamoe/mirai/utils/Utils.java
Normal file
73
mirai-core/src/main/java/net/mamoe/mirai/utils/Utils.java
Normal file
@ -0,0 +1,73 @@
|
||||
package net.mamoe.mirai.utils;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class Utils {
|
||||
/**
|
||||
* File supporting from Nukkit
|
||||
* */
|
||||
public static void writeFile(String fileName, String content) throws IOException {
|
||||
writeFile(fileName, new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
public static void writeFile(String fileName, InputStream content) throws IOException {
|
||||
writeFile(new File(fileName), content);
|
||||
}
|
||||
|
||||
public static void writeFile(File file, String content) throws IOException {
|
||||
writeFile(file, new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
public static void writeFile(File file, InputStream content) throws IOException {
|
||||
if (content == null) {
|
||||
throw new IllegalArgumentException("content must not be null");
|
||||
}
|
||||
if (!file.exists()) {
|
||||
file.createNewFile();
|
||||
}
|
||||
try (FileOutputStream stream = new FileOutputStream(file)) {
|
||||
byte[] buffer = new byte[1024];
|
||||
int length;
|
||||
while ((length = content.read(buffer)) != -1) {
|
||||
stream.write(buffer, 0, length);
|
||||
}
|
||||
}
|
||||
content.close();
|
||||
}
|
||||
|
||||
public static String readFile(File file) throws IOException {
|
||||
if (!file.exists() || file.isDirectory()) {
|
||||
throw new FileNotFoundException();
|
||||
}
|
||||
return readFile(new FileInputStream(file));
|
||||
}
|
||||
|
||||
public static String readFile(String filename) throws IOException {
|
||||
File file = new File(filename);
|
||||
if (!file.exists() || file.isDirectory()) {
|
||||
throw new FileNotFoundException();
|
||||
}
|
||||
return readFile(new FileInputStream(file));
|
||||
}
|
||||
|
||||
public static String readFile(InputStream inputStream) throws IOException {
|
||||
return readFile(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private static String readFile(Reader reader) throws IOException {
|
||||
try (BufferedReader br = new BufferedReader(reader)) {
|
||||
String temp;
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
temp = br.readLine();
|
||||
while (temp != null) {
|
||||
if (stringBuilder.length() != 0) {
|
||||
stringBuilder.append("\n");
|
||||
}
|
||||
stringBuilder.append(temp);
|
||||
temp = br.readLine();
|
||||
}
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
package net.mamoe.mirai.utils.config;
|
||||
|
||||
import net.mamoe.mirai.MiraiServer;
|
||||
import net.mamoe.mirai.utils.Utils;
|
||||
import org.yaml.snakeyaml.DumperOptions;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* YAML-TYPE CONFIG
|
||||
* Thread SAFE
|
||||
* @author NaturalHG
|
||||
*/
|
||||
public class MiraiConfig extends MiraiConfigSection<Object>{
|
||||
|
||||
private volatile File root;
|
||||
|
||||
public MiraiConfig(File file){
|
||||
super();
|
||||
if(!file.toURI().getPath().contains(MiraiServer.getInstance().getParentFolder().getPath())){
|
||||
file = new File((MiraiServer.getInstance().getParentFolder().getPath() + "/" + file).replace("//","/"));
|
||||
}
|
||||
|
||||
this.root = file;
|
||||
|
||||
if(!file.exists()){
|
||||
try {
|
||||
if(!file.createNewFile()){
|
||||
return;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
this.parse();
|
||||
}
|
||||
|
||||
private MiraiConfig(){
|
||||
|
||||
}
|
||||
|
||||
public synchronized void save(){
|
||||
DumperOptions dumperOptions = new DumperOptions();
|
||||
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||
Yaml yaml = new Yaml(dumperOptions);
|
||||
String content = yaml.dump(this);
|
||||
try {
|
||||
Utils.writeFile(this.root,content);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void parse(){
|
||||
DumperOptions dumperOptions = new DumperOptions();
|
||||
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||
Yaml yaml = new Yaml(dumperOptions);
|
||||
this.clear();
|
||||
try {
|
||||
Map<String,Object> content = yaml.loadAs(Utils.readFile(this.root), LinkedHashMap.class);
|
||||
if(content != null) {
|
||||
this.putAll(content);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package net.mamoe.mirai.utils.config;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentSkipListMap;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
public class MiraiConfigSection<T> extends ConcurrentSkipListMap<String, T> {
|
||||
|
||||
public MiraiConfigSection(){
|
||||
/*
|
||||
* Ensure the key will be in order
|
||||
* */
|
||||
super((a,b) -> 1);
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user