| 561 | } |
| 562 | |
| 563 | void World::load_from_file(const std::filesystem::path& filePathToLoadFrom, std::string_view buffer) { |
| 564 | filePath = filePathToLoadFrom; |
| 565 | |
| 566 | std::string byteDataFromFile; |
| 567 | |
| 568 | if(buffer.empty()) { |
| 569 | byteDataFromFile = read_file_to_string(filePath); |
| 570 | buffer = byteDataFromFile; |
| 571 | } |
| 572 | |
| 573 | if(buffer.size() < VersionConstants::SAVEFILE_HEADER_LEN) |
| 574 | throw std::runtime_error("[World::load_from_file] File is not an InfiniPaint canvas (File too small)"); |
| 575 | |
| 576 | std::string_view fileHeader = buffer.substr(0, VersionConstants::SAVEFILE_HEADER_LEN); |
| 577 | VersionNumber fileVersion = VersionConstants::header_to_version_number(std::string(fileHeader)); |
| 578 | std::string_view uncompressedDataView; |
| 579 | std::vector<char> uncompressedDataVector; |
| 580 | |
| 581 | if(fileVersion < VersionNumber(0, 1, 0)) |
| 582 | uncompressedDataView = std::string_view(buffer.data() + VersionConstants::SAVEFILE_HEADER_LEN, buffer.size() - VersionConstants::SAVEFILE_HEADER_LEN); |
| 583 | else { |
| 584 | uncompressedDataVector.resize(ZSTD_getFrameContentSize(buffer.data() + VersionConstants::SAVEFILE_HEADER_LEN, buffer.size() - VersionConstants::SAVEFILE_HEADER_LEN)); |
| 585 | size_t trueUncompressedSize = ZSTD_decompress(uncompressedDataVector.data(), uncompressedDataVector.size(), buffer.data() + VersionConstants::SAVEFILE_HEADER_LEN, buffer.size() - VersionConstants::SAVEFILE_HEADER_LEN); |
| 586 | uncompressedDataView = std::string_view(uncompressedDataVector.data(), trueUncompressedSize); |
| 587 | } |
| 588 | |
| 589 | ByteMemStream f((char*)uncompressedDataView.data(), uncompressedDataView.size()); |
| 590 | |
| 591 | Logger::get().log(Logger::LogType::INFO, "Loading file from version " + version_numbers_to_version_str(fileVersion)); |
| 592 | |
| 593 | cereal::PortableBinaryInputArchive a(f); |
| 594 | load_file(a, fileVersion); |
| 595 | |
| 596 | Logger::get().log(Logger::LogType::DESKTOP_USERINFO, "File loaded"); |
| 597 | set_name(filePath.stem().string()); |
| 598 | } |
| 599 | |
| 600 | void World::save_file(cereal::PortableBinaryOutputArchive& a) const { |
| 601 | drawData.cam.save_file(a, *this); |
nothing calls this directly
no test coverage detected