This commit is contained in:
Him188moe 2019-09-01 16:18:11 +08:00
parent a7d3e62fa6
commit 801d727629
36 changed files with 132 additions and 207 deletions

View File

@ -1,6 +1,6 @@
package net.mamoe.mirai.contact
import net.mamoe.mirai.message.At
import net.mamoe.mirai.message.defaults.At
/**
* @author Him188moe

View File

@ -47,14 +47,17 @@ public class MiraiEventManager {
if (!hook.isMount()) {
hook.setMount(true);
hooksLock.writeLock().lock();
if (!hooks.containsKey(hook.getEventClass())) {
hooks.put(hook.getEventClass(), new LinkedList<>() {{
add(hook);
}});
} else {
hooks.get(hook.getEventClass()).add(hook);
try {
if (!hooks.containsKey(hook.getEventClass())) {
hooks.put(hook.getEventClass(), new LinkedList<>() {{
add(hook);
}});
} else {
hooks.get(hook.getEventClass()).add(hook);
}
} finally {
hooksLock.writeLock().unlock();
}
hooksLock.writeLock().unlock();
}
}
@ -98,16 +101,19 @@ public class MiraiEventManager {
public void broadcastEvent(MiraiEvent event) {
hooksLock.readLock().lock();
if (hooks.containsKey(event.getClass())) {
hooks.put(event.getClass(),
hooks.get(event.getClass())
.stream()
.sorted(Comparator.comparingInt(MiraiEventHook::getPriority))
.filter(a -> !a.accept(event))
.collect(Collectors.toList())
);
try {
if (hooks.containsKey(event.getClass())) {
hooks.put(event.getClass(),
hooks.get(event.getClass())
.stream()
.sorted(Comparator.comparingInt(MiraiEventHook::getPriority))
.filter(a -> !a.accept(event))
.collect(Collectors.toList())
);
}
} finally {
hooksLock.readLock().unlock();
}
hooksLock.readLock().unlock();
}

View File

@ -1,7 +1,6 @@
package net.mamoe.mirai.event
import net.mamoe.mirai.event.events.MiraiEvent
import net.mamoe.mirai.event.events.robot.RobotLoginSucceedEvent
import kotlin.reflect.KClass
@ -13,17 +12,27 @@ fun <C : Class<E>, E : MiraiEvent> C.hookOnce(hook: (E) -> Unit) {
MiraiEventManager.getInstance().hookOnce(MiraiEventHook<E>(this, hook))
}
fun <C : Class<E>, E : MiraiEvent> C.hookWhile(hook: (E) -> Boolean) {
MiraiEventManager.getInstance().hookAlways(MiraiEventHookSimple<E>(this, hook))
}
fun <C : KClass<E>, E : MiraiEvent> C.hookAlways(hook: (E) -> Unit) {
this.java.hookOnce(hook)
this.java.hookAlways(hook)
}
fun <C : KClass<E>, E : MiraiEvent> C.hookOnce(hook: (E) -> Unit) {
this.java.hookOnce(hook)
}
fun <C : KClass<E>, E : MiraiEvent> C.hookWhile(hook: (E) -> Boolean) {
this.java.hookWhile(hook)
}
fun main() {
RobotLoginSucceedEvent::class.hookOnce {
private class MiraiEventHookSimple<E : MiraiEvent>(clazz: Class<E>, val hook: (E) -> Boolean) : MiraiEventHook<E>(clazz) {
override fun accept(event: MiraiEvent?): Boolean {
@Suppress("UNCHECKED_CAST")
return hook.invoke(event as E)
}
}

View File

@ -1,6 +1,9 @@
package net.mamoe.mirai.message;
import net.mamoe.mirai.contact.QQ;
import net.mamoe.mirai.message.defaults.At;
import net.mamoe.mirai.message.defaults.MessageChain;
import net.mamoe.mirai.message.defaults.PlainText;
import org.jetbrains.annotations.NotNull;
import java.awt.image.BufferedImage;

View File

@ -1,5 +1,7 @@
package net.mamoe.mirai.message
import net.mamoe.mirai.message.defaults.PlainText
/**
* 实现使用 '+' 操作符连接 [Message] [Message]
*/

View File

@ -1,6 +1,7 @@
package net.mamoe.mirai.message;
package net.mamoe.mirai.message.defaults;
import net.mamoe.mirai.contact.QQ;
import net.mamoe.mirai.message.Message;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;

View File

@ -1,9 +1,12 @@
package net.mamoe.mirai.message;
package net.mamoe.mirai.message.defaults;
import net.mamoe.mirai.message.FaceID;
import net.mamoe.mirai.message.Message;
/**
* @author Him188moe
*/
public class Face extends Message {
public final class Face extends Message {
private final FaceID id;
public Face(FaceID id) {

View File

@ -1,4 +1,6 @@
package net.mamoe.mirai.message;
package net.mamoe.mirai.message.defaults;
import net.mamoe.mirai.message.Message;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

View File

@ -1,5 +1,6 @@
package net.mamoe.mirai.message;
package net.mamoe.mirai.message.defaults;
import net.mamoe.mirai.message.Message;
import org.jetbrains.annotations.NotNull;
import java.util.LinkedList;
@ -12,7 +13,7 @@ import java.util.stream.Collectors;
public final class MessageChain extends Message {
private LinkedList<Message> list = new LinkedList<>();
MessageChain(@NotNull Message head, @NotNull Message tail) {
public MessageChain(@NotNull Message head, @NotNull Message tail) {
Objects.requireNonNull(head);
Objects.requireNonNull(tail);

View File

@ -1,4 +1,6 @@
package net.mamoe.mirai.message;
package net.mamoe.mirai.message.defaults;
import net.mamoe.mirai.message.Message;
/**
* @author Him188moe

View File

@ -9,8 +9,7 @@ import net.mamoe.mirai.network.packet.message.ClientSendFriendMessagePacket
import net.mamoe.mirai.network.packet.verification.ServerVerificationCodePacket
import net.mamoe.mirai.network.packet.verification.ServerVerificationCodePacketEncrypted
import net.mamoe.mirai.task.MiraiThreadPool
import net.mamoe.mirai.util.*
import net.mamoe.mirai.utils.MiraiLogger
import net.mamoe.mirai.utils.*
import java.io.ByteArrayInputStream
import java.io.FileOutputStream
import java.net.DatagramPacket

View File

@ -1,7 +1,7 @@
package net.mamoe.mirai.network.packet
import net.mamoe.mirai.network.Protocol
import net.mamoe.mirai.util.TEACryptor
import net.mamoe.mirai.utils.TEACryptor
import java.io.DataInputStream
/**

View File

@ -2,7 +2,8 @@ package net.mamoe.mirai.network.packet
import lombok.Getter
import net.mamoe.mirai.network.Protocol
import net.mamoe.mirai.util.*
import net.mamoe.mirai.util.TestedSuccessfully
import net.mamoe.mirai.utils.*
import java.io.DataOutputStream
import java.io.IOException
import java.net.InetAddress

View File

@ -1,8 +1,8 @@
package net.mamoe.mirai.network.packet
import net.mamoe.mirai.network.Protocol
import net.mamoe.mirai.util.TEACryptor
import net.mamoe.mirai.util.toUHexString
import net.mamoe.mirai.utils.TEACryptor
import net.mamoe.mirai.utils.toUHexString
import java.io.DataInputStream

View File

@ -1,7 +1,7 @@
package net.mamoe.mirai.network.packet
import net.mamoe.mirai.network.Protocol
import net.mamoe.mirai.util.TEACryptor
import net.mamoe.mirai.utils.TEACryptor
import java.io.DataInputStream

View File

@ -1,6 +1,6 @@
package net.mamoe.mirai.network.packet
import net.mamoe.mirai.util.toUHexString
import net.mamoe.mirai.utils.toUHexString
import java.io.ByteArrayOutputStream
import java.io.DataInputStream
import java.util.zip.GZIPInputStream

View File

@ -2,9 +2,9 @@ package net.mamoe.mirai.network.packet
import net.mamoe.mirai.network.packet.login.*
import net.mamoe.mirai.network.packet.verification.ServerVerificationCodePacketEncrypted
import net.mamoe.mirai.util.getAllDeclaredFields
import net.mamoe.mirai.util.hexToBytes
import net.mamoe.mirai.util.toUHexString
import net.mamoe.mirai.utils.getAllDeclaredFields
import net.mamoe.mirai.utils.hexToBytes
import net.mamoe.mirai.utils.toUHexString
import java.io.DataInputStream
/**

View File

@ -1,10 +1,10 @@
package net.mamoe.mirai.network.packet
import net.mamoe.mirai.network.Protocol
import net.mamoe.mirai.util.ByteArrayDataOutputStream
import net.mamoe.mirai.util.TEACryptor
import net.mamoe.mirai.util.getRandomKey
import net.mamoe.mirai.util.lazyEncode
import net.mamoe.mirai.utils.ByteArrayDataOutputStream
import net.mamoe.mirai.utils.TEACryptor
import net.mamoe.mirai.utils.getRandomKey
import net.mamoe.mirai.utils.lazyEncode
import java.io.DataInputStream
import java.net.InetAddress

View File

@ -1,7 +1,7 @@
package net.mamoe.mirai.network.packet
import net.mamoe.mirai.network.Protocol
import net.mamoe.mirai.util.*
import net.mamoe.mirai.utils.*
import java.io.DataInputStream
import java.io.IOException

View File

@ -2,7 +2,11 @@ package net.mamoe.mirai.network.packet.login
import net.mamoe.mirai.network.Protocol
import net.mamoe.mirai.network.packet.*
import net.mamoe.mirai.util.*
import net.mamoe.mirai.util.TestedSuccessfully
import net.mamoe.mirai.utils.ByteArrayDataOutputStream
import net.mamoe.mirai.utils.TEACryptor
import net.mamoe.mirai.utils.hexToBytes
import net.mamoe.mirai.utils.toUHexString
import java.io.DataOutputStream
/**

View File

@ -2,7 +2,7 @@ package net.mamoe.mirai.network.packet.login
import net.mamoe.mirai.network.Protocol
import net.mamoe.mirai.network.packet.*
import net.mamoe.mirai.util.ClientLoginStatus
import net.mamoe.mirai.utils.ClientLoginStatus
/**
* @author Him188moe

View File

@ -2,8 +2,8 @@ package net.mamoe.mirai.network.packet.login
import net.mamoe.mirai.network.Protocol
import net.mamoe.mirai.network.packet.*
import net.mamoe.mirai.util.ByteArrayDataOutputStream
import net.mamoe.mirai.util.TEACryptor
import net.mamoe.mirai.utils.ByteArrayDataOutputStream
import net.mamoe.mirai.utils.TEACryptor
/**
* @author Him188moe

View File

@ -5,10 +5,10 @@ import net.mamoe.mirai.network.packet.ServerPacket
import net.mamoe.mirai.network.packet.goto
import net.mamoe.mirai.network.packet.readNBytes
import net.mamoe.mirai.network.packet.readVarString
import net.mamoe.mirai.util.TEACryptor
import net.mamoe.mirai.util.TestedSuccessfully
import net.mamoe.mirai.util.hexToBytes
import net.mamoe.mirai.util.toUHexString
import net.mamoe.mirai.utils.TEACryptor
import net.mamoe.mirai.utils.hexToBytes
import net.mamoe.mirai.utils.toUHexString
import java.io.DataInputStream
/**

View File

@ -4,10 +4,10 @@ import net.mamoe.mirai.network.packet.PacketId
import net.mamoe.mirai.network.packet.ServerPacket
import net.mamoe.mirai.network.packet.dataInputStream
import net.mamoe.mirai.network.packet.goto
import net.mamoe.mirai.util.TEACryptor
import net.mamoe.mirai.util.TestedSuccessfully
import net.mamoe.mirai.util.hexToUBytes
import net.mamoe.mirai.util.toUHexString
import net.mamoe.mirai.utils.TEACryptor
import net.mamoe.mirai.utils.hexToUBytes
import net.mamoe.mirai.utils.toUHexString
import java.io.DataInputStream
/**

View File

@ -3,7 +3,7 @@ package net.mamoe.mirai.network.packet.login
import net.mamoe.mirai.network.packet.ServerPacket
import net.mamoe.mirai.network.packet.dataInputStream
import net.mamoe.mirai.network.packet.goto
import net.mamoe.mirai.util.TEACryptor
import net.mamoe.mirai.utils.TEACryptor
import java.io.DataInputStream
/**

View File

@ -2,7 +2,8 @@ package net.mamoe.mirai.network.packet.message
import net.mamoe.mirai.network.Protocol
import net.mamoe.mirai.network.packet.*
import net.mamoe.mirai.util.lazyEncode
import net.mamoe.mirai.utils.lazyEncode
import net.mamoe.mirai.utils.toUHexString
/**
* @author Him188moe
@ -54,4 +55,15 @@ class ClientSendFriendMessagePacket(
}//todo check
}
}
}
fun main() {
println(lazyEncode {
val bytes = "hahaha".toByteArray()
it.writeByte(0x01)
it.writeShort(bytes.size)
it.writeByte(0x01)
it.writeShort(bytes.size - 1)
it.write(bytes)
}.toUHexString())
}

View File

@ -3,7 +3,7 @@ package net.mamoe.mirai.network.packet.verification
import net.mamoe.mirai.network.packet.ServerPacket
import net.mamoe.mirai.network.packet.dataInputStream
import net.mamoe.mirai.network.packet.goto
import net.mamoe.mirai.util.TEACryptor
import net.mamoe.mirai.utils.TEACryptor
import java.io.DataInputStream
/**

View File

@ -1,34 +0,0 @@
package net.mamoe.mirai.util
import net.mamoe.mirai.network.packet.ClientPacket
import net.mamoe.mirai.network.packet.ServerPacket
/**
* @author Him188moe
*/
object DebugLogger {
val buff = StringBuilder()
}
fun ByteArray.encryptionDebugLogging() {
DebugLogger.buff.append("TEA encrypt: " + this.toUHexString()).append("\n")
}
fun ByteArray.packetSentDebugLogging() {
DebugLogger.buff.append("packet sent: " + this.toUHexString()).append("\n")
}
fun ByteArray.decryptionDebugLogging() {
DebugLogger.buff.append("TEA decrypted: " + this.toUHexString()).append("\n")
}
fun ServerPacket.logging() {
DebugLogger.buff.append(this.toString()).append("\n")
}
@ExperimentalUnsignedTypes
fun ClientPacket.logging() {
DebugLogger.buff.append(this.toString()).append("\n")
}

View File

@ -1,4 +1,4 @@
package net.mamoe.mirai.util;
package net.mamoe.mirai.utils;
/**
* @author Him188moe

View File

@ -38,4 +38,8 @@ object MiraiLogger {
}
fun log(any: Any?) = MiraiLogger.info(any)
fun Any.logInfo() = MiraiLogger.info(this)
fun Any.logDebug() = MiraiLogger.debug(this)
fun Any.logError() = MiraiLogger.error(this)

View File

@ -1,4 +1,4 @@
package net.mamoe.mirai.util;
package net.mamoe.mirai.utils;
import net.mamoe.mirai.network.Protocol;
@ -239,13 +239,10 @@ public class TEACryptor {
}
public byte[] encrypt(byte[] plaintext) {
DebugLoggerKt.encryptionDebugLogging(plaintext);
//System.out.println("TEA加密, 原文=" + Utils.INSTANCE.toHexString(plaintext, ""));
return encrypt(plaintext, 0, plaintext.length);
}
public byte[] decrypt(byte[] ciphertext) {
DebugLoggerKt.decryptionDebugLogging(ciphertext);
return decrypt(ciphertext, 0, ciphertext.length);
}
}

View File

@ -1,73 +0,0 @@
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();
}
}
}

View File

@ -1,4 +1,4 @@
package net.mamoe.mirai.util
package net.mamoe.mirai.utils
import net.mamoe.mirai.network.Protocol
import java.io.ByteArrayOutputStream
@ -8,30 +8,15 @@ import java.lang.reflect.Field
import java.util.*
import java.util.zip.CRC32
/**
* @author Him188moe
*/
object Utils {
fun toHexString(byteArray: ByteArray, separator: String = " "): String = byteArray.joinToString(separator) {
var ret = it.toString(16).toUpperCase()
if (ret.length == 1) {
ret = "0$ret"
}
return@joinToString ret
}
@ExperimentalUnsignedTypes
fun toHexString(byteArray: UByteArray, separator: String = " "): String = byteArray.joinToString(separator) {
var ret = it.toString(16).toUpperCase()
if (ret.length == 1) {
ret = "0$ret"
}
return@joinToString ret
fun ByteArray.toHexString(): String = toHexString(" ")
fun ByteArray.toHexString(separator: String = " "): String = this.joinToString(separator) {
var ret = it.toString(16).toUpperCase()
if (ret.length == 1) {
ret = "0$ret"
}
return@joinToString ret
}
fun ByteArray.toHexString(): String = toHexString(" ")
fun ByteArray.toHexString(separator: String = " "): String = Utils.toHexString(this, separator)
@ExperimentalUnsignedTypes
fun ByteArray.toUHexString(separator: String = " "): String = this.toUByteArray().toUHexString(separator)
@ -39,7 +24,13 @@ fun ByteArray.toUHexString(separator: String = " "): String = this.toUByteArray(
fun ByteArray.toUHexString(): String = this.toUByteArray().toUHexString()
@ExperimentalUnsignedTypes
fun UByteArray.toUHexString(separator: String = " "): String = Utils.toHexString(this, separator)
fun UByteArray.toUHexString(separator: String = " "): String = this.joinToString(separator) {
var ret = it.toString(16).toUpperCase()
if (ret.length == 1) {
ret = "0$ret"
}
return@joinToString ret
}
@ExperimentalUnsignedTypes
fun UByteArray.toUHexString(): String = this.toUHexString(" ")
@ -94,10 +85,6 @@ fun getGTK(sKey: String): Int {
return value
}
fun main() {
println(getGTK("ABCDEFGEFC"))
}
fun getCrc32(key: ByteArray): Int = CRC32().let { it.update(key); it.value.toInt() }

View File

@ -1,14 +1,18 @@
package net.mamoe.mirai.utils.config;
import kotlin.io.FilesKt;
import net.mamoe.mirai.MiraiServer;
import net.mamoe.mirai.utils.Utils;
import org.jetbrains.annotations.NotNull;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
import java.nio.charset.Charset;
import java.util.LinkedHashMap;
import java.util.Objects;
/**
* YAML-TYPE CONFIG
@ -35,7 +39,7 @@ public class MiraiConfig extends MiraiConfigSection<Object> {
Yaml yaml = new Yaml(dumperOptions);
String content = yaml.dump(this.sortedMap);
try {
Utils.writeFile(this.root, content);
new ByteArrayInputStream(content.getBytes()).transferTo(new FileOutputStream(this.root));
} catch (IOException e) {
e.printStackTrace();
}
@ -59,12 +63,7 @@ public class MiraiConfig extends MiraiConfigSection<Object> {
DumperOptions dumperOptions = new DumperOptions();
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Yaml yaml = new Yaml(dumperOptions);
try {
return yaml.loadAs(Utils.readFile(file), LinkedHashMap.class);
} catch (IOException e) {
e.printStackTrace();
}
return new LinkedHashMap<>();
return yaml.loadAs(String.join("\n", FilesKt.readLines(file, Charset.defaultCharset())), LinkedHashMap.class);
}

View File

@ -1,5 +1,5 @@
import net.mamoe.mirai.network.packet.login.ClientPasswordSubmissionPacket
import net.mamoe.mirai.util.toUHexString
import net.mamoe.mirai.utils.toUHexString
@ExperimentalUnsignedTypes
fun main(){