/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "stdafx.h" #include "model/eu07m/eu07m_pack.h" #include "model/eu07m/eu07m_io.h" #include "model/eu07m/eu07m_store.h" #include #include #include #include #include #include namespace eu07m { namespace { struct folder_model { std::filesystem::path path; model loaded; }; // a block met in the models: its bytes and the files which have it struct candidate { std::vector bytes; std::size_t uses{0}; bool collision{false}; // another block has the same hash }; std::vector read_file(std::filesystem::path const &Path) { std::ifstream file{Path, std::ios::binary | std::ios::ate}; if (false == file.good()) return {}; std::vector data(static_cast(file.tellg())); file.seekg(0); file.read(reinterpret_cast(data.data()), static_cast(data.size())); return file.good() ? data : std::vector{}; } bool write_file(std::filesystem::path const &Path, std::span Data) { // next to it first, so a failed write leaves the file as it was auto const temporary{std::filesystem::path{Path}.concat(".tmp")}; std::error_code error; { std::ofstream file{temporary, std::ios::binary}; file.write(reinterpret_cast(Data.data()), static_cast(Data.size())); // closed before the check: what still sat in the buffer of the stream is written by the close file.close(); if (file.fail()) { std::filesystem::remove(temporary, error); return false; } } std::filesystem::rename(temporary, Path, error); if (error) { std::error_code ignored; std::filesystem::remove(temporary, ignored); return false; } return true; } bool is_model(std::filesystem::directory_entry const &Entry) { auto extension{Entry.path().extension().string()}; std::ranges::transform(extension, extension.begin(), [](unsigned char const Letter) { return static_cast(std::tolower(Letter)); }); return Entry.is_regular_file() && extension == ".eu07m"; } // the models of the folder read whole, with the store there is; false when one can't be read bool read_models(std::filesystem::path const &Folder, std::vector &Models, std::uintmax_t &Bytes, std::ostream &Log) { auto const storepath{Folder / store_file_name}; auto store{std::make_shared()}; std::error_code error; if (std::string reason; std::filesystem::exists(storepath, error) && false == store->open(storepath, reason)) { Log << std::format("{}: {}\n", storepath.string(), reason); return false; } if (std::filesystem::exists(storepath, error)) Bytes += std::filesystem::file_size(storepath, error); read_options options; options.blocks = store_source(store); for (auto const &entry : std::filesystem::directory_iterator{Folder, error}) { if (false == is_model(entry)) continue; auto const data{read_file(entry.path())}; folder_model item{entry.path(), {}}; if (auto const result{read(data, item.loaded, options)}; false == result.ok) { Log << std::format("{}: {}\n", entry.path().string(), result.error); return false; } Bytes += data.size(); Models.push_back(std::move(item)); } std::ranges::sort(Models, {}, &folder_model::path); return true; } bool same_geometry(model const &A, model const &B) { return A.positions.size() == B.positions.size() && std::ranges::equal(std::as_bytes(std::span{A.positions}), std::as_bytes(std::span{B.positions})) && std::ranges::equal(std::as_bytes(std::span{A.vertex_attributes}), std::as_bytes(std::span{B.vertex_attributes})) && std::ranges::equal(std::as_bytes(std::span{A.weights}), std::as_bytes(std::span{B.weights})) && std::ranges::equal(std::as_bytes(std::span{A.weights_wide}), std::as_bytes(std::span{B.weights_wide})) && std::ranges::equal(std::as_bytes(std::span{A.vertex_userdata}), std::as_bytes(std::span{B.vertex_userdata})) && std::ranges::equal(std::as_bytes(std::span{A.bindings}), std::as_bytes(std::span{B.bindings})) && A.indices16 == B.indices16 && A.indices32 == B.indices32; } // the blocks of the shareable primitives of at least MinBytes in all the models, with the files using each std::map gather(std::vector const &Models, std::size_t const MinBytes) { std::map candidates; for (auto const &item : Models) { for (std::uint32_t idx = 0; idx < item.loaded.primitives.size(); ++idx) { if (false == shareable(item.loaded, idx)) continue; auto block{make_block(item.loaded, item.loaded.primitives[idx])}; if (block.size() < MinBytes) continue; auto &found{candidates[block_hash(block)]}; if (found.bytes.empty()) found.bytes = std::move(block); else if (found.bytes != block) found.collision = true; ++found.uses; } } return candidates; } // the references of the model to the blocks of the store std::vector references(model const &Model, std::map const &Shared, std::size_t const MinBytes) { std::vector result; for (std::uint32_t idx = 0; idx < Model.primitives.size(); ++idx) { if (false == shareable(Model, idx)) continue; auto const block{make_block(Model, Model.primitives[idx])}; if (block.size() < MinBytes) continue; auto const hash{block_hash(block)}; if (false == Shared.contains(hash)) continue; auto const &primitive{Model.primitives[idx]}; geometry_reference_record reference; reference.primitive = idx; reference.hash = hash; reference.vertex_count = primitive.vertex_count; reference.index_count = (primitive.flags & primitive_flags::indexed) != 0 ? primitive.index_count : 0u; result.push_back(reference); } return result; } // the file of the model, read back with the store and compared with the model before it's written bool write_checked(folder_model const &Item, model const &Packed, geometry_store const &Store, std::uintmax_t &Bytes, std::ostream &Log) { auto const file{write(Packed)}; model check; read_options options; options.blocks = [&Store](geometry_reference_record const &Reference, std::vector &Block) { return Store.block(Reference.hash, Block); }; if (auto const result{read(file, check, options)}; false == result.ok || false == same_geometry(check, Item.loaded)) { Log << std::format("{}: the model read back isn't the same, left as it was{}\n", Item.path.string(), result.ok ? "" : ": " + result.error); return false; } if (false == write_file(Item.path, file)) { Log << std::format("{}: can't write it\n", Item.path.string()); return false; } Bytes += file.size(); return true; } // the store of the folder made of the blocks it has (when it has one which can be read) and the new ones bool write_bridge_store(std::filesystem::path const &Folder, std::vector> const &Blocks, std::ostream &Log) { auto all{Blocks}; std::error_code error; if (auto const path{Folder / store_file_name}; std::filesystem::exists(path, error)) { geometry_store old; if (std::string reason; old.open(path, reason)) { for (auto const &id : old.blocks()) { if (std::vector block; old.block(id, block)) all.push_back(std::move(block)); } } else { Log << std::format("the old store can't be read ({}), its blocks aren't kept\n", reason); } } if (false == write_file(Folder / store_file_name, write_store(all))) { Log << "can't write the store\n"; return false; } return true; } } // namespace pack_result pack_folder(std::filesystem::path const &Folder, std::size_t const MinBytes, std::ostream &Log) { pack_result result; std::vector models; if (false == read_models(Folder, models, result.bytes_before, Log)) return result; result.models = models.size(); auto candidates{gather(models, MinBytes)}; std::erase_if(candidates, [](auto const &Entry) { return Entry.second.uses < 2 || Entry.second.collision; }); std::vector> blocks; blocks.reserve(candidates.size()); for (auto const &entry : candidates) blocks.push_back(entry.second.bytes); auto const storefile{write_store(blocks)}; geometry_store store; if (std::string reason; false == store.open(storefile, reason)) { Log << std::format("the store made isn't valid: {}\n", reason); return result; } // first a store with the blocks of the old one and the new ones together: while the models are written one by one, the folder // holds models packed against either, and a write failing half way leaves them all loading if (false == write_bridge_store(Folder, blocks, Log)) return result; // then the models, each checked against the store; the store of the new blocks alone replaces the other once they all are for (auto const &item : models) { auto packed{item.loaded}; packed.geometry_references = references(packed, candidates, MinBytes); result.references += packed.geometry_references.size(); if (false == write_checked(item, packed, store, result.bytes_after, Log)) return result; } if (false == write_file(Folder / store_file_name, storefile)) { Log << "can't write the store\n"; return result; } result.bytes_after += storefile.size(); result.blocks = store.size(); result.ok = true; Log << std::format("{}: {} models, {} blocks in the store, {} references; {} bytes -> {} bytes\n", Folder.string(), result.models, result.blocks, result.references, result.bytes_before, result.bytes_after); return result; } pack_result unpack_folder(std::filesystem::path const &Folder, std::ostream &Log) { pack_result result; std::vector models; if (false == read_models(Folder, models, result.bytes_before, Log)) return result; result.models = models.size(); for (auto &item : models) { item.loaded.geometry_references.clear(); auto const file{write(item.loaded)}; if (false == write_file(item.path, file)) { Log << std::format("{}: can't write it\n", item.path.string()); return result; } result.bytes_after += file.size(); } result.ok = true; Log << std::format("{}: {} models written whole, {} bytes; the store can be deleted\n", Folder.string(), result.models, result.bytes_after); return result; } } // namespace eu07m