mirror of
https://github.com/mamoe/mirai.git
synced 2025-02-07 23:49:21 +08:00
update
This commit is contained in:
parent
d3ba95c5b0
commit
b2f11120b5
@ -1,7 +1,19 @@
|
||||
import kotlin.ranges.IntRange;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
import net.mamoe.mirai.network.Protocol;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author NaturalHG
|
||||
* This could be used to check packet encoding..
|
||||
* but better to run under UNIX
|
||||
*
|
||||
* @author NaturalHG
|
||||
*/
|
||||
public class HexComparator {
|
||||
|
||||
@ -9,17 +21,74 @@ public class HexComparator {
|
||||
* a string result
|
||||
*/
|
||||
|
||||
private static String RED = "\033[31m";
|
||||
private static final String RED = "\033[31m";
|
||||
|
||||
private static String GREEN = "\033[33m";
|
||||
private static final String GREEN = "\033[33m";
|
||||
|
||||
private static String UNKNOWN = "\033[30m";
|
||||
private static final String UNKNOWN = "\033[30m";
|
||||
|
||||
public static String compare(String hex1s, String hex2s) {
|
||||
private static final String UNKNOWN2 = "\033[34m";
|
||||
|
||||
private static class ConstMatcher {
|
||||
private static final List<Field> CONST_FIELDS = new LinkedList<>() {{
|
||||
List.of(Protocol.class).forEach(aClass -> Arrays.stream(aClass.getDeclaredFields()).peek(this::add).forEach(Field::trySetAccessible));
|
||||
}};
|
||||
|
||||
private final List<Match> matches = new LinkedList<>();
|
||||
|
||||
private ConstMatcher(String hex) {
|
||||
CONST_FIELDS.forEach(field -> {
|
||||
for (IntRange match : match(hex, field)) {
|
||||
matches.add(new Match(match, field.getName()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private String getMatchedConstName(int hexNumber) {
|
||||
for (Match match : this.matches) {
|
||||
if (match.range.contains(hexNumber)) {
|
||||
return match.constName;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static List<IntRange> match(String hex, Field field) {
|
||||
final String constValue;
|
||||
try {
|
||||
constValue = ((String) field.get(null)).trim();
|
||||
if (constValue.length() / 3 <= 3) {//Minimum numbers of const hex bytes
|
||||
return new LinkedList<>();
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (ClassCastException ignored) {
|
||||
return new LinkedList<>();
|
||||
}
|
||||
return new LinkedList<>() {{
|
||||
int index = 0;
|
||||
while ((index = hex.indexOf(constValue, index + 1)) != -1) {
|
||||
add(new IntRange(index / 3, (index + constValue.length()) / 3));
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
@ToString
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
private static class Match {
|
||||
private IntRange range;
|
||||
private String constName;
|
||||
}
|
||||
}
|
||||
|
||||
private static String compare(String hex1s, String hex2s) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
String[] hex1 = hex1s.trim().replace("\n", "").split(" ");
|
||||
String[] hex2 = hex2s.trim().replace("\n", "").split(" ");
|
||||
ConstMatcher constMatcher1 = new ConstMatcher(hex1s);
|
||||
ConstMatcher constMatcher2 = new ConstMatcher(hex2s);
|
||||
|
||||
if (hex1.length == hex2.length) {
|
||||
builder.append(GREEN).append("长度一致:").append(hex1.length);
|
||||
@ -40,11 +109,22 @@ public class HexComparator {
|
||||
if (hex1.length <= i) {
|
||||
h1 = RED + "__";
|
||||
isDif = true;
|
||||
} else {
|
||||
String matchedConstName = constMatcher1.getMatchedConstName(i);
|
||||
if (matchedConstName != null) {
|
||||
h1 = UNKNOWN2 + hex1[i];
|
||||
}
|
||||
}
|
||||
if (hex2.length <= i) {
|
||||
h2 = RED + "__";
|
||||
isDif = true;
|
||||
} else {
|
||||
String matchedConstName = constMatcher2.getMatchedConstName(i);
|
||||
if (matchedConstName != null) {
|
||||
h2 = UNKNOWN2 + hex2[i];
|
||||
}
|
||||
}
|
||||
|
||||
if (h1 == null && h2 == null) {
|
||||
h1 = hex1[i];
|
||||
h2 = hex2[i];
|
||||
@ -90,7 +170,6 @@ public class HexComparator {
|
||||
return String.valueOf(number);
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(HexComparator.compare(
|
||||
"02 37 13 08 28 31 03 76 E4 B8 DD 03 00 00 00 01 01 01 00 00 68 20 00 00 00 00 00 01 01 03 00 19 02 6D 28 41 D2 A5 6F D2 FC 3E 2A 1F 03 75 DE 6E 28 8F A8 19 3E 5F 16 49 D3 00 00 00 10 EF 4A 36 6A 16 A8 E6 3D 2E EA BD 1F 98 C1 3C DA B4 4E C2 36 01 12 00 38 A9 76 88 3D C3 26 28 32 77 D1 EA 55 23 52 12 A5 75 1F EB F9 3B 6D 36 50 5E CF 8E 16 16 19 10 6B 83 FA 6F 0E D6 8E ED A4 BF BC 6C DE 34 61 D9 BB 96 16 C5 CE FE 76 27 22 03 0F 00 11 00 0F 44 45 53 4B 54 4F 50 2D 4D 31 37 4A 52 45 55 00 05 00 06 00 02 76 E4 B8 DD 00 06 00 78 9F DE A7 F7 6D 6C B3 D6 91 47 B8 3D 97 EB 79 ED 82 A6 9C 77 1E FC 74 C6 0C 31 99 DA 97 21 CD 90 00 15 00 30 00 01 01 27 9B C7 F5 00 10 65 03 FD 8B 00 00 00 00 00 00 00 00 00 00 00 00 02 90 49 55 33 00 10 15 74 C4 89 85 7A 19 F5 5E A9 C9 A3 5E 8A 5A 9B 00 1A 00 40 2D FB 2B 26 88 BE 00 43 A9 25 00 06 5A DD D3 1F 7D 95 9C A3 50 25 C4 65 F8 C6 0E 09 A0 00 30 57 26 13 7F 5A C6 01 F8 F6 63 4C 67 13 B3 B1 0F 30 89 60 81 B8 1A CD D5 02 FB 26 45 A5 27 79 34 D0 00 18 00 16 00 01 00 00 04 53 00 00 00 01 00 00 15 85 76 E4 B8 DD 00 00 00 00 01 03 00 14 00 01 00 10 60 C9 5D A7 45 70 04 7F 21 7D 84 50 5C 66 A5 C6 03 12 00 05 01 00 00 00 01 05 08 00 05 10 00 00 00 00 03 13 00 19 01 01 02 00 10 04 EA 78 D1 A4 FF CD CC 7C B8 D4 12 7D BB 03 AA 00 00 00 00 01 02 00 62 00 01 04 EB B7 C1 86 F9 08 96 ED 56 84 AB 50 85 2E 48 00 38 E9 AA 2B 4D 26 4C 76 18 FE 59 D5 A9 82 6A 0C 04 B4 49 50 D7 9B B1 FE 5D 97 54 8D 82 F3 22 C2 48 B9 C9 22 69 CA 78 AD 3E 2D E9 C9 DF A8 9E 7D 8C 8D 6B DF 4C D7 34 D0 D3 00 14 54 30 26 84 06 E4 97 E3 63 37 C5 8C 34 72 2D C3 6B 74 83 4C 57 17 EF 65 F7 66 94 6B 2A A4 7F E9 72 11 3B 33 0A 03\n"
|
||||
|
Loading…
Reference in New Issue
Block a user