GCC Code Coverage Report


Directory: ./
File: src/importer/importer.cpp
Date: 2026-04-01 15:09:43
Exec Total Coverage
Lines: 0 79 0.0%
Functions: 0 8 0.0%
Branches: 0 174 0.0%

Line Branch Exec Source
1 #include <fstream>
2 #include <filesystem>
3 #include <spdlog/spdlog.h>
4 #include "importer.hpp"
5
6 #include "assets_mpq_importer/blp.hpp"
7 #include "assets_mpq_importer/mdlx.hpp"
8 #include "assets_mpq_importer/w3m.hpp"
9
10 namespace assmpq::importer {
11
12 /**
13 * @brief Save file data to the specified output path
14 * @param file_data The file data to save
15 * @param archived_file_path The path where the file should be saved
16 * @param popt Program options containing output folder and other settings
17 * @return true if file was saved successfully, false otherwise
18 */
19 auto import_save(const assmpq::FileData& file_data, const std::filesystem::path& archived_file_path, const ProgramOptions& popt)-> bool
20 {
21 auto output_filename = popt.output_folder / archived_file_path;
22
23 std::filesystem::create_directories(output_filename.parent_path());
24 std::ofstream output_file(output_filename, std::ios::out | std::ios::binary);
25 if (!output_file) {
26 spdlog::error("File write error: {}", output_filename.string());
27 std::perror("System error message");
28 return false;
29 }
30
31 output_file.write( file_data.data(), static_cast<std::streamsize>(file_data.size()));
32 output_file.close();
33
34 spdlog::info("File {} saved.", output_filename.string());
35
36 return true;
37 }
38
39 /**
40 * @brief Import and convert BLP texture file to either DDS or PNG format
41 * @param file_data The BLP file data to convert
42 * @param archived_file_path The original file path in the archive
43 * @param popt Program options containing conversion settings
44 * @return true if conversion and save was successful, false otherwise
45 */
46 auto import_blp(const assmpq::FileData& file_data, const std::filesystem::path& archived_file_path, const ProgramOptions& popt)-> bool
47 {
48 const auto converted_file_data = [&file_data, &popt]() {
49 if (popt.is_dds && popt.is_nvtt) {
50 return assmpq::blp::convert_blp_to_dds_texture_nvtt(file_data, popt.compression, popt.is_regen_mipmaps);
51 } else if (popt.is_dds) {
52 return assmpq::blp::convert_blp_to_dds_texture_amdc(file_data, popt.compression, popt.is_regen_mipmaps);
53 } else {
54 return assmpq::blp::convert_blp_to_png_image(file_data);
55 }
56 }();
57
58 if (!converted_file_data.has_value()) {
59 spdlog::error("File convertation error: {}", archived_file_path.string());
60 return false;
61 }
62
63 auto output_path = archived_file_path;
64 output_path.replace_extension(popt.is_dds ? "dds" : "png");
65 return import_save(converted_file_data.value(), output_path, popt);
66 }
67
68 /**
69 * @brief Import and convert MDX model file to OBJ mesh format
70 * @param file_data The MDX file data to convert
71 * @param archived_file_path The original file path in the archive
72 * @param popt Program options
73 * @return true if conversion and save was successful, false otherwise
74 */
75 auto import_mdx(const assmpq::FileData& file_data, const std::filesystem::path& archived_file_path, const ProgramOptions& popt)-> bool
76 {
77 const auto converted_file_data = assmpq::mdlx::convert_mdlx_to_obj_mesh(archived_file_path.stem().string(), file_data);
78 if (!converted_file_data.has_value()) {
79 spdlog::error("File convertation error: {}", archived_file_path.string());
80 return false;
81 }
82
83 auto output_path = archived_file_path;
84 output_path.replace_extension("obj");
85 return import_save(converted_file_data.value(), output_path, popt);
86 }
87
88 /**
89 * @brief Import and extract W3E map environment file
90 * @param file_data The map file data to extract
91 * @param archived_file_path The original file path in the archive
92 * @param popt Program options
93 * @return true if extraction and save was successful, false otherwise
94 */
95 auto import_w3e(const assmpq::FileData& file_data, const std::filesystem::path& archived_file_path, const ProgramOptions& popt)-> bool
96 {
97 const auto extracted_file_data = assmpq::w3m::extract_w3e_file(file_data);
98 if (!extracted_file_data.has_value()) {
99 spdlog::error("File convertation error: {}", archived_file_path.string());
100 return false;
101 }
102
103 auto output_path = archived_file_path;
104 output_path.replace_extension("w3e");
105 return import_save(extracted_file_data.value(), output_path, popt);
106 }
107
108 /**
109 * @brief Import and extract SHD shadows map file
110 * @param file_data The map file data to extract
111 * @param archived_file_path The original file path in the archive
112 * @param popt Program options
113 * @return true if extraction and save was successful, false otherwise
114 */
115 auto import_shd(const assmpq::FileData& file_data, const std::filesystem::path& archived_file_path, const ProgramOptions& popt)-> bool
116 {
117 if (popt.is_w3e_only) {
118 return true;
119 }
120
121 const auto extracted_file_data = assmpq::w3m::extract_shd_file(file_data);
122 if (!extracted_file_data.has_value()) {
123 spdlog::error("File convertation error: {}", archived_file_path.string());
124 return false;
125 }
126
127 auto output_path = archived_file_path;
128 output_path.replace_extension("shd");
129 return import_save(extracted_file_data.value(), output_path, popt);
130 }
131
132 /**
133 * @brief Import and extract WPM path map file
134 * @param file_data The map file data to extract
135 * @param archived_file_path The original file path in the archive
136 * @param popt Program options
137 * @return true if extraction and save was successful, false otherwise
138 */
139 auto import_wpm(const assmpq::FileData& file_data, const std::filesystem::path& archived_file_path, const ProgramOptions& popt)-> bool
140 {
141 if (popt.is_w3e_only) {
142 return true;
143 }
144
145 const auto extracted_file_data = assmpq::w3m::extract_wpm_file(file_data);
146 if (!extracted_file_data.has_value()) {
147 spdlog::error("File convertation error: {}", archived_file_path.string());
148 return false;
149 }
150
151 auto output_path = archived_file_path;
152 output_path.replace_extension("wpm");
153 return import_save(extracted_file_data.value(), output_path, popt);
154 }
155
156 /**
157 * @brief Import and extract DOO tree doodas file
158 * @param file_data The map file data to extract
159 * @param archived_file_path The original file path in the archive
160 * @param popt Program options
161 * @return true if extraction and save was successful, false otherwise
162 */
163 auto import_doo(const assmpq::FileData& file_data, const std::filesystem::path& archived_file_path, const ProgramOptions& popt)-> bool
164 {
165 if (popt.is_w3e_only) {
166 return true;
167 }
168
169 const auto extracted_file_data = assmpq::w3m::extract_doo_file(file_data);
170 if (!extracted_file_data.has_value()) {
171 spdlog::error("File convertation error: {}", archived_file_path.string());
172 return false;
173 }
174
175 auto output_path = archived_file_path;
176 output_path.replace_extension("doo");
177 return import_save(extracted_file_data.value(), output_path, popt);
178 }
179
180 } // namespace assmpq::importer
181