osb/source/utility/asset_unpacker.cpp

54 lines
1.5 KiB
C++
Raw Normal View History

2023-06-20 04:33:09 +00:00
#include "StarPackedAssetSource.hpp"
#include "StarTime.hpp"
#include "StarJsonExtra.hpp"
#include "StarFile.hpp"
using namespace Star;
int main(int argc, char** argv) {
try {
double startTime = Time::monotonicTime();
if (argc != 3) {
2023-06-27 10:23:44 +00:00
cerrf("Usage: {} <assets pak path> <target output directory>\n", argv[0]);
2023-06-20 04:33:09 +00:00
cerrf("If the target output directory does not exist it will be created\n");
return 1;
}
String inputFile = argv[1];
String outputFolderPath = argv[2];
PackedAssetSource assetsPack(inputFile);
if (!File::isDirectory(outputFolderPath))
File::makeDirectory(outputFolderPath);
File::changeDirectory(outputFolderPath);
auto allFiles = assetsPack.assetPaths();
for (auto file : allFiles) {
try {
auto fileData = assetsPack.read(file);
auto relativePath = "." + file;
auto relativeDir = File::dirName(relativePath);
File::makeDirectoryRecursive(relativeDir);
File::writeFile(fileData, relativePath);
} catch (AssetSourceException const& e) {
2023-06-27 10:23:44 +00:00
cerrf("Could not open file: {}\n", file);
cerrf("Reason: {}\n", outputException(e, false));
2023-06-20 04:33:09 +00:00
}
}
auto metadata = assetsPack.metadata();
if (!metadata.empty())
File::writeFile(Json(std::move(metadata)).printJson(2), "_metadata");
2023-06-20 04:33:09 +00:00
2023-06-27 10:23:44 +00:00
coutf("Unpacked assets to {} in {}s\n", outputFolderPath, Time::monotonicTime() - startTime);
2023-06-20 04:33:09 +00:00
return 0;
} catch (std::exception const& e) {
2023-06-27 10:23:44 +00:00
cerrf("Exception caught: {}\n", outputException(e, true));
2023-06-20 04:33:09 +00:00
return 1;
}
}