-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresources_compiler.cpp
More file actions
361 lines (294 loc) · 12.6 KB
/
resources_compiler.cpp
File metadata and controls
361 lines (294 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
MIT License
Copyright (c) 2021 pavel.sokolov@gmail.com / CEZEO software Ltd. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <spdlog/spdlog.h>
#include <filesystem>
#include <fstream>
#include <string>
#include <string_view>
#include <vector>
namespace fs = std::filesystem;
namespace {
constexpr std::string_view kHelpText(R"%(
usage:
resources_compiler --sources=file1,file2 --output=full/path/to/file_without_extension
without supplying --sources param, an empty resources holder classes will be generated
once again, output file path with file name only - no extension please, because tool will use it as base for .h and .cpp files
note: spaces are not allowed in paths nor between the tags nor the equal signs!
)%");
constexpr std::string_view kHeaderTop(R"%(#pragma once
/*)%");
constexpr std::string_view kHeaderBottom(R"%(
*/
#include <string>
#include <string_view>
#include <unordered_map>
namespace resources {
class manager final {
public:
manager();
std::string_view get(const std::string_view& name) const;
private:
std::unordered_map<std::string_view, std::string_view> resources_;
};
} // namespace resources
)%");
constexpr std::string_view kSourceTop(R"%(
*/
namespace resources {
namespace {
// resources list
)%");
constexpr std::string_view kSourceMiddle(R"%(// resources list
template <typename T, size_t size>
size_t array_size(T (&)[size]) {
return size;
}
} // namespace
manager::manager() {
// resources list
)%");
constexpr std::string_view kSourceBottom(R"%(
// resources list
}
std::string_view manager::get(const std::string_view& name) const {
const auto iterator = resources_.find(name);
if (iterator == resources_.end()) {
return {};
}
return iterator->second;
}
} // namespace resources
)%");
constexpr std::string_view kBanner(R"%(
.:+oooooooooooooooooooooooooooooooooooooo: `/ooooooooooo/` :ooooo+/-`
`+d##########################################sh#############do#########Ns.
:#####N#ddddddddddddddddddddddddddddddN######h.:hdddddddddddh/.ydddd######N+
:N###N+. .-----------.` `+#####d/ .-----------. `:#####/
h####/ :############Nd. `/d#####+` sN###########Ny -#####
h####/ :#N##########Nd. :h####No` oN###########Ny -#####
:N###No.` `-----------.` -yN###Ns. `.-----------.` `/#####/
:#####N#####d/.yd##########do.sN#####################################N####N+
`+d#########do#############N+###########################################s.
.:+ooooo/` :+oooooooooo+. .+ooooooooooooooooooooooooooooooooooooo+/.
C E Z E O S O F T W A R E R E S O U R C E S C O M P I L E R
)%");
constexpr std::string_view kSourcesListTag("--sources=");
constexpr std::string_view kOutputPathTag("--output=");
constexpr const char* kReset = "\033[0m"; /* Reset output */
constexpr const char* kYellow = "\033[1m\033[33m"; /* Bright Yellow */
constexpr const char* kCyan = "\033[1m\033[36m"; /* Bright Cyan */
// slice string with one delimiter char
// zero length pieces will be skipped
template <typename container_type>
void each_slice_view(const char* begin, size_t size, char delimiter, container_type& container) {
const char* first = begin;
const char* current = begin;
const char* end = begin + size;
for (; begin != end; ++begin) {
if (*begin == delimiter) {
if (first != current) {
container.push_back(std::basic_string_view<char>(first, size_t(current - first)));
}
first = ++current;
} else {
++current;
}
}
if (first != current) {
container.push_back(std::basic_string_view<char>(first, size_t(current - first)));
}
}
// if input container begins with match
template <typename container_type>
bool begins_with(const container_type& input, const container_type& match) {
return input.size() >= match.size() && std::equal(match.begin(), match.end(), input.begin());
}
struct file_entry final {
file_entry(std::string source_array, std::string source_map)
: source_array_entry(std::move(source_array)), source_map_entry(std::move(source_map)) {}
std::string source_array_entry;
std::string source_map_entry;
};
// save the string to file
void save_string_to_file(const std::string& data, const fs::path& file_path) {
std::ofstream header_file_output(file_path, std::ios::out | std::ios::binary);
if (header_file_output.is_open()) {
header_file_output.write(data.data(), data.length());
} else {
SPDLOG_ERROR("can't open file for write operation: {}", file_path.u8string());
}
header_file_output.close();
SPDLOG_INFO("file generated: '{}', file size: {}", file_path.u8string(), data.length());
}
} // namespace
void print_help() {
spdlog::set_pattern("%v");
SPDLOG_INFO("{}{}{}", kYellow, kBanner, kReset);
SPDLOG_INFO(kHelpText);
}
int main(int argc, char* argv[]) {
spdlog::set_pattern("[%H:%M:%S.%e] %^[%l]%$ %v");
if (argc < 2) {
SPDLOG_ERROR("not enough params");
print_help();
return -1;
}
std::vector<std::string_view> source_files;
std::string_view output_file;
for (int i = 1; i < argc; i++) {
const std::string_view argument(argv[i]);
if (begins_with(argument, kSourcesListTag)) {
// --sources=
const auto sources_paths = argument.substr(kSourcesListTag.size());
each_slice_view(sources_paths.data(), sources_paths.size(), ',', source_files);
} else if (begins_with(argument, kOutputPathTag)) {
// --output=
output_file = argument.substr(kOutputPathTag.size());
} else {
SPDLOG_WARN("unknown option: {}", argument);
}
}
if (source_files.empty()) {
SPDLOG_WARN("no source files specified, an empty project will be generated");
}
if (nullptr == output_file.data() || output_file.size() == 0) {
SPDLOG_ERROR("no output file specified");
print_help();
return -3;
}
// filesystem throw errors if no std::errc supplied
try {
// process output files
fs::path output_header_path(output_file);
output_header_path.replace_extension(".h");
fs::path output_source_path(output_file);
output_source_path.replace_extension(".cpp");
if (fs::exists(output_header_path)) {
// delete output header file if it's exist
SPDLOG_INFO("output header file exists: we are going to delete it: {}", output_header_path.u8string());
fs::remove(output_header_path);
}
if (fs::exists(output_source_path)) {
// delete output source file if it's exist
SPDLOG_INFO("output source file exists: we are going to delete it: {}", output_source_path.u8string());
fs::remove(output_source_path);
}
// container for resource files
std::vector<file_entry> result_data;
size_t result_source_size = kBanner.size();
// process files
for (const auto& source_file : source_files) {
const fs::path file_path(source_file);
// open the stream to 'lock' the file.
std::ifstream file_stream(file_path, std::ios::in | std::ios::binary);
const auto file_size = fs::file_size(file_path);
if (file_size > 0) {
if (file_size > (1024 * 1024 * 16)) {
SPDLOG_WARN("we are going to add file: '{}', with size: {} bytes to the resources!", file_size, source_file);
}
std::vector<char> binary_buffer;
binary_buffer.resize(file_size);
// read the whole file into the buffer
file_stream.read(binary_buffer.data(), binary_buffer.size());
// 4 chars for byte, '255,' + new line for every 0x1f chars + 64 for { }
// etc.
const size_t precalc_stirng_size((binary_buffer.size() * 4) + (binary_buffer.size() / 0x1F) + 64);
// variable safe name for file
std::string resource_name(file_path.filename().u8string());
std::replace(resource_name.begin(), resource_name.end(), '.', '_');
// build header entry
std::string source_array_entry;
source_array_entry.reserve(precalc_stirng_size);
source_array_entry.append(fmt::format("constexpr const char {}[{}] = ", resource_name, binary_buffer.size()));
source_array_entry.append("{");
size_t char_counter = 0;
// use unsigned type in for
for (unsigned char byte : binary_buffer) {
if ((char_counter & 0x1F) == 0) {
// add some line breaks
source_array_entry.append("\n");
}
source_array_entry.append(fmt::format("{},", byte));
++char_counter;
}
const std::string_view end_token("};\n\n");
const std::string_view token_at_end(",");
const auto last_comma = source_array_entry.rfind(token_at_end);
if (std::string::npos != last_comma) {
source_array_entry.replace(last_comma, last_comma + token_at_end.size(), end_token);
} else {
SPDLOG_WARN("end token is not found, so it looks like an error");
source_array_entry.append(end_token);
}
// now build the map insertion
std::string source_map_entry(
fmt::format(" resources_.insert({{\"{}\", std::string_view({}, array_size({}))}});\n",
resource_name,
resource_name,
resource_name));
double increase = (double)source_array_entry.size() / (double)file_size;
SPDLOG_INFO("file: '{}' added, size in binary: {}, size in source: {} bytes, increase: {:.2f}x",
source_file,
file_size,
source_array_entry.size(),
increase);
// store size calculations before move
result_source_size += source_array_entry.size() + source_map_entry.size();
// move to result_data
result_data.emplace_back(std::move(source_array_entry), std::move(source_map_entry));
} else {
SPDLOG_WARN("empty file skipped: {}", source_file);
}
file_stream.close();
}
// now build the result header
const auto precalc_header_size(kBanner.size() + kHeaderTop.size() + kHeaderBottom.size() + 256);
const auto precalc_source_size(result_source_size + kSourceTop.size() + kSourceMiddle.size() +
kSourceBottom.size() + 256);
//
// build header
//
std::string result_header;
result_header.reserve(precalc_header_size);
result_header.append(kHeaderTop);
result_header.append(kBanner);
result_header.append(kHeaderBottom);
save_string_to_file(result_header, output_header_path);
//
// build source
//
std::string result_source;
result_source.reserve(precalc_source_size);
result_source.append(fmt::format("#include \"{}\"\n\n/*", output_header_path.filename().u8string()));
result_source.append(kBanner);
result_source.append(kSourceTop);
for (auto& result_entry : result_data) {
// add arrays with actual data
result_source.append(std::move(result_entry.source_array_entry));
}
result_source.append(kSourceMiddle);
for (auto& result_entry : result_data) {
// add map inserts
result_source.append(std::move(result_entry.source_map_entry));
}
result_source.append(kSourceBottom);
// save the result
save_string_to_file(result_source, output_source_path);
} catch (const std::exception& e) {
SPDLOG_ERROR("exception during processing: {}", e.what());
return -255;
}
return 0;
}