-
Notifications
You must be signed in to change notification settings - Fork 11
/
DofusSocketHelper.java
105 lines (87 loc) 路 2.65 KB
/
DofusSocketHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package fr.B4D.socket;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import fr.B4D.utils.HexHelper;
/**
* The {@code DofusSocketHelper} class is used to find values within a socket.
*
* @author Lucas
*
*/
public class DofusSocketHelper {
/**
* Minimum number of bytes on which the values can be stored.
*/
public Integer MIN_NUMBER_BYTES = 1;
/**
* Maximum number of bytes on which the values can be stored.
*/
public Integer MAX_NUMBER_BYTES = 5;
/**
* Socket in which look for a value.
*/
private byte[] socket;
/**
* Result of the research.
*/
private Map<Integer,String> results;
/**
* Constructs a {@code DofusSocketHelper} with a socket.
* @param dofusSocket - Socket in which look for a value.
*/
public DofusSocketHelper(DofusSocket dofusSocket) {
this.socket = dofusSocket.getPayload();
this.results = new HashMap<Integer,String>();
}
/**
* Returns the result of a research.
* @return Result of the research.
*/
public Map<Integer,String> getResults(){
return results;
}
/**
* Find for a given value if the socket.
* @param value - Value to find in the socket.
*/
public void find(Integer value) {
for(int length=MIN_NUMBER_BYTES; length <= MAX_NUMBER_BYTES; length++) { //For each length
for(int index=0; index <= socket.length-length; index++) {
byte[] subArray = Arrays.copyOfRange(socket, index, index+length);
DofusSocketElement element = new DofusSocketElement(subArray);
Integer asBigEndian = element.asBigEndian();
Integer asSmallEndian = element.asSmallEndian();
if(value.equals(asBigEndian) || value.equals(asSmallEndian)) { //If value found
/*byte[] before = Arrays.copyOfRange(socket, 0, index);
byte[] after = Arrays.copyOfRange(socket, index+length, socket.length);*/
StringBuilder builder = new StringBuilder();
builder.append(value + " found at index " + index);
if(value.equals(asBigEndian)) {
builder.append("(BigEndian)");
}
if(value.equals(asSmallEndian)) {
builder.append("(SmallEndian)");
}
builder.append(" : [" + HexHelper.toString(subArray) + "]");
results.put(index, builder.toString());
}
}
}
}
/**
* Prints the results of a research sorted by position.
*/
public void printResults() {
if(results.isEmpty()) {
System.out.println("No result found.");
}
else {
results
.entrySet()
.stream()
.sorted(Map.Entry.<Integer, String>comparingByKey())
.forEach(r -> System.out.println(r.getValue()));
}
}
}