Porytiles
Loading...
Searching...
No Matches
command_decompile_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
13
14#include "command.hpp"
15#include "option.hpp"
17
18class DecompileTilesetCommand final : public Command {
19 public:
20 explicit DecompileTilesetCommand(CLI::App &parent_app)
21 : 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 decompile")->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 decompile 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 decompile 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
57 PrimaryTilesetDecompiler decompiler{
58 &env.config,
59 env.text_formatter,
60 env.diag.get(),
61 services.tile_printer.get(),
62 services.palette_printer.get()};
63 DecompilePrimaryTileset decompile_use_case{
64 &services.repo,
65 &decompiler,
66 &services.compiler,
67 &services.metadata_provider,
68 &services.tileset_manager,
69 &env.config,
70 &env.config,
71 env.diag.get()};
72
73 // Run the use case
74 auto decompile_result = decompile_use_case.decompile(tileset_name_);
75 if (!decompile_result.has_value()) {
76 const auto fail_result = ChainableResult<std::unique_ptr<Tileset>>{
77 FormattableError{"Failed to decompile tileset '{}'.", FormatParam{tileset_name_, Style::bold}},
78 decompile_result};
79 env.diag->fatal(fail_result);
80 throw CLI::RuntimeError{1};
81 }
82 }
83
84 static constexpr auto command_name = "decompile-tileset";
85 static constexpr auto command_desc =
86 "Decompile a tileset -- update the Porytiles assets to match the Porymap assets.";
87 static constexpr auto command_group = "COMMANDS";
88 std::string tileset_name_;
89 OptProjectRoot project_root_opt_;
90 porytiles::CliOptionStorage cli_storage_;
91};
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
DecompileTilesetCommand(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.
Use case for decompiling a primary Tileset.
ChainableResult< void > decompile(const std::string &tileset_name) const
Decompiles the primary Tileset with the given tileset name.
A text parameter with associated styling for formatted output.
General-purpose error implementation with formatted message support.
Definition error.hpp:57
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.