| 2367 | } |
| 2368 | |
| 2369 | bool ResourcePack::SavePack(const std::string& sFile, const std::string& sKey) |
| 2370 | { |
| 2371 | // Create/Overwrite the resource file |
| 2372 | std::ofstream ofs(sFile, std::ofstream::binary); |
| 2373 | if (!ofs.is_open()) return false; |
| 2374 | |
| 2375 | // Iterate through map |
| 2376 | uint32_t nIndexSize = 0; // Unknown for now |
| 2377 | ofs.write((char*)&nIndexSize, sizeof(uint32_t)); |
| 2378 | uint32_t nMapSize = uint32_t(mapFiles.size()); |
| 2379 | ofs.write((char*)&nMapSize, sizeof(uint32_t)); |
| 2380 | for (auto& e : mapFiles) |
| 2381 | { |
| 2382 | // Write the path of the file |
| 2383 | size_t nPathSize = e.first.size(); |
| 2384 | ofs.write((char*)&nPathSize, sizeof(uint32_t)); |
| 2385 | ofs.write(e.first.c_str(), nPathSize); |
| 2386 | |
| 2387 | // Write the file entry properties |
| 2388 | ofs.write((char*)&e.second.nSize, sizeof(uint32_t)); |
| 2389 | ofs.write((char*)&e.second.nOffset, sizeof(uint32_t)); |
| 2390 | } |
| 2391 | |
| 2392 | // 2) Write the individual Data |
| 2393 | std::streampos offset = ofs.tellp(); |
| 2394 | nIndexSize = (uint32_t)offset; |
| 2395 | for (auto& e : mapFiles) |
| 2396 | { |
| 2397 | // Store beginning of file offset within resource pack file |
| 2398 | e.second.nOffset = (uint32_t)offset; |
| 2399 | |
| 2400 | // Load the file to be added |
| 2401 | std::vector<uint8_t> vBuffer(e.second.nSize); |
| 2402 | std::ifstream i(e.first, std::ifstream::binary); |
| 2403 | i.read((char*)vBuffer.data(), e.second.nSize); |
| 2404 | i.close(); |
| 2405 | |
| 2406 | // Write the loaded file into resource pack file |
| 2407 | ofs.write((char*)vBuffer.data(), e.second.nSize); |
| 2408 | offset += e.second.nSize; |
| 2409 | } |
| 2410 | |
| 2411 | // 3) Scramble Index |
| 2412 | std::vector<char> stream; |
| 2413 | auto write = [&stream](const char* data, size_t size) { |
| 2414 | size_t sizeNow = stream.size(); |
| 2415 | stream.resize(sizeNow + size); |
| 2416 | memcpy(stream.data() + sizeNow, data, size); |
| 2417 | }; |
| 2418 | |
| 2419 | // Iterate through map |
| 2420 | write((char*)&nMapSize, sizeof(uint32_t)); |
| 2421 | for (auto& e : mapFiles) |
| 2422 | { |
| 2423 | // Write the path of the file |
| 2424 | size_t nPathSize = e.first.size(); |
| 2425 | write((char*)&nPathSize, sizeof(uint32_t)); |
| 2426 | write(e.first.c_str(), nPathSize); |