Skip to content

Commit 0e2da05

Browse files
committed
fix for old(?) weird wpk files
1 parent a49f29e commit 0e2da05

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Whether .wem files are extracted as is or converted to .ogg format beforehand ca
3232

3333
# Building
3434
To build, I recommend using the msys/mingw toolchain. Build dependencies for the makefile-based build are `make`, `cmake` and `ninja`.
35-
A simple `make` call in the root directory should be enough to build the project.
35+
A simple `make` call in the root directory should be enough to build the project.
3636
Alternatively, `cmake` can be used as normal as well.
3737
As this project uses the winapi, it can only be compiled on/for windows platforms.
3838
For releases, I'm using an additional `-mwindows` linker command to make the program run console-less (just the window).

bnk-extract/wpk.c

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,38 @@ void parse_offsets(FILE* wpk_file, struct WPKFile* wpkfile)
4343

4444
void parse_data(FILE* wpk_file, struct WPKFile* wpkfile)
4545
{
46-
wpkfile->wpk_file_entries = malloc(wpkfile->file_count * sizeof(struct WPKFileEntry));
46+
int real_file_count = 0;
4747
for (uint32_t i = 0; i < wpkfile->offset_amount; i++) {
48-
fseek(wpk_file, wpkfile->offsets[i], SEEK_SET);
48+
if (wpkfile->offsets[i] != 0) // riot with their padding bytes :)
49+
real_file_count++;
50+
}
51+
wpkfile->file_count = real_file_count;
52+
53+
wpkfile->wpk_file_entries = malloc(wpkfile->file_count * sizeof(struct WPKFileEntry));
54+
int real_index = 0;
55+
for (uint32_t i = 0; i < wpkfile->offset_amount; i++, real_index++) {
56+
if (wpkfile->offsets[i] == 0) {
57+
real_index--;
58+
continue;
59+
}
60+
fseek(wpk_file, wpkfile->offsets[real_index], SEEK_SET);
4961

50-
assert(fread(&wpkfile->wpk_file_entries[i].data_offset, 4, 1, wpk_file) == 1);
51-
assert(fread(&wpkfile->wpk_file_entries[i].data_length, 4, 1, wpk_file) == 1);
62+
assert(fread(&wpkfile->wpk_file_entries[real_index].data_offset, 4, 1, wpk_file) == 1);
63+
assert(fread(&wpkfile->wpk_file_entries[real_index].data_length, 4, 1, wpk_file) == 1);
5264

5365
int filename_size;
5466
assert(fread(&filename_size, 4, 1, wpk_file) == 1);
55-
wpkfile->wpk_file_entries[i].filename = malloc(filename_size + 1);
67+
wpkfile->wpk_file_entries[real_index].filename = malloc(filename_size + 1);
5668
for (int j = 0; j < filename_size; j++) {
57-
wpkfile->wpk_file_entries[i].filename[j] = getc(wpk_file);
69+
wpkfile->wpk_file_entries[real_index].filename[j] = getc(wpk_file);
5870
fseek(wpk_file, 1, SEEK_CUR);
5971
}
60-
wpkfile->wpk_file_entries[i].filename[filename_size] = '\0';
61-
dprintf("string: \"%s\"\n", wpkfile->wpk_file_entries[i].filename);
72+
wpkfile->wpk_file_entries[real_index].filename[filename_size] = '\0';
73+
dprintf("string: \"%s\"\n", wpkfile->wpk_file_entries[real_index].filename);
6274

63-
wpkfile->wpk_file_entries[i].data = malloc(wpkfile->wpk_file_entries[i].data_length);
64-
fseek(wpk_file, wpkfile->wpk_file_entries[i].data_offset, SEEK_SET);
65-
assert(fread(wpkfile->wpk_file_entries[i].data, 1, wpkfile->wpk_file_entries[i].data_length, wpk_file) == wpkfile->wpk_file_entries[i].data_length);
75+
wpkfile->wpk_file_entries[real_index].data = malloc(wpkfile->wpk_file_entries[real_index].data_length);
76+
fseek(wpk_file, wpkfile->wpk_file_entries[real_index].data_offset, SEEK_SET);
77+
assert(fread(wpkfile->wpk_file_entries[real_index].data, 1, wpkfile->wpk_file_entries[real_index].data_length, wpk_file) == wpkfile->wpk_file_entries[real_index].data_length);
6678
}
6779
}
6880

0 commit comments

Comments
 (0)