forked from marieismywaifu/BNKEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BNKEditor.java
245 lines (220 loc) · 9.6 KB
/
BNKEditor.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
package bnkeditor;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* A tool for changing the sounds inside an Audiokinetic Wwise SoundBank, witout modifying any of the additional information inside the SoundBank.
* Just about all I know about this format, I know from reading <a href="http://wiki.xentax.com/index.php/Wwise_SoundBank_(*.bnk)">wiki.xentax.com/index.php/Wwise_SoundBank_(*.bnk)</a>.
* @author marieismywaifu
* @version 1.0
*/
public class BNKEditor {
private final CustomInputStream input;
private final byte[] bkhd;
private final int numWEMs;
private final byte[][] bufferedWEMs;
private final int[] ids, offsets, originalLengths, replacedLengths;
private final File[] replacements;
private final int offsetAbsolute, dataLength;
private byte[] rest;
/**
* Constructs a new <code>BNKEditor</code>.
* Initializes all the stuff that needs to be initialized, and gets ready for modifying.
* @param bnk the file to modify
* @param littleEndian the endianness of the file to modify
* @throws FileNotFoundException if the file cannot be found
* @throws IllegalArgumentException if the file has an unexpected layout (for example when it's not a SoundBank at all)
* @throws IOException if something else goes wrong
*/
public BNKEditor(File bnk, boolean littleEndian) throws IOException {
if (bnk.length() > Integer.MAX_VALUE) throw new IllegalArgumentException("The file is too big!");
input = new CustomInputStream(bnk, littleEndian);
// read and verify BKHD section
if (!"BKHD".equals(input.readMagic())) throw new IllegalArgumentException("The file doesn't have a BKHD section!");
int bkhdLength = input.readInt();
bkhd = input.read(bkhdLength);
// read and verify DIDX section header
if (!"DIDX".equals(input.readMagic())) throw new IllegalArgumentException("The file doesn't have a DIDX section!");
int didxLength = input.readInt();
if (didxLength % 12 != 0) throw new IllegalArgumentException("The file has a corrupted DIDX section! (its length is " + didxLength + ", which is not divisible by 12)");
// read DIDX section data
numWEMs = didxLength / 12;
bufferedWEMs = new byte[numWEMs][];
ids = new int[numWEMs];
offsets = new int[numWEMs];
originalLengths = new int[numWEMs];
replacedLengths = new int[numWEMs];
replacements = new File[numWEMs];
for (int i = 0; i < numWEMs; i++) {
int id = input.readInt(), offset = input.readInt(), length_ = input.readInt();
if (i > 0 && offset < offsets[i - 1]) throw new IllegalArgumentException("The file has a corrupted DIDX section! (WEM number " + (i + 1) + " is located at offset " + offset + ", while WEM number " + i + " is located at offset " + offsets[i - 1] + ")");
ids[i] = id;
offsets[i] = offset;
originalLengths[i] = length_;
replacedLengths[i] = length_;
}
// read and verify DATA section header
if (!"DATA".equals(input.readMagic())) throw new IllegalArgumentException("The file doesn't have a DATA section!");
dataLength = input.readInt();
int calc = 0;
for (int wem = 0; wem < numWEMs; wem++) {
calc += originalLengths[wem];
}
if (dataLength < calc) throw new IllegalArgumentException("The file has a corrupted DATA section! (calculated length: " + calc + ", actual length: " + dataLength + ")");
offsetAbsolute = (int) input.getCurrentPosition();
}
/**
* Returns an array containing the IDs of all the WEMs inside this SoundBank.
* Note that IDs seem to be completely random, so neighboring IDs might be scattered across multiple SoundBanks.
* Some (if not most) of the possible IDs may not exist at all.
* @return an array containing the IDs of all the WEMs inside this SoundBank
*/
public int[] getIDs() {
return ids;
}
/**
* Writes the specified WEM into a seperate file.
* @param index purpose explained below
* @param isID <code>false</code>: <code><b>index</b></code> is the position of the WEM inside the SoundBank
* <br><code>true</code>: <code><b>index</b></code> is the ID of the WEM to write
* @param wem the file to write the WEM to
* @throws ArrayIndexOutOfBoundsException if <code><b>isID</b></code> is true and the ID <code><b>index</b></code> does not exist in this SoundBank
* @throws IOException if something else goes wrong
*/
public void writeWEM(int index, boolean isID, File wem) throws IOException {
// find out the position in the arrays
int position = index;
if (isID) {
for (int i = 0; i < numWEMs; i++) {
if (ids[i] == index) {
position = i;
break;
}
}
}
// read all WEMs before the one you have to write
for (int i = 0; i <= position; i++) {
if (input.getCurrentPosition() <= offsets[i] + offsetAbsolute) {
input.skipUntil(offsets[i] + offsetAbsolute);
bufferedWEMs[i] = input.read(originalLengths[i]);
}
}
// actually write
wem.createNewFile();
CustomOutputStream output = new CustomOutputStream(wem, true);
output.write(bufferedWEMs[position]);
output.flushAndClose();
}
/**
* Marks the specified WEM as replaced with the specified file.
* Note that the specified file is not accessed until {@link #writeBNK(File, boolean)} is called.
* @param index purpose explained below
* @param isID <code>false</code>: <code><b>index</b></code> is the position of the WEM inside the SoundBank
* <br><code>true</code>: <code><b>index</b></code> is the ID of the WEM to replace
* @param replacement the file to replace the specified WEM with
* @throws ArrayIndexOutOfBoundsException if <code><b>isID</b></code> is true and the ID <code><b>index</b></code> does not exist in this SoundBank
* @throws IllegalArgumentException if the specified file is longer than <code>Integer.MAX_VALUE</code> bytes
*/
public void replace(int index, boolean isID, File replacement) {
if (replacement.length() > Integer.MAX_VALUE) throw new IllegalArgumentException("The WEM is too large!");
// find out the position in the arrays
int position = index;
if (isID) {
for (int i = 0; i < numWEMs; i++) {
if (ids[i] == index) {
position = i;
break;
}
}
}
replacements[position] = replacement;
replacedLengths[position] = (int) replacement.length();
}
/**
* Cancels the replacement of the specified WEM.
* Calling this method before marking the specified WEM as replaced causes no problems.
* @param index purpose explained below
* @param isID <code>false</code>: <code><b>index</b></code> is the position of the WEM inside the SoundBank
* <br><code>true</code>: <code><b>index</b></code> is the ID of the WEM to replace
* @throws ArrayIndexOutOfBoundsException if <code><b>isID</b></code> is true and the ID <code><b>index</b></code> does not exist in this SoundBank
*/
public void cancelReplacement(int index, boolean isID) {
// find out the position in the arrays
int position = index;
if (isID) {
for (int i = 0; i < numWEMs; i++) {
if (ids[i] == index) {
position = i;
break;
}
}
}
replacements[position] = null;
replacedLengths[position] = originalLengths[position];
}
/**
* Returns an array of all the files that will be accessed if {@link #writeBNK(File, boolean)} is called now.
* <code>null</code> means the WEM in this position in the SoundBank will not be replaced.
* Anything else means that file is accessed when <code>writeBNK</code> is called.
* @return an array of all the files that will be accessed if <code>writeBNK</code> is called now
*/
public File[] getReplacements() {
return replacements;
}
/**
* Writes the modified SoundBank to the specified file.
* Note that all replacements are accessed upon calling this method.
* If they've been deleted between calling {@link #replace(int, boolean, File)} and now, you're gonna run into some problems.
* @param bnk the file to write the modified SoundBank to
* @param littleEndian the endianness of the SoundBank
* @throws IOException if something goes wrong
*/
public void writeBNK(File bnk, boolean littleEndian) throws IOException {
bnk.createNewFile();
CustomOutputStream output = new CustomOutputStream(bnk, littleEndian);
// write BKHD section
output.writeString("BKHD");
output.writeInt(bkhd.length);
output.write(bkhd);
// write DIDX section header
output.writeString("DIDX");
output.writeInt(numWEMs * 12);
// write DIDX section data
output.writeInt(ids[0]);
output.writeInt(0);
output.writeInt(replacedLengths[0]);
int currentAddress = replacedLengths[0];
for (int i = 1; i < numWEMs; i++) {
output.writeInt(ids[i]);
output.writeInt(currentAddress);
output.writeInt(replacedLengths[i]);
currentAddress += replacedLengths[i];
}
// write DATA section header
output.writeString("DATA");
int calc = 0;
for (int i = 0; i < numWEMs; i++) {
calc += replacedLengths[i];
}
output.writeInt(calc);
// write DATA section data
for (int i = 0; i < numWEMs; i++) {
if (replacements[i] != null) {
CustomInputStream replacement = new CustomInputStream(replacements[i], littleEndian);
output.write(replacement.readRest());
continue;
}
for (int j = 0; j <= i; j++) {
if (input.getCurrentPosition() <= offsets[j] + offsetAbsolute) {
input.skipUntil(offsets[j] + offsetAbsolute);
bufferedWEMs[j] = input.read(originalLengths[j]);
}
}
output.write(bufferedWEMs[i]);
}
// write rest of file
if (input.getRemaining() > 0) rest = input.readRest();
output.write(rest);
output.flushAndClose();
}
}