Porytiles
Loading...
Searching...
No Matches
command_import_tileset.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <string>
5
6#include "CLI/CLI.hpp"
7
14
15#include "command.hpp"
16#include "option.hpp"
18
19class ImportTilesetCommand final : public Command {
20 public:
21 explicit ImportTilesetCommand(CLI::App &parent_app) : Command{parent_app, command_name, command_desc, command_group}
22 {
23 CLI::App &cmd = get_app();
24 cmd.add_option("<tileset-name>", tileset_name_, "Name of the tileset to import")->required();
25 project_root_opt_.RegisterOpt(cmd);
26 porytiles::register_config_options(cmd, cli_storage_);
27 }
28
29 private:
30 void Run() override
31 {
32 using namespace porytiles;
33
34 TilesetCommandEnv env{project_root_opt_.project_root(), cli_storage_};
35
36 // Env initialization and schema resolution can fail, so they run first and their failures chain and report
37 // through the unfiltered stderr diagnostics.
38 const auto env_result = env.initialize(tileset_name_);
39 if (!env_result.has_value()) {
40 const auto env_fail_result = ChainableResult<void>{
41 FormattableError{"Failed to import tileset '{}'.", FormatParam{tileset_name_, Style::bold}},
42 env_result};
43 env.stderr_diag.fatal(env_fail_result);
44 throw CLI::RuntimeError{1};
45 }
46
47 auto attribute_context = resolve_attribute_context(env, tileset_name_);
48 if (!attribute_context.has_value()) {
49 const auto fail_result = ChainableResult<void>{
50 FormattableError{"Failed to import tileset '{}'.", FormatParam{tileset_name_, Style::bold}},
51 attribute_context};
52 env.diag->fatal(fail_result);
53 throw CLI::RuntimeError{1};
54 }
55 TilesetCommandServices services{env, std::move(attribute_context).value()};
56
58 env.project_root,
59 &services.resolved.schema,
60 &env.config,
61 env.text_formatter,
62 env.diag.get(),
63 services.tile_printer.get(),
64 services.palette_printer.get(),
65 &services.metadata_provider,
66 &services.png_indexed_loader,
67 &services.jasc_loader,
68 };
69 PrimaryTilesetDecompiler decompiler{
70 &env.config,
71 env.text_formatter,
72 env.diag.get(),
73 services.tile_printer.get(),
74 services.palette_printer.get()};
75 ImportPrimaryTileset import_use_case{
76 &importer,
77 &decompiler,
78 &services.compiler,
79 &services.repo,
80 &services.metadata_provider,
81 &services.tileset_manager,
82 &env.config,
83 &env.config,
84 env.diag.get()};
85
86 // Run the use case
87 auto import_result = import_use_case.import(tileset_name_);
88 if (!import_result.has_value()) {
89 const auto fail_result = ChainableResult<std::unique_ptr<Tileset>>{
90 FormattableError{"Failed to import tileset '{}'.", FormatParam{tileset_name_, Style::bold}},
91 import_result};
92 env.diag->fatal(fail_result);
93 throw CLI::RuntimeError{1};
94 }
95 }
96
97 static constexpr auto command_name = "import-tileset";
98 static constexpr auto command_desc = "Import a pre-existing tileset into Porytiles.";
99 static constexpr auto command_group = "COMMANDS";
100 std::string tileset_name_;
101 OptProjectRoot project_root_opt_;
102 porytiles::CliOptionStorage cli_storage_;
103};
Command is an abstract class that provides basic command functionality for the Porytiles CLI driver.
Definition command.hpp:16
CLI::App & get_app() const
Definition command.hpp:42
ImportTilesetCommand(CLI::App &parent_app)
std::filesystem::path project_root() const
Definition option.hpp:58
void RegisterOpt(CLI::App &app) override
Definition option.hpp:46
A result type that maintains a chainable sequence of errors for debugging and error reporting.
A text parameter with associated styling for formatted output.
General-purpose error implementation with formatted message support.
Definition error.hpp:57
Use case for importing a primary Tileset.
ChainableResult< void > import(const std::string &tileset_name) const
Infra-layer implementation of PrimaryTilesetImporter for pokeemerald project structure.
Bootstrap class so every tileset command can share common config and diagnostic setup.
The schema-driven service graph shared by the compile, create, import, and decompile commands.
void register_config_options(CLI::App &app, CliOptionStorage &storage)
Registers all config options with a CLI11 App.
ChainableResult< ResolvedAttributeContext > resolve_attribute_context(TilesetCommandEnv &env, const std::string &tileset_name)
Resolves the invocation's metatile attribute schema and builds the provider map for its fields.
Storage struct for CLI option values.