|
| 1 | +/** |
| 2 | + * |
| 3 | + * @file GameExportCommand.cpp |
| 4 | + * @author Gaspard Kirira |
| 5 | + * |
| 6 | + * Copyright 2026, Gaspard Kirira. All rights reserved. |
| 7 | + * https://github.com/vixcpp/vix |
| 8 | + * Use of this source code is governed by a MIT license |
| 9 | + * that can be found in the License file. |
| 10 | + * |
| 11 | + * Vix.cpp |
| 12 | + * |
| 13 | + * vix game export command. |
| 14 | + * |
| 15 | + */ |
| 16 | + |
| 17 | +#include <vix/cli/commands/GameExportCommand.hpp> |
| 18 | +#include <vix/cli/util/Ui.hpp> |
| 19 | + |
| 20 | +#ifdef VIX_CLI_HAS_GAME |
| 21 | +#include <vix/game/GameExportConfig.hpp> |
| 22 | +#include <vix/game/GameExporter.hpp> |
| 23 | +#endif |
| 24 | + |
| 25 | +#include <cstddef> |
| 26 | +#include <iostream> |
| 27 | +#include <string> |
| 28 | +#include <vector> |
| 29 | + |
| 30 | +namespace vix::commands::GameCommand |
| 31 | +{ |
| 32 | + namespace |
| 33 | + { |
| 34 | + bool has_flag( |
| 35 | + const std::vector<std::string> &args, |
| 36 | + const std::string &flag) |
| 37 | + { |
| 38 | + for (const auto &arg : args) |
| 39 | + { |
| 40 | + if (arg == flag) |
| 41 | + return true; |
| 42 | + } |
| 43 | + |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + std::string option_value( |
| 48 | + const std::vector<std::string> &args, |
| 49 | + const std::string &name) |
| 50 | + { |
| 51 | + for (std::size_t i = 0; i < args.size(); ++i) |
| 52 | + { |
| 53 | + if (args[i] == name && i + 1 < args.size()) |
| 54 | + return args[i + 1]; |
| 55 | + |
| 56 | + const std::string prefix = name + "="; |
| 57 | + |
| 58 | + if (args[i].rfind(prefix, 0) == 0) |
| 59 | + return args[i].substr(prefix.size()); |
| 60 | + } |
| 61 | + |
| 62 | + return {}; |
| 63 | + } |
| 64 | + |
| 65 | + bool wants_help(const std::vector<std::string> &args) |
| 66 | + { |
| 67 | + return has_flag(args, "-h") || has_flag(args, "--help"); |
| 68 | + } |
| 69 | + } // namespace |
| 70 | + |
| 71 | + ExportOptions parse_export_options( |
| 72 | + const std::vector<std::string> &args) |
| 73 | + { |
| 74 | + ExportOptions options; |
| 75 | + |
| 76 | + const auto project_root = option_value(args, "--project-root"); |
| 77 | + if (!project_root.empty()) |
| 78 | + options.project_root = project_root; |
| 79 | + |
| 80 | + const auto output = option_value(args, "--output"); |
| 81 | + if (!output.empty()) |
| 82 | + options.output_directory = output; |
| 83 | + |
| 84 | + const auto name = option_value(args, "--name"); |
| 85 | + if (!name.empty()) |
| 86 | + options.name = name; |
| 87 | + |
| 88 | + options.overwrite = !has_flag(args, "--no-overwrite"); |
| 89 | + options.copy_assets = !has_flag(args, "--no-assets"); |
| 90 | + |
| 91 | + return options; |
| 92 | + } |
| 93 | + |
| 94 | + int export_game( |
| 95 | + const std::vector<std::string> &args) |
| 96 | + { |
| 97 | + using namespace vix::cli::util; |
| 98 | + |
| 99 | + if (wants_help(args)) |
| 100 | + return help(); |
| 101 | + |
| 102 | +#ifndef VIX_CLI_HAS_GAME |
| 103 | + (void)args; |
| 104 | + |
| 105 | + err_line(std::cerr, "Game support is not enabled in this Vix build."); |
| 106 | + warn_line(std::cerr, "Rebuild Vix with -DVIX_ENABLE_GAME=ON."); |
| 107 | + return 1; |
| 108 | +#else |
| 109 | + const auto options = parse_export_options(args); |
| 110 | + |
| 111 | + vix::game::GameExportConfig config; |
| 112 | + config.project_root = options.project_root; |
| 113 | + config.overwrite = options.overwrite; |
| 114 | + config.copy_assets = options.copy_assets; |
| 115 | + |
| 116 | + if (!options.output_directory.empty()) |
| 117 | + config.output_directory = options.output_directory; |
| 118 | + |
| 119 | + if (!options.name.empty()) |
| 120 | + config.name = options.name; |
| 121 | + |
| 122 | + vix::game::GameExporter exporter; |
| 123 | + |
| 124 | + auto result = exporter.export_project(config); |
| 125 | + if (!result) |
| 126 | + { |
| 127 | + err_line(std::cerr, "Game export failed."); |
| 128 | + warn_line(std::cerr, result.error().message()); |
| 129 | + return 1; |
| 130 | + } |
| 131 | + |
| 132 | + ok_line(std::cout, "Game exported."); |
| 133 | + kv(std::cout, "Output", result.value().output_path.string()); |
| 134 | + kv(std::cout, "Name", result.value().name); |
| 135 | + kv(std::cout, "Version", result.value().version); |
| 136 | + kv(std::cout, "Asset root", result.value().asset_root); |
| 137 | + kv(std::cout, "Copied files", std::to_string(result.value().copied_files)); |
| 138 | + kv(std::cout, "Copied directories", std::to_string(result.value().copied_directories)); |
| 139 | + |
| 140 | + return 0; |
| 141 | +#endif |
| 142 | + } |
| 143 | + |
| 144 | + int run( |
| 145 | + const std::vector<std::string> &args) |
| 146 | + { |
| 147 | + if (args.empty()) |
| 148 | + return help(); |
| 149 | + |
| 150 | + if (args[0] == "-h" || args[0] == "--help") |
| 151 | + return help(); |
| 152 | + |
| 153 | + if (args[0] == "export") |
| 154 | + { |
| 155 | + std::vector<std::string> rest; |
| 156 | + rest.reserve(args.size()); |
| 157 | + |
| 158 | + for (std::size_t i = 1; i < args.size(); ++i) |
| 159 | + rest.push_back(args[i]); |
| 160 | + |
| 161 | + return export_game(rest); |
| 162 | + } |
| 163 | + |
| 164 | + vix::cli::util::err_line(std::cerr, "Unknown game command."); |
| 165 | + vix::cli::util::warn_line(std::cerr, "Usage: vix game export [options]"); |
| 166 | + return 1; |
| 167 | + } |
| 168 | + |
| 169 | + int help() |
| 170 | + { |
| 171 | + std::ostream &out = std::cout; |
| 172 | + |
| 173 | + out |
| 174 | + << "vix game\n" |
| 175 | + << "Game project tools.\n\n" |
| 176 | + |
| 177 | + << "Usage\n" |
| 178 | + << " vix game export [options]\n\n" |
| 179 | + |
| 180 | + << "Options\n" |
| 181 | + << " --project-root <path> Project root, default: .\n" |
| 182 | + << " --project-root=<path> Same as --project-root <path>\n" |
| 183 | + << " --output <path> Override output directory\n" |
| 184 | + << " --output=<path> Same as --output <path>\n" |
| 185 | + << " --name <name> Override export name\n" |
| 186 | + << " --name=<name> Same as --name <name>\n" |
| 187 | + << " --no-overwrite Fail if output already exists\n" |
| 188 | + << " --no-assets Do not copy assets\n" |
| 189 | + << " -h, --help Show help\n\n" |
| 190 | + |
| 191 | + << "Examples\n" |
| 192 | + << " vix game export\n" |
| 193 | + << " vix game export --project-root .\n" |
| 194 | + << " vix game export --output dist\n" |
| 195 | + << " vix game export --name mario-release\n" |
| 196 | + << " vix game export --no-overwrite\n\n"; |
| 197 | + |
| 198 | + return 0; |
| 199 | + } |
| 200 | + |
| 201 | +} // namespace vix::commands::GameCommand |
0 commit comments