This just loads the directory part of a pakfile such that subsequent LoadFile calls know how to load from it.
| 190 | // This just loads the directory part of a pakfile such that subsequent LoadFile calls know how |
| 191 | // to load from it. |
| 192 | bool LoadPakDir(const char *lpak) { |
| 193 | // This supports reading from a pakfile > 4GB even on a 32bit system! (as long as individual |
| 194 | // files in it are <= 4GB). |
| 195 | auto plen = LoadFile(lpak, nullptr, 0, 0); |
| 196 | if (plen < 0) return false; |
| 197 | string header; |
| 198 | if (LoadFile(lpak, &header, plen - (int64_t)header_size, header_size) < 0 || |
| 199 | memcmp(header.c_str() + header_size - magic_size, magic, magic_size)) return false; |
| 200 | auto read_unaligned64 = [](const void *p) { |
| 201 | int64_t r; |
| 202 | memcpy(&r, p, sizeof(int64_t)); |
| 203 | return LE(r); |
| 204 | }; |
| 205 | auto num = (size_t)read_unaligned64(header.c_str()); |
| 206 | auto dirstart = read_unaligned64((int64_t *)header.c_str() + 1); |
| 207 | auto version = read_unaligned64((int64_t *)header.c_str() + 2); |
| 208 | if (version > 1) return false; |
| 209 | if (dirstart > plen) return false; |
| 210 | string dir; |
| 211 | if (LoadFile(lpak, &dir, dirstart, plen - dirstart - (int64_t)header_size) < 0) |
| 212 | return false; |
| 213 | auto namestarts = (int64_t *)(dir.c_str() + dir.length()) - num; |
| 214 | auto filestarts = namestarts - num; |
| 215 | auto uncompressed = filestarts - num; |
| 216 | for (size_t i = 0; i < num; i++) { |
| 217 | auto name = string_view(dir.c_str() + (read_unaligned64(namestarts + i) - dirstart)); |
| 218 | auto off = read_unaligned64(filestarts + i); |
| 219 | auto end = i < num + 1 ? read_unaligned64(filestarts + i + 1) : dirstart; |
| 220 | auto len = end - off; |
| 221 | LOG_INFO("pakfile dir: ", name, " : ", len); |
| 222 | AddPakFileEntry(lpak, name, off, len, read_unaligned64(uncompressed + i)); |
| 223 | } |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | bool LoadByteCode(string &bytecode) { |
| 228 | if (LoadFile(bcname, &bytecode) < 0) return false; |