18
18
****************************************************************************/
19
19
package sporemodder .file .simulator ;
20
20
21
+ import java .io .IOException ;
22
+ import java .util .ArrayList ;
21
23
import java .util .LinkedHashMap ;
24
+ import java .util .List ;
22
25
import java .util .Map ;
23
26
24
27
import sporemodder .file .filestructures .StreamReader ;
25
28
import sporemodder .file .filestructures .StreamWriter ;
26
29
import sporemodder .HashManager ;
27
30
import sporemodder .file .simulator .attributes .SimulatorAttribute ;
31
+ import sporemodder .util .IntPair ;
32
+
33
+ import javax .management .Attribute ;
28
34
29
35
public abstract class SimulatorClass {
30
36
37
+ public static int GENERIC_SERIALIZER_ID = 0x1A80D26 ;
38
+
39
+ public class AttributeHeader {
40
+ public int id ;
41
+ public int size ;
42
+
43
+ public AttributeHeader (int id , int size ) {
44
+ this .id = id ;
45
+ this .size = size ;
46
+ }
47
+ }
48
+
31
49
protected final LinkedHashMap <String , SimulatorAttribute > attributes = new LinkedHashMap <String , SimulatorAttribute >();
32
50
protected int classID ;
51
+ protected int classSize ;
33
52
34
53
public SimulatorClass (int classID ) {
35
54
this .classID = classID ;
@@ -42,6 +61,9 @@ public SimulatorClass(int classID) {
42
61
public int getClassID () {
43
62
return classID ;
44
63
}
64
+ public int getClassSize () {
65
+ return classSize ;
66
+ }
45
67
46
68
/**
47
69
* Returns the correct attribute type for the given attribute name. This must be implemented in subclasses to
@@ -60,33 +82,31 @@ public int calculateSize() {
60
82
61
83
return size ;
62
84
}
63
-
64
- public void read (StreamReader stream ) throws Exception {
65
-
85
+
86
+ public List <AttributeHeader > readHeader (StreamReader stream ) throws IOException {
66
87
classID = stream .readInt ();
67
-
68
- // size
69
- stream .readInt ();
70
-
88
+ classSize = stream .readInt ();
89
+
71
90
int count = stream .readInt ();
72
- int [] ids = new int [count ];
73
- int [] sizes = new int [count ];
74
-
75
- HashManager hasher = HashManager .get ();
76
-
77
- for (int i = 0 ; i < count ; i ++) {
78
- ids [i ] = stream .readInt ();
79
- sizes [i ] = stream .readInt ();
80
- }
81
-
91
+ List <AttributeHeader > headers = new ArrayList <>();
82
92
for (int i = 0 ; i < count ; i ++) {
83
- String name = hasher .getSimulatorName (ids [i ]);
84
-
85
- SimulatorAttribute attribute = createAttribute (name );
86
- attribute .read (stream , sizes [i ]);
87
-
88
- setAttribute (name , attribute );
93
+ headers .add (new AttributeHeader (stream .readInt (), stream .readInt ()));
89
94
}
95
+ return headers ;
96
+ }
97
+
98
+ public void read (StreamReader stream ) throws Exception {
99
+ List <AttributeHeader > headers = readHeader (stream );
100
+
101
+ HashManager hasher = HashManager .get ();
102
+ for (AttributeHeader header : headers ) {
103
+ String name = hasher .getSimulatorName (header .id );
104
+
105
+ SimulatorAttribute attribute = createAttribute (name );
106
+ attribute .read (stream , header .size );
107
+
108
+ setAttribute (name , attribute );
109
+ }
90
110
}
91
111
92
112
public void write (StreamWriter stream ) throws Exception {
@@ -168,4 +188,49 @@ public void printXML(StringBuilder sb, String tabulation) {
168
188
// default: return null;
169
189
// }
170
190
// }
191
+
192
+ public static class UnknownSimulatorClass extends SimulatorClass {
193
+ public UnknownSimulatorClass (int classID ) {
194
+ super (classID );
195
+ }
196
+
197
+ @ Override
198
+ public SimulatorAttribute createAttribute (String name ) {
199
+ return null ;
200
+ }
201
+ }
202
+
203
+ public static void scanClasses (StreamReader stream ) throws IOException {
204
+ while (stream .getFilePointer () + 4 < stream .length ()) {
205
+ int testValue = stream .readInt ();
206
+ if (testValue == GENERIC_SERIALIZER_ID ) {
207
+ stream .seek (stream .getFilePointer () - 4 );
208
+ SimulatorClass simulatorClass = new UnknownSimulatorClass (GENERIC_SERIALIZER_ID );
209
+ long classOffset = stream .getFilePointer ();
210
+ List <AttributeHeader > headers = simulatorClass .readHeader (stream );
211
+ long offset = stream .getFilePointer ();
212
+
213
+ System .out .println ("Found class at offset " + classOffset + ", attributes:" );
214
+ for (AttributeHeader header : headers ) {
215
+ StringBuilder sb = new StringBuilder ();
216
+ sb .append (" " );
217
+ sb .append (HashManager .get ().hexToString (header .id ));
218
+ String attributeName = HashManager .get ().getSimulatorName (header .id );
219
+ if (attributeName != null ) {
220
+ sb .append (" " );
221
+ sb .append (attributeName );
222
+ }
223
+ sb .append (": offset = " );
224
+ sb .append (offset );
225
+ sb .append (" size = " );
226
+ sb .append (header .size );
227
+ System .out .println (sb .toString ());
228
+
229
+ offset += header .size ;
230
+ }
231
+ System .out .println ();
232
+ stream .seek (offset );
233
+ }
234
+ }
235
+ }
171
236
}
0 commit comments