| 397 | } |
| 398 | |
| 399 | void AppStateIO::loadBinary(Lattice::Simulation& simulation, BaseRenderer& renderer, std::string_view path) { |
| 400 | std::ifstream file(path.data(), std::ios::binary | std::ios::ate); |
| 401 | if (!file) { |
| 402 | throw std::runtime_error("Failed to open save file: " + std::string(path)); |
| 403 | } |
| 404 | |
| 405 | std::streamsize fileSize = file.tellg(); |
| 406 | if (fileSize < static_cast<std::streamsize>(sizeof(uint32_t))) { |
| 407 | throw std::runtime_error("Save file is too small"); |
| 408 | } |
| 409 | file.seekg(0, std::ios::beg); |
| 410 | |
| 411 | uint32_t originalSize = 0; |
| 412 | file.read(reinterpret_cast<char*>(&originalSize), sizeof(originalSize)); |
| 413 | |
| 414 | size_t compressedSize = static_cast<size_t>(fileSize) - sizeof(uint32_t); |
| 415 | std::vector<std::byte> compressedBuffer(compressedSize); |
| 416 | if (!file.read(reinterpret_cast<char*>(compressedBuffer.data()), compressedSize)) { |
| 417 | throw std::runtime_error("Failed to read compressed data"); |
| 418 | } |
| 419 | |
| 420 | std::vector<std::byte> decompressedBuffer(originalSize); |
| 421 | size_t const dSize = ZSTD_decompress(decompressedBuffer.data(), originalSize, compressedBuffer.data(), compressedSize); |
| 422 | if (ZSTD_isError(dSize)) { |
| 423 | throw std::runtime_error(std::string("Zstd decompression failed: ") + ZSTD_getErrorName(dSize)); |
| 424 | } |
| 425 | |
| 426 | if (dSize != originalSize) { |
| 427 | throw std::runtime_error("Decompressed size mismatch"); |
| 428 | } |
| 429 | |
| 430 | AppSaveState appState{}; |
| 431 | try { |
| 432 | auto in = zpp::bits::in(decompressedBuffer); |
| 433 | in(appState).or_throw(); |
| 434 | } |
| 435 | catch (const std::exception& e) { |
| 436 | std::cerr << "Failed to deserialize save file: " << e.what() << std::endl; |
| 437 | return; |
| 438 | } |
| 439 | |
| 440 | // Заголовок |
| 441 | const auto& header = appState.header; |
| 442 | |
| 443 | // Симуляция |
| 444 | const auto& simState = appState.simulation; |
| 445 | |
| 446 | simulation.clear(); |
| 447 | |
| 448 | simulation.setSizeBox(simState.boxSize, simState.gridCellSize); |
| 449 | simulation.world().getNeighborList().setCutoff(simState.neighborListCutoff); |
| 450 | simulation.world().getNeighborList().setSkin(simState.neighborListSkin); |
| 451 | |
| 452 | simulation.setDt(simState.dt); |
| 453 | simulation.setIntegrator(simState.integrator); |
| 454 | simulation.setGravity(simState.gravity); |
| 455 | simulation.setBondFormationEnabled(simState.bondFormationEnabled); |
| 456 | simulation.setLJEnabled(simState.LJEnabled); |
nothing calls this directly
no test coverage detected