diff options
| author | KiritoDv <kiritodev01@gmail.com> | 2025-01-29 21:02:51 -0600 |
|---|---|---|
| committer | Lywx <kiritodev01@gmail.com> | 2025-01-30 10:35:05 -0600 |
| commit | 043fc76ebc8ff401f5d121660bf58497c71dff2c (patch) | |
| tree | 64bc3219a00794faf3aac88fd90435c25cd1256d | |
| parent | 18b85b04eea0e0fede4f9410fa7cdd87618029f1 (diff) | |
Replaced recursive iteration to be sorted by name and depth
| -rw-r--r-- | src/Companion.cpp | 4 | ||||
| -rw-r--r-- | src/utils/TorchUtils.cpp | 28 | ||||
| -rw-r--r-- | src/utils/TorchUtils.h | 5 |
3 files changed, 34 insertions, 3 deletions
diff --git a/src/Companion.cpp b/src/Companion.cpp index 2344070..4a7a3fc 100644 --- a/src/Companion.cpp +++ b/src/Companion.cpp @@ -1195,7 +1195,7 @@ void Companion::Process() { vWriter.Write((uint32_t) 0); } - for (const auto & entry : fs::recursive_directory_iterator(this->gAssetPath)){ + for (const auto & entry : Torch::getRecursiveEntries(this->gAssetPath)){ if(entry.is_directory()) { continue; } @@ -1258,7 +1258,7 @@ void Companion::Pack(const std::string& folder, const std::string& output, const auto start = duration_cast<milliseconds>(system_clock::now().time_since_epoch()); std::unordered_map<std::string, std::vector<char>> files; - for (const auto & entry : fs::recursive_directory_iterator(folder)){ + for (const auto & entry : Torch::getRecursiveEntries(folder)){ if(entry.is_directory()) { continue; } diff --git a/src/utils/TorchUtils.cpp b/src/utils/TorchUtils.cpp index 5dc4621..497b545 100644 --- a/src/utils/TorchUtils.cpp +++ b/src/utils/TorchUtils.cpp @@ -1,9 +1,12 @@ #include "TorchUtils.h" +#include <stack> #include <Companion.h> #include <factories/BaseFactory.h> #include "spdlog/spdlog.h" +namespace fs = std::filesystem; + uint32_t Torch::translate(const uint32_t offset) { if(SEGMENT_NUMBER(offset) > 0x01) { auto segment = SEGMENT_NUMBER(offset); @@ -18,3 +21,28 @@ uint32_t Torch::translate(const uint32_t offset) { return offset; } + +int getFileDepth(const fs::path& base, const fs::path& p) { + return std::distance(base.begin(), p.begin()); +} + +std::vector<fs::directory_entry> Torch::getRecursiveEntries(const fs::path baseDir) { + std::vector<fs::directory_entry> result; + + for (const auto& entry : fs::recursive_directory_iterator(baseDir)) { + result.push_back(entry); + } + + std::sort(result.begin(), result.end(), [](const fs::directory_entry& a, const fs::directory_entry& b) { + std::string nameA = a.path().parent_path().string(); + std::string nameB = b.path().parent_path().string(); + int depthA = std::distance(a.path().begin(), a.path().end()); + int depthB = std::distance(b.path().begin(), b.path().end()); + + if (nameA != nameB) + return nameA < nameB; + return depthA < depthB; + }); + + return result; +}
\ No newline at end of file diff --git a/src/utils/TorchUtils.h b/src/utils/TorchUtils.h index 271ab98..da30acb 100644 --- a/src/utils/TorchUtils.h +++ b/src/utils/TorchUtils.h @@ -4,8 +4,9 @@ #include <fstream> #include <sstream> #include <iomanip> -#include <algorithm> #include <cstdint> +#include <algorithm> +#include <filesystem> namespace Torch { template< typename T > @@ -23,4 +24,6 @@ std::string to_hex(T number, const bool append0x = true) { } uint32_t translate(uint32_t offset); +std::vector<std::filesystem::directory_entry> getRecursiveEntries(const std::filesystem::path baseDir); + };
\ No newline at end of file |
