| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Descent Developers |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | #include <iostream> |
| 19 | #include <algorithm> |
| 20 | #include <chrono> |
| 21 | #include <filesystem> |
| 22 | #include <fstream> |
| 23 | #include <string> |
| 24 | |
| 25 | #include "d3_version.h" |
| 26 | #include "HogFormat.h" |
| 27 | |
| 28 | // TODO: To our descendants from the future: remove it when code will support C++20 |
| 29 | template <typename TP> std::time_t to_time_t(TP tp) { |
| 30 | auto sctp = std::chrono::duration_cast<std::chrono::system_clock::duration>(tp - TP::clock::now()) + |
| 31 | std::chrono::system_clock::now(); |
| 32 | return std::chrono::system_clock::to_time_t(sctp); |
| 33 | } |
| 34 | |
| 35 | // Find requested file in search paths |
| 36 | std::filesystem::path resolve_path(const std::vector<std::filesystem::path> &search_path, |
| 37 | const std::filesystem::path &file) { |
| 38 | for (const auto &i : search_path) { |
| 39 | if (is_regular_file((i / file))) { |
| 40 | return (i / file); |
| 41 | } |
| 42 | } |
| 43 | // Return empty path |
| 44 | return std::filesystem::path{}; |
| 45 | } |
| 46 | |
| 47 | int main(int argc, char *argv[]) { |
| 48 | std::vector<std::filesystem::path> input_files; |
| 49 | |
| 50 | if (argc < 3) { |
| 51 | std::cout << "HogMaker v" << D3_MAJORVER << "." << D3_MINORVER << "." << D3_BUILD << "-g" << D3_GIT_HASH << "\n" |
| 52 | << "Usage:\n" |
| 53 | << " " << argv[0] << " <hogfile.hog> <inputfile.txt> [search_path]\n" |
| 54 | << std::endl; |
| 55 | return 1; |
| 56 | } |
| 57 | |
| 58 | std::filesystem::path hog_filename{argv[1]}; |
| 59 | std::filesystem::path input_filename{argv[2]}; |
| 60 | // Default search path |
| 61 | std::vector<std::filesystem::path> search_paths{input_filename.parent_path()}; |
| 62 | // Additional search paths |
| 63 | for (int i = 3; i < argc; i++) { |
| 64 | if (is_directory(std::filesystem::path(argv[i]))) { |
| 65 | search_paths.emplace_back(argv[i]); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | if (!is_regular_file(input_filename)) { |
| 70 | std::cout << input_filename << " is not a regular file!" << std::endl; |
| 71 | return 1; |
| 72 | } |
| 73 | |
| 74 | D3::HogFormat hog_table; |
| 75 | |
| 76 | try { |
| 77 | create_directories(absolute(hog_filename).parent_path()); |
| 78 | std::ifstream input(input_filename, std::ios_base::in); |
| 79 | for (std::string line; std::getline(input, line);) { |
| 80 | auto resolved_file = resolve_path(search_paths, line); |
| 81 | if (resolved_file.empty()) { |
| 82 | std::cout << "Warning! File " << line << " from " << input_filename << " not found! Skipping..." << std::endl; |
| 83 | } else if (line.size() > 36) { |
| 84 | std::cout << "Warning! Length of name of file " << line << " is more than 36 symbols! Skipping..." << std::endl; |
| 85 | } else { |
| 86 | hog_table.AddEntry(D3::HogFileEntry(resolved_file)); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | } catch (std::exception &e) { |
| 91 | std::cout << "Exception: " << e.what() << std::endl; |
| 92 | return 1; |
| 93 | } |
| 94 | |
| 95 | hog_table.SortEntries(); |
| 96 | |
| 97 | std::cout << "Creating " << absolute(hog_filename) << "..." << std::endl; |
| 98 | std::fstream ofs(hog_filename, std::ios_base::out | std::ios_base::binary); |
| 99 | |
| 100 | ofs << hog_table; |
| 101 | for (const auto& i : hog_table.GetEntries()) { |
| 102 | std::cout << "Adding " << i.GetName() << "... "; |
| 103 | try { |
| 104 | std::fstream ifs(i.GetRealPath(), std::ios_base::in | std::ios_base::binary); |
| 105 | std::copy((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>(), |
| 106 | std::ostreambuf_iterator<char>(ofs)); |
| 107 | } catch (std::exception &e) { |
| 108 | std::cout << "Exception: " << e.what() << std::endl; |
| 109 | return 1; |
| 110 | } |
| 111 | std::cout << "[ok]" << std::endl; |
| 112 | } |
| 113 | std::cout << "Done!" << std::endl; |
| 114 | |
| 115 | return 0; |
| 116 | } |