mirror of
https://github.com/mamoe/mirai.git
synced 2025-02-11 09:58:03 +08:00
test
This commit is contained in:
parent
325110e909
commit
1ac45fa724
@ -1,96 +0,0 @@
|
||||
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.HashMap;
|
||||
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<>();
|
||||
|
||||
public MiraiConfig(File file){
|
||||
if(!file.getName().contains(".")){
|
||||
file = new File(file.getParent() + file.getName() + ".ini");
|
||||
}
|
||||
this.file = file;
|
||||
try {
|
||||
if(file.exists()){
|
||||
file.createNewFile();
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
public MiraiMapSection getMapSection(String key){
|
||||
if(!cacheSection.containsKey(key)) {
|
||||
MiraiMapSection section = new MiraiMapSection();
|
||||
if(ini.containsKey(key)){
|
||||
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();
|
||||
if(ini.containsKey(key)){
|
||||
section.addAll(ini.get(key).values());
|
||||
}
|
||||
cacheSection.put(key, section);
|
||||
}
|
||||
return (MiraiListSection) cacheSection.get(key);
|
||||
}
|
||||
|
||||
|
||||
public synchronized void save(){
|
||||
cacheSection.forEach((k,a) -> {
|
||||
if(!ini.containsKey(k)) {
|
||||
ini.put(k,"",new HashMap<>());
|
||||
}
|
||||
a.saveAsSection(ini.get(k));
|
||||
});
|
||||
this.clearCache();
|
||||
try {
|
||||
ini.store(file);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void clearCache(){
|
||||
cacheSection.clear();
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +0,0 @@
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -1,52 +0,0 @@
|
||||
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 {
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,88 +0,0 @@
|
||||
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 Integer.parseInt(String.valueOf(this.get(key, defaultValue)));
|
||||
}
|
||||
|
||||
public double getDouble(String key) {
|
||||
return this.getDouble(key, 0D);
|
||||
}
|
||||
|
||||
public double getDouble(String key, double defaultValue) {
|
||||
return Double.parseDouble(String.valueOf(this.get(key, defaultValue)));
|
||||
}
|
||||
|
||||
public float getFloat(String key) {
|
||||
return this.getFloat(key, 0F);
|
||||
}
|
||||
|
||||
public float getFloat(String key, float defaultValue) {
|
||||
return Float.parseFloat(String.valueOf(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 {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,12 +1,22 @@
|
||||
import net.mamoe.mirai.network.packet.server.Server0825Packet;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* @author Him188moe @ Mirai Project
|
||||
*/
|
||||
public class NetworkTest {
|
||||
public static void main(String[] args) {
|
||||
|
||||
/*
|
||||
System.out.println(Short.valueOf("37 13", 16));
|
||||
|
||||
System.out.println(1040400290L & 0x0FFFFFFFF);
|
||||
System.out.println(Long.valueOf("3E033FA2", 16));
|
||||
*/
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
6
mirai-core/src/test/java/PacketTest.kt
Normal file
6
mirai-core/src/test/java/PacketTest.kt
Normal file
@ -0,0 +1,6 @@
|
||||
import net.mamoe.mirai.network.packet.server.Server0825Packet
|
||||
|
||||
fun main(){
|
||||
val v = "00 37 13 08 25 31 01 76 E4 B8 DD 03 00 00 00 01 2E 01 00 00 68 52 00 00 00 00 A4 F1 91 88 C9 82 14 99 0C 9E 56 55 91 23 C8 3D C3 47 F0 25 A1 8E 74 EF 1E 0B 32 5B 20 8A FA 3B 0B 52 8F 86 E6 04 F1 D6 F8 63 75 60 8C 0C 7D 06 D1 E0 22 F8 49 EF AF 61 EE 7E 69 72 EB 10 08 30 69 50 1C 84 A9 C2 16 D7 52 B9 1C 79 CA 5A CF FD BC AE D8 A6 BB DC 21 6E 79 26 E1 A2 23 11 AA B0 9A 49 39 72 ED 61 12 B6 88 4D A2 56 23 E9 92 11 92 27 4A 70 00 C9 01 7B 03";
|
||||
v.split(" ")
|
||||
}
|
Loading…
Reference in New Issue
Block a user