app/main.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | /// @file main.cpp | ||
| 3 | /// @brief The ernie program: the viewer, and the schema and scene tools. | ||
| 4 | #include "ernie/app/viewer.h" | ||
| 5 | #include "ernie/device/device.h" | ||
| 6 | #include "ernie/scene/reference.h" | ||
| 7 | #include "ernie/util/file.h" | ||
| 8 | #include "ernie/util/version.h" | ||
| 9 | |||
| 10 | #include <algorithm> | ||
| 11 | #include <cstdio> | ||
| 12 | #include <exception> | ||
| 13 | #include <filesystem> | ||
| 14 | #include <stdexcept> | ||
| 15 | #include <string> | ||
| 16 | #include <string_view> | ||
| 17 | #include <vector> | ||
| 18 | |||
| 19 | namespace { | ||
| 20 | |||
| 21 | namespace fs = std::filesystem; | ||
| 22 | |||
| 23 | ✗ | void PrintUsage() { | |
| 24 | ✗ | std::printf("Usage: %.*s\n" | |
| 25 | " ernie --version | --devices | --help\n" | ||
| 26 | " ernie schema [--json-schema FILE] [--markdown DIR] [--usda DIR]\n" | ||
| 27 | " ernie validate SCENE.json...\n", | ||
| 28 | ✗ | static_cast<int>(ernie::app::ViewUsage().size()), ernie::app::ViewUsage().data()); | |
| 29 | ✗ | } | |
| 30 | |||
| 31 | ✗ | int PrintDevices() { | |
| 32 | ✗ | for (const ernie::VulkanDeviceInfo& device : ernie::ListVulkanDevices()) { | |
| 33 | ✗ | std::printf("%s (%s, Vulkan %u.%u%s)\n", device.name.c_str(), ernie::ToString(device.type), | |
| 34 | ✗ | device.apiMajor, device.apiMinor, | |
| 35 | ✗ | device.capabilities.rayQuery ? ", ray queries" : ""); | |
| 36 | ✗ | } | |
| 37 | ✗ | return 0; | |
| 38 | } | ||
| 39 | |||
| 40 | /// Writes the views of the schema registry that `arguments` ask for. | ||
| 41 | ✗ | int Schema(const std::vector<std::string_view>& arguments) { | |
| 42 | ✗ | if (arguments.empty() || arguments.size() % 2 != 0) { | |
| 43 | ✗ | PrintUsage(); | |
| 44 | ✗ | return 2; | |
| 45 | } | ||
| 46 | ✗ | for (std::size_t i = 0; i < arguments.size(); i += 2) { | |
| 47 | ✗ | const fs::path out(arguments[i + 1]); | |
| 48 | ✗ | if (arguments[i] == "--json-schema") { | |
| 49 | ✗ | ernie::WriteTextFile(out, ernie::SceneJsonSchema()); | |
| 50 | ✗ | } else if (arguments[i] == "--markdown") { | |
| 51 | ✗ | for (const ernie::SchemaFamily& family : ernie::SchemaFamilies()) { | |
| 52 | ✗ | ernie::WriteTextFile(out / (std::string(family.name) + ".md"), | |
| 53 | ✗ | ernie::ReferencePage(family)); | |
| 54 | } | ||
| 55 | ✗ | } else if (arguments[i] == "--usda") { | |
| 56 | ✗ | const ernie::UsdSchemaPlugin plugin = ernie::ErnieUsdSchemaPlugin(); | |
| 57 | ✗ | ernie::WriteTextFile(out / "generatedSchema.usda", plugin.generatedSchema); | |
| 58 | ✗ | ernie::WriteTextFile(out / "plugInfo.json", plugin.plugInfo); | |
| 59 | ✗ | } else { | |
| 60 | ✗ | PrintUsage(); | |
| 61 | ✗ | return 2; | |
| 62 | } | ||
| 63 | ✗ | } | |
| 64 | return 0; | ||
| 65 | } | ||
| 66 | |||
| 67 | /// Prints every problem of the scene files; fails if any has one. | ||
| 68 | ✗ | int Validate(const std::vector<std::string_view>& files) { | |
| 69 | ✗ | if (files.empty()) { | |
| 70 | ✗ | PrintUsage(); | |
| 71 | ✗ | return 2; | |
| 72 | } | ||
| 73 | ✗ | int status = 0; | |
| 74 | ✗ | for (const std::string_view file : files) { | |
| 75 | ✗ | for (const ernie::SceneProblem& problem : | |
| 76 | ✗ | ernie::ValidateScene(ernie::ReadTextFile(fs::path(file)))) { | |
| 77 | ✗ | std::printf("%.*s: %s\n", static_cast<int>(file.size()), file.data(), | |
| 78 | ✗ | problem.Message().c_str()); | |
| 79 | ✗ | status = 1; | |
| 80 | ✗ | } | |
| 81 | } | ||
| 82 | ✗ | return status; | |
| 83 | } | ||
| 84 | |||
| 85 | /// Opens the viewer; only `ernie`, `ernie view` and a scene file or viewer options get here. | ||
| 86 | ✗ | int View(const std::vector<std::string_view>& arguments) { | |
| 87 | ✗ | ernie::app::ViewCommand command; | |
| 88 | ✗ | try { | |
| 89 | ✗ | command = ernie::app::ParseViewCommand(arguments); | |
| 90 | ✗ | } catch (const std::invalid_argument& error) { | |
| 91 | ✗ | std::fprintf(stderr, "ernie: %s\n", error.what()); | |
| 92 | ✗ | PrintUsage(); | |
| 93 | ✗ | return 2; | |
| 94 | ✗ | } | |
| 95 | ✗ | return ernie::app::RunView(command); | |
| 96 | ✗ | } | |
| 97 | |||
| 98 | } // namespace | ||
| 99 | |||
| 100 | ✗ | int main(int argc, char** argv) { | |
| 101 | ✗ | const std::vector<std::string_view> arguments(argv + std::min(argc, 1), argv + argc); | |
| 102 | ✗ | const std::string_view command = arguments.empty() ? "" : arguments.front(); | |
| 103 | ✗ | const std::vector<std::string_view> rest(arguments.begin() + (arguments.empty() ? 0 : 1), | |
| 104 | ✗ | arguments.end()); | |
| 105 | ✗ | try { | |
| 106 | ✗ | if (command == "--version") { | |
| 107 | ✗ | std::printf("ernie %.*s\n", static_cast<int>(ernie::Version().size()), | |
| 108 | ✗ | ernie::Version().data()); | |
| 109 | ✗ | return 0; | |
| 110 | } | ||
| 111 | ✗ | if (command == "--help") { | |
| 112 | ✗ | PrintUsage(); | |
| 113 | return 0; | ||
| 114 | } | ||
| 115 | ✗ | if (command == "--devices") { | |
| 116 | ✗ | return PrintDevices(); | |
| 117 | } | ||
| 118 | ✗ | if (command == "schema") { | |
| 119 | ✗ | return Schema(rest); | |
| 120 | } | ||
| 121 | ✗ | if (command == "validate") { | |
| 122 | ✗ | return Validate(rest); | |
| 123 | } | ||
| 124 | ✗ | return View(command == "view" ? rest : arguments); | |
| 125 | ✗ | } catch (const std::exception& error) { | |
| 126 | ✗ | std::fprintf(stderr, "ernie: %s\n", error.what()); | |
| 127 | ✗ | return 1; | |
| 128 | ✗ | } | |
| 129 | ✗ | } | |
| 130 |