From 974f173a7b5a28c7098ed9dd862df0d4158a59ea Mon Sep 17 00:00:00 2001 From: modeco80 Date: Wed, 8 Jan 2025 13:36:19 -0500 Subject: [PATCH] libeuropa/io: Move some stuff in PakWriter.cpp to util/ headers I wanted to do this anyways, so hey. --- include/europa/util/AlignHelpers.hpp | 24 +++++++++++++++++++++++ include/europa/util/UsefulConstants.hpp | 21 ++++++++++++++++++++ src/libeuropa/io/PakWriter.cpp | 26 +++++++------------------ 3 files changed, 52 insertions(+), 19 deletions(-) create mode 100644 include/europa/util/AlignHelpers.hpp create mode 100644 include/europa/util/UsefulConstants.hpp diff --git a/include/europa/util/AlignHelpers.hpp b/include/europa/util/AlignHelpers.hpp new file mode 100644 index 0000000..0ac322f --- /dev/null +++ b/include/europa/util/AlignHelpers.hpp @@ -0,0 +1,24 @@ +// +// EuropaTools +// +// (C) 2021-2025 modeco80 +// +// SPDX-License-Identifier: MIT +// + +#ifndef EUROPA_UTIL_ALIGNHELPERS_HPP +#define EUROPA_UTIL_ALIGNHELPERS_HPP + +#include + +namespace europa::util { + + /// Aligns a integral (e.g: file offset) to the provided value. + template + constexpr T AlignBy(T value, std::size_t alignment) { + return static_cast(((value + (alignment - 1)) & ~(alignment - 1))); + } + +} // namespace europa::util + +#endif \ No newline at end of file diff --git a/include/europa/util/UsefulConstants.hpp b/include/europa/util/UsefulConstants.hpp new file mode 100644 index 0000000..92c0034 --- /dev/null +++ b/include/europa/util/UsefulConstants.hpp @@ -0,0 +1,21 @@ +// +// EuropaTools +// +// (C) 2021-2025 modeco80 +// +// SPDX-License-Identifier: MIT +// + +#ifndef EUROPA_UTIL_USEFULCONSTANTS_HPP +#define EUROPA_UTIL_USEFULCONSTANTS_HPP + +#include + +namespace europa::util { + + /// The size of a CD-ROM (ISO 9660) secor. + constexpr static std::size_t kCDSectorSize = 0x800; + +} // namespace europa::util + +#endif \ No newline at end of file diff --git a/src/libeuropa/io/PakWriter.cpp b/src/libeuropa/io/PakWriter.cpp index 3c60061..6d52e6b 100644 --- a/src/libeuropa/io/PakWriter.cpp +++ b/src/libeuropa/io/PakWriter.cpp @@ -9,33 +9,25 @@ #include #include #include +#include +#include #include +#include #include #include #include #include #include "europa/structs/Pak.hpp" -#include "europa/util/Overloaded.hpp" #include "StreamUtils.h" namespace europa::io { - /// The size of a CD-ROM (ISO 9660) secor. - constexpr auto kCDSectorSize = 0x800; - void PakWriter::SetVersion(structs::PakVersion version) { // for now. this->version = version; } - // FIXME: It would be nice to move to a util/ header - - template - constexpr T AlignBy(T value, std::size_t alignment) { - return static_cast(((value + (alignment - 1)) & ~(alignment - 1))); - } - void PakWriter::Write(std::ostream& os, std::vector&& vec, PakProgressReportSink& sink, SectorAlignment sectorAlignment) { // Depending on the version, do a mix of runtime/compile-time dispatch to the right // package format version we have been told to write. @@ -74,11 +66,9 @@ namespace europa::io { 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) - os.seekp( - AlignBy(static_cast(os.tellp()), kCDSectorSize), - std::istream::beg); + os.seekp(util::AlignBy(static_cast(os.tellp()), util::kCDSectorSize), std::istream::beg); // Write all the file data for(auto& [filename, file] : sortedFiles) { @@ -115,11 +105,9 @@ namespace europa::io { }); // clang-format on - // Align to sector boundary. + // Align to the next sector boundary. if(sectorAlignment == SectorAlignment::Align) - os.seekp( - AlignBy(static_cast(os.tellp()), kCDSectorSize), - std::istream::beg); + os.seekp(util::AlignBy(static_cast(os.tellp()), util::kCDSectorSize), std::istream::beg); sink.OnEvent({ PakProgressReportSink::FileEvent::EventCode::FileWriteEnd, filename });