Skip to content
This repository was archived by the owner on Jun 28, 2025. It is now read-only.

Commit 8fbc8a3

Browse files
committed
Binary search for Pak01.pak entries
1 parent e2a3c73 commit 8fbc8a3

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

pak.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ void Pak::close() {
4141
_entriesCount = 0;
4242
}
4343

44+
static int comparePakEntry(const void *a, const void *b) {
45+
return strcasecmp(((PakEntry *)a)->name, ((PakEntry *)b)->name);
46+
}
47+
4448
void Pak::readEntries() {
4549
uint8_t header[12];
4650

@@ -75,6 +79,7 @@ void Pak::readEntries() {
7579
e->size = READ_LE_UINT32(buf + 0x3C);
7680
debug(DBG_PAK, "Pak::readEntries() buf '%s' size %d", e->name, e->size);
7781
}
82+
qsort(_entries, _entriesCount, sizeof(PakEntry), comparePakEntry);
7883
// the original executable descrambles the (ke)y.txt file and check the last 4 bytes.
7984
// this has been disabled in later re-releases and a key is bundled in the data files
8085
if (0) {
@@ -92,13 +97,9 @@ void Pak::readEntries() {
9297

9398
const PakEntry *Pak::find(const char *name) {
9499
debug(DBG_PAK, "Pak::find() '%s'", name);
95-
for (int i = 0; i < _entriesCount; ++i) {
96-
const PakEntry *e = &_entries[i];
97-
if (strcasecmp(e->name, name) == 0) {
98-
return e;
99-
}
100-
}
101-
return 0;
100+
PakEntry tmp;
101+
strcpy(tmp.name, name);
102+
return (const PakEntry *)bsearch(&tmp, _entries, _entriesCount, sizeof(PakEntry), comparePakEntry);
102103
}
103104

104105
void Pak::loadData(const PakEntry *e, uint8_t *buf, uint32_t *size) {

0 commit comments

Comments
 (0)