From 3786b760fe9dcd6e290a40170c6901372bf294d9 Mon Sep 17 00:00:00 2001 From: modeco80 Date: Tue, 7 Jan 2025 15:13:10 -0500 Subject: [PATCH] *: Various cleanup --- include/europa/io/PakFile.hpp | 12 ++++----- include/europa/io/PakProgressReportSink.hpp | 27 ++++++++------------ include/europa/io/PakReader.hpp | 12 +++++---- include/europa/io/PakWriter.hpp | 6 +++++ include/europa/io/YatfReader.hpp | 8 +----- src/libeuropa/io/PakWriter.cpp | 11 ++++---- src/tools/eupak/tasks/CreateTask.cpp | 28 ++++++++++----------- src/tools/eupak/tasks/InfoTask.cpp | 26 ++++++++----------- 8 files changed, 59 insertions(+), 71 deletions(-) diff --git a/include/europa/io/PakFile.hpp b/include/europa/io/PakFile.hpp index 389d5b0..ffca052 100644 --- a/include/europa/io/PakFile.hpp +++ b/include/europa/io/PakFile.hpp @@ -82,10 +82,8 @@ namespace europa::io { PakFileData::Variant variant_; }; - /// Repressents a package file. - /// FIXME: Maybe make this not hold a buffer at some point, - /// or a sumtype which can contain either buffer OR path to os file - /// (which we can then efficiently tee into) + /// Repressents a package file. Can either hold a memory buffer of contents + /// or a filesystem path (for creating packages). struct PakFile { using DataType = PakFileData; @@ -181,9 +179,9 @@ namespace europa::io { return size; } - template - void Visit(const Cb& cb) { - std::visit(cb, toc); + template + auto Visit(Visitor&& cb) { + return std::visit(cb, toc); } private: diff --git a/include/europa/io/PakProgressReportSink.hpp b/include/europa/io/PakProgressReportSink.hpp index db9f16c..e146cf4 100644 --- a/include/europa/io/PakProgressReportSink.hpp +++ b/include/europa/io/PakProgressReportSink.hpp @@ -13,30 +13,26 @@ namespace europa::io { - /** - * Interface for the writer to output detailed progress information. - */ + /// Interface for [PakWriter] to output detailed progress information. struct PakProgressReportSink { - struct PakEvent { - enum class Type { - FillInHeader, ///< Filling in header - WritingHeader, ///< Writing header - - WritingToc ///< Writing archive TOC + enum class EventCode { + FillInHeader, /// Filling in header. + WritingHeader, /// Writing header. + WritingToc /// Writing archive TOC. }; - Type type; + EventCode eventCode; }; struct FileEvent { - enum class Type { - FileBeginWrite, ///< File has began write to package - FileEndWrite, ///< File writing finished + enum class EventCode { + FileWriteBegin, /// File has began write to package + FileWriteEnd, /// File has been written to package }; - Type type; - std::string filename; + EventCode eventCode; + const std::string& targetFileName; }; virtual ~PakProgressReportSink() = default; @@ -45,7 +41,6 @@ namespace europa::io { virtual void OnEvent(const FileEvent& event) = 0; }; - } // namespace europa::io #endif // EUROPA_IO_PAKPROGRESSREPORTSINK_H diff --git a/include/europa/io/PakReader.hpp b/include/europa/io/PakReader.hpp index 69bb9f9..02dc6e4 100644 --- a/include/europa/io/PakReader.hpp +++ b/include/europa/io/PakReader.hpp @@ -15,14 +15,14 @@ #include #include -#include - namespace europa::io { + /// Reader for Europa package files (.pak). struct PakReader { using MapType = std::unordered_map; - + /// Constructor. Takes in a input stream to read pak data from. + /// This stream should only be used by the PakReader, nothing else. explicit PakReader(std::istream& is); void ReadData(); @@ -42,10 +42,12 @@ namespace europa::io { const MapType& GetFiles() const; // implement in cpp later, lazy and just wanna get this out :vvv - const structs::PakHeaderVariant& GetHeader() const { return header; } + const structs::PakHeaderVariant& GetHeader() const { + return header; + } private: - template + template void ReadData_Impl(); std::istream& stream; diff --git a/include/europa/io/PakWriter.hpp b/include/europa/io/PakWriter.hpp index d344a2e..2e0400f 100644 --- a/include/europa/io/PakWriter.hpp +++ b/include/europa/io/PakWriter.hpp @@ -29,6 +29,12 @@ namespace europa::io { using FlattenedType = std::pair; + constexpr PakWriter() = default; + + PakWriter(structs::PakVersion version) { + SetVersion(version); + } + /// Initalize for the given package version. void SetVersion(structs::PakVersion version); diff --git a/include/europa/io/YatfReader.hpp b/include/europa/io/YatfReader.hpp index e3a5d37..799f359 100644 --- a/include/europa/io/YatfReader.hpp +++ b/include/europa/io/YatfReader.hpp @@ -16,9 +16,7 @@ namespace europa::io { - /** - * Reader for PS2 Europa .tex files. - */ + /// Reader for PS2 Europa .tex files. struct YatfReader { explicit YatfReader(std::istream& is); @@ -39,10 +37,6 @@ namespace europa::io { bool invalid { false }; structs::YatfHeader header; - - /** - * converted image. - */ pixel::RgbaImage image; }; diff --git a/src/libeuropa/io/PakWriter.cpp b/src/libeuropa/io/PakWriter.cpp index 46fe8c9..18e1287 100644 --- a/src/libeuropa/io/PakWriter.cpp +++ b/src/libeuropa/io/PakWriter.cpp @@ -81,7 +81,7 @@ namespace europa::io { // Write all the file data for(auto& [filename, file] : sortedFiles) { - sink.OnEvent({ PakProgressReportSink::FileEvent::Type::FileBeginWrite, + sink.OnEvent({ PakProgressReportSink::FileEvent::EventCode::FileWriteBegin, filename }); // Update the offset to where we currently are, since we will be writing the file there @@ -95,6 +95,7 @@ namespace europa::io { // For filesystem paths, we open the file and then tee it into the package file // effiently saving a lot of memory usage when packing (trading off some IO overhead, // but hey.) + // For buffers, we just write the buffer. // clang-format off fileData.Visit(overloaded { @@ -119,13 +120,13 @@ namespace europa::io { AlignBy(os.tellp(), kCDSectorSize), std::istream::beg); - sink.OnEvent({ PakProgressReportSink::FileEvent::Type::FileEndWrite, + sink.OnEvent({ PakProgressReportSink::FileEvent::EventCode::FileWriteEnd, filename }); } pakHeader.tocOffset = os.tellp(); - sink.OnEvent({ PakProgressReportSink::PakEvent::Type::WritingToc }); + sink.OnEvent({ PakProgressReportSink::PakEvent::EventCode::WritingToc }); // Write the TOC for(auto& [filename, file] : sortedFiles) { @@ -137,14 +138,14 @@ namespace europa::io { }); } - sink.OnEvent({ PakProgressReportSink::PakEvent::Type::FillInHeader }); + sink.OnEvent({ PakProgressReportSink::PakEvent::EventCode::FillInHeader }); // Fill out the rest of the header. pakHeader.fileCount = sortedFiles.size(); pakHeader.tocSize = static_cast(os.tellp()) - (pakHeader.tocOffset - 1); pakHeader.creationUnixTime = 132890732; - sink.OnEvent({ PakProgressReportSink::PakEvent::Type::WritingHeader }); + sink.OnEvent({ PakProgressReportSink::PakEvent::EventCode::WritingHeader }); // As the last step, write it. os.seekp(0, std::ostream::beg); diff --git a/src/tools/eupak/tasks/CreateTask.cpp b/src/tools/eupak/tasks/CreateTask.cpp index 31d0309..71d3169 100644 --- a/src/tools/eupak/tasks/CreateTask.cpp +++ b/src/tools/eupak/tasks/CreateTask.cpp @@ -14,6 +14,7 @@ #include #include #include + #include "europa/io/PakFile.hpp" namespace eupak::tasks { @@ -30,8 +31,8 @@ namespace eupak::tasks { } void OnEvent(const PakEvent& event) override { - using enum PakEvent::Type; - switch(event.type) { + using enum PakEvent::EventCode; + switch(event.eventCode) { case WritingHeader: progress.set_option(indicators::option::PostfixText { "Writing header" }); progress.print_progress(); @@ -50,15 +51,15 @@ namespace eupak::tasks { } void OnEvent(const FileEvent& event) override { - using enum FileEvent::Type; - switch(event.type) { - case FileBeginWrite: - progress.set_option(indicators::option::PostfixText { "Writing " + event.filename }); + using enum FileEvent::EventCode; + switch(event.eventCode) { + case FileWriteBegin: + progress.set_option(indicators::option::PostfixText { "Writing " + event.targetFileName }); progress.print_progress(); break; - case FileEndWrite: - progress.set_option(indicators::option::PostfixText { "Written " + event.filename }); + case FileWriteEnd: + progress.set_option(indicators::option::PostfixText { "Written " + event.targetFileName }); progress.tick(); break; } @@ -77,10 +78,6 @@ namespace eupak::tasks { }; int CreateTask::Run(Arguments&& args) { - europa::io::PakWriter writer; - - writer.SetVersion(args.pakVersion); - auto currFile = 0; auto fileCount = 0; @@ -137,7 +134,7 @@ namespace eupak::tasks { // Setup other stuff like modtime file.Visit([&](auto& tocEntry) { // Need to figure out why this is broken and fucked up - //auto casted = std::chrono::clock_cast(lastModified); + // auto casted = std::chrono::clock_cast(lastModified); auto seconds = std::chrono::time_point_cast(lastModified); tocEntry.creationUnixTime = static_cast(seconds.time_since_epoch().count()); }); @@ -156,9 +153,10 @@ namespace eupak::tasks { return 1; } - CreateArchiveReportSink sink(fileCount); + CreateArchiveReportSink reportSink(fileCount); + europa::io::PakWriter writer(args.pakVersion); - writer.Write(ofs, std::move(files), sink); + writer.Write(ofs, std::move(files), reportSink); return 0; } diff --git a/src/tools/eupak/tasks/InfoTask.cpp b/src/tools/eupak/tasks/InfoTask.cpp index fbc1b09..a1b3aff 100644 --- a/src/tools/eupak/tasks/InfoTask.cpp +++ b/src/tools/eupak/tasks/InfoTask.cpp @@ -6,13 +6,11 @@ // SPDX-License-Identifier: LGPL-3.0-or-later // -#include - -#include #include +#include #include #include - +#include #include namespace eupak::tasks { @@ -36,34 +34,30 @@ namespace eupak::tasks { return 1; } - std::visit([&](auto& header){ + std::visit([&](auto& header) { std::string version; if constexpr(std::decay_t::VERSION == europa::structs::PakVersion::Ver3) version = "Version 3 (PMDL)"; else if constexpr(std::decay_t::VERSION == europa::structs::PakVersion::Ver4) version = "Version 4 (Starfighter)"; - else if constexpr(std::decay_t::VERSION == europa::structs::PakVersion::Ver5) + else if constexpr(std::decay_t::VERSION == europa::structs::PakVersion::Ver5) version = "Version 5 (Jedi Starfighter)"; - std::cout << "Archive " << args.inputPath << ":\n"; std::cout << " Created: " << FormatUnixTimestamp(header.creationUnixTime, DATE_FORMAT) << '\n'; std::cout << " Version: " << version << '\n'; std::cout << " Size: " << FormatUnit(header.tocOffset + header.tocSize) << '\n'; std::cout << " File Count: " << header.fileCount << " files\n"; - - }, reader.GetHeader()); - + }, + reader.GetHeader()); // Print a detailed file list if verbose. if(args.verbose) { - for(auto& [ filename, file ] : reader.GetFiles()) { + for(auto& [filename, file] : reader.GetFiles()) { std::cout << "File \"" << filename << "\":\n"; file.Visit([&](auto& tocEntry) { - - std::cout << " Created: " << FormatUnixTimestamp(tocEntry.creationUnixTime, DATE_FORMAT) << '\n'; - std::cout << " Size: " << FormatUnit(tocEntry.size) << '\n'; - + std::cout << " Created: " << FormatUnixTimestamp(tocEntry.creationUnixTime, DATE_FORMAT) << '\n'; + std::cout << " Size: " << FormatUnit(tocEntry.size) << '\n'; }); } } @@ -71,4 +65,4 @@ namespace eupak::tasks { return 0; } -} \ No newline at end of file +} // namespace eupak::tasks \ No newline at end of file