| 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 | #pragma once |
| 19 | |
| 20 | #include <array> |
| 21 | #include <cstdint> |
| 22 | #include <filesystem> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include "IOOps.h" |
| 26 | |
| 27 | namespace D3 { |
| 28 | |
| 29 | /* |
| 30 | HOG FILE FORMAT v2.0 |
| 31 | |
| 32 | HOG_TAG_STR [strlen()] |
| 33 | NFILES [int32] |
| 34 | HDRINFO [HOG_HDR_SIZE] |
| 35 | FILE_TABLE [sizeof(FILE_ENTRY) * NFILES] |
| 36 | FILE 0 [filelen(FILE 0)] |
| 37 | FILE 1 [filelen(FILE 1)] |
| 38 | ... |
| 39 | FILE NFILES-1 [filelen(NFILES -1)] |
| 40 | */ |
| 41 | |
| 42 | static inline std::array<char, 56> array_with(char val) { |
| 43 | std::array<char, 56> arr; |
| 44 | arr.fill(val); |
| 45 | return arr; |
| 46 | } |
| 47 | |
| 48 | struct HogHeader { |
| 49 | std::array<char, 4> tag = {'H', 'O', 'G', '2'}; // "HOG2" |
| 50 | uint32_t nfiles = 0; // number of files in header |
| 51 | uint32_t file_data_offset = 68; // offset in file to filedata. |
| 52 | std::array<char, 56> reserved = array_with(-1); // filled with 0xff |
| 53 | }; |
| 54 | |
| 55 | class HogFileEntry { |
| 56 | private: |
| 57 | std::array<char, 36> m_name = {}; // file name (36 char max) |
| 58 | uint32_t m_flags = 0; // extra info |
| 59 | uint32_t m_len = 0; // length of file |
| 60 | uint32_t m_timestamp = 0; // time of file |
| 61 | std::filesystem::path m_real_path = {}; // real path |
| 62 | |
| 63 | public: |
| 64 | HogFileEntry() = default; |
| 65 | explicit HogFileEntry(const std::filesystem::path &input); |
| 66 | explicit HogFileEntry(std::string name, uint32_t flags = 0, uint32_t len = 0, uint32_t timestamp = 0); |
| 67 | |
| 68 | std::filesystem::path GetRealPath() const { return m_real_path; } |
| 69 | void SetRealPath(const std::filesystem::path &input); |
| 70 | |
| 71 | std::string GetName() const { return {m_name.data()}; }; |
| 72 | |
| 73 | friend std::ostream &operator<<(std::ostream &output, const HogFileEntry &entry); |
| 74 | friend class HogFormat; |
| 75 | }; |
| 76 | |
| 77 | class HogFormat { |
| 78 | private: |
| 79 | HogHeader m_header; |
| 80 | std::vector<HogFileEntry> m_file_entries; |
| 81 | |
| 82 | public: |
| 83 | void AddEntry(const HogFileEntry &entry) { |
| 84 | m_file_entries.push_back(entry); |
| 85 | m_header.nfiles++; |
| 86 | m_header.file_data_offset += 48; |
| 87 | }; |
| 88 | |
| 89 | /** |
| 90 | * Sort entries case-insensitive alphabetically. Call this method before any |
| 91 | * writing on disk: Descent 3 expects that entries in HOG are sorted. |
| 92 | */ |
| 93 | void SortEntries(); |
| 94 | |
| 95 | uint32_t Count() const { return m_header.nfiles; } |
| 96 | std::vector<HogFileEntry> GetEntries() const { return m_file_entries; } |
| 97 | |
| 98 | friend std::ostream &operator<<(std::ostream &output, const HogFormat &format); |
| 99 | }; |
| 100 | |
| 101 | } // namespace D3 |