Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/utility/asset_unpacker.cpp
blob: 9e5d5f377106b1bdcde0e1e3b6e1d989f710c8b7 (plain)
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
#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) {
      cerrf("Usage: {} <assets pak path> <target output directory>\n", argv[0]);
      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) {
        cerrf("Could not open file: {}\n", file);
        cerrf("Reason: {}\n", outputException(e, false));
      }
    }

    auto metadata = assetsPack.metadata();
    if (!metadata.empty())
      File::writeFile(Json(std::move(metadata)).printJson(2), "_metadata");

    coutf("Unpacked assets to {} in {}s\n", outputFolderPath, Time::monotonicTime() - startTime);
    return 0;
  } catch (std::exception const& e) {
    cerrf("Exception caught: {}\n", outputException(e, true));
    return 1;
  }
}