/
converter_app.cpp
297 lines (252 loc) · 11.4 KB
/
converter_app.cpp
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
// Copyright (C) 2019 Sean Middleditch, all rights reserverd.
#include "converter_app.h"
#include "potato/foundation/string_view.h"
#include "potato/foundation/std_iostream.h"
#include "potato/filesystem/path.h"
#include "potato/filesystem/filesystem.h"
#include "potato/filesystem/native.h"
#include "potato/filesystem/stream.h"
#include "potato/assetdb/hash_cache.h"
#include "converters/convert_hlsl.h"
#include "converters/convert_copy.h"
#include "converters/convert_json.h"
#include "converters/convert_ignore.h"
#include "converters/convert_model.h"
#include <set>
#include <algorithm>
up::recon::ConverterApp::ConverterApp() : _programName("recon"), _fileSystem(new_box<NativeFileSystem>()), _hashes(*_fileSystem), _logger("recon") {}
up::recon::ConverterApp::~ConverterApp() = default;
bool up::recon::ConverterApp::run(span<char const*> args) {
zstring_view const configFile = "recon.config.json";
if (_fileSystem->fileExists(configFile)) {
_logger.info("Loading config file `{}'", configFile);
parseConfigFile(_config, *_fileSystem, configFile, _logger);
}
if (!parseArguments(_config, args, *_fileSystem, _logger)) {
_logger.error("Failed to parse arguments");
return false;
}
registerConverters();
auto libraryPath = path::join({string_view(_config.destinationFolderPath), "library$.json"});
_outputs.push_back("library$.json");
if (_fileSystem->fileExists(libraryPath.c_str())) {
auto libraryReadStream = _fileSystem->openRead(libraryPath.c_str(), FileOpenMode::Text);
if (!libraryReadStream) {
_logger.error("Failed to open asset library `{}'", libraryPath);
}
if (!_library.deserialize(libraryReadStream)) {
_logger.error("Failed to load asset library `{}'", libraryPath);
}
_logger.info("Loaded asset library `{}'", libraryPath);
}
auto hashCachePath = path::join({string_view(_config.destinationFolderPath), "hashes$.json"});
_outputs.push_back("hashes$.json");
if (_fileSystem->fileExists(hashCachePath.c_str())) {
auto hashesReadStream = _fileSystem->openRead(hashCachePath.c_str(), FileOpenMode::Text);
if (!hashesReadStream) {
_logger.error("Failed to open hash cache `{}'", hashCachePath);
}
if (!_hashes.deserialize(hashesReadStream)) {
_logger.error("Failed to load hash cache `{}'", hashCachePath);
}
_logger.info("Loaded hash cache `{}'", hashCachePath);
}
auto sources = collectSourceFiles();
if (sources.empty()) {
_logger.error("No source files found");
return false;
}
if (_config.sourceFolderPath.empty()) {
_logger.error("Source directory must be specified.");
return false;
}
if (_config.destinationFolderPath.empty()) {
_logger.error("Destination directory must be specified.");
return false;
}
if (_config.cacheFolderPath.empty()) {
_logger.error("Cache directory must be specified.");
return false;
}
_logger.info("Source: `{}'", _config.sourceFolderPath);
_logger.info("Destination: `{}'", _config.destinationFolderPath);
_logger.info("Cache: `{}'", _config.cacheFolderPath);
if (!_fileSystem->directoryExists(_config.destinationFolderPath.c_str())) {
if (_fileSystem->createDirectories(_config.destinationFolderPath.c_str()) != IOResult::Success) {
_logger.error("Failed to create `{}'", _config.destinationFolderPath);
return false;
}
}
if (!_fileSystem->directoryExists(_config.cacheFolderPath.c_str())) {
if (_fileSystem->createDirectories(_config.cacheFolderPath.c_str()) != IOResult::Success) {
_logger.error("Failed to create `{}'", _config.cacheFolderPath);
return false;
}
}
bool success = convertFiles(sources);
if (!success) {
_logger.error("Conversion failed");
}
auto hashesWriteStream = _fileSystem->openWrite(hashCachePath.c_str(), FileOpenMode::Text);
if (!_hashes.serialize(hashesWriteStream)) {
_logger.error("Failed to write hash cache `{}'", hashCachePath);
return false;
}
hashesWriteStream.close();
auto libraryWriteStream = _fileSystem->openWrite(libraryPath.c_str(), FileOpenMode::Text);
if (!_library.serialize(libraryWriteStream)) {
_logger.error("Failed to write asset library `{}'", libraryPath);
return false;
}
libraryWriteStream.close();
if (success) {
deleteUnusedFiles(_outputs, !_config.deleteStale);
}
return success;
}
void up::recon::ConverterApp::registerConverters() {
#if UP_GPU_ENABLE_D3D11
_converters.push_back({[](string_view path) { return path::extension(path) == ".hlsl"; },
new_box<HlslConverter>()});
#else
_converters.push_back({[](string_view path) { return path::extension(path) == ".hlsl"; },
new_box<IgnoreConverter>()});
#endif
_converters.push_back({[](string_view path) { return path::extension(path) == ".hlsli"; },
new_box<IgnoreConverter>()});
_converters.push_back({[](string_view path) { return path::extension(path) == ".txt"; },
new_box<IgnoreConverter>()});
_converters.push_back({[](string_view path) { return path::extension(path) == ".json"; },
new_box<JsonConverter>()});
_converters.push_back({[](string_view path) { return path::extension(path) == ".png"; },
new_box<CopyConverter>()});
_converters.push_back({[](string_view path) { return path::extension(path) == ".jpg"; },
new_box<CopyConverter>()});
_converters.push_back({[](string_view path) { return path::extension(path) == ".ttf"; },
new_box<CopyConverter>()});
_converters.push_back({[](string_view path) { return path::extension(path) == ".obj"; },
new_box<ModelConverter>()});
}
bool up::recon::ConverterApp::convertFiles(vector<string> const& files) {
bool failed = false;
for (auto const& path : files) {
auto assetId = _library.pathToAssetId(string_view(path));
auto record = _library.findRecord(assetId);
auto osPath = path::join({_config.sourceFolderPath.c_str(), path.c_str()});
auto const contentHash = _hashes.hashAssetAtPath(osPath.c_str());
Converter* converter = findConverter(string_view(path));
if (converter == nullptr) {
failed = true;
_logger.error("Converter not found for `{}'", path);
continue;
}
bool upToDate = record != nullptr && isUpToDate(*record, contentHash, *converter) && isUpToDate(record->sourceDependencies);
if (upToDate) {
_logger.info("Asset `{}' is up-to-date", path);
for (auto const& rec : record->outputs) {
_outputs.push_back(rec.path);
}
continue;
}
auto name = converter->name();
_logger.info("Asset `{}' requires import ({} {})", path.c_str(), std::string_view(name.data(), name.size()), converter->revision());
Context context(path.c_str(), _config.sourceFolderPath.c_str(), _config.destinationFolderPath.c_str(), *_fileSystem, _logger);
if (!converter->convert(context)) {
failed = true;
_logger.error("Failed conversion for `{}'", path);
continue;
}
// range insert won't work - explicit copy fails
for (auto const& output : context.outputs()) {
_outputs.push_back(string(output));
}
AssetImportRecord newRecord;
newRecord.assetId = assetId;
newRecord.path = string(path);
newRecord.contentHash = contentHash;
newRecord.category = AssetCategory::Source;
newRecord.importerName = string(converter->name());
newRecord.importerRevision = converter->revision();
for (auto const& sourceDepPath : context.sourceDependencies()) {
auto osPath = path::join({_config.sourceFolderPath.c_str(), sourceDepPath.c_str()});
auto const contentHash = _hashes.hashAssetAtPath(osPath.c_str());
newRecord.sourceDependencies.push_back(AssetDependencyRecord{
string(sourceDepPath),
contentHash});
}
for (auto const& outputPath : context.outputs()) {
auto osPath = path::join({_config.destinationFolderPath.c_str(), outputPath.c_str()});
auto const contentHash = _hashes.hashAssetAtPath(osPath.c_str());
newRecord.outputs.push_back(AssetOutputRecord{
string(outputPath),
contentHash});
}
_library.insertRecord(std::move(newRecord));
}
return !failed;
}
bool up::recon::ConverterApp::deleteUnusedFiles(vector<string> const& files, bool dryRun) {
std::set<string> keepFiles(files.begin(), files.end());
std::set<string> foundFiles;
auto cb = [&foundFiles](FileInfo const& info) {
if (info.type == FileType::Regular) {
foundFiles.insert(string(info.path));
}
return EnumerateResult::Recurse;
};
_fileSystem->enumerate(_config.destinationFolderPath.c_str(), cb);
vector<string> deleteFiles;
std::set_difference(foundFiles.begin(), foundFiles.end(), keepFiles.begin(), keepFiles.end(), std::back_inserter(deleteFiles));
for (auto const& deletePath : deleteFiles) {
_logger.info("Stale output file `{}'", deletePath);
if (!dryRun) {
string osPath = path::join({_config.destinationFolderPath.c_str(), deletePath.c_str()});
auto rs = _fileSystem->remove(osPath.c_str());
if (rs != IOResult::Success) {
_logger.error("Failed to remove `{}'", osPath);
}
}
}
return true;
}
bool up::recon::ConverterApp::isUpToDate(AssetImportRecord const& record, up::uint64 contentHash, Converter const& converter) const noexcept {
return record.contentHash == contentHash &&
string_view(record.importerName) == converter.name() &&
record.importerRevision == converter.revision();
}
bool up::recon::ConverterApp::isUpToDate(span<AssetDependencyRecord const> records) {
for (auto const& rec : records) {
auto osPath = path::join({_config.sourceFolderPath.c_str(), rec.path.c_str()});
auto const contentHash = _hashes.hashAssetAtPath(osPath.c_str());
if (contentHash != rec.contentHash) {
return false;
}
}
return true;
}
auto up::recon::ConverterApp::findConverter(string_view path) const -> Converter* {
for (auto const& mapping : _converters) {
if (mapping.predicate(path)) {
return mapping.conveter.get();
}
}
return nullptr;
}
auto up::recon::ConverterApp::collectSourceFiles() -> vector<string> {
if (!_fileSystem->directoryExists(_config.sourceFolderPath.c_str())) {
_logger.error("`{}' does not exist or is not a directory", _config.sourceFolderPath);
return {};
}
vector<string> files;
auto cb = [&files](FileInfo const& info) -> EnumerateResult {
if (info.type == FileType::Regular) {
files.push_back(info.path);
}
else if (info.type == FileType::Directory) {
return EnumerateResult::Recurse;
}
return EnumerateResult::Continue;
};
_fileSystem->enumerate(_config.sourceFolderPath.c_str(), cb, EnumerateOptions::None);
return files;
}