From f7b80326fffe85cdaf4d1665b9bec48ae63b786d Mon Sep 17 00:00:00 2001 From: modeco80 Date: Tue, 7 Jan 2025 17:56:30 -0500 Subject: [PATCH] eupak: Fix creation timestamps Also, PakWriter will now write a proper creation time. For unit testing/etc, I may instead have it just take in a system_clock::time_point, but idk. --- include/europa/io/PakFile.hpp | 2 +- src/libeuropa/io/PakWriter.cpp | 19 +++++++++++++++---- src/tools/eupak/tasks/CreateTask.cpp | 8 ++++---- src/tools/eupak/tasks/ExtractTask.cpp | 4 ---- src/tools/eupak/tasks/InfoTask.cpp | 2 +- 5 files changed, 21 insertions(+), 14 deletions(-) diff --git a/include/europa/io/PakFile.hpp b/include/europa/io/PakFile.hpp index ffca052..f2f7cbd 100644 --- a/include/europa/io/PakFile.hpp +++ b/include/europa/io/PakFile.hpp @@ -180,7 +180,7 @@ namespace europa::io { } template - auto Visit(Visitor&& cb) { + auto VisitTocEntry(Visitor&& cb) { return std::visit(cb, toc); } diff --git a/src/libeuropa/io/PakWriter.cpp b/src/libeuropa/io/PakWriter.cpp index 18e1287..e998a0a 100644 --- a/src/libeuropa/io/PakWriter.cpp +++ b/src/libeuropa/io/PakWriter.cpp @@ -7,6 +7,7 @@ // #include +#include #include #include #include @@ -61,7 +62,7 @@ namespace europa::io { // Sort the flattened array. std::ranges::sort(sortedFiles, std::greater {}, [](const FlattenedType& elem) { - return elem.second.GetCreationUnixTime(); + return elem.second.GetSize(); }); // Leave space for the header @@ -85,7 +86,7 @@ namespace europa::io { filename }); // Update the offset to where we currently are, since we will be writing the file there - file.Visit([&](auto& tocEntry) { + file.VisitTocEntry([&](auto& tocEntry) { tocEntry.offset = os.tellp(); }); @@ -128,12 +129,19 @@ namespace europa::io { sink.OnEvent({ PakProgressReportSink::PakEvent::EventCode::WritingToc }); +#if 0 + // Sort for toc stuff? idk + std::ranges::sort(sortedFiles, std::less {}, [](const FlattenedType& elem) { + return elem.second.GetCreationUnixTime(); + }); +#endif + // Write the TOC for(auto& [filename, file] : sortedFiles) { // Write the filename Pascal string. impl::WritePString(os, filename); - file.Visit([&](auto& tocEntry) { + file.VisitTocEntry([&](auto& tocEntry) { impl::WriteStreamType(os, tocEntry); }); } @@ -143,7 +151,10 @@ namespace europa::io { // Fill out the rest of the header. pakHeader.fileCount = sortedFiles.size(); pakHeader.tocSize = static_cast(os.tellp()) - (pakHeader.tocOffset - 1); - pakHeader.creationUnixTime = 132890732; + + // Timestamp. + auto now = std::chrono::system_clock::now(); + pakHeader.creationUnixTime = static_cast(std::chrono::time_point_cast(now).time_since_epoch().count()); sink.OnEvent({ PakProgressReportSink::PakEvent::EventCode::WritingHeader }); diff --git a/src/tools/eupak/tasks/CreateTask.cpp b/src/tools/eupak/tasks/CreateTask.cpp index 71d3169..9805afc 100644 --- a/src/tools/eupak/tasks/CreateTask.cpp +++ b/src/tools/eupak/tasks/CreateTask.cpp @@ -132,10 +132,10 @@ namespace eupak::tasks { file.SetData(std::move(pakData)); // 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 seconds = std::chrono::time_point_cast(lastModified); + file.VisitTocEntry([&](auto& tocEntry) { + // Kinda stupid but works + auto sys = std::chrono::file_clock::to_sys(lastModified); + auto seconds = std::chrono::time_point_cast(sys); tocEntry.creationUnixTime = static_cast(seconds.time_since_epoch().count()); }); diff --git a/src/tools/eupak/tasks/ExtractTask.cpp b/src/tools/eupak/tasks/ExtractTask.cpp index 71be28a..e210cef 100644 --- a/src/tools/eupak/tasks/ExtractTask.cpp +++ b/src/tools/eupak/tasks/ExtractTask.cpp @@ -14,8 +14,6 @@ #include #include -// this actually is pretty fast so maybe I won't bother doing crazy thread optimizations.. - namespace eupak::tasks { int ExtractTask::Run(Arguments&& args) { @@ -67,8 +65,6 @@ namespace eupak::tasks { if(!fs::exists(outpath.parent_path())) fs::create_directories(outpath.parent_path()); - - std::ofstream ofs(outpath.string(), std::ofstream::binary); if(!ofs) { diff --git a/src/tools/eupak/tasks/InfoTask.cpp b/src/tools/eupak/tasks/InfoTask.cpp index a1b3aff..7f5a696 100644 --- a/src/tools/eupak/tasks/InfoTask.cpp +++ b/src/tools/eupak/tasks/InfoTask.cpp @@ -55,7 +55,7 @@ namespace eupak::tasks { if(args.verbose) { for(auto& [filename, file] : reader.GetFiles()) { std::cout << "File \"" << filename << "\":\n"; - file.Visit([&](auto& tocEntry) { + file.VisitTocEntry([&](auto& tocEntry) { std::cout << " Created: " << FormatUnixTimestamp(tocEntry.creationUnixTime, DATE_FORMAT) << '\n'; std::cout << " Size: " << FormatUnit(tocEntry.size) << '\n'; });