-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcScenarioResource.java
192 lines (135 loc) · 5.85 KB
/
cScenarioResource.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
package sporemodder.file.simulator;
import java.io.IOException;
import java.io.PrintWriter;
import emord.filestructures.FileStream;
import emord.filestructures.MemoryStream;
import emord.filestructures.StreamReader;
import emord.filestructures.StreamWriter;
import sporemodder.MainApp;
import sporemodder.file.ResourceKey;
import sporemodder.file.dbpf.DBPFItem;
import sporemodder.file.dbpf.DatabasePackedFile;
import sporemodder.file.prop.PropertyList;
public class cScenarioResource {
// for testing
private byte[] propData;
private final PropertyList propertyList = new PropertyList();
private MemoryStream dataStream;
private final cScenarioResourceAttributes scenario = new cScenarioResourceAttributes();
public void read(StreamReader stream) throws Exception {
long magic = stream.readLEUInt();
if (magic != 0xB523D4F9L) {
throw new IOException("Incorrect magic number.");
}
int version = stream.readLEInt();
if (version < 3) {
throw new IOException("Unsupported version.");
}
propertyList.read(stream);
long endPos = stream.getFilePointer();
stream.seek(8);
propData = new byte[(int) (endPos - 8)];
stream.read(propData);
stream.seek(endPos);
int dataVersion = stream.readLEInt();
int dbpfSize = stream.readLEInt();
byte[] dbpfData = new byte[dbpfSize];
stream.read(dbpfData);
MemoryStream dbpfStream = new MemoryStream(dbpfData);
DatabasePackedFile dbpf = new DatabasePackedFile();
dbpf.read(dbpfStream);
dataStream = dbpf.getItem(new ResourceKey(0x1897C18, 0x1897C18, 0x1897C18)).processFile(dbpfStream);
scenario.read(dataStream);
}
public void write(StreamWriter stream, boolean convertData) throws IOException {
stream.writeLEUInt(0xB523D4F9L);
stream.writeLEInt(17);
if (propData != null) {
stream.write(propData);
}
else {
propertyList.write(stream);
}
// Data version
stream.writeLEInt(17);
// Write the DBPF
MemoryStream dbpfStream = new MemoryStream();
DatabasePackedFile dbpf = new DatabasePackedFile();
DBPFItem item = new DBPFItem();
item.name.setGroupID(0x1897C18);
item.name.setInstanceID(0x1897C18);
item.name.setTypeID(0x1897C18);
if (convertData) {
dataStream = new MemoryStream();
//TODO write data
}
dbpf.writeHeader(dbpfStream);
dbpf.writeFile(dbpfStream, item, dataStream.toByteArray(), true);
dbpf.writeIndex(dbpfStream);
// Rewrite the DBPF header
dbpfStream.seek(0);
dbpf.writeHeader(dbpfStream);
// DBPF size
stream.writeLEUInt(dbpfStream.length());
stream.write(dbpfStream.toByteArray());
}
public String printData() {
StringBuilder sb = new StringBuilder();
String tabulation = "\t";
sb.append('{');
sb.append('\n');
scenario.print(sb, tabulation);
sb.append('}');
return sb.toString();
}
public String printDataXML() {
StringBuilder sb = new StringBuilder();
String tabulation = "\t";
sb.append("<cScenarioResource>\n");
scenario.printXML(sb, tabulation);
sb.append("</cScenarioResource>\n");
return sb.toString();
}
private static void extractTest() throws Exception {
MainApp.testInit();
// String outputPath = "E:\\Eric\\Eclipse Projects\\SporeModder FX\\Projects\\Terrain Test\\extracted\\";
// String path = "E:\\Eric\\Eclipse Projects\\SporeModder FX\\Projects\\Terrain Test\\adventureImages_1~\\0x1999746C.0x366A930D";
String outputPath = "E:\\Eric\\Eclipse Projects\\SporeModder FX\\Projects\\Terrain Test\\real extracted\\";
String path = "E:\\Eric\\Eclipse Projects\\SporeModder FX\\Projects\\Terrain Test\\adventureImages_1~\\0x199C129D.0x366A930D";
// String outputPath = "E:\\Eric\\Eclipse Projects\\SporeModder FX\\Projects\\Terrain Test\\re extracted\\";
// String path = "E:\\Eric\\Eclipse Projects\\SporeModder FX\\Projects\\Terrain Test\\adventureImages_1~\\SRNS_PlanetTemplate_100.0x366A930D";
// String outputPath = "E:\\Eric\\Eclipse Projects\\SporeModder FX\\Projects\\Terrain Test\\small_planet_extracted\\";
// String path = "E:\\Eric\\Eclipse Projects\\SporeModder FX\\Projects\\Terrain Test\\adventureImages_1~\\SRNS_PlanetTemplate_100_original.0x366A930D";
try (FileStream stream = new FileStream(path, "r")) {
cScenarioResource resource = new cScenarioResource();
resource.read(stream);
try (PrintWriter writer = new PrintWriter(outputPath + "planet.prop.prop_t")) {
writer.write(resource.propertyList.toArgScript());
}
resource.dataStream.writeToFile(outputPath + "data.0x1897C18");
try (PrintWriter writer = new PrintWriter(outputPath + "data.json")) {
writer.write(resource.printData());
}
}
}
public static void main(String[] args) throws Exception {
MainApp.testInit();
// String path = "E:\\Eric\\SporeModder\\Projects\\Spore_EP1_Data.package.unpacked\\adventureImages_1~\\#0D19B6DA.#366A930D";
// String outputPath = "E:\\Eric\\Eclipse Projects\\SporeModder FX\\Projects\\scenarioTest\\#0D19B6DA\\";
String path = "E:\\Eric\\SporeModder\\Projects\\Spore_EP1_Data.package.unpacked\\adventureImages_1~\\#3C546825.#366A930D";
String outputPath = "E:\\Eric\\Eclipse Projects\\SporeModder FX\\Projects\\scenarioTest\\temple #3C546825\\";
// String path = "E:\\Eric\\Eclipse Projects\\SporeModder FX\\Projects\\EditorSaves\\adventureImages_1~\\0x1999746C.0x366A930D";
// String outputPath = "E:\\Eric\\Eclipse Projects\\SporeModder FX\\Projects\\Terrain Test\\extracted\\";
try (FileStream stream = new FileStream(path, "r")) {
cScenarioResource resource = new cScenarioResource();
resource.read(stream);
try (PrintWriter writer = new PrintWriter(outputPath + "planet.prop.prop_t")) {
writer.write(resource.propertyList.toArgScript());
}
resource.dataStream.writeToFile(outputPath + "data.0x1897C18");
try (PrintWriter writer = new PrintWriter(outputPath + "data.xml")) {
writer.write(resource.printDataXML());
}
}
}
}