libeuropa/io: Move some stuff in PakWriter.cpp to util/ headers
I wanted to do this anyways, so hey.
This commit is contained in:
parent
6fa9f7122b
commit
974f173a7b
3 changed files with 52 additions and 19 deletions
24
include/europa/util/AlignHelpers.hpp
Normal file
24
include/europa/util/AlignHelpers.hpp
Normal 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
|
21
include/europa/util/UsefulConstants.hpp
Normal file
21
include/europa/util/UsefulConstants.hpp
Normal 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
|
|
@ -9,33 +9,25 @@
|
|||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <europa/io/PakWriter.hpp>
|
||||
#include <europa/util/AlignHelpers.hpp>
|
||||
#include <europa/util/Overloaded.hpp>
|
||||
#include <europa/util/TupleElement.hpp>
|
||||
#include <europa/util/UsefulConstants.hpp>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
|
||||
#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 <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) {
|
||||
// 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<int>(os.tellp()), kCDSectorSize),
|
||||
std::istream::beg);
|
||||
os.seekp(util::AlignBy(static_cast<std::size_t>(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<int>(os.tellp()), kCDSectorSize),
|
||||
std::istream::beg);
|
||||
os.seekp(util::AlignBy(static_cast<std::size_t>(os.tellp()), util::kCDSectorSize), std::istream::beg);
|
||||
|
||||
sink.OnEvent({ PakProgressReportSink::FileEvent::EventCode::FileWriteEnd,
|
||||
filename });
|
||||
|
|
Loading…
Reference in a new issue