mirror of
https://github.com/mamoe/mirai.git
synced 2025-03-03 15:10:14 +08:00
Config
This commit is contained in:
parent
7c1cba29a0
commit
24ef0c1d93
@ -46,7 +46,9 @@ public class MiraiServer {
|
||||
|
||||
protected void shutdown(){
|
||||
if(this.enabled) {
|
||||
this.getLogger().log(LoggerTextFormat.SKY_BLUE + "About to shutdown Mirai");
|
||||
this.getEventManager().boardcastEvent(new ServerDisableEvent());
|
||||
this.getLogger().log(LoggerTextFormat.SKY_BLUE + "Data have been saved");
|
||||
}
|
||||
}
|
||||
|
||||
@ -61,9 +63,10 @@ public class MiraiServer {
|
||||
this.eventManager = MiraiEventManager.getInstance();
|
||||
this.taskManager = MiraiTaskManager.getInstance();
|
||||
|
||||
this.getLogger().log(LoggerTextFormat.SKY_BLUE + "About to run Mirai" + MiraiServer.getMiraiVersion() + " under " + (isUnix()?"unix":"windows") );
|
||||
this.getLogger().log("Loading data under " + this.parentFolder);
|
||||
this.getLogger().log(LoggerTextFormat.SKY_BLUE + "About to run Mirai (" + MiraiServer.getMiraiVersion() + ") under " + (isUnix()?"unix":"windows") );
|
||||
this.getLogger().log("Loading data under " + LoggerTextFormat.GREEN + this.parentFolder);
|
||||
|
||||
|
||||
/*
|
||||
try {
|
||||
Network.start(Network.getAvailablePort());
|
||||
|
@ -0,0 +1,88 @@
|
||||
package net.mamoe.mirai.utils.config;
|
||||
|
||||
import org.ini4j.Config;
|
||||
import org.ini4j.Ini;
|
||||
import org.ini4j.Profile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Vector;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* Mirai Config
|
||||
* Only support {INI} format
|
||||
* Support MAP and LIST
|
||||
* Thread safe
|
||||
*/
|
||||
public class MiraiConfig {
|
||||
|
||||
private File file;
|
||||
|
||||
private Ini ini;
|
||||
|
||||
private volatile Map<String, MiraiConfigSection> cacheSection = new ConcurrentHashMap<>();
|
||||
private volatile List<String> needSaving = new Vector<>();
|
||||
|
||||
public MiraiConfig(File file){
|
||||
if(!file.getName().contains(".")){
|
||||
file = new File(file.getParent() + file.getName() + ".ini");
|
||||
}
|
||||
this.file = file;
|
||||
try {
|
||||
Config config = new Config();
|
||||
config.setMultiSection(true);
|
||||
ini = new Ini();
|
||||
ini.setConfig(config);
|
||||
ini.load(this.file.toURI().toURL());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void setSection(String key, MiraiConfigSection section){
|
||||
cacheSection.put(key, section);
|
||||
needSaving.add(key);
|
||||
}
|
||||
|
||||
|
||||
public MiraiMapSection getMapSection(String key){
|
||||
if(!cacheSection.containsKey(key)) {
|
||||
MiraiMapSection section = new MiraiMapSection();
|
||||
section.putAll(ini.get(key));
|
||||
cacheSection.put(key, section);
|
||||
}
|
||||
return (MiraiMapSection) cacheSection.get(key);
|
||||
}
|
||||
|
||||
public MiraiListSection getListSection(String key){
|
||||
if(!cacheSection.containsKey(key)) {
|
||||
MiraiListSection section = new MiraiListSection();
|
||||
section.addAll(ini.get(key).values());
|
||||
cacheSection.put(key, section);
|
||||
}
|
||||
return (MiraiListSection) cacheSection.get(key);
|
||||
}
|
||||
|
||||
|
||||
public synchronized void save(){
|
||||
needSaving.forEach(a -> {
|
||||
cacheSection.get(a).saveAsSection(ini.get(a));
|
||||
});
|
||||
try {
|
||||
ini.store(file);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
needSaving.clear();
|
||||
}
|
||||
|
||||
public void clearCache(){
|
||||
needSaving.clear();
|
||||
cacheSection.clear();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,12 @@
|
||||
package net.mamoe.mirai.utils.config;
|
||||
|
||||
import org.ini4j.Profile;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.util.Map;
|
||||
|
||||
public interface MiraiConfigSection extends Closeable {
|
||||
void saveAsSection(Profile.Section section);
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,52 @@
|
||||
package net.mamoe.mirai.utils.config;
|
||||
|
||||
|
||||
import org.ini4j.Profile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.Vector;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
|
||||
public class MiraiListSection extends Vector<Object> implements MiraiConfigSection{
|
||||
private Lock lock = new ReentrantLock();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T getAs(int index){
|
||||
return (T)super.get(index);
|
||||
}
|
||||
|
||||
public int getInt(int index){
|
||||
return this.getAs(index);
|
||||
}
|
||||
|
||||
public int getDouble(int index){
|
||||
return this.getAs(index);
|
||||
}
|
||||
|
||||
public int getString(int index){
|
||||
return this.getAs(index);
|
||||
}
|
||||
|
||||
public int getFloat(int index) {
|
||||
return this.getAs(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void saveAsSection(Profile.Section section) {
|
||||
section.clear();
|
||||
AtomicInteger integer = new AtomicInteger(0);
|
||||
this.forEach(a -> {
|
||||
section.put(String.valueOf(integer.getAndAdd(1)),a);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
package net.mamoe.mirai.utils.config;
|
||||
|
||||
|
||||
import org.ini4j.Profile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
public class MiraiMapSection extends ConcurrentHashMap<String, Object> implements MiraiConfigSection {
|
||||
|
||||
public Object get(String key){
|
||||
return this.get(key,null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T get(String key, T defaultValue) {
|
||||
if (key == null || key.isEmpty()){
|
||||
return defaultValue;
|
||||
}
|
||||
if (super.containsKey(key)){
|
||||
return (T) super.get(key);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public void set(String key, Object value){
|
||||
this.put(key,value);
|
||||
}
|
||||
|
||||
public void remove(String key){
|
||||
super.remove(key);
|
||||
}
|
||||
|
||||
public int getInt(String key) {
|
||||
return this.getInt(key, 0);
|
||||
}
|
||||
|
||||
public int getInt(String key, int defaultValue) {
|
||||
return this.get(key, defaultValue);
|
||||
}
|
||||
|
||||
public double getDouble(String key) {
|
||||
return this.getDouble(key, 0D);
|
||||
}
|
||||
|
||||
public double getDouble(String key, double defaultValue) {
|
||||
return this.get(key, defaultValue);
|
||||
}
|
||||
|
||||
public float getFloat(String key) {
|
||||
return this.getFloat(key, 0F);
|
||||
}
|
||||
|
||||
public float getFloat(String key, float defaultValue) {
|
||||
return this.get(key, defaultValue);
|
||||
}
|
||||
|
||||
public String getString(String key) {
|
||||
return this.getString(key, "");
|
||||
}
|
||||
|
||||
public String getString(String key, String defaultValue) {
|
||||
return String.valueOf(this.get(key, defaultValue));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> List<T> asList(){
|
||||
return this.values().stream().map(a -> (T)(a)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void saveAsSection(Profile.Section section) {
|
||||
section.clear();
|
||||
this.forEach(section::put);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
14
pom.xml
14
pom.xml
@ -83,6 +83,13 @@
|
||||
<version>1.18.8</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.ini4j</groupId>
|
||||
<artifactId>ini4j</artifactId>
|
||||
<version>0.5.2</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
@ -100,6 +107,13 @@
|
||||
<artifactId>netty-all</artifactId>
|
||||
<version>4.1.38.Final</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.ini4j</groupId>
|
||||
<artifactId>ini4j</artifactId>
|
||||
<version>0.5.2</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user