-
Notifications
You must be signed in to change notification settings - Fork 2
/
WSFile.java
292 lines (263 loc) · 9.95 KB
/
WSFile.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package swg;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Created by Cekis on 2/18/2017.
*/
public class WSFile {
private List<WSNode> _nodes;
private String[] _types;
private String _fileName;
private String _areaName;
public List<WSNode> getNodes() {
if(_nodes == null){
_nodes = new ArrayList<>();
}
return _nodes;
}
public void setNodes(List<WSNode> _nodes) {
this._nodes = _nodes;
}
public String[] getTypes() {
return _types;
}
public void setTypes(String[] _types) {
this._types = _types;
}
public String getFileName() {
return _fileName;
}
public void setFileName(String fileName) {
this._fileName = fileName;
}
public String getAreaName() {
return _areaName;
}
public void setAreaName(String area) {
this._areaName = area;
}
public void readFile(String filename){
try{
_areaName = filename.replaceAll("snapshot/","").replaceAll(".ws","");
parseFile(new File(filename));
}
catch(Exception e){
e.printStackTrace();
}
}
private void parseFile(File file) throws IOException {
FileInputStream fis = null;
BufferedInputStream bi = null;
try {
byte[] FORM = new byte[4];
byte[] WSNPFORM = new byte[8];
byte[] t0001FORM = new byte[8];
byte[] lengthBuff = new byte[4];
byte[] OTNL = new byte[4];
fis = new FileInputStream(file);
bi = new BufferedInputStream(fis);
bi.read(FORM);
bi.read(new byte[4]); // length of form
bi.read(WSNPFORM);
bi.read(new byte[4]); // length of wsnpform
bi.read(t0001FORM);
bi.read(lengthBuff); // length of t0001form
int buffLength = ByteBuffer.wrap(lengthBuff).getInt();
byte[] nodesBuffer = new byte[buffLength];
bi.read(nodesBuffer);
bi.read(OTNL); // OTNL
bi.read(new byte[4]); // length of OTNL
bi.read(new byte[4]); // count (string)
List<String> listTypes = new ArrayList<>();
String type = "";
while(bi.available() > 0){
byte[] ba = new byte[1];
bi.read(ba);
while(ba[0] != 0){
type += (char) ba[0];
bi.read(ba);
}
listTypes.add(type);
type = "";
}
_types = listTypes.toArray(new String[listTypes.size()]);
parseNodes(nodesBuffer);
//System.out.println("objid\tcontainer\tserver_template_crc\tcell_index\tpx\tpy\tpz\tqw\tqx\tqy\tqz\tscripts\tobjvars");
//System.out.println("i\ti\th\ti\tf\tf\tf\tf\tf\tf\tf\ts\tp");
//printNodes(_nodes);
//saveNodesToFiles(_nodes);
}
catch(Exception e){
e.printStackTrace();
}
finally{
if(bi != null){
bi.close();
}
if(fis != null){
fis.close();
}
}
}
private void parseNodes(byte[] data) throws IOException {
int remaining = 0;
byte[] FORM = new byte[4];
byte[] formBuffer = new byte[4];
byte[] NODEFORM = new byte[8];
byte[] t0000DATA = new byte[8];
byte[] value = new byte[4];
WSNode tempNode = null;
BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(data));
try {
while (remaining < data.length) {
// header data
bis.read(FORM);
bis.read(formBuffer); // length
if(WSFile.IsAlpha(formBuffer)){
bis.read(new byte[4]);
remaining += 4;
}
bis.read(NODEFORM);
bis.read(new byte[4]); // length
bis.read(t0000DATA);
bis.read(new byte[4]); // length
remaining += 32; // total of header data
// start reading actual node
WSNode node = new WSNode();
bis.read(value);
node.setId(ByteBuffer.wrap(value).order(ByteOrder.LITTLE_ENDIAN).getInt());
bis.read(value);
node.setParentId(ByteBuffer.wrap(value).order(ByteOrder.LITTLE_ENDIAN).getInt());
bis.read(value);
node.setObjIndex(ByteBuffer.wrap(value).order(ByteOrder.LITTLE_ENDIAN).getInt());
node.setTemplate(getTypes()[node.getObjIndex()]);
bis.read(value); // no idea what this value is supposed to be.
bis.read(value);
node.setObjW(ByteBuffer.wrap(value).order(ByteOrder.LITTLE_ENDIAN).getFloat());
bis.read(value);
node.setObjX(ByteBuffer.wrap(value).order(ByteOrder.LITTLE_ENDIAN).getFloat());
bis.read(value);
node.setObjY(ByteBuffer.wrap(value).order(ByteOrder.LITTLE_ENDIAN).getFloat());
bis.read(value);
node.setObjZ(ByteBuffer.wrap(value).order(ByteOrder.LITTLE_ENDIAN).getFloat());
bis.read(value);
node.setX(ByteBuffer.wrap(value).order(ByteOrder.LITTLE_ENDIAN).getFloat());
bis.read(value);
node.setY(ByteBuffer.wrap(value).order(ByteOrder.LITTLE_ENDIAN).getFloat());
bis.read(value);
node.setZ(ByteBuffer.wrap(value).order(ByteOrder.LITTLE_ENDIAN).getFloat());
bis.read(value);
node.setType(ByteBuffer.wrap(value).order(ByteOrder.LITTLE_ENDIAN).getFloat());
bis.read(value);
node.setPOBCRC(ByteBuffer.wrap(value).order(ByteOrder.LITTLE_ENDIAN).getInt());
// 13 * 4 is 13 values read * 4 bytes each.
remaining += (13 * 4);
// add the node to our list of nodes.
if(node.getParentId() == 0) {
getNodes().add(node);
}
else{
tempNode = FindNodeById(node.getParentId());
if(tempNode == null){
throw new Exception("Parent Node " + node.getParentId() + " could not be found!");
}
tempNode.getNodes().add(node);
}
}
}
catch(Exception e){
System.out.println("EXCEPTION: " + e.getMessage());
e.printStackTrace();
}
finally {
bis.close();
}
}
// IsAlpha identifies if this is the first node or not. If so, we need to offset the read 4 bytes to make sure stuff lines up.
private static boolean IsAlpha(byte[] data) {
for (byte character : data) {
if ((((character <= 0x2f) || (character >= 0x3a)) && ((character <= 0x40) || (character >= 0x5c))) && (character != 0x20)) {
return false;
}
}
return true;
}
private WSNode FindNodeById(int parentId){
if(getNodes().size() > 0){
return FindChildren(parentId, getNodes().toArray(new WSNode[getNodes().size()]));
}
return null;
}
private WSNode FindChildren(int id, WSNode[] nodes){
for (WSNode node : nodes){
if(node == null) continue;
if(node.getId() == id){
return node;
}
else{
if(node.hasChildNodes()){
WSNode returnVal = FindChildren(id, node.getNodes().toArray(new WSNode[getNodes().size()]));
if(returnVal != null){
return returnVal;
}
}
}
}
return null;
}
private void saveNodesToFiles(List<WSNode> nodes) throws IOException {
String area = getAreaName();
String oldDir = "output/" + area;
File rmDir = new File(oldDir);
if(rmDir.exists()){
System.out.print("Removing files from folder " + area + "... ");
boolean result = false;
for(File f : rmDir.listFiles()){
result = f.delete();
if(!result) break;
}
System.out.println((result ? "removed." : "not removed."));
if(!result) System.exit(1);
System.out.print("Removing folder " + area + "... ");
System.out.println((rmDir.delete() ? "removed." : "not removed."));
}
boolean newFile = false;
for (WSNode node : nodes) {
int x = (int) Math.floor((double) (node.getX() + 8192) / 2048) + 1;
int y = (int) Math.floor((double) (node.getZ() + 8192) / 2048) + 1;
String fileName = "output/" + area + "/" + area + "_" + x + "_" + y + "_ws.tab";
File outFile = new File(fileName);
if(!outFile.exists()){
outFile.getParentFile().mkdirs();
newFile = true;
}
Writer writer;
try {
FileWriter fw = new FileWriter(outFile, true);
if(newFile){
fw.write("objid\tcontainer\tserver_template_crc\tcell_index\tpx\tpy\tpz\tqw\tqx\tqy\tqz\tscripts\tobjvars\n");
fw.write("i\ti\th\ti\tf\tf\tf\tf\tf\tf\tf\ts\tp\n");
}
fw.write(node.serialize(0,true));
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
newFile = false;
}
}
public List<WSNode> getAllNodes(){
List<WSNode> tmp = new ArrayList<>();
for (WSNode node : _nodes) {
tmp.add(node);
if(node.hasChildNodes()){
tmp.addAll(node.getAllNodes());
}
}
return tmp;
}
}