libeuropa/io: Move some stuff in PakWriter.cpp to util/ headers

I wanted to do this anyways, so hey.
This commit is contained in:
Lily Tsuru 2025-01-08 13:36:19 -05:00
parent 6fa9f7122b
commit 974f173a7b
3 changed files with 52 additions and 19 deletions

View file

@ -0,0 +1,24 @@
//
// EuropaTools
//
// (C) 2021-2025 modeco80 <lily.modeco80@protonmail.ch>
//
// SPDX-License-Identifier: MIT
//
#ifndef EUROPA_UTIL_ALIGNHELPERS_HPP
#define EUROPA_UTIL_ALIGNHELPERS_HPP
#include <cstddef>
namespace europa::util {
/// Aligns a integral (e.g: file offset) to the provided value.
template <class T>
constexpr T AlignBy(T value, std::size_t alignment) {
return static_cast<T>(((value + (alignment - 1)) & ~(alignment - 1)));
}
} // namespace europa::util
#endif

View file

@ -0,0 +1,21 @@
//
// EuropaTools
//
// (C) 2021-2025 modeco80 <lily.modeco80@protonmail.ch>
//
// SPDX-License-Identifier: MIT
//
#ifndef EUROPA_UTIL_USEFULCONSTANTS_HPP
#define EUROPA_UTIL_USEFULCONSTANTS_HPP
#include <cstddef>
namespace europa::util {
/// The size of a CD-ROM (ISO 9660) secor.
constexpr static std::size_t kCDSectorSize = 0x800;
} // namespace europa::util
#endif

View file

@ -9,33 +9,25 @@
#include <algorithm> #include <algorithm>
#include <chrono> #include <chrono>
#include <europa/io/PakWriter.hpp> #include <europa/io/PakWriter.hpp>
#include <europa/util/AlignHelpers.hpp>
#include <europa/util/Overloaded.hpp>
#include <europa/util/TupleElement.hpp> #include <europa/util/TupleElement.hpp>
#include <europa/util/UsefulConstants.hpp>
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
#include "europa/structs/Pak.hpp" #include "europa/structs/Pak.hpp"
#include "europa/util/Overloaded.hpp"
#include "StreamUtils.h" #include "StreamUtils.h"
namespace europa::io { namespace europa::io {
/// The size of a CD-ROM (ISO 9660) secor.
constexpr auto kCDSectorSize = 0x800;
void PakWriter::SetVersion(structs::PakVersion version) { void PakWriter::SetVersion(structs::PakVersion version) {
// for now. // for now.
this->version = version; this->version = version;
} }
// FIXME: It would be nice to move to a util/ header
template <class T>
constexpr T AlignBy(T value, std::size_t alignment) {
return static_cast<T>(((value + (alignment - 1)) & ~(alignment - 1)));
}
void PakWriter::Write(std::ostream& os, std::vector<FlattenedType>&& vec, PakProgressReportSink& sink, SectorAlignment sectorAlignment) { void PakWriter::Write(std::ostream& os, std::vector<FlattenedType>&& vec, PakProgressReportSink& sink, SectorAlignment sectorAlignment) {
// Depending on the version, do a mix of runtime/compile-time dispatch to the right // Depending on the version, do a mix of runtime/compile-time dispatch to the right
// package format version we have been told to write. // package format version we have been told to write.
@ -74,11 +66,9 @@ namespace europa::io {
os.seekp(6, std::ostream::cur); os.seekp(6, std::ostream::cur);
} }
// Align first file to sector boundary. // Align the first file to start on the next sector boundary.
if(sectorAlignment == SectorAlignment::Align) if(sectorAlignment == SectorAlignment::Align)
os.seekp( os.seekp(util::AlignBy(static_cast<std::size_t>(os.tellp()), util::kCDSectorSize), std::istream::beg);
AlignBy(static_cast<int>(os.tellp()), kCDSectorSize),
std::istream::beg);
// Write all the file data // Write all the file data
for(auto& [filename, file] : sortedFiles) { for(auto& [filename, file] : sortedFiles) {
@ -115,11 +105,9 @@ namespace europa::io {
}); });
// clang-format on // clang-format on
// Align to sector boundary. // Align to the next sector boundary.
if(sectorAlignment == SectorAlignment::Align) if(sectorAlignment == SectorAlignment::Align)
os.seekp( os.seekp(util::AlignBy(static_cast<std::size_t>(os.tellp()), util::kCDSectorSize), std::istream::beg);
AlignBy(static_cast<int>(os.tellp()), kCDSectorSize),
std::istream::beg);
sink.OnEvent({ PakProgressReportSink::FileEvent::EventCode::FileWriteEnd, sink.OnEvent({ PakProgressReportSink::FileEvent::EventCode::FileWriteEnd,
filename }); filename });